-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathl58.py
104 lines (83 loc) · 3.02 KB
/
l58.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from machine import Pin, I2C
from collections import namedtuple
TouchData = namedtuple("TouchData", ["id", "state", "x", "y"])
class L58():
def __init__(self, bus, address):
self.bus = bus
self.address = address
self.touchData = []
def begin(self):
self.wakeup()
def readBytes(self, write, nbytes):
self.bus.start()
self.bus.writeto(self.address, write)
read = self.bus.readfrom(self.address, nbytes)
return list(read)
def clearFlags(self):
self.bus.writeto(self.address, [0xD0, 0x00, 0xAB])
def scanPoint(self):
sumL = 0
sumH = 0
pointData = []
buffer = self.readBytes([0xD0, 0x00], 7)
if buffer[0] == 0xAB:
self.clearFlags()
return 0
pointData.extend(buffer[0:5])
point = buffer[5] & 0x0F
if point == 1:
buffer = self.readBytes( [0xD0, 0x07], 2)
sumL = buffer[0] << 8 | buffer [1]
pointData.extend(buffer)
elif point > 1:
buffer = self.readBytes([0xD0, 0x07], 5 * (point - 1) + 3)
pointData.extend(buffer)
sumL = pointData[5 * point + 1] << 8 | pointData[5 * point + 2]
self.clearFlags()
for i in range(0, 5 * point):
sumH += pointData[i]
if sumH != sumL:
point = 0
if (point):
for i in range(0, point):
if i == 0:
offset = 0
else:
offset = 4
id = (pointData[i * 5 + offset] >> 4) & 0x0F
state = pointData[i * 5 + offset] & 0x0F
if state == 0x06:
state = 0x07
else:
state = 0x06
y = ((pointData[i * 5 + 1 + offset] << 4) | ((pointData[i * 5 + 3 + offset] >> 4) & 0x0F))
x = ((pointData[i * 5 + 2 + offset] << 4) | (pointData[i * 5 + 3 + offset] & 0x0F))
touchdata = TouchData(id = id, state= state, x = x, y = y)
self.touchData.append(touchdata)
# print(id, state, x, y)
else:
point = 1
id = pointData[0] >> 4 & 0x0F
state = 0x06
y = pointData[1] << 4 | pointData[3] >> 4 & 0x0F
x = pointData[2] << 4 | pointData[3] & 0x0F
touchdata = TouchData(id = id, state= state, x = x, y = y)
self.touchData.append(touchdata)
point = 1
# print(id, state, x, y)
return point
def getPoint(self):
return self.touchData.pop()
def sleep(self):
self.bus.writeto(self.address, bytearray([0xD1, 0x05]))
def wakeup(self):
self.bus.writeto(self.address, bytearray([0xD1, 0x06]))
if __name__ == '__main__':
from time import sleep
tp = L58(I2C(1, scl=Pin(14), sda=Pin(15)), 0x5A)
tp.begin()
while True:
for i in range(0, tp.scanPoint()):
data = tp.getPoint()
print(data)
sleep(0.1)