-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi_lcd.py
61 lines (50 loc) · 1.8 KB
/
rpi_lcd.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
import pcd8544
import RPIO
class LCD(pcd8544.LCD):
# button numbers
BUTTON_1 = 4
BUTTON_2 = 17
BUTTON_3 = 21
BUTTON_4 = 22
# event type
BUTTON_UP = 1
BUTTON_DOWN = 0
BUTTON_EITHER = 2
def __init__(self, backlight_pin=24):
RPIO.setwarnings(False)
pcd8544.LCD.__init__(self)
self._backlight_pin = backlight_pin
def initialize(self):
""" sets up the rpi status board pins"""
pcd8544.LCD.initialize(self)
RPIO.setup(self._backlight_pin, RPIO.OUT, initial=RPIO.LOW)
def set_backlight(self, enabled=True):
""" enables or disable the backlight leds"""
if enabled:
RPIO.output(self._backlight_pin, RPIO.HIGH)
else:
RPIO.output(self._backlight_pin, RPIO.LOW)
def add_button_callback(self, button, function, event=BUTTON_DOWN, threaded=True):
""" attaches a calback function to the button interrupt
The callback receives two arguments, the button number (ie BUTTON_1),
and the value of the button (ie BUTTON_UP or BUTTON_DOWN).
Event determines if this assigns to BUTTON_UP or BUTTON_DOWN or BUTTON_EITHER.
If threaded is True, the interrupt will spawn a new thread, otherwise
this thread will be used and no other interrupts can occur while this
one is being serviced.
"""
if event == LCD.BUTTON_DOWN:
edge = 'falling'
elif event == LCD.BUTTON_UP:
edge = 'rising'
elif event == LCD.BUTTON_EITHER:
edge = 'both'
RPIO.add_interrupt_callback(button, function, edge, RPIO.PUD_UP, threaded, 20)
def delete_button_callback(self, button):
""" deletes all callbacks set to button event"""
RPIO.del_interrupt_callback(button)
def wait_for_buttons(self, threaded=True):
""" waits for button interrupts to occur
If threaded is True, this polling is done in a separate thread, if
False, it will block forever, handling events"""
RPIO.wait_for_interrupts(threaded)