-
Notifications
You must be signed in to change notification settings - Fork 74
/
pico_project.py
executable file
·1491 lines (1206 loc) · 58.2 KB
/
pico_project.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#
# Copyright (c) 2020-2023 Raspberry Pi (Trading) Ltd.
#
# SPDX-License-Identifier: BSD-3-Clause
#
import argparse
import os
import shutil
from pathlib import Path
import sys
import subprocess
import platform
import shlex
import csv
# TODO: conditional import of tkinter if --gui option is used
try:
import tkinter as tk
from tkinter import \
messagebox as mb, \
filedialog as fd, \
simpledialog as sd, \
ttk
except ImportError:
print("[\033[91mERROR\033[0m] tkinter module not found")
sys.exit(1)
class ExitCodes:
"""Exit codes per reason; range starts at 0xA00 to avoid clashing with OS reserved exit codes"""
SUCCESS = 0
INVALID_PROJECT_PATH = -0xA01
EXISTING_PROJECT_NO_OVERWRITE = -0xA02
NO_COMPILER_FOUND = -0xA03
NO_PROJECT_NAME = -0xA04
PICO_SDK_NOT_FOUND = -0xA05
CMAKELIST_FILENAME = 'CMakeLists.txt'
CMAKECACHE_FILENAME = 'CMakeCache.txt'
COMPILER_NAME = 'arm-none-eabi-gcc'
VSCODE_LAUNCH_FILENAME = 'launch.json'
VSCODE_C_PROPERTIES_FILENAME = 'c_cpp_properties.json'
VSCODE_SETTINGS_FILENAME ='settings.json'
VSCODE_EXTENSIONS_FILENAME ='extensions.json'
VSCODE_FOLDER='.vscode'
CONFIG_UNSET="Not set"
# Standard libraries for all builds
# And any more to string below, space separator
STANDARD_LIBRARIES = 'pico_stdlib'
# Indexed on feature name, tuple contains the C file, the H file and the CMake project name for the feature.
# Some lists may contain an extra/ancillary file needed for that feature
GUI_TEXT = 0
C_FILE = 1
H_FILE = 2
LIB_NAME = 3
ANCILLARY_FILE = 4
features_list = {
'spi' : ("SPI", "spi.c", "hardware/spi.h", "hardware_spi"),
'i2c' : ("I2C interface", "i2c.c", "hardware/i2c.h", "hardware_i2c"),
'dma' : ("DMA support", "dma.c", "hardware/dma.h", "hardware_dma"),
'pio' : ("PIO interface", "pio.c", "hardware/pio.h", "hardware_pio"),
'interp' : ("HW interpolation", "interp.c", "hardware/interp.h", "hardware_interp"),
'timer' : ("HW timer", "timer.c", "hardware/timer.h", "hardware_timer"),
'watch' : ("HW watchdog", "watch.c", "hardware/watchdog.h", "hardware_watchdog"),
'clocks' : ("HW clocks", "clocks.c", "hardware/clocks.h", "hardware_clocks"),
}
picow_options_list = {
'picow_none' : ("None", "", "", "", ""),
'picow_led' : ("PicoW onboard LED", "", "pico/cyw43_arch.h", "pico_cyw43_arch_none", ""),
'picow_poll' : ("Polled lwIP", "", "pico/cyw43_arch.h", "pico_cyw43_arch_lwip_poll", "lwipopts.h"),
'picow_background' :("Background lwIP", "", "pico/cyw43_arch.h", "pico_cyw43_arch_lwip_threadsafe_background", "lwipopts.h"),
# 'picow_freertos' : ("Full lwIP (FreeRTOS)", "", "pico/cyw43_arch.h", "pico_cyw43_arch_lwip_sys_freertos", "lwipopts.h"),
}
stdlib_examples_list = {
'uart': ("UART", "uart.c", "hardware/uart.h", "hardware_uart"),
'gpio' : ("GPIO interface", "gpio.c", "hardware/gpio.h", "hardware_gpio"),
'div' : ("Low level HW Divider", "divider.c", "hardware/divider.h", "hardware_divider")
}
debugger_list = ["SWD", "PicoProbe", "CMSIS-DAP Debug Probe"]
debugger_config_list = ["raspberrypi-swd.cfg", "picoprobe.cfg", "cmsis-dap.cfg"]
debug_server_args_list = ["", "", "\"-c\", \"adapter speed 5000\" "]
DEFINES = 0
INITIALISERS = 1
# Could add an extra item that shows how to use some of the available functions for the feature
#EXAMPLE = 2
# This also contains example code for the standard library (see stdlib_examples_list)
code_fragments_per_feature = {
'uart' : [
("// UART defines",
"// By default the stdout UART is `uart0`, so we will use the second one",
"#define UART_ID uart1",
"#define BAUD_RATE 9600", "",
"// Use pins 4 and 5 for UART1",
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments",
"#define UART_TX_PIN 4",
"#define UART_RX_PIN 5" ),
( "// Set up our UART",
"uart_init(UART_ID, BAUD_RATE);",
"// Set the TX and RX pins by using the function select on the GPIO",
"// Set datasheet for more information on function select",
"gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);",
"gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);", "" )
],
'spi' : [
( "// SPI Defines",
"// We are going to use SPI 0, and allocate it to the following GPIO pins",
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments",
"#define SPI_PORT spi0",
"#define PIN_MISO 16",
"#define PIN_CS 17",
"#define PIN_SCK 18",
"#define PIN_MOSI 19" ),
( "// SPI initialisation. This example will use SPI at 1MHz.",
"spi_init(SPI_PORT, 1000*1000);",
"gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);",
"gpio_set_function(PIN_CS, GPIO_FUNC_SIO);",
"gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);",
"gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);", "",
"// Chip select is active-low, so we'll initialise it to a driven-high state",
"gpio_set_dir(PIN_CS, GPIO_OUT);",
"gpio_put(PIN_CS, 1);", "")
],
'i2c' : [
(
"// I2C defines",
"// This example will use I2C0 on GPIO8 (SDA) and GPIO9 (SCL) running at 400KHz.",
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments",
"#define I2C_PORT i2c0",
"#define I2C_SDA 8",
"#define I2C_SCL 9",
),
(
"// I2C Initialisation. Using it at 400Khz.",
"i2c_init(I2C_PORT, 400*1000);","",
"gpio_set_function(I2C_SDA, GPIO_FUNC_I2C);",
"gpio_set_function(I2C_SCL, GPIO_FUNC_I2C);",
"gpio_pull_up(I2C_SDA);",
"gpio_pull_up(I2C_SCL);"
)
],
"gpio" : [
(
"// GPIO defines",
"// Example uses GPIO 2",
"#define GPIO 2"
),
(
"// GPIO initialisation.",
"// We will make this GPIO an input, and pull it up by default",
"gpio_init(GPIO);",
"gpio_set_dir(GPIO, GPIO_IN);",
"gpio_pull_up(GPIO);","",
)
],
"interp" :[
(),
(
"// Interpolator example code",
"interp_config cfg = interp_default_config();",
"// Now use the various interpolator library functions for your use case",
"// e.g. interp_config_clamp(&cfg, true);",
"// interp_config_shift(&cfg, 2);",
"// Then set the config ",
"interp_set_config(interp0, 0, &cfg);",
)
],
"timer" : [
(
"int64_t alarm_callback(alarm_id_t id, void *user_data) {",
" // Put your timeout handler code in here",
" return 0;",
"}"
),
(
"// Timer example code - This example fires off the callback after 2000ms",
"add_alarm_in_ms(2000, alarm_callback, NULL, false);"
)
],
"watchdog":[ (),
(
"// Watchdog example code",
"if (watchdog_caused_reboot()) {",
" // Whatever action you may take if a watchdog caused a reboot",
"}","",
"// Enable the watchdog, requiring the watchdog to be updated every 100ms or the chip will reboot",
"// second arg is pause on debug which means the watchdog will pause when stepping through code",
"watchdog_enable(100, 1);","",
"// You need to call this function at least more often than the 100ms in the enable call to prevent a reboot"
"watchdog_update();",
)
],
"div" : [ (),
(
"// Example of using the HW divider. The pico_divider library provides a more user friendly set of APIs ",
"// over the divider (and support for 64 bit divides), and of course by default regular C language integer",
"// divisions are redirected thru that library, meaning you can just use C level `/` and `%` operators and",
"// gain the benefits of the fast hardware divider.",
"int32_t dividend = 123456;",
"int32_t divisor = -321;",
"// This is the recommended signed fast divider for general use.",
"divmod_result_t result = hw_divider_divmod_s32(dividend, divisor);",
"printf(\"%d/%d = %d remainder %d\\n\", dividend, divisor, to_quotient_s32(result), to_remainder_s32(result));",
"// This is the recommended unsigned fast divider for general use.",
"int32_t udividend = 123456;",
"int32_t udivisor = 321;",
"divmod_result_t uresult = hw_divider_divmod_u32(udividend, udivisor);",
"printf(\"%d/%d = %d remainder %d\\n\", udividend, udivisor, to_quotient_u32(uresult), to_remainder_u32(uresult));"
)
]
}
configuration_dictionary = list(dict())
isMac = False
isWindows = False
compilerPath = Path("/usr/bin/arm-none-eabi-gcc")
def GetBackground():
return 'white'
def GetButtonBackground():
return 'white'
def GetTextColour():
return 'black'
def GetButtonTextColour():
return '#c51a4a'
def RunGUI(sdkpath, args):
root = tk.Tk()
style = ttk.Style(root)
style.theme_use('default')
ttk.Style().configure("TButton", padding=6, relief="groove", border=2, foreground=GetButtonTextColour(), background=GetButtonBackground())
ttk.Style().configure("TLabel", foreground=GetTextColour(), background=GetBackground() )
ttk.Style().configure("TCheckbutton", foreground=GetTextColour(), background=GetBackground())
ttk.Style().configure("TRadiobutton", foreground=GetTextColour(), background=GetBackground() )
ttk.Style().configure("TLabelframe", foreground=GetTextColour(), background=GetBackground() )
ttk.Style().configure("TLabelframe.Label", foreground=GetTextColour(), background=GetBackground() )
ttk.Style().configure("TCombobox", foreground=GetTextColour(), background=GetBackground() )
ttk.Style().configure("TListbox", foreground=GetTextColour(), background=GetBackground() )
ttk.Style().map("TCheckbutton", background = [('disabled', GetBackground())])
ttk.Style().map("TRadiobutton", background = [('disabled', GetBackground())])
ttk.Style().map("TButton", background = [('disabled', GetBackground())])
ttk.Style().map("TLabel", background = [('background', GetBackground())])
app = ProjectWindow(root, sdkpath, args)
app.configure(background=GetBackground())
root.mainloop()
sys.exit(ExitCodes.SUCCESS)
def RunWarning(message):
mb.showwarning('Raspberry Pi Pico Project Generator', message)
sys.exit(ExitCodes.SUCCESS)
import threading
def thread_function(text, command, ok):
l = shlex.split(command)
proc = subprocess.Popen(l, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(proc.stdout.readline,''):
if not line:
if ok:
ok["state"] = tk.NORMAL
return
text.insert(tk.END, line)
text.see(tk.END)
# Function to run an OS command and display the output in a new modal window
class DisplayWindow(tk.Toplevel):
def __init__(self, parent, title):
tk.Toplevel.__init__(self, parent)
self.parent = parent
self.init_window(title)
def init_window(self, title):
self.title(title)
frame = tk.Frame(self, borderwidth=5, relief=tk.RIDGE)
frame.pack(fill=tk.X, expand=True, side=tk.TOP)
scrollbar = tk.Scrollbar(frame)
self.text = tk.Text(frame, bg='gray14', fg='gray99')
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.text.pack(side=tk.LEFT, fill=tk.Y)
scrollbar.config(command=self.text.yview)
self.text.config(yscrollcommand=scrollbar.set)
frame1 = tk.Frame(self, borderwidth=1)
frame1.pack(fill=tk.X, expand=True, side=tk.BOTTOM)
self.OKButton = ttk.Button(frame1, text="OK", command=self.OK)
self.OKButton["state"] = tk.DISABLED
self.OKButton.pack()
# make dialog modal
self.transient(self.parent)
self.grab_set()
def OK(self):
self.grab_release()
self.destroy()
def RunCommandInWindow(parent, command):
w = DisplayWindow(parent, command)
x = threading.Thread(target=thread_function, args=(w.text, command, w.OKButton))
x.start()
parent.wait_window(w)
class EditBoolWindow(sd.Dialog):
def __init__(self, parent, configitem, current):
self.parent = parent
self.config_item = configitem
self.current = current
sd.Dialog.__init__(self, parent, "Edit boolean configuration")
def body(self, master):
self.configure(background=GetBackground())
ttk.Label(self, text=self.config_item['name']).pack()
self.result = tk.StringVar()
self.result.set(self.current)
ttk.Radiobutton(master, text="True", variable=self.result, value="True").pack(anchor=tk.W)
ttk.Radiobutton(master, text="False", variable=self.result, value="False").pack(anchor=tk.W)
ttk.Radiobutton(master, text=CONFIG_UNSET, variable=self.result, value=CONFIG_UNSET).pack(anchor=tk.W)
def get(self):
return self.result.get()
class EditIntWindow(sd.Dialog):
def __init__(self, parent, configitem, current):
self.parent = parent
self.config_item = configitem
self.current = current
sd.Dialog.__init__(self, parent, "Edit integer configuration")
def body(self, master):
self.configure(background=GetBackground())
str = self.config_item['name'] + " Max = " + self.config_item['max'] + " Min = " + self.config_item['min']
ttk.Label(self, text=str).pack()
self.input = tk.Entry(self)
self.input.pack(pady=4)
self.input.insert(0, self.current)
ttk.Button(self, text=CONFIG_UNSET, command=self.unset).pack(pady=5)
def validate(self):
self.result = self.input.get()
# Check for numeric entry
return True
def unset(self):
self.result = CONFIG_UNSET
self.destroy()
def get(self):
return self.result
class EditEnumWindow(sd.Dialog):
def __init__(self, parent, configitem, current):
self.parent = parent
self.config_item = configitem
self.current = current
sd.Dialog.__init__(self, parent, "Edit Enumeration configuration")
def body(self, master):
#self.configure(background=GetBackground())
values = self.config_item['enumvalues'].split('|')
values.insert(0,'Not set')
self.input = ttk.Combobox(self, values=values, state='readonly')
self.input.set(self.current)
self.input.pack(pady=12)
def validate(self):
self.result = self.input.get()
return True
def get(self):
return self.result
class ConfigurationWindow(tk.Toplevel):
def __init__(self, parent, initial_config):
tk.Toplevel.__init__(self, parent)
self.master = parent
self.results = initial_config
self.init_window(self)
def init_window(self, args):
self.configure(background=GetBackground())
self.title("Advanced Configuration")
ttk.Label(self, text="Select the advanced options you wish to enable or change. Note that you really should understand the implications of changing these items before using them!").grid(row=0, column=0, columnspan=5)
ttk.Label(self, text="Name").grid(row=1, column=0, sticky=tk.W)
ttk.Label(self, text="Type").grid(row=1, column=1, sticky=tk.W)
ttk.Label(self, text="Min").grid(row=1, column=2, sticky=tk.W)
ttk.Label(self, text="Max").grid(row=1, column=3, sticky=tk.W)
ttk.Label(self, text="Default").grid(row=1, column=4, sticky=tk.W)
ttk.Label(self, text="User").grid(row=1, column=5, sticky=tk.W)
okButton = ttk.Button(self, text="OK", command=self.ok)
cancelButton = ttk.Button(self, text="Cancel", command=self.cancel)
self.namelist = tk.Listbox(self, selectmode=tk.SINGLE)
self.typelist = tk.Listbox(self, selectmode=tk.SINGLE)
self.minlist = tk.Listbox(self, selectmode=tk.SINGLE)
self.maxlist = tk.Listbox(self, selectmode=tk.SINGLE)
self.defaultlist = tk.Listbox(self, selectmode=tk.SINGLE)
self.valuelist = tk.Listbox(self, selectmode=tk.SINGLE)
self.descriptionText = tk.Text(self, state=tk.DISABLED, height=2)
## Make a list of our list boxes to make it all easier to handle
self.listlist = [self.namelist, self.typelist, self.minlist, self.maxlist, self.defaultlist, self.valuelist]
scroll = tk.Scrollbar(self, orient=tk.VERTICAL, command=self.yview)
for box in self.listlist:
box.config(width=0)
box.config(yscrollcommand=scroll.set)
box.bind("<MouseWheel>", self.mousewheel)
box.bind("<Button-4>", self.mousewheel)
box.bind("<Button-5>", self.mousewheel)
box.bind("<<ListboxSelect>>", self.changeSelection)
box.bind("<Double-Button>", self.doubleClick)
box.config(exportselection=False)
box.bind("<Down>", self.OnEntryUpDown)
box.bind("<Up>", self.OnEntryUpDown)
scroll.grid(column=7, sticky=tk.N + tk.S)
i = 0
for box in self.listlist:
box.grid(row=2, column=i, padx=0, sticky=tk.W + tk.E)
i+=1
self.descriptionText.grid(row = 3, column=0, columnspan=4, sticky=tk.W + tk.E)
cancelButton.grid(column=5, row = 3, padx=5)
okButton.grid(column=4, row = 3, sticky=tk.E, padx=5)
# populate the list box with our config options
for conf in configuration_dictionary:
self.namelist.insert(tk.END, conf['name'])
s = conf['type']
if s == "":
s = "int"
self.typelist.insert(tk.END, s)
self.maxlist.insert(tk.END, conf['max'])
self.minlist.insert(tk.END, conf['min'])
self.defaultlist.insert(tk.END, conf['default'])
# see if this config has a setting, our results member has this predefined from init
val = self.results.get(conf['name'], CONFIG_UNSET)
self.valuelist.insert(tk.END, val)
if val != CONFIG_UNSET:
self.valuelist.itemconfig(self.valuelist.size() - 1, {'bg':'green'})
def yview(self, *args):
for box in self.listlist:
box.yview(*args)
def mousewheel(self, event):
if (event.num == 4): # Linux encodes wheel as 'buttons' 4 and 5
delta = -1
elif (event.num == 5):
delta = 1
else: # Windows & OSX
delta = event.delta
for box in self.listlist:
box.yview("scroll", delta, "units")
return "break"
def changeSelection(self, evt):
box = evt.widget
sellist = box.curselection()
if sellist:
index = int(sellist[0])
config = self.namelist.get(index)
# Now find the description for that config in the dictionary
for conf in configuration_dictionary:
if conf['name'] == config:
self.descriptionText.config(state=tk.NORMAL)
self.descriptionText.delete(1.0,tk.END)
str = config + "\n" + conf['description']
self.descriptionText.insert(1.0, str)
self.descriptionText.config(state=tk.DISABLED)
break
# Set all the other list boxes to the same index
for b in self.listlist:
if b != box:
b.selection_clear(0, tk.END)
b.selection_set(index)
def OnEntryUpDown(self, event):
box = event.widget
selection = box.curselection()
if selection:
index = int(selection[0])
if event.keysym == 'Up':
index -= 1
elif event.keysym == 'Down':
index += 1
if 0 <= index < box.size():
for b in self.listlist:
b.selection_clear(0, tk.END)
b.selection_set(index)
b.see(index)
def doubleClick(self, evt):
box = evt.widget
index = int(box.curselection()[0])
config = self.namelist.get(index)
# Get the associated dict entry from our list of configs
for conf in configuration_dictionary:
if conf['name'] == config:
if (conf['type'] == 'bool'):
result = EditBoolWindow(self, conf, self.valuelist.get(index)).get()
elif (conf['type'] == 'int' or conf['type'] == ""): # "" defaults to int
result = EditIntWindow(self, conf, self.valuelist.get(index)).get()
elif conf['type'] == 'enum':
result = EditEnumWindow(self, conf, self.valuelist.get(index)).get()
# Update the valuelist with our new item
self.valuelist.delete(index)
self.valuelist.insert(index, result)
if result != CONFIG_UNSET:
self.valuelist.itemconfig(index, {'bg':'green'})
break
def ok(self):
# Get the selections, and create a list of them
for i, val in enumerate(self.valuelist.get(0, tk.END)):
if val != CONFIG_UNSET:
self.results[self.namelist.get(i)] = val
else:
self.results.pop(self.namelist.get(i), None)
self.destroy()
def cancel(self):
self.destroy()
def get(self):
return self.results
class WirelessSettingsWindow(sd.Dialog):
def __init__(self, parent):
sd.Dialog.__init__(self, parent, "Wireless settings")
self.parent = parent
def body(self, master):
self.configure(background=GetBackground())
master.configure(background=GetBackground())
self.ssid = tk.StringVar()
self.password = tk.StringVar()
a = ttk.Label(master, text='SSID :', background=GetBackground())
a.grid(row=0, column=0, sticky=tk.E)
a.configure(background=GetBackground())
ttk.Entry(master, textvariable=self.ssid).grid(row=0, column=1, sticky=tk.W+tk.E, padx=5)
ttk.Label(master, text='Password :').grid(row=1, column=0, sticky=tk.E)
ttk.Entry(master, textvariable=self.password).grid(row=1, column=1, sticky=tk.W+tk.E, padx=5)
self.transient(self.parent)
self.grab_set()
def ok(self):
self.grab_release()
self.destroy()
def cancel(self):
self.destroy()
def get(self):
return (self.ssid.get(), self.password.get())
# Our main window
class ProjectWindow(tk.Frame):
def __init__(self, parent, sdkpath, args):
tk.Frame.__init__(self, parent)
self.master = parent
self.sdkpath = sdkpath
self.init_window(args)
self.configs = dict()
self.ssid = str()
self.password = str()
def setState(self, thing, state):
for child in thing.winfo_children():
child.configure(state=state)
def boardtype_change_callback(self, event):
boardtype = self.boardtype.get()
if boardtype == "pico_w":
self.setState(self.picowSubframe, "enabled")
else:
self.setState(self.picowSubframe, "disabled")
def wirelessSettings(self):
result = WirelessSettingsWindow(self)
self.ssid, self.password = result.get()
def init_window(self, args):
self.master.title("Raspberry Pi Pico Project Generator")
self.master.configure(bg=GetBackground())
optionsRow = 0
mainFrame = tk.Frame(self, bg=GetBackground()).grid(row=optionsRow, column=0, columnspan=6, rowspan=12)
# Need to keep a reference to the image or it will not appear.
self.logo = tk.PhotoImage(file=GetFilePath("logo_alpha.gif"))
logowidget = ttk.Label(mainFrame, image=self.logo, borderwidth=0, relief="solid").grid(row=0,column=0, columnspan=5, pady=10)
optionsRow += 2
namelbl = ttk.Label(mainFrame, text='Project Name :').grid(row=optionsRow, column=0, sticky=tk.E)
self.projectName = tk.StringVar()
if args.name != None:
self.projectName.set(args.name)
else:
self.projectName.set('ProjectName')
nameEntry = ttk.Entry(mainFrame, textvariable=self.projectName).grid(row=optionsRow, column=1, sticky=tk.W+tk.E, padx=5)
optionsRow += 1
locationlbl = ttk.Label(mainFrame, text='Location :').grid(row=optionsRow, column=0, sticky=tk.E)
self.locationName = tk.StringVar()
self.locationName.set(os.getcwd() if not args.projectRoot else args.projectRoot)
locationEntry = ttk.Entry(mainFrame, textvariable=self.locationName).grid(row=optionsRow, column=1, columnspan=3, sticky=tk.W+tk.E, padx=5)
locationBrowse = ttk.Button(mainFrame, text='Browse', command=self.browse).grid(row=3, column=4)
optionsRow += 1
ttk.Label(mainFrame, text = "Board Type :").grid(row=optionsRow, column=0, padx=4, sticky=tk.E)
self.boardtype = ttk.Combobox(mainFrame, values=boardtype_list, )
self.boardtype.grid(row=4, column=1, padx=4, sticky=tk.W+tk.E)
self.boardtype.set('pico')
self.boardtype.bind('<<ComboboxSelected>>',self.boardtype_change_callback)
optionsRow += 1
# Features section
featuresframe = ttk.LabelFrame(mainFrame, text="Library Options", relief=tk.RIDGE, borderwidth=2)
featuresframe.grid(row=optionsRow, column=0, columnspan=5, rowspan=5, ipadx=5, padx=5, pady=5, sticky=tk.E+tk.W)
s = (len(features_list)/3)
self.feature_checkbox_vars = []
row = 0
col = 0
for i in features_list:
var = tk.StringVar(value='') # Off by default for the moment
c = features_list[i][GUI_TEXT]
cb = ttk.Checkbutton(featuresframe, text = c, var=var, onvalue=i, offvalue='')
cb.grid(row=row, column=col, padx=15, pady=2, ipadx=1, ipady=1, sticky=tk.E+tk.W)
self.feature_checkbox_vars.append(var)
row+=1
if row >= s:
col+=1
row = 0
optionsRow += 5
# PicoW options section
self.picowSubframe = ttk.LabelFrame(mainFrame, relief=tk.RIDGE, borderwidth=2, text="Pico Wireless Options")
self.picowSubframe.grid(row=optionsRow, column=0, columnspan=5, rowspan=2, padx=5, pady=5, ipadx=5, ipady=3, sticky=tk.E+tk.W)
self.pico_wireless = tk.StringVar()
col = 0
row = 0
for i in picow_options_list:
rb = ttk.Radiobutton(self.picowSubframe, text=picow_options_list[i][GUI_TEXT], variable=self.pico_wireless, val=i)
rb.grid(row=row, column=col, padx=15, pady=1, sticky=tk.E+tk.W)
col+=1
if col == 3:
col=0
row+=1
# DOnt actually need any settings at the moment.
# ttk.Button(self.picowSubframe, text='Settings', command=self.wirelessSettings).grid(row=0, column=4, padx=5, pady=2, sticky=tk.E)
self.setState(self.picowSubframe, "disabled")
optionsRow += 3
# output options section
ooptionsSubframe = ttk.LabelFrame(mainFrame, relief=tk.RIDGE, borderwidth=2, text="Console Options")
ooptionsSubframe.grid(row=optionsRow, column=0, columnspan=5, rowspan=2, padx=5, pady=5, ipadx=5, ipady=3, sticky=tk.E+tk.W)
self.wantUART = tk.IntVar()
self.wantUART.set(args.uart)
ttk.Checkbutton(ooptionsSubframe, text="Console over UART", variable=self.wantUART).grid(row=0, column=0, padx=4, sticky=tk.W)
self.wantUSB = tk.IntVar()
self.wantUSB.set(args.usb)
ttk.Checkbutton(ooptionsSubframe, text="Console over USB (Disables other USB use)", variable=self.wantUSB).grid(row=0, column=1, padx=4, sticky=tk.W)
optionsRow += 2
# Code options section
coptionsSubframe = ttk.LabelFrame(mainFrame, relief=tk.RIDGE, borderwidth=2, text="Code Options")
coptionsSubframe.grid(row=optionsRow, column=0, columnspan=5, rowspan=3, padx=5, pady=5, ipadx=5, ipady=3, sticky=tk.E+tk.W)
self.wantExamples = tk.IntVar()
self.wantExamples.set(args.examples)
ttk.Checkbutton(coptionsSubframe, text="Add examples for Pico library", variable=self.wantExamples).grid(row=0, column=0, padx=4, sticky=tk.W)
self.wantRunFromRAM = tk.IntVar()
self.wantRunFromRAM.set(args.runFromRAM)
ttk.Checkbutton(coptionsSubframe, text="Run from RAM", variable=self.wantRunFromRAM).grid(row=0, column=1, padx=4, sticky=tk.W)
self.wantCPP = tk.IntVar()
self.wantCPP.set(args.cpp)
ttk.Checkbutton(coptionsSubframe, text="Generate C++", variable=self.wantCPP).grid(row=0, column=3, padx=4, sticky=tk.W)
ttk.Button(coptionsSubframe, text="Advanced...", command=self.config).grid(row=0, column=4, sticky=tk.E)
self.wantCPPExceptions = tk.IntVar()
self.wantCPPExceptions.set(args.cppexceptions)
ttk.Checkbutton(coptionsSubframe, text="Enable C++ exceptions", variable=self.wantCPPExceptions).grid(row=1, column=0, padx=4, sticky=tk.W)
self.wantCPPRTTI = tk.IntVar()
self.wantCPPRTTI.set(args.cpprtti)
ttk.Checkbutton(coptionsSubframe, text="Enable C++ RTTI", variable=self.wantCPPRTTI).grid(row=1, column=1, padx=4, sticky=tk.W)
optionsRow += 3
# Build Options section
boptionsSubframe = ttk.LabelFrame(mainFrame, relief=tk.RIDGE, borderwidth=2, text="Build Options")
boptionsSubframe.grid(row=optionsRow, column=0, columnspan=5, rowspan=2, padx=5, pady=5, ipadx=5, ipady=3, sticky=tk.E+tk.W)
self.wantBuild = tk.IntVar()
self.wantBuild.set(args.build)
ttk.Checkbutton(boptionsSubframe, text="Run build after generation", variable=self.wantBuild).grid(row=0, column=0, padx=4, sticky=tk.W)
self.wantOverwrite = tk.IntVar()
self.wantOverwrite.set(args.overwrite)
ttk.Checkbutton(boptionsSubframe, text="Overwrite existing projects", variable=self.wantOverwrite).grid(row=0, column=1, padx=4, sticky=tk.W)
optionsRow += 2
# IDE Options section
vscodeoptionsSubframe = ttk.LabelFrame(mainFrame, relief=tk.RIDGE, borderwidth=2, text="IDE Options")
vscodeoptionsSubframe.grid(row=optionsRow, column=0, columnspan=5, rowspan=2, padx=5, pady=5, ipadx=5, ipady=3, sticky=tk.E+tk.W)
self.wantVSCode = tk.IntVar()
if args.project is None:
self.wantVSCode.set(False)
else:
self.wantVSCode.set('vscode' in args.project)
ttk.Checkbutton(vscodeoptionsSubframe, text="Create VSCode project", variable=self.wantVSCode).grid(row=0, column=0, padx=4, sticky=tk.W)
ttk.Label(vscodeoptionsSubframe, text = " Debugger:").grid(row=0, column=1, padx=4, sticky=tk.W)
self.debugger = ttk.Combobox(vscodeoptionsSubframe, values=debugger_list, state="readonly")
self.debugger.grid(row=0, column=2, padx=4, sticky=tk.W)
self.debugger.current(args.debugger)
optionsRow += 2
# OK, Cancel, Help section
# creating buttons
QuitButton = ttk.Button(mainFrame, text="Quit", command=self.quit).grid(row=optionsRow, column=4, stick=tk.E, padx=10, pady=5)
OKButton = ttk.Button(mainFrame, text="OK", command=self.OK).grid(row=optionsRow, column=3, padx=4, pady=5, sticky=tk.E)
# TODO help not implemented yet
# HelpButton = ttk.Button(mainFrame, text="Help", command=self.help).grid(row=optionsRow, column=0, pady=5)
# You can set a default path here, replace the string with whereever you want.
# self.locationName.set('/home/pi/pico_projects')
def GetFeatures(self):
features = []
i = 0
for cb in self.feature_checkbox_vars:
s = cb.get()
if s != '':
features.append(s)
picow_extra = self.pico_wireless.get()
if picow_extra != 'picow_none':
features.append(picow_extra)
return features
def quit(self):
# TODO Check if we want to exit here
sys.exit(ExitCodes.SUCCESS)
def OK(self):
# OK, grab all the settings from the page, then call the generators
projectPath = self.locationName.get()
features = self.GetFeatures()
projects = list()
if (self.wantVSCode.get()):
projects.append("vscode")
params={
'sdkPath' : self.sdkpath,
'projectRoot' : Path(projectPath),
'projectName' : self.projectName.get(),
'wantGUI' : True,
'wantOverwrite' : self.wantOverwrite.get(),
'wantBuild' : self.wantBuild.get(),
'boardtype' : self.boardtype.get(),
'features' : features,
'projects' : projects,
'configs' : self.configs,
'wantRunFromRAM': self.wantRunFromRAM.get(),
'wantExamples' : self.wantExamples.get(),
'wantUART' : self.wantUART.get(),
'wantUSB' : self.wantUSB.get(),
'wantCPP' : self.wantCPP.get(),
'debugger' : self.debugger.current(),
'exceptions' : self.wantCPPExceptions.get(),
'rtti' : self.wantCPPRTTI.get(),
'ssid' : self.ssid,
'password' : self.password,
}
DoEverything(self, params)
def browse(self):
name = fd.askdirectory()
self.locationName.set(name)
def help(self):
print("Help TODO")
def config(self):
# Run the configuration window
self.configs = ConfigurationWindow(self, self.configs).get()
def CheckPrerequisites():
global isMac, isWindows
isMac = (platform.system() == 'Darwin')
isWindows = (platform.system() == 'Windows')
# Do we have a compiler?
return shutil.which(COMPILER_NAME)
def CheckSDKPath(gui):
sdkPath = os.getenv('PICO_SDK_PATH')
if sdkPath == None:
m = 'Unable to locate the Raspberry Pi Pico SDK, PICO_SDK_PATH is not set'
if (gui):
RunWarning(m)
else:
print(m)
elif not os.path.isdir(sdkPath):
m = 'Unable to locate the Raspberry Pi Pico SDK, PICO_SDK_PATH does not point to a directory'
if (gui):
RunWarning(m)
else:
print(m)
sdkPath = None
return sdkPath
def GetFilePath(filename):
if os.path.islink(__file__):
script_file = os.readlink(__file__)
else:
script_file = __file__
return os.path.join(os.path.dirname(script_file), filename)
def ParseCommandLine():
debugger_flags = ', '.join('{} = {}'.format(i, v) for i, v in enumerate(debugger_list))
parser = argparse.ArgumentParser(description='Pico Project generator')
parser.add_argument("name", nargs="?", help="Name of the project")
parser.add_argument("-t", "--tsv", help="Select an alternative pico_configs.tsv file", default=GetFilePath("pico_configs.tsv"))
parser.add_argument("-o", "--output", help="Set an alternative CMakeList.txt filename", default="CMakeLists.txt")
parser.add_argument("-x", "--examples", action='store_true', help="Add example code for the Pico standard library")
parser.add_argument("-l", "--list", action='store_true', help="List available features")
parser.add_argument("-c", "--configs", action='store_true', help="List available project configuration items")
parser.add_argument("-f", "--feature", action='append', help="Add feature to generated project")
parser.add_argument("-over", "--overwrite", action='store_true', help="Overwrite any existing project AND files")
parser.add_argument("-b", "--build", action='store_true', help="Build after project created")
parser.add_argument("-g", "--gui", action='store_true', help="Run a GUI version of the project generator")
parser.add_argument("-p", "--project", action='append', help="Generate projects files for IDE. Options are: vscode")
parser.add_argument("-r", "--runFromRAM", action='store_true', help="Run the program from RAM rather than flash")
parser.add_argument("-uart", "--uart", action='store_true', default=1, help="Console output to UART (default)")
parser.add_argument("-nouart", "--nouart", action='store_true', default=0, help="Disable console output to UART")
parser.add_argument("-usb", "--usb", action='store_true', help="Console output to USB (disables other USB functionality")
parser.add_argument("-cpp", "--cpp", action='store_true', default=0, help="Generate C++ code")
parser.add_argument("-cpprtti", "--cpprtti", action='store_true', default=0, help="Enable C++ RTTI (Uses more memory)")
parser.add_argument("-cppex", "--cppexceptions", action='store_true', default=0, help="Enable C++ exceptions (Uses more memory)")
parser.add_argument("-d", "--debugger", type=int, help="Select debugger ({})".format(debugger_flags), default=0)
parser.add_argument("-board", "--boardtype", action="store", default='pico', help="Select board type (see --boardlist for available boards)")
parser.add_argument("-bl", "--boardlist", action="store_true", help="List available board types")
parser.add_argument("-cp", "--cpath", help="Override default VSCode compiler path")
parser.add_argument("-root", "--projectRoot", help="Override default project root where the new project will be created")
return parser.parse_args()
def GenerateMain(folder, projectName, features, cpp):
if cpp:
filename = Path(folder) / (projectName + '.cpp')
else:
filename = Path(folder) / (projectName + '.c')
file = open(filename, 'w')
main = ('#include <stdio.h>\n'
'#include "pico/stdlib.h"\n'
)
file.write(main)
if (features):
# Add any includes
for feat in features:
if (feat in features_list):
o = f'#include "{features_list[feat][H_FILE]}"\n'
file.write(o)
if (feat in stdlib_examples_list):
o = f'#include "{stdlib_examples_list[feat][H_FILE]}"\n'
file.write(o)
if (feat in picow_options_list):
o = f'#include "{picow_options_list[feat][H_FILE]}"\n'
file.write(o)
file.write('\n')
# Add any defines
for feat in features:
if (feat in code_fragments_per_feature):
for s in code_fragments_per_feature[feat][DEFINES]:
file.write(s)
file.write('\n')
file.write('\n')
main = ('\n\n'
'int main()\n'
'{\n'
' stdio_init_all();\n\n'
)
if (features):
# Add any initialisers
indent = 4
for feat in features:
if (feat in code_fragments_per_feature):
for s in code_fragments_per_feature[feat][INITIALISERS]:
main += (" " * indent)
main += s
main += '\n'
main += '\n'
main += (' puts("Hello, world!");\n\n'
' return 0;\n'
'}\n'
)
file.write(main)
file.close()