Skip to content

Commit c135927

Browse files
committed
creates a common API for the touch/pointer drivers
1 parent b710da7 commit c135927

File tree

16 files changed

+925
-681
lines changed

16 files changed

+925
-681
lines changed

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ for the binding.
1616
### *New changes*
1717
___________________________
1818

19+
ALL MCU's
20+
I have started to nail down a commoin API for the indev drivers, specifically the pointer/touch drivers.
21+
In order to do this I had to change the handling of the type of bus being used. Just like the displays the
22+
touch/pointer driver IC's can sometimes accept an SPI bus or an I2C bus as the way to communicate.
23+
Instead of having to duplicate code for these driver IC's I decided to make the software driver completely
24+
unaware of the bus that is being used. To do this i made the I2C driver work in the same manner as the SPI driver.
25+
26+
Here is a code example of how to use the I2C bus with a touch driver.
27+
28+
29+
from i2c import I2C
30+
import ft5x06
31+
32+
i2c_bus = I2C.Bus(host=1, sda=10, sdl=11)
33+
touch_i2c = I2C.Device(i2c_bus, ft5x06.I2C_ADDR, ft5x06.BITS)
34+
touch = ft5x06.FT5x06(touch_i2c)
35+
36+
37+
If a touch driver doesn't have the variable `I2C_ADDR` or `BITS` then that driver
38+
doesn't support the I2C bus.
39+
40+
1941
ESP32-ALL
2042
* `--optimize-size`: If you are having an issue with getting the firmware to fit into your esp32
2143
or if space is more of a concern than speed you can set this command line option. This will tell the compiler that the
@@ -35,23 +57,23 @@ ESP32-ALL
3557
They exactly what they seem. It is easier to show a code example then it is to explain it.
3658

3759

38-
import machine
60+
from machine import SPI
3961

