|
1 | 1 | import time
|
2 | 2 | import board
|
3 |
| -import adafruit_lps35hw |
4 |
| - |
5 | 3 | import displayio
|
6 | 4 | import terminalio
|
7 | 5 | from adafruit_display_text import label
|
8 | 6 | import adafruit_displayio_ssd1306
|
9 |
| -from puff_detector import PuffDetector |
| 7 | +import adafruit_lps35hw |
| 8 | +from puff_detector import PuffDetector, STARTED, DETECTED |
| 9 | + |
| 10 | +from adafruit_hid.keyboard import Keyboard |
| 11 | +from adafruit_hid.keycode import Keycode |
| 12 | + |
| 13 | +# The keycode sent for each button, will be paired with a control key |
| 14 | +buttonkeys = [Keycode.A, Keycode.B, Keycode.C, Keycode.D, Keycode.E, Keycode.F] |
| 15 | +controlkey = Keycode.LEFT_CONTROL |
| 16 | + |
| 17 | +# the keyboard object! |
| 18 | +kbd = Keyboard() |
10 | 19 |
|
11 | 20 | displayio.release_displays()
|
12 |
| -oled_reset = board.D9 |
13 | 21 |
|
14 | 22 | DISPLAY_WIDTH = 128
|
15 |
| -# DISPLAY_HEIGHT = 32 |
16 |
| -DISPLAY_HEIGHT = 64 # Change to 64 if needed |
17 |
| -BORDER = 1 |
| 23 | +DISPLAY_HEIGHT = 64 |
18 | 24 | Y_OFFSET = 3
|
19 | 25 | TEXT_HEIGHT = 8
|
20 | 26 | BOTTOM_ROW = DISPLAY_HEIGHT - TEXT_HEIGHT
|
21 | 27 |
|
22 |
| -# Get wifi details and more from a secrets.py file |
23 |
| - |
24 |
| - |
25 |
| -# States: |
26 |
| -WAITING = 0 |
27 |
| -STARTED = 1 |
28 |
| -DETECTED = 2 |
| 28 | +SOFT_SIP = 0 |
| 29 | +HARD_SIP = 1 |
| 30 | +SOFT_PUFF = 2 |
| 31 | +HARD_PUFF = 3 |
29 | 32 | i2c = board.I2C()
|
30 |
| -# 128x32 |
31 |
| -# display_bus = displayio.I2CDisplay(i2c, device_address=0x3C, reset=oled_reset) |
32 |
| -# 128x64 |
33 |
| -display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=oled_reset) |
34 | 33 |
|
| 34 | + |
| 35 | +display_bus = displayio.I2CDisplay(i2c, device_address=0x3D) |
35 | 36 | display = adafruit_displayio_ssd1306.SSD1306(
|
36 | 37 | display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT
|
37 | 38 | )
|
38 | 39 |
|
39 |
| - |
40 |
| -# i2c = DebugI2C(i2c) |
41 | 40 | lps = adafruit_lps35hw.LPS35HW(i2c, 0x5C)
|
42 |
| -CONSOLE = False |
43 |
| -DEBUG = True |
44 |
| - |
45 | 41 | lps.zero_pressure()
|
46 | 42 | lps.data_rate = adafruit_lps35hw.DataRate.RATE_75_HZ
|
47 | 43 |
|
48 |
| -min_pressure = 8 |
49 |
| -high_pressure = 20 |
50 |
| -# sip/puff is "over" on keyup/pressure polarity reversal |
51 |
| -# averaging code |
52 |
| - |
53 | 44 | lps.filter_enabled = True
|
54 |
| -# if CONSOLE: print("Filter enabled:", lps.low_pass_enabled) |
55 |
| - |
56 | 45 | lps.filter_config = True
|
57 |
| -# if CONSOLE: print("Filter Config:", lps.low_pass_config) |
58 | 46 | detector = PuffDetector()
|
59 | 47 | time.sleep(1)
|
60 |
| - |
61 |
| -print("det timeout:", detector.display_timeout) |
62 |
| - |
63 | 48 | color = 0xFFFFFF
|
64 | 49 | font = terminalio.FONT
|
65 | 50 |
|
|
72 | 57 | state_display_timeout = 1.0
|
73 | 58 | state_display_start = 0
|
74 | 59 | while True:
|
| 60 | + detected_puff = None |
75 | 61 | curr_time = time.monotonic()
|
76 | 62 | # Set text, font, and color
|
77 | 63 |
|
78 | 64 | current_pressure = lps.pressure
|
79 | 65 | pressure_string = "Press: %0.3f" % current_pressure
|
80 |
| - if CONSOLE: |
81 |
| - print(pressure_string) |
82 |
| - else: |
83 |
| - # print((current_pressure,)) |
84 |
| - pass |
85 | 66 |
|
86 | 67 | puff_polarity, puff_peak_level, puff_duration = detector.check_for_puff(
|
87 | 68 | current_pressure
|
88 | 69 | )
|
89 |
| - if CONSOLE: |
90 |
| - print("STATE:", detector.state) |
91 |
| - if DEBUG and CONSOLE: |
92 |
| - print( |
93 |
| - "Pol: %s Peak: %s Dir: %s" |
94 |
| - % (str(puff_polarity), str(puff_peak_level), str(puff_duration)) |
95 |
| - ) |
96 | 70 |
|
97 | 71 | # if puff_duration:
|
98 | 72 | if detector.state == DETECTED:
|
|
101 | 75 | "Duration: %0.2f" % puff_duration
|
102 | 76 | ) # puff duration can be none? after detect?
|
103 | 77 | state_string = "DETECTED:"
|
104 |
| - if puff_peak_level == 1: |
105 |
| - input_type_string = "SOFT" |
106 |
| - if puff_peak_level == 2: |
107 |
| - input_type_string = "HARD" |
108 | 78 |
|
109 | 79 | if puff_polarity == 1:
|
110 |
| - input_type_string += " PUFF" |
| 80 | + if puff_peak_level == 1: |
| 81 | + input_type_string = "SOFT PUFF" |
| 82 | + detected_puff = SOFT_PUFF |
| 83 | + if puff_peak_level == 2: |
| 84 | + input_type_string = "HARD PUFF" |
| 85 | + detected_puff = HARD_PUFF |
| 86 | + |
111 | 87 | if puff_polarity == -1:
|
112 |
| - input_type_string += " SIP" |
| 88 | + if puff_peak_level == 1: |
| 89 | + input_type_string = "SOFT SIP" |
| 90 | + detected_puff = SOFT_SIP |
| 91 | + if puff_peak_level == 2: |
| 92 | + input_type_string = "HARD SIP" |
| 93 | + detected_puff = HARD_SIP |
113 | 94 | state_display_start = curr_time
|
114 | 95 |
|
115 | 96 | elif detector.state == STARTED:
|
|
120 | 101 | if puff_polarity == -1:
|
121 | 102 | dir_string = "SIP"
|
122 | 103 | state_string = "%s START" % dir_string
|
123 |
| - else: |
124 |
| - state = WAITING |
| 104 | + else: # WAITING |
125 | 105 | if (curr_time - state_display_start) > detector.display_timeout:
|
126 | 106 | state_string = "Waiting for Input"
|
127 | 107 | input_type_string = " "
|
|
139 | 119 | min_pressure_label = label.Label(font, text=min_press_str, color=color)
|
140 | 120 | high_pressure_label = label.Label(font, text=high_press_str, color=color)
|
141 | 121 | pressure_label = label.Label(font, text=pressure_string, color=color)
|
142 |
| - if CONSOLE: |
143 |
| - print(banner.text) |
144 |
| - print(state.text) |
145 |
| - print(detector_result.text) |
146 |
| - print(duration.text) |
147 |
| - print(min_pressure_label.text) |
148 |
| - print(high_pressure_label.text) |
149 |
| - print(pressure_label.text) |
150 | 122 |
|
151 | 123 | banner.x = 0
|
152 | 124 | banner.y = 0 + Y_OFFSET
|
|
179 | 151 | splash.append(pressure_label)
|
180 | 152 | # Show it
|
181 | 153 | display.show(splash)
|
182 |
| - if CONSOLE: |
183 |
| - print("----------------------------------------------") |
184 |
| - time.sleep(0.01) |
| 154 | + |
| 155 | + # press some buttons |
| 156 | + if detected_puff == SOFT_PUFF: |
| 157 | + kbd.press(Keycode.LEFT_ARROW) |
| 158 | + |
| 159 | + if detected_puff == HARD_PUFF: |
| 160 | + kbd.press(Keycode.DOWN_ARROW) |
| 161 | + |
| 162 | + if detected_puff == SOFT_SIP: |
| 163 | + kbd.press(Keycode.RIGHT_ARROW) |
| 164 | + |
| 165 | + if detected_puff == HARD_SIP: |
| 166 | + kbd.press(Keycode.UP_ARROW) |
| 167 | + kbd.release_all() |
0 commit comments