-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.py
141 lines (115 loc) · 3.49 KB
/
examples.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import time
from raspi_gpio import GPIO
from rpi_reactive_gpio import managers
# test all pins
def test_all_pins():
pins: list[int] = []
for manager in managers:
if isinstance(manager, RGBLedManager):
pins.append(manager.red_pin)
pins.append(manager.green_pin)
pins.append(manager.blue_pin)
elif isinstance(manager, LedManager):
pins.append(manager.pin)
for pin in pins:
GPIO.output(pin, GPIO.LOW)
time.sleep(0.5)
for pin in pins:
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pin, GPIO.LOW)
from rpi_reactive_gpio import ButtonClick, LedManager, RGBLedManager, LedState, RGBLedState, main_loop
# Global style
led_mode = 0
color_index = 0
@ButtonClick(pin = 5)
def button_click(num_clicks: int):
global led_mode
if num_clicks == 1:
led_mode = int(not bool(led_mode))
elif num_clicks == 2:
led_mode = 2
elif num_clicks == 3:
led_mode = 3
else:
print(f'button2 clicked {num_clicks} times')
@ButtonClick(pin = 5, debounce_time_ms = 2000)
def button_long_press(_):
color_index += 1
@LedManager(pin = 29)
def get_led_state() -> LedState:
global led_mode
match led_mode:
case 0:
return LedState.off
case 1:
return LedState.on
case 2:
return LedState.blink
case 3:
return LedState.fast_blink
return LedState.off
@RGBLedManager(red_pin = 33, green_pin = 35, blue_pin = 37)
def get_rgb_led_state() -> RGBLedState:
global color_index
match color_index % 5:
case 0:
return RGBLedState.off
case 1:
return RGBLedState.red
case 2:
return RGBLedState.yellow
case 3:
return RGBLedState.green
case 4:
return RGBLedState.blue
return RGBLedState.off
# if __name__ == '__main__':
# test_all_pins()
# main_loop()
# OOP style
from rpi_reactive_gpio.scene import Scene
class ExampleScene(Scene):
led_mode = 0
color_index = 0
@ButtonClick(pin = 5)
def button_click(self, num_clicks: int):
if num_clicks == 1:
self.led_mode = int(not bool(self.led_mode))
elif num_clicks == 2:
self.led_mode = 2
elif num_clicks == 3:
self.led_mode = 3
else:
print(f'button2 clicked {num_clicks} times')
@ButtonClick(pin = 5, debounce_time_ms = 2000)
def button_long_press(self, _):
self.color_index += 1
@LedManager(pin = 29)
def get_led_state(self) -> LedState:
match self.led_mode:
case 0:
return LedState.off
case 1:
return LedState.on
case 2:
return LedState.blink
case 3:
return LedState.fast_blink
return LedState.off
@RGBLedManager(red_pin = 33, green_pin = 35, blue_pin = 37)
def get_rgb_led_state(self) -> RGBLedState:
match self.color_index % 5:
case 0:
return RGBLedState.off
case 1:
return RGBLedState.red
case 2:
return RGBLedState.yellow
case 3:
return RGBLedState.green
case 4:
return RGBLedState.blue
return RGBLedState.off
if __name__ == '__main__':
ExampleScene().main_loop()