-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_mpr121.py
62 lines (49 loc) · 1.84 KB
/
detect_mpr121.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import sys
import time
import board
import busio
import adafruit_mpr121
from lib.functions import load_json
from lib.player import play
# GLOBAL VARIABLES
PINS = 12
AVERAGE_TIMES = 40
def calculate_average(pins):
for i in range(PINS):
pins[i] = sum(pins[i]) / len(pins[i])
return pins
def run():
print("============================================")
print("========= RPI TOUCHING EXPERIENCE ==========")
print("============================================")
# Count amount of touch events happening
touch_count = 0
average_loop_count = 1
# Set sensor sensitivity
#adafruit_mpr121.MPR121_TOUCHTH_0 = 50
#adafruit_mpr121.MPR121_RELEASETH_0 = 51
# Start sensor
i2c = busio.I2C(board.SCL, board.SDA)
mpr121 = adafruit_mpr121.MPR121(i2c)
average_per_pin = False
total_per_pin = [[] for i in range(PINS)]
while True:
# Loop through all 12 inputs (0-11).
for i in range(PINS):
if average_loop_count <= AVERAGE_TIMES:
total_per_pin[i].append(mpr121[i].raw_value)
if average_loop_count == AVERAGE_TIMES and i == (PINS - 1) and not average_per_pin:
average_per_pin = calculate_average(total_per_pin)
print("AVERAGE PER PIN", average_per_pin)
print("\nWAITING FOR TOUCH EVENTS...\n")
# Call is_touched and pass it then number of the input. If it's touched
# it will return True, otherwise it will return False.
if average_per_pin and mpr121[i].value:
touch_count += 1
print(f'{touch_count}: PIN {i} TH {mpr121[i].raw_value}')
# play(i)
time.sleep(0.25) # Small delay to keep from spamming output messages.
average_loop_count += 1
if __name__ == '__main__':
run()