-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_frame_writer_demo.py
40 lines (28 loc) · 1.11 KB
/
serial_frame_writer_demo.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
import serial
import serial.tools.list_ports
import sys
from time import sleep, time
from numpy import sin
from tqdm import tqdm
baudrate = 115200
device = None
if len(sys.argv) >= 2:
device = sys.argv[1]
else:
# connect to the first com port in the list
comlist = serial.tools.list_ports.comports()
print(f'Available ports: {[c.device for c in comlist]}\nAttempting to connect to {comlist[0]}...')
device = comlist[0].device
ser = serial.Serial(device, baudrate, timeout = 1)
print(f'connected to {ser.name}')
disp = tqdm()
while True:
data = b'[.' # start the frame, specify 64 pixels of data
for i in range(64): # for each pixel
data += bytes( [int( 127*sin(2*time()) + 127), 5, i*2] ) # the rgb values for this pixel
data += b']' # end the frame (arduino updates the display)
# print(''.join([str(chr(c)) for c in data])) # print the string we sent
ser.write(data)
disp.update()
# it may be necessary to sleep for a while at this point if the arduino takes a long time to
# write to the pixels, which will be the case when you have more than a couple hundred leds