-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfioutils.py
54 lines (47 loc) · 1.63 KB
/
pfioutils.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
from time import sleep
import pifacedigitalio as pfio
class PFIOUtils:
def __init__(self, *args):
if len(args) == 1:
self.p = args[0]
else:
self.p = pfio.PiFaceDigital()
self.listeners = [None, None, None, None]
def turn_off_leds(self):
self.p.output_port.all_off()
def turn_on_leds(self, leds_count, total_leds):
for i in range(total_leds):
if i < leds_count and self.p.leds[i].value == 0:
self.p.leds[i].turn_on()
elif i >= leds_count and self.p.leds[i].value == 1:
self.p.leds[i].turn_off()
def blink_leds(self, blinksRemaining):
for i in range(blinksRemaining):
self.p.output_port.toggle()
sleep(0.2)
self.p.output_port.toggle()
sleep(0.2)
def swipe_leds(self, leds_count, light_sleep_time):
self.turn_off_leds()
for i in range(0, leds_count):
self.p.leds[i].turn_on()
sleep(light_sleep_time)
for i in range(0, leds_count):
self.p.leds[i].turn_off()
sleep(light_sleep_time)
for i in range(leds_count - 1, -1, -1):
self.p.leds[i].turn_on()
sleep(light_sleep_time)
for i in range(leds_count - 1, -1, -1):
self.p.leds[i].turn_off()
sleep(light_sleep_time)
def registerButtonAction(self, buttonId, action):
if len(self.listeners) > buttonId and self.listeners[buttonId] != None:
self.unregisterButtonAction(buttonId)
self.listeners[buttonId] = pfio.InputEventListener(chip=self.p)
self.listeners[buttonId].register(buttonId, pfio.IODIR_RISING_EDGE, action)
self.listeners[buttonId].activate()
def unregisterButtonAction(self, buttonId):
self.listeners[buttonId].deactivate()
self.listeners[buttonId].deregister(buttonId)
self.listeners[buttonId] = None