40-
spi_bus = machine.SPI.Bus(
62+
spi_bus = SPI.Bus(
4163
host=1,
4264
mosi=15,
4365
miso=16,
4466
sck=10
4567
)
4668

47-
spi_device = machine.SPI.Device(
69+
spi_device = SPI.Device(
4870
spi_bus=spi_bus,
4971
freq=10000000,
5072
cs=3,
5173
polarity=0,
5274
phase=0,
5375
bits=8,
54-
first_bit=machine.SPI.MSB
76+
first_bit=SPI.MSB
5577
)
5678

5779
# if you want to delete a device from being used you have to deinit it first
@@ -66,6 +88,7 @@ ESP32-ALL
6688
# The SPI.Bus instance you need to pass to machine.SDCard, lcd_bus.SPIBus
6789
# and any of the touch drivers that use SPI.
6890

91+
6992
All methods that existed for the original `machine.SPI` are available in
7093
the `machine.SPI.Device` class. They work exactly how they did before.
7194

Lines changed: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,132 @@
11
import machine
22

33

4-
class I2CBus(object):
4+
class I2C(object):
5+
class Bus(object):
6+
7+
def __init__(self, host, scl, sda, freq=400000, use_locks=False):
8+
self._bus = machine.I2C(
9+
host,
10+
scl=machine.Pin(scl),
11+
sda=machine.Pin(sda),
12+
freq=freq
13+
)
514

6-
def __init__(self, scl, sda, freq=400000, host=None, use_locks=False):
7-
if host is None:
8-
if (scl, sda) == (19, 18):
9-
host = 0
15+
if use_locks:
16+
import _thread
17+
self._lock = _thread.allocate_lock()
1018
else:
11-
host = 1
12-
13-
self._bus = machine.I2C(
14-
host,
15-
scl=machine.Pin(scl),
16-
sda=machine.Pin(sda),
17-
freq=freq
18-
)
19-
20-
if use_locks:
21-
import _thread
22-
self._lock = _thread.allocate_lock()
23-
else:
24-
class Lock(object):
19+
class Lock(object):
2520

26-
def acquire(self):
27-
pass
21+
def acquire(self):
22+
pass
2823

29-
def release(self):
30-
pass
24+
def release(self):
25+
pass
3126

32-
def is_locked(self):
33-
return False
27+
def is_locked(self):
28+
return False
3429

35-
self._lock = Lock()
30+
self._lock = Lock()
3631

37-
def __enter__(self):
38-
self._lock.acquire()
39-
return self
32+
def __enter__(self):
33+
self._lock.acquire()
34+
return self
4035

41-
def __exit__(self, exc_type, exc_val, exc_tb):
42-
self._lock.release()
36+
def __exit__(self, exc_type, exc_val, exc_tb):
37+
self._lock.release()
4338

44-
def scan(self):
45-
self._lock.acquire()
46-
data = self._bus.scan()
47-
self._lock.release()
48-
return data
39+
def scan(self):
40+
self._lock.acquire()
41+
data = self._bus.scan()
42+
self._lock.release()
43+
return data
4944

50-
def start(self):
51-
self._bus.start()
45+
def start(self):
46+
self._bus.start()
5247

53-
def stop(self):
54-
self._bus.stop()
48+
def stop(self):
49+
self._bus.stop()
5550

56-
def readinto(self, buf, nack=True):
57-
self._bus.readinto(buf, nack)
51+
def readinto(self, buf, nack=True):
52+
self._bus.readinto(buf, nack)
5853

59-
def write(self, buf):
60-
self._bus.write(buf)
54+
def write(self, buf):
55+
self._bus.write(buf)
6156

62-
def readfrom(self, addr, nbytes, stop=True):
63-
return self._bus.readfrom(addr, nbytes, stop)
57+
def readfrom(self, addr, nbytes, stop=True):
58+
return self._bus.readfrom(addr, nbytes, stop)
6459

65-
def readfrom_into(self, addr, buf, stop=True):
66-
self._bus.readfrom_into(addr, buf, stop)
60+
def readfrom_into(self, addr, buf, stop=True):
61+
self._bus.readfrom_into(addr, buf, stop)
6762

68-
def writeto(self, addr, buf, stop=True):
69-
self._bus.writeto(addr, buf, stop)
63+
def writeto(self, addr, buf, stop=True):
64+
self._bus.writeto(addr, buf, stop)
7065

71-
def writevto(self, addr, vector, stop=True):
72-
self._bus.writevto(addr, vector, stop)
66+
def writevto(self, addr, vector, stop=True):
67+
self._bus.writevto(addr, vector, stop)
7368

74-
def readfrom_mem(self, addr, memaddr, nbytes, addrsize=8):
75-
return self._bus.readfrom_mem(addr, memaddr, nbytes, addrsize=addrsize)
69+
def readfrom_mem(self, addr, memaddr, nbytes, addrsize=8):
70+
return self._bus.readfrom_mem(addr, memaddr, nbytes, addrsize=addrsize)
7671

77-
def readfrom_mem_into(self, addr, memaddr, buf, addrsize=8):
78-
self._bus.readfrom_mem_into(addr, memaddr, buf, addrsize=addrsize)
72+
def readfrom_mem_into(self, addr, memaddr, buf, addrsize=8):
73+
self._bus.readfrom_mem_into(addr, memaddr, buf, addrsize=addrsize)
7974

80-
def writeto_mem(self, addr, memaddr, buf, addrsize=8):
81-
self._bus.writeto_mem(addr, memaddr, buf, addrsize=addrsize)
75+
def writeto_mem(self, addr, memaddr, buf, addrsize=8):
76+
self._bus.writeto_mem(addr, memaddr, buf, addrsize=addrsize)
8277

83-
def add_device(self, dev_id, reg_bits=8):
84-
return I2CDevice(self, dev_id, reg_bits)
78+
class Device(object):
8579

80+
def __init__(self, bus, dev_id, reg_bits=8):
81+
self._bus = bus
82+
self.dev_id = dev_id
83+
self._reg_bits = reg_bits
8684

87-
class I2CDevice(object):
85+
def write_readinto(self, write_buf, read_buf):
86+
memaddr = 0
87+
for i in range(int(self._reg_bits / 8)):
88+
memaddr |= write_buf[i] << i
89+
self.read_mem(memaddr, buf=read_buf)
8890

89-
def __init__(self, bus, dev_id, reg_bits=8):
90-
self._bus = bus
91-
self.dev_id = dev_id
92-
self._reg_bits = reg_bits
91+
def read_mem(self, memaddr, num_bytes=None, buf=None):
92+
with self._bus:
93+
if num_bytes is not None:
94+
return self._bus.readfrom_mem(
95+
self.dev_id,
96+
memaddr,
97+
num_bytes,
98+
addrsize=self._reg_bits
99+
)
100+
else:
101+
self._bus.readfrom_mem_into(
102+
self.dev_id,
103+
memaddr,
104+
buf,
105+
addrsize=self._reg_bits
106+
)
107+
return
93108

94-
def read_mem(self, memaddr, num_bytes=None, buf=None):
95-
with self._bus:
96-
if num_bytes is not None:
97-
return self._bus.readfrom_mem(
98-
self.dev_id,
99-
memaddr,
100-
num_bytes,
101-
addrsize=self._reg_bits
102-
)
103-
else:
104-
self._bus.readfrom_mem_into(
109+
def write_mem(self, memaddr, buf):
110+
with self._bus:
111+
self._bus.writeto_mem(
105112
self.dev_id,
106113
memaddr,
107114
buf,
108115
addrsize=self._reg_bits
109116
)
110-
return
111-
112-
def write_mem(self, memaddr, buf):
113-
with self._bus:
114-
self._bus.writeto_mem(
115-
self.dev_id,
116-
memaddr,
117-
buf,
118-
addrsize=self._reg_bits
119-
)
120117

121-
def read(self, num_bytes=None, buf=None):
122-
with self._bus:
123-
if num_bytes is not None:
124-
return self._bus.readfrom(self.dev_id, num_bytes)
118+
def read(self, num_bytes=None, buf=None):
119+
with self._bus:
120+
if num_bytes is not None:
121+
return self._bus.readfrom(self.dev_id, num_bytes)
122+
else:
123+
self._bus.readfrom_into(self.dev_id, buf)
124+
125+
def write(self, buf):
126+
memaddr = 0
127+
for i in range(int(self._reg_bits / 8)):
128+
memaddr |= buf[i] << i
125129
else:
126-
self._bus.readfrom_into(self.dev_id, buf)
130+
i = int(self._reg_bits / 8) - 1
127131

128-
def write(self, buf):
129-
with self._bus:
130-
self._bus.writeto(self.dev_id, buf)
132+
self.write_mem(memaddr, buf=buf[i + 1:])

0 commit comments

Comments
 (0)