-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c test server.py
More file actions
57 lines (41 loc) · 1.43 KB
/
i2c test server.py
File metadata and controls
57 lines (41 loc) · 1.43 KB
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
'''I2CPeripheral test program
** this requires firmware which includes the i2cperipheral library **
opens up I2C server mode with address 42
blinks the on-board neopixel blue on and off
waits for the server to send a single byte, which should be a "g"
this will change the neopixel to a solid green, indicating success.
if the server asks for a byte, will respond with "c" if the server is initialized
but hasn't received it's command yet, and "g" if it has.
'''
import time
import board
from i2cperipheral import I2CPeripheral
import neopixel
led = neopixel.NeoPixel(board.NEOPIXEL, 1)
led.brightness = 0.3
def set(r, g, b): led[0] = (r, g, b)
mode = 'c'
cycle = 0
device = I2CPeripheral(board.SCL, board.SDA, (0x42, 0x43))
print('peripheral registered')
while True:
print('perf cycle, m=%s, c=%d' % (mode, cycle))
if mode == '0': set(0, 0, 0)
elif mode == 'g': set(0, 255, 0)
else:
cycle += 1
if cycle >= 2: cycle = 0
set(0, 0, 150 * cycle)
time.sleep(0.5)
r = device.request()
if not r: continue
with r: # Closes the transfer if necessary by sending a NACK or feeding dummy bytes
print('got msg to addr %d' % r.address)
if r.is_read:
# Master is asking for data.
n = r.write(bytes(mode[0]))
else:
# Master is sending data.
b = r.read(1)
mode = str(b, 'ascii')
print('got new mode: %s' % mode)