-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatter.py
More file actions
executable file
·79 lines (61 loc) · 2.11 KB
/
chatter.py
File metadata and controls
executable file
·79 lines (61 loc) · 2.11 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python2
import sys
import os
from PIL import Image
import serial
import time
from subprocess import call
SERIAL_BUFFER_SIZE = 32
SERIAL_PORT = '/dev/ttyACM0'
SERIAL_BAUD = 9600
TIMEOUT = 2000
MARQUEES_PATH = '/emulators/mame/scripts/marquees'
def millis():
return int(round(time.time() * 1000))
def main(romname):
filename = os.path.join(MARQUEES_PATH, romname + '.png')
if not os.path.exists(filename):
sys.exit(1)
im = Image.open(filename)
pixels = list(im.getdata())
if len(pixels) != 256:
print("Image is of the wrong size, must be 32x8 pixel PNG file")
sys.exit(1)
image = []
for pixel in pixels:
image.append(pixel[0])
image.append(pixel[1])
image.append(pixel[2])
call([
"stty", "-F", SERIAL_PORT, str(SERIAL_BAUD), "time", "0", "-hupcl", "-brkint", "-icrnl", "-imaxbel", "-opost", "-isig",
"-icanon", "-iexten", "-echo", "-echoe", "-echok", "-echoctl", "-echoke"
])
ser = serial.Serial(SERIAL_PORT, SERIAL_BAUD)
time.sleep(2)
ser.write('mode_data_1\n')
in_buffer = ""
try:
start = millis()
while in_buffer[-5:] != "READY":
in_buffer += ser.read(ser.inWaiting())
if millis() - start > TIMEOUT:
print('Buffer: %s' % in_buffer)
raise Exception('Handshake timeout')
for position in range(0, len(image), SERIAL_BUFFER_SIZE):
ser.write(image[position:position + SERIAL_BUFFER_SIZE])
in_buffer = ""
start = millis()
while in_buffer[-5:] != "BLOCK" and in_buffer[-6:] != "FINISH":
if millis() - start > TIMEOUT:
print("Exception hit: '" + in_buffer + "'")
raise Exception('Transfer timeout')
in_buffer += ser.read(ser.inWaiting())
print("Written %d bytes" % SERIAL_BUFFER_SIZE)
print("Image sent")
except Exception as error:
print(error)
if __name__ == "__main__":
if len(sys.argv) == 1:
print("No romname specified")
sys.exit(1)
main(sys.argv[1])