Skip to content

Commit b11b6e0

Browse files
committed
screen updates
1 parent 2e9ea5d commit b11b6e0

File tree

2 files changed

+76
-36
lines changed

2 files changed

+76
-36
lines changed

SipNPyuff/code.py

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
TEXT_HEIGHT = 8
2020
BOTTOM_ROW = DISPLAY_HEIGHT - TEXT_HEIGHT - Y_OFFSET
2121

22+
# States:
23+
WAITING = 0
24+
STARTED = 1
25+
DETECTED = 2
2226
i2c = board.I2C()
2327
# 128x32
2428
# display_bus = displayio.I2CDisplay(i2c, device_address=0x3C, reset=oled_reset)
@@ -48,30 +52,28 @@
4852

4953
lps.filter_config = True
5054
# if CONSOLE: print("Filter Config:", lps.low_pass_config)
51-
detector = PuffDetector(min_pressure=8, high_pressure=40)
55+
detector = PuffDetector()
5256
time.sleep(1)
5357

54-
55-
puff_start = time.monotonic()
56-
57-
5858
if CONSOLE:
5959
print("CONSOLE?")
6060
if DEBUG and CONSOLE:
6161
print("Debug CONSOLE")
6262

63-
color = 0xFAFAFA
63+
color = 0xFFFFFF
6464

6565
font = terminalio.FONT
6666

6767
banner_string = "PUFF-O-TRON-9000"
6868
state_string = " "
6969
pressure_string = " "
7070
input_type_string = " "
71+
duration_string = " "
7172

7273
state_display_timeout = 1.0
7374
state_display_start = 0
7475
while True:
76+
curr_time = time.monotonic()
7577
######################################
7678
splash = displayio.Group(max_size=10)
7779
# Set text, font, and color
@@ -84,16 +86,16 @@
8486
puff_polarity, puff_peak_level, puff_duration = detector.check_for_puff(
8587
current_pressure
8688
)
87-
88-
if CONSOLE and puff_duration is None and puff_polarity:
89-
if puff_polarity == 1:
90-
state_string = "PUFF"
91-
if puff_polarity == -1:
92-
state_string = "SIP"
93-
state_string += " START"
94-
95-
if CONSOLE and puff_duration:
96-
89+
if DEBUG and CONSOLE:
90+
print(
91+
"Pol: %s Peak: %s Dir: %s"
92+
% (str(puff_polarity), str(puff_peak_level), str(puff_duration))
93+
)
94+
95+
# if puff_duration:
96+
if detector.state == DETECTED:
97+
state = DETECTED
98+
duration_string = "Duration: %0.2f" % puff_duration
9799
state_string = "DETECTED:"
98100
print(state_string)
99101
if puff_peak_level == 1:
@@ -105,17 +107,43 @@
105107
input_type_string += " PUFF"
106108
if puff_polarity == -1:
107109
input_type_string += " SIP"
108-
print("END", end=" ")
109-
print(input_type_string)
110-
duration_string = "Duration: %0.2f" % puff_duration
111-
print(duration_string)
110+
state_display_start = curr_time
111+
112+
if CONSOLE:
113+
print("END", end=" ")
114+
if CONSOLE:
115+
print(input_type_string)
116+
if CONSOLE:
117+
print(duration_string)
118+
119+
elif detector.state == STARTED:
120+
# elif puff_duration is None and puff_polarity:
121+
dir_string = ""
122+
if puff_polarity == 1:
123+
dir_string = "PUFF"
124+
if puff_polarity == -1:
125+
dir_string = "SIP"
126+
state_string = "%s START" % dir_string
127+
if CONSOLE:
128+
print(state_string)
129+
else:
130+
state = WAITING
131+
if (curr_time - state_display_start) > state_display_timeout:
132+
state_string = "Waiting for Input"
133+
detector_result_string = " "
134+
duration_string = " "
135+
136+
if CONSOLE:
137+
print("STATE:", state)
112138
# Create the tet label
113-
if puff_polarity == 0:
114-
state_string = "Waiting for Input"
139+
print((curr_time - state_display_start))
140+
141+
# if it's been >timeout since we started displaying puff result
142+
115143
banner = label.Label(font, text=banner_string, color=color)
116144
state = label.Label(font, text=state_string, color=color)
117145
detector_result = label.Label(font, text=input_type_string, color=color)
118-
text_area4 = label.Label(font, text=state_string, color=color)
146+
duration = label.Label(font, text=duration_string, color=color)
119147
pressure_label = label.Label(font, text=pressure_string, color=color)
120148

