-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadhex.py
More file actions
executable file
·142 lines (117 loc) · 5.75 KB
/
loadhex.py
File metadata and controls
executable file
·142 lines (117 loc) · 5.75 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
'''------------------------------------------------------------------------ upload.py
This is a quick reboot mechanism for loading code onto a stock arduino leonardo
It sends and recieves the Univeral midi sysex id request when it finds the a
device it sends the device a sysex to reset it. Then it waits to see where the
new device and uploads the hex file to that port.
Copyright (c) 2013 Donald Delmar Davis, Suspect Devices
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import rtmidi_python as rtmidi
import time
import sys
import serial
import serial.tools.list_ports
#from subprocess import call
import os
# this stuff should probably go in a constants file.
SYSEX_BEGIN =0xf0
SYSEX_NON_REAL_TIME =0x7e
SYSEX_NON_REAL =0x7f
SYSEX_ALL_CHANNELS =0x7f
SYSEX_GENERAL_INFO =0x06
SYSEX_IDENTITY_REQUEST =0x01
SYSEX_IDENTITY_REPLY =0x02
SYSEX_END_SYSEX =0xF7
ARDUINO_MMA_VENDOR_1 =0x7D
ARDUINO_MMA_VENDOR_2 =0x23
ARDUINO_MMA_VENDOR_3 =0x41
LGL_RESET_CMD =0x1f
LGL_BOOTLOADER_CMD =0x1e
we_are_done=False
askfortheids=[SYSEX_BEGIN,SYSEX_NON_REAL_TIME,SYSEX_ALL_CHANNELS,
SYSEX_GENERAL_INFO,SYSEX_IDENTITY_REQUEST,SYSEX_END_SYSEX]
resetthedevice=[SYSEX_BEGIN,
ARDUINO_MMA_VENDOR_1,
ARDUINO_MMA_VENDOR_2,
ARDUINO_MMA_VENDOR_3,
0x0a, #device/channel is currently ignored...
LGL_BOOTLOADER_CMD,
SYSEX_END_SYSEX]
# main code begins here.
'''
Monophonic....
This should poll all avaliable midi ports and channels until we find one that has one of
our devices on it.
eventually this should be expanded to handle multiple devices and understand multiple clients
'''
if (len(sys.argv)<2):
print "USAGE: %s <hex file>" % sys.argv[0]
exit()
hex_file_name = sys.argv[1]
we_are_done = False
try:
serial_ports_before=serial.tools.list_ports.comports()
midi_in = rtmidi.MidiIn()
midi_out = rtmidi.MidiOut()
if len(midi_in.ports):
print midi_in.ports
midi_in.ignore_types(False,False,False) # do not ignore sysexs or any other messeges.
midi_in.open_port(0)
midi_out.open_port(0)
midi_out.send_message(askfortheids)
while not we_are_done:
message, delta_time = midi_in.get_message()
if message:
if (len(message)>14 and message[0]==SYSEX_BEGIN): #if its a sysex message
if (message[1]==SYSEX_NON_REAL_TIME and message[3]==SYSEX_GENERAL_INFO
and message[4]==SYSEX_IDENTITY_REPLY ):
print 'MANUFACTURER ID: 0x%02X 0x%02X 0x%02X' % (
message[5],message[6],message[7])
print "FAMILY ID : 0x%02X%02x" % (
message[8], message[9])
print "PRODUCT ID : 0x%02X%02x" % (
message[10], message[11])
print "VERSION : 0x%02X%02x%02X%02x" % (
message[12],message[13],message[14],message[15])
if (message[15] == ord('!')) :
print "OK! LETS GET LOADED!!!"
we_are_done=True
midi_out.send_message(resetthedevice)
we_are_done=True
midi_in.close_port()
midi_out.close_port()
new_serial_ports=list(set(serial.tools.list_ports.comports())-set(serial_ports_before))
# the code below should have a timeout in case the os decides not to enumerate the
# new serial port. Which happens on mountain lion more than it should.
# wait for the bootloader to show up as a serial port.
while len(new_serial_ports) < 1 :
new_serial_ports=list(set(serial.tools.list_ports.comports())-set(serial_ports_before))
print new_serial_ports[0][0]
print "avrdude -p atmega32u4 -P %s -c avr109 -b57600 -U flash:w:%s" % (
new_serial_ports[0][0], hex_file_name
)
os.system("avrdude -p atmega32u4 -P %s -c avr109 -b57600 -U flash:w:%s" % (
new_serial_ports[0][0], hex_file_name
))
# Now we can call avrdude with the new serial port.
#exit()
else:
print "Bailing: No Midi Devices attached!"
exit()
except Exception as e:
print str(e)
exit()