1
+ import time
2
+
3
+ PRINT_FLOOR = 5
4
+ CONSOLE = True
5
+ DEBUG = True
6
+
7
+
1
8
class PuffDetector :
2
9
def __init__ (self , min_pressure = 8 , high_pressure = 20 ):
3
10
self .high_pressure = high_pressure
4
11
self .min_pressure = min_pressure
5
12
13
+ self .start_polarity = 0
14
+ self .peak_level = 0
15
+ self .counter = 0
16
+ self .duration = 0
17
+ self .puff_start = 0
18
+
6
19
@classmethod
7
20
def rolling_average (cls , measurements , window_size = 3 ):
8
21
# print("measurements", measurements)
9
22
window = measurements [- window_size :]
10
23
11
24
return sum (window ) / window_size
12
25
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 ):
41
27
"""determine the strength and polarity of the pressure reading"""
42
28
level = 0
43
- direction = 0
44
29
polarity = 0
45
30
abs_pressure = abs (pressure )
46
31
@@ -55,16 +40,11 @@ def catagorize_pressure(self, pressure, prev_pressure):
55
40
else :
56
41
polarity = - 1
57
42
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 )
64
44
65
45
@staticmethod
66
46
def pressure_string (pressure_type ):
67
- polarity , level , direction = pressure_type # pylint:disable=unused-variable
47
+ polarity , level = pressure_type # pylint:disable=unused-variable
68
48
pressure_str = "HIGH"
69
49
if level == 0 or polarity == 0 :
70
50
return ""
@@ -80,9 +60,51 @@ def pressure_string(pressure_type):
80
60
pressure_str += "SIP"
81
61
return pressure_str
82
62
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 \t pressure:" , 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
+
83
99
84
100
# pylint:disable=pointless-string-statement
85
101
"""
102
+ pressure_list = []
103
+ prev_pressure_type = tuple()
104
+ prev_polarity = None
105
+ current_level = 0
106
+ prev_level = 0
107
+
86
108
def old_detect(self):
87
109
if pressure_type != prev_pressure_type:
88
110
puff_end = time.monotonic()
@@ -114,4 +136,6 @@ def old_detect(self):
114
136
if CONSOLE: print("____________________")
115
137
prev_pressure_type = pressure_type
116
138
prev_duration = puff_duration
139
+ prev_level = level
140
+ prev_level = level
117
141
"""
0 commit comments