Skip to content

Commit d275f45

Browse files
committed
refactored
1 parent 075280f commit d275f45

File tree

2 files changed

+87
-87
lines changed

2 files changed

+87
-87
lines changed

SipNPyuff/code.py

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,67 +25,43 @@
2525
# if CONSOLE: print("Filter Config:", lps.low_pass_config)
2626
detector = PuffDetector(min_pressure=8, high_pressure=20)
2727
time.sleep(1)
28-
prev_polarity = None
29-
pressure_list = []
30-
pressure_type = tuple()
31-
prev_pressure_type = tuple()
28+
29+
3230
puff_start = time.monotonic()
33-
current_level = 0
34-
prev_level = 0
35-
puff_end = puff_start
36-
prev_duration = 0
37-
start_polarity = 0
38-
prev_pressure = 0
39-
PRINT_FLOOR = 5
40-
peak_level = 0
31+
32+
4133
if CONSOLE:
4234
print("CONSOLE?")
4335
if DEBUG and CONSOLE:
44-
print("Debug Console")
45-
counter = 0
36+
print("Debug CONSOLE")
37+
38+
4639
while True:
4740
current_pressure = lps.pressure
4841
if not CONSOLE:
4942
print((current_pressure,))
43+
puff_polarity, puff_peak_level, puff_duration = detector.check_for_puff(
44+
current_pressure
45+
)
5046

51-
pressure_type = detector.catagorize_pressure(current_pressure, prev_pressure)
52-
polarity, level, direction = pressure_type
53-
# if (polarity != 0) or (level != 0):
54-
if abs(current_pressure) > PRINT_FLOOR:
55-
if counter % 4 == 0:
56-
if DEBUG and CONSOLE:
57-
print("\t\t\tpressure:", current_pressure)
47+
if CONSOLE and puff_duration is None and puff_polarity:
48+
print("START", end=" ")
49+
if puff_polarity == 1:
50+
print("PUFF")
51+
if puff_polarity == -1:
52+
print("SIP")
53+
if CONSOLE and puff_duration:
5854

59-
if level != 0 and start_polarity == 0: ###
60-
start_polarity = polarity
61-
puff_start = time.monotonic()
62-
if CONSOLE:
63-
print("START", end=" ")
64-
if start_polarity == 1:
65-
print("PUFF")
66-
if start_polarity == -1:
67-
print("SIP")
68-
if start_polarity != 0:
69-
if level > peak_level:
70-
peak_level = level
71-
if (level == 0) and (start_polarity != 0):
72-
duration = time.monotonic() - puff_start
73-
if CONSOLE:
74-
print("END", end=" ")
55+
print("END", end=" ")
56+
if puff_peak_level == 1:
57+
print("SOFT", end=" ")
58+
if puff_peak_level == 2:
59+
print("HARD", end=" ")
7560

76-
if peak_level == 1:
77-
print("SOFT", end=" ")
78-
if peak_level == 2:
79-
print("HARD", end=" ")
61+
if puff_polarity == 1:
62+
print("PUFF")
63+
if puff_polarity == -1:
64+
print("SIP")
8065

81-
if start_polarity == 1:
82-
print("PUFF")
83-
if start_polarity == -1:
84-
print("SIP")
85-
print("Duration:", duration)
86-
start_polarity = 0
87-
peak_level = 0
88-
prev_level = level
89-
prev_pressure = current_pressure
66+
print("Duration:", puff_duration)
9067
time.sleep(0.01)
91-
counter += 1

SipNPyuff/puff_detector.py

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,31 @@
1+
import time
2+
3+
PRINT_FLOOR = 5
4+
CONSOLE = True
5+
DEBUG = True
6+
7+
18
class PuffDetector:
29
def __init__(self, min_pressure=8, high_pressure=20):
310
self.high_pressure = high_pressure
411
self.min_pressure = min_pressure
512

