Skip to content

Commit 4cae38f

Browse files
committed
Load and save recent layouts [Fixes #14]
1 parent bfbf73c commit 4cae38f

10 files changed

+322
-174
lines changed

build.boot

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
[cljsjs/jszip "3.1.3-0"]
4646
[cljs-node-io "0.5.0"]
4747
[cljsjs/localforage "1.5.3-0"]
48+
[com.andrewmcveigh/cljs-time "0.5.0"]
4849
]
4950
:source-paths #{"src"}
5051
;;:asset-paths #{"assets"}

src/kii/store.cljs

+18-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
[kii.bindings.node.fs :as fs]
88
[kii.bindings.node.path :as path]
99
[kii.bindings.electron-renderer :refer [user-data-dir]]
10-
[cuerdas.core :as str]))
10+
[cuerdas.core :as str]
11+
[cljs-time.core :as time]
12+
[cljs-time.coerce :as time-coerce]))
1113

14+
(def log-file "log/build.log")
1215
(def bin-file "kiibohd.dfu.bin")
1316
(def cache-dir "firmware-cache")
1417

@@ -27,13 +30,24 @@
2730
zip (<? (p->chan (.loadAsync jszip file)))
2831
json-name (str/fmt "%s-%s.json" board layout)
2932
bin-data (<? (extract zip bin-file))
30-
json-data (<? (extract zip json-name)) ]
33+
json-data (<? (extract zip json-name))
34+
log-data (<? (extract zip log-file))
35+
log-out (path/join out-dir "build.log")
36+
json-out (path/join out-dir json-name)
37+
]
3138
(fs/write-file! bin-out bin-data)
32-
(fs/write-file! (path/join out-dir json-name) json-data)
39+
(fs/write-file! json-out json-data)
40+
(fs/write-file! log-out log-data)
3341

3442
(logf :info "Successfully extracted firmware and config to local cache: %s" bin-out)
3543

36-
(put! c bin-out)
44+
(put! c {:board board
45+
:layout layout
46+
:hash hash
47+
:bin bin-out
48+
:json json-out
49+
:log log-out
50+
:time (time-coerce/to-long (time/now))})
3751
)
3852
(catch js/Error e
3953
(logf :error e "Error extracting firmware"))))

src/kii/ui/components/flash.cljs

+110-114
Original file line numberDiff line numberDiff line change
@@ -25,127 +25,123 @@
2525
:properties ["openFile"]
2626
:filters filters})
2727
callback)))
28+
(defn- dfu-command
29+
[]
30+
(when ((.-sync command-exists) "dfu-util")
31+
"dfu-util"))
2832

