-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathmotion.py
65 lines (58 loc) · 1.74 KB
/
motion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pyb
import PIR
from terminalfont import terminalfont
class motion(PIR.PIR):
NONE = 0
UP = 1
DOWN = 2
txtsize = 2
displaytime = 4000
processdelay = 100
"""detect motion and print msg on TFT"""
def __init__( self, display ) :
super(motion, self).__init__(None, "X12", self.msg)
self._extpower = pyb.Pin("X11", pyb.Pin.OUT_PP)
self._extpower.high()
self._display = display
display.rotation(1)
self._state = motion.NONE
self._timer = 0
self._dirty = False
self._font = terminalfont
self._fontW = terminalfont['Width']
self._fontH = terminalfont['Height']
def msg( self, aArg ) :
self._state = motion.UP if aArg else motion.DOWN
def txt( self, aText, aColor ) :
x, y = self._display.size()
y >>= 1
y -= (self._fontH >> 1) * motion.txtsize
self._display.fillrect((0, y), (self._display.size()[0], self._fontH * motion.txtsize), self._display.BLACK)
self._dirty = (aText != None)
if self._dirty:
self._timer = 0
x >>= 1
x -= len(aText) * (self._fontW >> 1) * motion.txtsize
print(aText)
self._display.text((x, y), aText, aColor, terminalfont, motion.txtsize)
def processmsg( self, dt ) :
state = self._state
if state != motion.NONE:
self._state = motion.NONE
if state == motion.UP:
self.txt("Hello", self._display.GREEN)
else:
self.txt("Goodbye", self._display.RED)
elif self._dirty:
self._timer += dt
if self._timer >= motion.displaytime:
self._timer = 0
self.txt(None, 0)
def run( self ) :
self._display.fill(0)
self.on()
sw = pyb.Switch()
while sw() == False :
self.processmsg(motion.processdelay)
pyb.delay(motion.processdelay)
self.off()