|
4 | 4 | import adafruit_lps35hw
|
5 | 5 | from puff_detector import PuffDetector
|
6 | 6 |
|
7 |
| - |
8 |
| - |
9 |
| - |
10 | 7 | i2c = busio.I2C(board.SCL, board.SDA)
|
11 | 8 | # i2c = DebugI2C(i2c)
|
12 | 9 | lps = adafruit_lps35hw.LPS35HW(i2c, 0x5C)
|
| 10 | +CONSOLE = True |
| 11 | +DEBUG = True |
13 | 12 |
|
14 | 13 | lps.zero_pressure()
|
15 | 14 | lps.data_rate = adafruit_lps35hw.DataRate.RATE_75_HZ
|
16 | 15 |
|
17 | 16 | min_pressure = 8
|
18 | 17 | high_pressure = 20
|
19 |
| -# sip/puff is "over" on keyup/pressure direction reversal |
| 18 | +# sip/puff is "over" on keyup/pressure polarity reversal |
20 | 19 | # averaging code
|
21 | 20 |
|
22 | 21 | lps.filter_enabled = True
|
23 |
| -# print("Filter enabled:", lps.low_pass_enabled) |
| 22 | +# if CONSOLE: print("Filter enabled:", lps.low_pass_enabled) |
24 | 23 |
|
25 | 24 | lps.filter_config = True
|
26 |
| -# print("Filter Config:", lps.low_pass_config) |
| 25 | +# if CONSOLE: print("Filter Config:", lps.low_pass_config) |
27 | 26 | detector = PuffDetector(min_pressure=8, high_pressure=20)
|
28 | 27 | time.sleep(1)
|
29 |
| -prev_direction = None |
| 28 | +prev_polarity = None |
30 | 29 | pressure_list = []
|
31 | 30 | pressure_type = tuple()
|
32 | 31 | prev_pressure_type = tuple()
|
|
35 | 34 | prev_level = 0
|
36 | 35 | puff_end = puff_start
|
37 | 36 | prev_duration = 0
|
| 37 | +start_polarity = 0 |
| 38 | +prev_pressure = 0 |
| 39 | +PRINT_FLOOR = 5 |
| 40 | +if CONSOLE: |
| 41 | + print("CONSOLE?") |
| 42 | +if DEBUG and CONSOLE: |
| 43 | + print("Debug Console") |
| 44 | +counter = 0 |
38 | 45 | while True:
|
39 | 46 | current_pressure = lps.pressure
|
40 |
| - # print((current_pressure,)) |
41 |
| - |
42 |
| - pressure_type = detector.catagorize_pressure(current_pressure) |
43 |
| - |
44 |
| - if pressure_type != prev_pressure_type: |
45 |
| - puff_end = time.monotonic() |
46 |
| - puff_duration = puff_end - puff_start |
47 |
| - puff_start = puff_end |
48 |
| - # print("\tpressure type:", pressure_type) |
49 |
| - # print("duration:", puff_duration) |
50 |
| - direction, level = pressure_type |
51 |
| - # print("direction:", direction, "level:", level) |
| 47 | + if not CONSOLE: |
| 48 | + print((current_pressure,)) |
52 | 49 |
|
53 |
| - if (direction == 1) and (prev_level > level): |
54 |
| - print("Down") |
55 |
| - puff_duration += prev_duration |
56 |
| - level = prev_level |
57 |
| - if (direction == -1) and (prev_level < level): |
58 |
| - print("Up") |
59 |
| - puff_duration += prev_duration |
60 |
| - level = prev_level |
| 50 | + pressure_type = detector.catagorize_pressure(current_pressure, prev_pressure) |
| 51 | + polarity, level, direction = pressure_type |
| 52 | + # if (polarity != 0) or (level != 0): |
| 53 | + if abs(current_pressure) > PRINT_FLOOR: |
| 54 | + if counter % 4 == 0: |
| 55 | + if DEBUG and CONSOLE: |
| 56 | + print("\t\t\tpressure:", current_pressure) |
61 | 57 |
|
62 |
| - # print("direction:", direction, "level:", level) |
63 |
| - if puff_duration > 0.2: |
64 |
| - print("direction:", direction, "level:", level) |
| 58 | + if level != 0 and start_polarity == 0: ### |
| 59 | + start_polarity = polarity |
| 60 | + puff_start = time.monotonic() |
| 61 | + if CONSOLE: |
| 62 | + print("START", end=" ") |
| 63 | + if start_polarity == 1: |
| 64 | + print("PUFF") |
| 65 | + if start_polarity == -1: |
| 66 | + print("SIP") |
65 | 67 |
|
66 |
| - print("\tduration:", puff_duration) |
67 |
| - # print(current_pressure) |
68 |
| - label = detector.pressure_string((direction, level)) |
69 |
| - label = detector.pressure_string(pressure_type) |
70 |
| - print("\t\t\t\t", label) |
71 |
| - print("____________________") |
72 |
| - prev_pressure_type = pressure_type |
73 |
| - prev_duration = puff_duration |
| 68 | + if (level == 0) and (start_polarity != 0): |
| 69 | + duration = time.monotonic() - puff_start |
| 70 | + if CONSOLE: |
| 71 | + print("END", end=" ") |
| 72 | + if start_polarity == 1: |
| 73 | + print("PUFF", "") |
| 74 | + if start_polarity == -1: |
| 75 | + print("SIP", "") |
| 76 | + print("Duration:", duration) |
| 77 | + start_polarity = 0 |
74 | 78 | prev_level = level
|
| 79 | + prev_pressure = current_pressure |
75 | 80 | time.sleep(0.01)
|
| 81 | + counter += 1 |
0 commit comments