13+
self.start_polarity = 0
14+
self.peak_level = 0
15+
self.counter = 0
16+
self.duration = 0
17+
self.puff_start = 0
18+
619
@classmethod
720
def rolling_average(cls, measurements, window_size=3):
821
# print("measurements", measurements)
922
window = measurements[-window_size:]
1023

1124
return sum(window) / window_size
1225

13-
@classmethod
14-
def slope(cls, a, b):
15-
16-
if a > b:
17-
return 1
18-
elif a < b:
19-
return -1
20-
else:
21-
return 0
22-
23-
@classmethod
24-
def direction(cls, measurements): # requires 6 measurements
25-
average = cls.rolling_average(measurements)
26-
prev_average = cls.rolling_average(measurements[-6:-3])
27-
# print()
28-
# print("measurements:", measurements)
29-
# print("prev_average", prev_average)
30-
# print("average:", average)
31-
current_slope = cls.slope(average, prev_average)
32-
# print("slope:", current_slope)
33-
return current_slope
34-
35-
@classmethod
36-
def direction_changed(cls, measurements, prev_direction):
37-
direction = cls.direction(measurements)
38-
return prev_direction != direction
39-
40-
def catagorize_pressure(self, pressure, prev_pressure):
26+
def catagorize_pressure(self, pressure):
4127
"""determine the strength and polarity of the pressure reading"""
4228
level = 0
43-
direction = 0
4429
polarity = 0
4530
abs_pressure = abs(pressure)
4631

@@ -55,16 +40,11 @@ def catagorize_pressure(self, pressure, prev_pressure):
5540
else:
5641
polarity = -1
5742

58-
if pressure > prev_pressure:
59-
direction = 1
60-
if pressure < prev_pressure:
61-
direction = -1
62-
63-
return (polarity, level, direction)
43+
return (polarity, level)
6444

6545
@staticmethod
6646
def pressure_string(pressure_type):
67-
polarity, level, direction = pressure_type # pylint:disable=unused-variable
47+
polarity, level = pressure_type # pylint:disable=unused-variable
6848
pressure_str = "HIGH"
6949
if level == 0 or polarity == 0:
7050
return ""
@@ -80,9 +60,51 @@ def pressure_string(pressure_type):
8060
pressure_str += "SIP"
8161
return pressure_str
8262

63+
def check_for_puff(self, current_pressure):
64+
puff_polarity = None
65+
puff_peak_level = None
66+
puff_duration = None
67+
#######################
68+
polarity, level = self.catagorize_pressure(current_pressure)
69+
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)
75+
76+
if level != 0 and self.start_polarity == 0: ###
77+
self.start_polarity = polarity
78+
self.puff_start = time.monotonic()
79+
puff_polarity = self.start_polarity
80+
81+
if self.start_polarity != 0:
82+
if level > self.peak_level:
83+
self.peak_level = level
84+
85+
if (level == 0) and (self.start_polarity != 0):
86+
self.duration = time.monotonic() - self.puff_start
87+
88+
puff_polarity = self.start_polarity
89+
puff_peak_level = self.peak_level
90+
puff_duration = self.duration
91+
92+
self.start_polarity = 0
93+
self.peak_level = 0
94+
self.duration = 0
95+
self.counter += 1
96+
return (puff_polarity, puff_peak_level, puff_duration)
97+
##############################################
98+
8399

84100
# pylint:disable=pointless-string-statement
85101
"""
102+
pressure_list = []
103+
prev_pressure_type = tuple()
104+
prev_polarity = None
105+
current_level = 0
106+
prev_level = 0
107+
86108
def old_detect(self):
87109
if pressure_type != prev_pressure_type:
88110
puff_end = time.monotonic()
@@ -114,4 +136,6 @@ def old_detect(self):
114136
if CONSOLE: print("____________________")
115137
prev_pressure_type = pressure_type
116138
prev_duration = puff_duration
139+
prev_level = level
140+
prev_level = level
117141
"""

0 commit comments

Comments
 (0)