summaryrefslogtreecommitdiff
path: root/src/index.cljs.hl
blob: be6ef8d11d9e7b5a81b3a843fb779d36ef37a4dd (plain)
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
(page "index.html"
  (:require [app.rpc :as rpc]
            [cljs.pprint :refer [pprint]]
            [cljsjs.semantic-ui :as ui]
            [clojure.string :refer [trim]]
            [Blockly]
            [Blockly.Blocks.loops]
            [Blockly.Blocks.lists]
            [Blockly.Blocks.texts]
            [Blockly.Blocks.procedures]
            [Blockly.Blocks.math]
            [Blockly.Blocks.variables]
            [Blockly.Blocks.colour]
            [Blockly.Blocks.logic]
            [Blockly.Msg.en]))

(rpc/init)
(defc= state rpc/state)
(defc= error rpc/error)

(def toolbox
  "<xml>
    <block type=\"controls_if\"></block>
    <block type=\"logic_compare\"></block>
    <block type=\"controls_repeat_ext\"></block>
    <block type=\"math_number\"></block>
    <block type=\"math_arithmetic\"></block>
    <block type=\"text\"></block>
    <block type=\"text_print\"></block>
  </xml>")

(defelem blockly-workspace
         [{:keys [options] :as attr} kids]
         (let [elem (div (dissoc attr :options) kids)]
           (with-init!
             (set! (.-workspace elem)
                   (.inject js/Blockly elem (clj->js options))))
           elem))


(html
  (head
    (link :rel "stylesheet" :type "text/css"
          :href "cljsjs/semantic-ui/common/semantic.min.css")
    (title "Tankputer"))
  (body
    (h1 "Status")
    (let [current (cell= (reduce + (map #(float (second %))
                                        (:current (:pm state)))))]
      (h2 (text "Power (~{current} A)")))
    (table
      (loop-tpl
        :bindings [[port name state] (cell= (:status (:pm state)))]
        (let [toggle (cell false)
              name (cell= (trim name))
              name-input (input :toggle toggle :value name)]
          (tr
            (td port)
            (td :click #(reset! toggle true)
                (span :toggle (cell= (not toggle))
                      :text (cell= (if (empty? name) "(unnamed)" name)))
                (form :submit #(do (rpc/set-name @port (.-value name-input))
                                 (reset! toggle false))
                      name-input
                      (input :toggle toggle :type "submit")))
            (td state)
            (td (button :click #(rpc/turn-on @port) "On")
                (button :click #(rpc/turn-off @port) "Off"))))))
    (h2 "Temperature")
    (ul
      (loop-tpl :bindings [[id temp] (cell= (:state (:temps state)))]
                (let [millicelsius (cell= (int temp))
                      fahrenheit (cell= (+ 32 (* 0.0018 millicelsius)))]
                  (li (text "Sensor ~{id}: ~{fahrenheit} \u00b0F")))))
    (textarea :text (cell= (with-out-str (pprint state))))))

;; vim: set expandtab ts=2 sw=2 filetype=clojure :