121149
banner.x = 0
@@ -126,14 +154,20 @@
126154
detector_result.x = 20
127155
detector_result.y = 20 + Y_OFFSET
128156

157+
duration.x = 10
158+
duration.y = 30 + Y_OFFSET
159+
129160
x, y, w, h = pressure_label.bounding_box
130161
pressure_label.x = DISPLAY_WIDTH - w
131162
pressure_label.y = BOTTOM_ROW + Y_OFFSET
132163

133164
splash.append(banner)
134165
splash.append(state)
135166
splash.append(detector_result)
167+
splash.append(duration)
136168
splash.append(pressure_label)
137169
# Show it
138170
display.show(splash)
171+
if CONSOLE:
172+
print("----------------------------------------------")
139173
time.sleep(0.01)

SipNPyuff/puff_detector.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
CONSOLE = True
55
DEBUG = True
66

7+
MIN_PRESSURE = 8
8+
HIGH_PRESSURE = 40
9+
WAITING = 0
10+
STARTED = 1
11+
DETECTED = 2
12+
713

814
class PuffDetector:
9-
def __init__(self, min_pressure=8, high_pressure=20):
15+
def __init__(self, min_pressure=MIN_PRESSURE, high_pressure=HIGH_PRESSURE):
1016
self.high_pressure = high_pressure
1117
self.min_pressure = min_pressure
1218

@@ -15,6 +21,7 @@ def __init__(self, min_pressure=8, high_pressure=20):
1521
self.counter = 0
1622
self.duration = 0
1723
self.puff_start = 0
24+
self.state = WAITING
1825

1926
@classmethod
2027
def rolling_average(cls, measurements, window_size=3):
@@ -62,35 +69,34 @@ def pressure_string(pressure_type):
6269

6370
def check_for_puff(self, current_pressure):
6471
"""Updates the internal state to detect if a sip/puff has been started or stopped"""
65-
puff_polarity = None
6672
puff_peak_level = None
6773
puff_duration = None
6874
polarity, level = self.catagorize_pressure(current_pressure)
6975

70-
# if (polarity != 0) or (level != 0):
71-
if abs(current_pressure) > PRINT_FLOOR:
72-
if self.counter % 4 == 0:
73-
if DEBUG and CONSOLE:
74-
print("\t\t\tpressure:", current_pressure)
76+
if self.state == DETECTED:
77+
if polarity == 0 and level == 0:
78+
self.state = WAITING
7579

76-
if level != 0 and self.start_polarity == 0: ###
80+
if level != 0 and self.start_polarity == 0:
81+
self.state = STARTED
7782
self.start_polarity = polarity
7883
self.puff_start = time.monotonic()
79-
puff_polarity = self.start_polarity
8084

81-
if self.start_polarity != 0:
85+
if self.state == STARTED:
86+
# if self.start_polarity != 0:
8287
if level > self.peak_level:
8388
self.peak_level = level
8489

85-
if (level == 0) and (self.start_polarity != 0):
90+
# if (level == 0) and (self.start_polarity != 0):
91+
if (level == 0) and (self.state == STARTED):
92+
self.state = DETECTED
8693
self.duration = time.monotonic() - self.puff_start
8794

88-
puff_polarity = self.start_polarity
8995
puff_peak_level = self.peak_level
9096
puff_duration = self.duration
9197

9298
self.start_polarity = 0
9399
self.peak_level = 0
94100
self.duration = 0
95101
self.counter += 1
96-
return (puff_polarity, puff_peak_level, puff_duration)
102+
return (self.start_polarity, puff_peak_level, puff_duration)

0 commit comments

Comments
 (0)