2933
(defn flash-firmware
3034
[]
31-
(r/with-let [loaded? (r/atom false)
32-
dfu-path (r/atom (if ((.-sync command-exists) "dfu-util") "dfu-util" ""))
33-
bin-file (r/atom "")
34-
flashing? (r/atom false)
35-
progress (r/atom "")
36-
status (r/atom :none)]
37-
(fn []
38-
;; TODO - Read this in on start...
39-
(when (false? @loaded?)
40-
(go
41-
(let [cfg-val (<? (config/get :dfu-util-path))
42-
last-dl (<? (config/get :last-download))]
43-
(when (some? cfg-val) (reset! dfu-path cfg-val))
44-
(when (some? last-dl) (reset! bin-file last-dl))
45-
(reset! loaded? true)
46-
)))
47-
48-
(add-watch dfu-path :path-update
49-
(fn [k a prev next]
50-
(when (not= prev next)
51-
(config/set :dfu-util-path next))))
52-
(letfn [(exec-flash []
53-
(reset! progress "")
54-
(reset! flashing? true)
55-
(reset! status :in-progress)
56-
(let [cmd (.spawn child-process @dfu-path #js ["-D" @bin-file])
57-
stdout (.-stdout cmd)
58-
stderr (.-stderr cmd)]
59-
(.on cmd "close"
60-
(fn [code]
61-
(reset! status (if (= code 0) :success :failure))
62-
(swap! progress str
63-
(if (= code 0)
64-
"SUCCESS!"
65-
"FAILED!"))))
66-
(.on stdout "data" (fn [data] (swap! progress str data)))
67-
(.on stderr "data" (fn [data] (swap! progress str "ERROR: " data)))
68-
))]
69-
[:div
70-
[:div {:style {:float "left"
71-
:width "310px"}}
72-
[:h3 "Flash Firmware"]
73-
[:div {:style {:display "flex"}}
74-
[kii-mui/text-field
75-
{:floating-label-text "dfu-util command"
76-
:value @dfu-path
77-
:on-change (fn [_ val] (reset! dfu-path val))
78-
:error-text (when-not (or (= 0 (count @dfu-path)) (str/includes? @dfu-path "dfu-util"))
79-
"does not appear to be the dfu-util binary")
80-
:error-style {:color (mui-core/color :deep-orange300)}
81-
}
82-
]
35+
(let [last-download (<<= [:local/last-download])
36+
dfu-util-path (<<= [:local/dfu-util-path])]
37+
(logf :info "%s" last-download)
38+
(r/with-let [loaded? (r/atom false)
39+
dfu-path (r/atom (or dfu-util-path (dfu-command) nil))
40+
bin-file (r/atom (or (:bin last-download) ""))
41+
flashing? (r/atom false)
42+
progress (r/atom "")
43+
status (r/atom :none)]
44+
(fn []
45+
(letfn [(exec-flash []
46+
(reset! progress "")
47+
(reset! flashing? true)
48+
(reset! status :in-progress)
49+
(let [cmd (.spawn child-process @dfu-path #js ["-D" @bin-file])
50+
stdout (.-stdout cmd)
51+
stderr (.-stderr cmd)]
52+
(.on cmd "close"
53+
(fn [code]
54+
(reset! status (if (= code 0) :success :failure))
55+
(swap! progress str
56+
(if (= code 0)
57+
"SUCCESS!"
58+
"FAILED!"))))
59+
(.on stdout "data" (fn [data] (swap! progress str data)))
60+
(.on stderr "data" (fn [data] (swap! progress str "ERROR: " data)))
61+
))
62+
(change-dfu-path [val]
63+
(reset! dfu-path val)
64+
(=>> [:local/set-dfu-util-path val]))]
65+
[:div
66+
[:div {:style {:float "left"
67+
:width "310px"}}
68+
[:h3 "Flash Firmware"]
69+
[:div {:style {:display "flex"}}
70+
[kii-mui/text-field
71+
{:floating-label-text "dfu-util command"
72+
:value @dfu-path
73+
:on-change (fn [_ val] (change-dfu-path val))
74+
:error-text (when-not (or (= 0 (count @dfu-path)) (str/includes? @dfu-path "dfu-util"))
75+
"does not appear to be the dfu-util binary")
76+
:error-style {:color (mui-core/color :deep-orange300)}
77+
}
78+
]
8379

84-
[mui/icon-button
85-
{:style {:margin-left "1em" :align-self "flex-end"}
86-
:on-click #(open-dialog "path to dfu-util" [{:name "All Files" :extensions ["*"]}]
87-
(fn [selected]
88-
(when-let [file (first selected)]
89-
(reset! dfu-path file))))
80+
[mui/icon-button
81+
{:style {:margin-left "1em" :align-self "flex-end"}
82+
:on-click #(open-dialog "path to dfu-util" [{:name "All Files" :extensions ["*"]}]
83+
(fn [selected]
84+
(when-let [file (first selected)]
85+
(change-dfu-path file))))
9086

91-
}
92-
[mui-icons/navigation-more-vert
93-
{:color "black"}]
94-
]
95-
]
87+
}
88+
[mui-icons/navigation-more-vert
89+
{:color "black"}]
90+
]
91+
]
9692

97-
[:div {:style {:display "flex"}}
98-
[kii-mui/text-field
99-
{:floating-label-text ".bin file to flash"
100-
:value @bin-file
101-
:on-change (fn [_ val] (reset! bin-file val))
102-
:error-text (when-not (or (= 0 (count @bin-file)) (str/ends-with? @bin-file ".bin"))
103-
"does not appear to be a .bin file")
104-
:error-style {:color (mui-core/color :deep-orange300)}
105-
}
93+
[:div {:style {:display "flex"}}
94+
[kii-mui/text-field
95+
{:floating-label-text ".bin file to flash"
96+
:value @bin-file
97+
:on-change (fn [_ val] (reset! bin-file val))
98+
:error-text (when-not (or (= 0 (count @bin-file)) (str/ends-with? @bin-file ".bin"))
99+
"does not appear to be a .bin file")
100+
:error-style {:color (mui-core/color :deep-orange300)}
101+
}
106102

107-
]
103+
]
108104

109-
[mui/icon-button
110-
{:style {:margin-left "1em" :align-self "flex-end"}
111-
:on-click #(open-dialog "firmware to flash" [{:name "bin files" :extensions ["bin"]}]
112-
(fn [selected]
113-
(when-let [file (first selected)]
114-
(reset! bin-file file))))
105+
[mui/icon-button
106+
{:style {:margin-left "1em" :align-self "flex-end"}
107+
:on-click #(open-dialog "firmware to flash" [{:name "bin files" :extensions ["bin"]}]
108+
(fn [selected]
109+
(when-let [file (first selected)]
110+
(reset! bin-file file))))
115111

116-
}
117-
[mui-icons/navigation-more-vert
118-
{:color "black"}]
119-
]
120-
]
121-
[mui/raised-button
122-
{:style {:float "right" :margin-right "55px" :margin-top "20px"}
123-
:label "Flash"
124-
:primary true
125-
:disabled (or (str/empty-or-nil? @dfu-path) (str/empty-or-nil? @bin-file) (= @status :in-progress))
126-
:on-click #(exec-flash)}]
127-
]
128-
(when @flashing?
129-
[:div {:style {;:margin-right "320px"
130-
:display "inline-block"}}
131-
[mui/text-field
132-
{:value @progress
133-
:floating-label-fixed true
134-
:floating-label-text "flashing progress"
135-
:disabled true
136-
:multi-line true
137-
:rows 20
138-
:style {:display "block"
139-
:width "900px"
140-
:font-family styling/monospace-font-stack
141-
:border-top (str/fmt "10px solid %s" (case @status :success "green" :failure "red" "lightgray"))}
142-
:textarea-style {:white-space "pre"
143-
:padding-bottom "1.2em"
144-
:overflow-y "hidden"
145-
:color "black"
146-
:font-size "0.9em"}}
112+
}
113+
[mui-icons/navigation-more-vert
114+
{:color "black"}]
115+
]
147116
]
148-
])
149-
]))
150-
)
151-
)
117+
[mui/raised-button
118+
{:style {:float "right" :margin-right "55px" :margin-top "20px"}
119+
:label "Flash"
120+
:primary true
121+
:disabled (or (str/empty-or-nil? @dfu-path) (str/empty-or-nil? @bin-file) (= @status :in-progress))
122+
:on-click #(exec-flash)}]
123+
]
124+
(when @flashing?
125+
[:div {:style {;:margin-right "320px"
126+
:display "inline-block"}}
127+
[mui/text-field
128+
{:value @progress
129+
:floating-label-fixed true
130+
:floating-label-text "flashing progress"
131+
:disabled true
132+
:multi-line true
133+
:rows 20
134+
:style {:display "block"
135+
:width "900px"
136+
:font-family styling/monospace-font-stack
137+
:border-top (str/fmt "10px solid %s" (case @status :success "green" :failure "red" "lightgray"))}
138+
:textarea-style {:white-space "pre"
139+
:padding-bottom "1.2em"
140+
:overflow-y "hidden"
141+
:color "black"
142+
:font-size "0.9em"}}
143+
]
144+
])
145+
]))
146+
))
147+
)

0 commit comments

Comments
 (0)