-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.rkt
151 lines (129 loc) · 4.39 KB
/
upload.rkt
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
;; The MIT License (MIT)
;;
;; Copyright (c) 2013 Matthew C. Jadud
;;
;; 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.
#lang racket
(require (prefix-in srfi1: srfi/1)
"path-handling.rkt"
"util.rkt"
"code-execution.rkt"
"response-handling.rkt"
"debug.rkt"
)
(provide (all-defined-out))
(define (build-port sp)
(define PORT
(case (system-type)
[(macosx unix) (format "/dev/~a" sp)]
[(windows) sp]))
PORT)
(define (list-arduinos)
(case (system-type)
[(macosx)
(filter (λ (str)
(and (regexp-match "tty" str)
(regexp-match "usb" str)))
(directory-list "/dev"))]
[(unix)
(filter (λ (str)
(or (regexp-match "USB[0-9]+" str)
(regexp-match "ACM[0-9]+" str)))
(directory-list "/dev"))]
[(windows win)
(filter
string?
(map (λ (n)
(let ([path (format "\\\\.\\COM~a" n)])
(with-handlers ([exn:fail?
(λ (e) 'Oops)])
(call-with-input-file path
(λ (p) path)))))
(srfi1:iota 15)))
]
))
(define (avrdude-cmd file board serial-port)
(system-call
(get-config 'AVRDUDE)
`(-C ,(->string (get-config 'AVRDUDE.CONF))
-V -F
(-p ,(hash-ref board 'mcpu))
(-b ,(hash-ref board 'baud))
(-c arduino)
(-P ,serial-port)
-D -U
,(format "flash:w:~a" file))))
(define (avrdude-firmware serial-port)
(parameterize ([current-directory (get-config 'TEMPDIR)])
(define fhex "firmware.hex")
;; Get the board config
(define board (get-config 'BOARD))
(debug 'UPLOAD "Writing firmware to temp file.")
;; Dump the firmware
(when (file-exists? fhex)
(debug 'UPLOAD "Removing old firmware.")
(delete-file fhex))
(with-output-to-file fhex
(thunk
(begin
(debug 'UPLOAD "Writing firmware to disk.")
(printf "~a~n" (hash-ref board 'hex)))))
(debug 'UPLOAD "Firmware written.")
(debug 'UPLOAD "Attempting AVRDUDE.")
(let ()
(define result
(make-parameter
(exe-in-tempdir
(avrdude-cmd fhex board serial-port))))
(cond
[(zero? (result))
(debug 'UPLOAD "Upload successful.")
(get-response 'OK)]
[else (error)]))
))
(define (avrdude-code serial-port code)
(parameterize ([current-directory (get-config 'TEMPDIR)])
(define chex "code.hex")
;; Get the board config
(define board (get-config 'BOARD))
(debug 'UPLOAD "Writing code to temp file.")
;; Dump the firmware
(when (file-exists? chex)
(delete-file chex))
(with-output-to-file chex
(thunk (printf "~a~n" code)))
(debug 'UPLOAD "Code written.")
(let ()
(define result
(make-parameter
(exe-in-tempdir
(avrdude-cmd chex board serial-port))))
(cond
[(zero? (result)) (get-response 'OK)]
[else (error)]))
))
;; For uploading user code, not firmware.
#;(define (avrdude)
(define ARDUINO-PORT (get-data 'port))
(define cmd (avrdude-cmd ARDUINO-PORT
(format "~a.hex" temp-file-base)
#;(hex-file)
))
(when ARDUINO-PORT
(exe cmd)))