Skip to content

Commit 1d8ad0f

Browse files
committed
HID output workin' :)
1 parent 208e1c5 commit 1d8ad0f

File tree

2 files changed

+50
-79
lines changed

2 files changed

+50
-79
lines changed

SipNPyuff/code.py

Lines changed: 48 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,50 @@
11
import time
22
import board
3-
import adafruit_lps35hw
4-
53
import displayio
64
import terminalio
75
from adafruit_display_text import label
86
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()
1019

1120
displayio.release_displays()
12-
oled_reset = board.D9
1321

1422
DISPLAY_WIDTH = 128
15-
# DISPLAY_HEIGHT = 32
16-
DISPLAY_HEIGHT = 64 # Change to 64 if needed
17-
BORDER = 1
23+
DISPLAY_HEIGHT = 64
1824
Y_OFFSET = 3
1925
TEXT_HEIGHT = 8
2026
BOTTOM_ROW = DISPLAY_HEIGHT - TEXT_HEIGHT
2127

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
2932
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)
3433

34+
35+
display_bus = displayio.I2CDisplay(i2c, device_address=0x3D)
3536
display = adafruit_displayio_ssd1306.SSD1306(
3637
display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT
3738
)
3839

39-
40-
# i2c = DebugI2C(i2c)
4140
lps = adafruit_lps35hw.LPS35HW(i2c, 0x5C)
42-
CONSOLE = False
43-
DEBUG = True
44-
4541
lps.zero_pressure()
4642
lps.data_rate = adafruit_lps35hw.DataRate.RATE_75_HZ
4743

48-
min_pressure = 8
49-
high_pressure = 20
50-
# sip/puff is "over" on keyup/pressure polarity reversal
51-
# averaging code
52-
5344
lps.filter_enabled = True
54-
# if CONSOLE: print("Filter enabled:", lps.low_pass_enabled)
55-
5645
lps.filter_config = True
57-
# if CONSOLE: print("Filter Config:", lps.low_pass_config)
5846
detector = PuffDetector()
5947
time.sleep(1)
60-
61-
print("det timeout:", detector.display_timeout)
62-
6348
color = 0xFFFFFF
6449
font = terminalio.FONT
6550

@@ -72,27 +57,16 @@
7257
state_display_timeout = 1.0
7358
state_display_start = 0
7459
while True:
60+
detected_puff = None
7561
curr_time = time.monotonic()
7662
# Set text, font, and color
7763

7864
current_pressure = lps.pressure
7965
pressure_string = "Press: %0.3f" % current_pressure
80-
if CONSOLE:
81-
print(pressure_string)
82-
else:
83-
# print((current_pressure,))
84-
pass
8566

8667
puff_polarity, puff_peak_level, puff_duration = detector.check_for_puff(
8768
current_pressure
8869
)
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-
)
9670

9771
# if puff_duration:
9872
if detector.state == DETECTED:
@@ -101,15 +75,22 @@
10175
"Duration: %0.2f" % puff_duration
10276
) # puff duration can be none? after detect?
10377
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"
10878

10979
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+
11187
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
11394
state_display_start = curr_time
11495

11596
elif detector.state == STARTED:
@@ -120,8 +101,7 @@
120101
if puff_polarity == -1:
121102
dir_string = "SIP"
122103
state_string = "%s START" % dir_string
123-
else:
124-
state = WAITING
104+
else: # WAITING
125105
if (curr_time - state_display_start) > detector.display_timeout:
126106
state_string = "Waiting for Input"
127107
input_type_string = " "
@@ -139,14 +119,6 @@
139119
min_pressure_label = label.Label(font, text=min_press_str, color=color)
140120
high_pressure_label = label.Label(font, text=high_press_str, color=color)
141121
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)
150122

151123
banner.x = 0
152124
banner.y = 0 + Y_OFFSET
@@ -179,6 +151,17 @@
179151
splash.append(pressure_label)
180152
# Show it
181153
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()

SipNPyuff/puff_detector.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import json
44

5-
PRINT_FLOOR = 5
65
CONSOLE = False
76
DEBUG = True
87

@@ -47,21 +46,11 @@ def _load_config(self):
4746
return
4847
try:
4948
with open(self._config_filename, "r") as file:
50-
settings_dict = json.load(file)
49+
self.settings_dict = json.load(file)
5150
except (ValueError, OSError) as error:
5251
print("Error loading config file")
5352
print(type(error))
5453

55-
print("got config file:")
56-
print(settings_dict)
57-
58-
@classmethod
59-
def rolling_average(cls, measurements, window_size=3):
60-
# print("measurements", measurements)
61-
window = measurements[-window_size:]
62-
63-
return sum(window) / window_size
64-
6554
def catagorize_pressure(self, pressure):
6655
"""determine the strength and polarity of the pressure reading"""
6756
level = 0
@@ -104,8 +93,7 @@ def check_for_puff(self, current_pressure):
10493
puff_peak_level = None
10594
puff_duration = None
10695
polarity, level = self.catagorize_pressure(current_pressure)
107-
if CONSOLE:
108-
print("poll", polarity, "lev:", level)
96+
10997
if self.state == DETECTED:
11098
# if polarity == 0 and level == 0:
11199
self.state = WAITING

0 commit comments

Comments
 (0)