Skip to content

Commit 208e1c5

Browse files
committed
added config file
1 parent 2833073 commit 208e1c5

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

SipNPyuff/code.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
BORDER = 1
1818
Y_OFFSET = 3
1919
TEXT_HEIGHT = 8
20-
BOTTOM_ROW = DISPLAY_HEIGHT - TEXT_HEIGHT - Y_OFFSET
20+
BOTTOM_ROW = DISPLAY_HEIGHT - TEXT_HEIGHT
21+
22+
# Get wifi details and more from a secrets.py file
23+
2124

2225
# States:
2326
WAITING = 0
@@ -36,7 +39,7 @@
3639

3740
# i2c = DebugI2C(i2c)
3841
lps = adafruit_lps35hw.LPS35HW(i2c, 0x5C)
39-
CONSOLE = True
42+
CONSOLE = False
4043
DEBUG = True
4144

4245
lps.zero_pressure()
@@ -55,13 +58,9 @@
5558
detector = PuffDetector()
5659
time.sleep(1)
5760

58-
if CONSOLE:
59-
print("CONSOLE?")
60-
if DEBUG and CONSOLE:
61-
print("Debug CONSOLE")
61+
print("det timeout:", detector.display_timeout)
6262

6363
color = 0xFFFFFF
64-
6564
font = terminalio.FONT
6665

6766
banner_string = "PUFF-O-TRON-9000"
@@ -74,17 +73,15 @@
7473
state_display_start = 0
7574
while True:
7675
curr_time = time.monotonic()
77-
######################################
78-
splash = displayio.Group(max_size=10)
7976
# Set text, font, and color
8077

81-
######################################
8278
current_pressure = lps.pressure
83-
pressure_string = "Pressure: %0.3f" % current_pressure
79+
pressure_string = "Press: %0.3f" % current_pressure
8480
if CONSOLE:
8581
print(pressure_string)
8682
else:
87-
print((current_pressure,))
83+
# print((current_pressure,))
84+
pass
8885

8986
puff_polarity, puff_peak_level, puff_duration = detector.check_for_puff(
9087
current_pressure
@@ -104,7 +101,6 @@
104101
"Duration: %0.2f" % puff_duration
105102
) # puff duration can be none? after detect?
106103
state_string = "DETECTED:"
107-
print(state_string)
108104
if puff_peak_level == 1:
109105
input_type_string = "SOFT"
110106
if puff_peak_level == 2:
@@ -116,13 +112,6 @@
116112
input_type_string += " SIP"
117113
state_display_start = curr_time
118114

119-
if CONSOLE:
120-
print("END", end=" ")
121-
if CONSOLE:
122-
print(input_type_string)
123-
if CONSOLE:
124-
print(duration_string)
125-
126115
elif detector.state == STARTED:
127116
# elif puff_duration is None and puff_polarity:
128117
dir_string = ""
@@ -131,30 +120,32 @@
131120
if puff_polarity == -1:
132121
dir_string = "SIP"
133122
state_string = "%s START" % dir_string
134-
if CONSOLE:
135-
print(state_string)
136123
else:
137124
state = WAITING
138-
if (curr_time - state_display_start) > state_display_timeout:
125+
if (curr_time - state_display_start) > detector.display_timeout:
139126
state_string = "Waiting for Input"
140127
input_type_string = " "
141128
duration_string = " "
142129

143-
# Create the tet label
144-
print((curr_time - state_display_start))
145-
146130
# if it's been >timeout since we started displaying puff result
147131

