-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmulti_printer.py
More file actions
66 lines (45 loc) · 1.76 KB
/
multi_printer.py
File metadata and controls
66 lines (45 loc) · 1.76 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
import serial
import time
import os
from note_converter import *
from printer import Printer
directory = '../midi/'
files = os.listdir(directory)
print("Available songs:")
for i in range(len(files)):
print(' ', i, files[i])
file_index = int(input("Enter index of file: "))
FILENAME = os.path.join(directory, files[file_index])
all_tracks = notes_from_file(FILENAME)
print('\nAvailable tracks:')
for channel in all_tracks:
print(' ', channel, len(all_tracks[channel]))
track_nums = input("Enter tracks to use separated by spaces: ").split()
track_nums = [int(num) for num in track_nums]
tracks = [all_tracks[i] for i in track_nums]
serial_ports = ['/dev/ttyACM' + str(i) for i in range(len(tracks))]
printers = [Printer(port) for port in serial_ports]
for printer in printers:
printer.init()
startup_commands = ['G21', 'G90', 'G28', 'G1 Y{}'.format(START)]
for command in startup_commands:
while not all([printer.ready() for printer in printers]):
time.sleep(0.001)
for printer in printers:
printer.send(command)
print(command)
track_range = range(len(tracks))
note_index = [0 for i in track_range]
positions = [START for i in track_range]
time.sleep(5)
start_time = time.time()
print("Starting program...")
while any([note_index[i] < len(tracks[i]) for i in track_range]):
current_time = time.time()
for i in track_range:
if note_index[i] < len(tracks[i]) and tracks[i][note_index[i]].time / 1000 + start_time <= current_time:
gcode, pos = convert_note(tracks[i][note_index[i]], positions[i])
positions[i] = pos
print(i, repr(gcode), tracks[i][note_index[i]].time / 1000, tracks[i][note_index[i]].length / 1000)
printers[i].send(gcode.strip())
note_index[i] += 1