-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_helper.py
252 lines (180 loc) · 8.22 KB
/
arduino_helper.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import re
import subprocess
import os
import requests
from zipfile import ZipFile
cli_path = 'NOT SET'
exe_path = 'NOT SET'
if os.name == 'nt':
cli_path = os.path.join('.', 'arduino-cli', 'windows')
exe_path = os.path.join(cli_path, 'arduino-cli.exe')
elif os.name == 'posix':
cli_path = os.path.join('.', 'arduino-cli', 'mac_linux')
exe_path = os.path.join(cli_path, 'arduino-cli')
# installing the arduino-cli
def install_arduino_cli():
# check if the arduino-cli is installed
try:
vercmd = os.path.join(cli_path, 'arduino-cli.exe') + ' version'
subprocess.run(vercmd, shell=True, check=True)
except subprocess.CalledProcessError:
# install the arduino-cli
# if windows, download the cli exe
if os.name == 'nt':
print("Downloading Windows arduino-cli...")
try:
os.mkdir(os.path.join('.', 'arduino-cli'))
os.mkdir(os.path.join('.', 'arduino-cli', 'windows'))
except:
print('folder exists!')
raise
exit(1)
url = "https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Windows_64bit.zip"
r = requests.get(url)
with open(os.path.join(cli_path, "arduino-cli.zip"), "wb") as f:
f.write(r.content)
# extract the zip file
with ZipFile(os.path.join(cli_path, "arduino-cli.zip"), 'r') as zip_ref:
zip_ref.extractall(cli_path)
# remove the zip file and license
os.remove(os.path.join(cli_path, "arduino-cli.zip"))
os.remove(os.path.join(cli_path, "LICENSE.txt"))
elif os.name == 'posix':
print("Downloading Mac/Linux arduino-cli...")
try:
os.mkdir(os.path.join('.', 'arduino-cli'))
os.mkdir(os.path.join('.', 'arduino-cli', 'mac_linux'))
except:
print('folder exists!')
return
posixcmd = 'curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=./arduino-cli/mac_linux sh'
# run the command
subprocess.run(posixcmd, shell=True, check=True)
else:
print("OS not supported")
print("How did you get here?")
return
# run the arduino-cli with the given arguments
def run_arduino_cli(args: list[str]):
cmd = exe_path + ' '
cmd += '--additional-urls https://adafruit.github.io/arduino-board-index/package_adafruit_index.json '
for arg in args:
cmd += arg + ' '
try :
return subprocess.run(cmd, text=True, shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
print("Error running command: " + cmd)
print("Error code:\n" + e.stderr)
# compile, upload, and verify the sketch at the given path (path should be the .ino file)
def compile_upload_verify(port: str, fqbn: str, sketch_path: str, usbstack='arduino'):
# verify the path is valid
if not os.path.exists(sketch_path):
print("Invalid path")
# exit(1)
# shaboom
result = run_arduino_cli(['compile', '-p', port, '-b', fqbn + ':usbstack=' + usbstack, sketch_path, '-u', '-t', '--clean'])
return result
# install necessary cores (idk what theyre actually called) for the M0 board
def install_M0_reqs():
out = []
# install the Adafruit SAMD board library
out.append(run_arduino_cli(['core', 'install', 'adafruit:samd']))
# install the Adafruit AVR board library
out.append(run_arduino_cli(['core', 'install', 'adafruit:avr']))
# install the Arduino SAMD board library
out.append(run_arduino_cli(['core', 'install', 'arduino:samd']))
# install the Arduino SAM board library
out.append(run_arduino_cli(['core', 'install', 'arduino:sam']))
# install the Arduino AVR board library
out.append(run_arduino_cli(['core', 'install', 'arduino:avr']))
return out
# install libraries
def install_default_libs():
out = []
# install the RTCZero library
out.append(run_arduino_cli(['lib', 'install', 'RTCZero']))
# install the Adafruit NeoPixel library
out.append(run_arduino_cli(['lib', 'install', '"Adafruit NeoPixel"']))
# install the Adafruit SPIFlash library
out.append(run_arduino_cli(['lib', 'install', '"Adafruit SPIFlash"']))
# install the Adafruit SdFat library
out.append(run_arduino_cli(['lib', 'install', '"SdFat - Adafruit Fork"']))
# install the Adafruit TinyUSB library
out.append(run_arduino_cli(['lib', 'install', '"Adafruit TinyUSB Library"']))
# install the Adafruit ZeroDMA library
out.append(run_arduino_cli(['lib', 'install', '"Adafruit Zero DMA Library"']))
# install the Adafruit LibPrintf library
out.append(run_arduino_cli(['lib', 'install', '"LibPrintf"']))
return out
# returns a list of tuples of the port, FQBN, and core of all connected boards
# data may be imperfect so must be checked
def get_board_data():
# check if a string is a valid looking FQBN
def is_fqbn_valid(fqbn: str):
# Define a regular expression pattern for the desired format
pattern = r'^[^\s:]+:[^\s:]+:[^\s:]+$'
# Use re.match to check if the input string matches the pattern
match = re.match(pattern, fqbn)
return None if match is None else match.group()
# check if a string is a valid looking core
def is_core_valid(core: str):
# Define a regular expression pattern for the desired format
pattern = r'^[^\s:]+:[^\s:]+$'
# Use re.match to check if the input string matches the pattern
match = re.match(pattern, core)
return None if match is None else match.group()
# get the list of connected boards
board_list_data = run_arduino_cli(['board', 'list'])
# split the output by newlines
entries = board_list_data.stdout.split('\n')
# make sure there's at least 1 connected board
if len(entries) < 2:
return None
# entries[0] is the header/labels
entries = entries[1:]
grabbed_data = []
for entry in entries:
port, FQBN, core = None, None, None
# split the entry by spaces (gets messy)
entry = entry.split(' ')
# find the port (its the first entry lol)
# looks like "COM3" or "/dev/ttyACM0"
port = entry[0]
# find the FQBN (it should look like "xxx:yyy:zzz")
for j in entry:
if is_fqbn_valid(j) is not None:
FQBN = j
break
# find the core (it should look like "xxx:yyy")
for j in entry:
if is_core_valid(j) is not None:
core = j
break
grabbed_data.append((port, FQBN, core))
return grabbed_data
if __name__ == '__main__':
# install the arduino-cli, board reqs, and lib reqs if not installed
install_arduino_cli()
print('CLI installed')
install_M0_reqs()
print('M0 reqs installed')
install_default_libs()
print('Default libs installed')
boards = get_board_data()
print('found boards:')
for n, board in enumerate(boards):
print(f'{n}.', board)
# pick the right board manually :(
board_num = int(input('Enter the number of the board to use: '))
port, FQBN, core = boards[board_num]
print("Port: " + port)
print("FQBN: " + FQBN)
print("Core: " + core)
print('---------------------------------')
# compile_upload_verify(port, FQBN, '"C:\\Users\\SEVAK\\Documents\\GitHub\\IRIS-Project\\sandbox\\M0\\mass storage andrew\\msc_sdfat\\msc_sdfat.ino"', usbstack='tinyusb')
# compile_upload_verify(port, FQBN, '"/home/paelen/Documents/GitHub/IRIS-Project/sandbox/M0/SdFat/datalogger_tAv_bin/datalogger_tAv_bin.ino"', usbstack='tinyusb')
# compile_upload_verify(port, FQBN, '"/home/paelen/paelen.ino"')
while True:
# get input and split by space into list
args = input("Enter command: ").split(' ')
print(run_arduino_cli(args).stdout)