]> git.armaanb.net Git - atreides.git/blob - sim.fnl
Remove assembly directory
[atreides.git] / sim.fnl
1 ;; Atreus simulator
2 ;; Copyright © 2020 Phil Hagelberg and contributors
3 ;; released under the GPLv3 or later
4
5 ;; main.lua:
6 ;; dofile("../fennel/fennel.lua").dofile("sim.fnl", {correlate=true})
7
8 (var scale 2.5)
9 (local width 260)
10 (local height 150)
11
12 (local cols 6) ; per hand
13 (local rows 4)
14 (local angle (math.rad 10))
15 (local corner-radius 8)
16 (local bottom 100) ; outer bottom
17
18 (local switch-height 13.72)
19 (local switch-width 13.62)
20 (local switch-spacing 18.6)
21
22 ;;;; switches
23 (local offsets [8.0 5.0 0.0 6.0 11.0 15.0])
24
25 (local hand-width 106.621)
26 (local hand-height (+ (* switch-spacing rows) (- switch-spacing switch-height)
27                       (. offsets 1)))
28 (local switch-x-offset (* 1.75 corner-radius))
29 (local switch-y-offset (- bottom hand-height (-  (math.sqrt corner-radius))))
30
31 (local switches-left [])
32 (local switches-right [])
33 (local switches-fn-left [])
34 (local switches-fn-right [])
35
36 (local layout-l
37        [["'" "," :. :p :y]
38         [:a :o :e :u :i]
39         [";" :q :j :k :x "`"]
40         [:esc :tab :super :lshift :backspace :ctrl]])
41 (local layout-r
42        [[:f :g :c :r :l]
43         [:d :h :t :n :s]
44         ["\\" :b :m :w :v :z]
45         [:alt " " :fn :- :/ "\n"]])
46
47 (local layout-fl
48        [["!" "@" :up "$" "%"]
49         ["(" :left :down :right ")"]
50         ["[" "]" "#" "{" "}" "^"]
51         [:l2 :insert :super2 :rshift :backspace2 :ctrl2]])
52
53 (local layout-fr
54        [[:pgup :7 :8 :9 :backspace3]
55         [:pgdn :4 :5 :6 :blank]
56         ["&" "*" :1 :2 :3 :+]
57         [:alt2 :space2 :fn2 :.2 :0 :=]])
58
59 (local keys {})
60
61 (for [col 1 cols] ; switches
62   (for [row 1 rows]
63     (when (not (and (= 6 col) (<= row 2)))
64       (let [lx (+ (* switch-spacing (- col 1)))
65             rx (- (* (- cols 1) switch-spacing) lx)
66             y (+ (* switch-spacing (- row 1)) (. offsets col))
67             fn-y (+ y bottom)
68             kl (table.remove (. layout-l row) 1)
69             kr (table.remove (. layout-r row))
70             kfl (table.remove (. layout-fl row) 1)
71             kfr (table.remove (. layout-fr row))]
72         (table.insert switches-left {:x lx :y y :p [] :k kl})
73         (table.insert switches-right {:x rx :y y :p [] :k kr})
74         (table.insert switches-fn-left {:x lx :y fn-y :p [] :k kfl :fn true})
75         (table.insert switches-fn-right {:x rx :y fn-y :p [] :k kfr :fn true})
76         (tset keys kl (. switches-left (# switches-left)))
77         (tset keys kr (. switches-right (# switches-right)))
78         (tset keys kfl (. switches-fn-left (# switches-fn-left)))
79         (tset keys kfr (. switches-fn-right (# switches-fn-right)))))))
80
81 (fn press [pressed]
82   (let [key (. keys (pressed:lower))]
83     (when key
84       (table.insert key.p {:ox (love.math.randomNormal 2 0)
85                            :oy (love.math.randomNormal 2 0)
86                            :r (love.math.randomNormal 2 8)})
87       (when key.fn
88         (press :fn))
89       (when (not= pressed (pressed:lower))
90         (press :lshift)))))
91
92 (var text "The Atreus is a mechanical keyboard designed primarily to match the
93 shape of human hands and to be as portable as possible. The case
94 measures 26x12cm and lacks even a number row, relying heavily upon the
95 =fn= key. There is a circuit board for this design, but it's also
96 possible to [[http://wiki.geekhack.org/index.php?title=Hard-Wiring_How-To]
97 [manually wire the matrix]]. ")
98
99 (fn tick []
100   (when (< 0 (# text))
101     (let [key (text:sub 1 1)]
102       (press key)
103       (set text (text:sub 2)))))
104
105 (var t 0)
106 (fn love.update [dt]
107   (set t (+ t dt))
108   (when (< 0.2 t)
109     (tick)
110     (set t 0)))
111
112 (fn draw-switch [{: x : y : k : p}]
113   (love.graphics.rectangle :line x y switch-width switch-height)
114   (when (= (# k) 1)
115     (love.graphics.print k (+ x 2) y))
116   (love.graphics.setColor 0 0.8 0.9 0.1)
117   (each [_ p (ipairs p)]
118     (love.graphics.circle :fill
119                           (+ x p.ox (/ switch-width 2))
120                           (+ y p.oy (/ switch-height 2))
121                           p.r))
122   (love.graphics.setColor 0 0 0))
123
124 (fn love.draw []
125   (let [(w h) (love.graphics.getDimensions)]
126     (love.graphics.setColor 1 1 1)
127     (love.graphics.rectangle :fill 0 0 w h)
128     (love.graphics.setColor 0 0 0)
129     (love.graphics.scale scale)
130
131     (love.graphics.push)
132     (love.graphics.translate (- width hand-width switch-x-offset)
133                              switch-y-offset)
134     (love.graphics.rotate angle 0 hand-height)
135     (love.graphics.translate (- hand-width) 0)
136     (each [_ s (pairs switches-left)]
137       (draw-switch s))
138     (each [_ s (pairs switches-fn-left)]
139       (draw-switch s))
140     (love.graphics.pop)
141
142     (love.graphics.push)
143     (love.graphics.translate (- width hand-width switch-x-offset)
144                              switch-y-offset)
145     (love.graphics.rotate (- angle))
146     (each [_ s (pairs switches-right)]
147       (draw-switch s))
148     (each [_ s (pairs switches-fn-right)]
149       (draw-switch s))
150     (love.graphics.pop)))
151
152 (fn love.keypressed [key]
153   (match key
154     "=" (set scale (+ scale 0.2))
155     "-" (set scale (- scale 0.2))
156     "escape" (love.event.quit)
157     "return" (let [fennel (dofile "../fennel/fennel.lua")]
158                (fennel.dofile "sim.fnl"))
159     key (press key)))