-
Notifications
You must be signed in to change notification settings - Fork 6
/
TTP229_BSF.py
41 lines (37 loc) · 1.25 KB
/
TTP229_BSF.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
# MicroPython ESP8266 driver for TTP229-BSF 16-key capacitive keypad in serial interface mode
# by Alan Wang
import utime
class Keypad:
def __init__(self, scl, sdo, inputs=8, multi=False, raw=False):
self._scl_pin = scl
self._sdo_pin = sdo
self._inputs = inputs
self._multi_mode = multi
self._raw_mode = raw
def read(self):
key = [1] * self._inputs
self._scl_pin.on()
utime.sleep_ms(1)
for i in range(self._inputs):
self._scl_pin.off()
utime.sleep_ms(1)
key[i] = self._sdo_pin.value()
self._scl_pin.on()
utime.sleep_ms(1)
utime.sleep_ms(1)
if self._raw_mode:
return tuple(key)
else:
if self._multi_mode:
key_multi = []
for i in range(self._inputs):
if key[i] == 0:
key_multi.append(i)
return tuple(key_multi)
else:
key_single = -1
for i in range(self._inputs):
if key[i] == 0:
key_single = i
break
return key_single