132+
min_press_str = "min: %d" % detector.min_pressure
133+
high_press_str = "hi: %d" % detector.high_pressure
134+
148135
banner = label.Label(font, text=banner_string, color=color)
149136
state = label.Label(font, text=state_string, color=color)
150137
detector_result = label.Label(font, text=input_type_string, color=color)
151138
duration = label.Label(font, text=duration_string, color=color)
139+
min_pressure_label = label.Label(font, text=min_press_str, color=color)
140+
high_pressure_label = label.Label(font, text=high_press_str, color=color)
152141
pressure_label = label.Label(font, text=pressure_string, color=color)
153142
if CONSOLE:
154143
print(banner.text)
155144
print(state.text)
156145
print(detector_result.text)
157146
print(duration.text)
147+
print(min_pressure_label.text)
148+
print(high_pressure_label.text)
158149
print(pressure_label.text)
159150

160151
banner.x = 0
@@ -168,14 +159,23 @@
168159
duration.x = 10
169160
duration.y = 30 + Y_OFFSET
170161

162+
min_pressure_label.x = 0
163+
min_pressure_label.y = BOTTOM_ROW - 10
164+
171165
x, y, w, h = pressure_label.bounding_box
172166
pressure_label.x = DISPLAY_WIDTH - w
173-
pressure_label.y = BOTTOM_ROW + Y_OFFSET
167+
pressure_label.y = BOTTOM_ROW
174168

169+
high_pressure_label.x = 0
170+
high_pressure_label.y = BOTTOM_ROW
171+
172+
splash = displayio.Group(max_size=10)
175173
splash.append(banner)
176174
splash.append(state)
177175
splash.append(detector_result)
178176
splash.append(duration)
177+
splash.append(min_pressure_label)
178+
splash.append(high_pressure_label)
179179
splash.append(pressure_label)
180180
# Show it
181181
display.show(splash)

SipNPyuff/puff_detector.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import time
2+
import os
3+
import json
24

35
PRINT_FLOOR = 5
4-
CONSOLE = True
6+
CONSOLE = False
57
DEBUG = True
68

79
MIN_PRESSURE = 8
@@ -12,7 +14,13 @@
1214

1315

1416
class PuffDetector:
15-
def __init__(self, min_pressure=MIN_PRESSURE, high_pressure=HIGH_PRESSURE):
17+
def __init__(
18+
self,
19+
min_pressure=MIN_PRESSURE,
20+
high_pressure=HIGH_PRESSURE,
21+
config_filename="settings.json",
22+
display_timeout=1,
23+
):
1624
self.high_pressure = high_pressure
1725
self.min_pressure = min_pressure
1826

@@ -22,6 +30,30 @@ def __init__(self, min_pressure=MIN_PRESSURE, high_pressure=HIGH_PRESSURE):
2230
self.duration = 0
2331
self.puff_start = 0
2432
self.state = WAITING
33+
self.settings_dict = {}
34+
35+
self.display_timeout = display_timeout
36+
37+
self._config_filename = config_filename
38+
self._load_config()
39+
if self.settings_dict:
40+
self.min_pressure = self.settings_dict["min_pressure"]
41+
self.high_pressure = self.settings_dict["high_pressure"]
42+
if "display_timeout" in self.settings_dict.keys():
43+
self.display_timeout = self.settings_dict["display_timeout"]
44+
45+
def _load_config(self):
46+
if not self._config_filename in os.listdir("/"):
47+
return
48+
try:
49+
with open(self._config_filename, "r") as file:
50+
settings_dict = json.load(file)
51+
except (ValueError, OSError) as error:
52+
print("Error loading config file")
53+
print(type(error))
54+
55+
print("got config file:")
56+
print(settings_dict)
2557

2658
@classmethod
2759
def rolling_average(cls, measurements, window_size=3):
@@ -73,7 +105,7 @@ def check_for_puff(self, current_pressure):
73105
puff_duration = None
74106
polarity, level = self.catagorize_pressure(current_pressure)
75107
if CONSOLE:
76-
print("pol", polarity, "lev:", level)
108+
print("poll", polarity, "lev:", level)
77109
if self.state == DETECTED:
78110
# if polarity == 0 and level == 0:
79111
self.state = WAITING

0 commit comments

Comments
 (0)