sourcework/hex1000/libs/Panel/src/Panel.xtl

1⍝│ Panel: HEX1000's calculator panel as arrays: the 7-segment font, the keys and display in 3D, where a click lands, a key pressed at the machine 2⍝│ Import it with an alias of your choice: "pn:" u_se< "Panel". 3⍝│ Put libs/Panel/src on XETAL_PATH ("just path"); the reference is libs/Panel/docs. 4⍝│ Names with l: are exported; the others are private to this file. 5⍝│ 6⍝│ The physical calculator (docs/goals.md): a 4x4 hexadecimal pad, a 7⍝│ column of four operator keys (+ - * =) and an 8-digit 7-segment 8⍝│ display, around HEX1000 running the calculator ROM. Everything here 9⍝│ is pure arrays: the segments a digit lights (the font the display 10⍝│ driver will get), the panel's boxes and strokes in scene units (the 11⍝│ face in the plane z = 0, facing +z, y up), the projection of a fixed 12⍝│ camera onto a window (so a click can be mapped back to a key), and a 13⍝│ key pressed at the machine through its own key scan (hx:t_ypeOn). 14⍝│ A window or a renderer only draws what these give (mockup/). 15"hx:" u̲se< "Hex1000" 16 17⍝━━ The font 18 19⍝│ Font: the segments of the hexadecimal digits 0 to F, a number each: 20⍝│ bit 0 is segment a (the top), then b to f clockwise from the top 21⍝│ right, bit 6 g (the middle). A b C d E F: lowercase b and d tell them 22⍝│ from 8 and 0. 23⍝│ >> "pn:" u_se< "Panel" 24⍝│ >> 4 t_ake pn:Font 25⍝│ 63 6 91 79 26ˡFont ← 63 6 91 79 102 109 125 7 127 111 119 124 57 94 121 113 27 28⍝│ s_egs d: the segments lit for each digit latch d (O as the program 29⍝│ latched it: the digit in bits 0 to 3, bit 4 set to show it), a row a 30⍝│ to g each; a latch with bit 4 clear is blank. 31⍝│ >> "pn:" u_se< "Panel" 32⍝│ >> pn:s_egs 16 17 1 33⍝│ 1 1 1 1 1 1 0 34⍝│ 0 1 1 0 0 0 0 35⍝│ 0 0 0 0 0 0 0 36ˡs̲egs ← { d → 37 p ← (d ≥ 16) × (1 + d m̲od 16) s̲elect ˡFont 38 (p 'd̲iv t̲able 1 2 4 8 16 32 64) m̲od 2 39} 40 41⍝│ Seg: a digit's seven segments as strokes, a row each (x1 y1 x2 y2), in 42⍝│ a digit 0.3 wide and 0.5 tall centered on 0 0, a to g. 43Seg ← 7 4 r̲eshape -0.11 0.25 0.11 0.25 0.15 0.21 0.15 0.04 0.15 -0.04 0.15 -0.21 -0.11 -0.25 0.11 -0.25 -0.15 -0.04 -0.15 -0.21 -0.15 0.21 -0.15 0.04 -0.11 0 0.11 0 44 45⍝│ Ops: the operator keys' legends as strokes, a row each (the key's 46⍝│ code, x1 y1 x2 y2): + - * (a cross) and =. 47Ops ← 7 5 r̲eshape 16 -0.15 0 0.15 0 16 0 -0.15 0 0.15 17 -0.15 0 0.15 0 18 -0.12 -0.12 0.12 0.12 18 -0.12 0.12 0.12 -0.12 19 -0.15 0.07 0.15 0.07 19 -0.15 -0.07 0.15 -0.07 48 49⍝│ Column k of a matrix m, as a vector. 50ʰc̲ol ← { k m → r̲avel k s̲elect 2 1 t̲ranspose m } 51 52⍝│ k columns, each a vector of n, as an n by k matrix. 53ʰr̲ows ← { k cs → 2 1 t̲ranspose (k c̲at (t̲ally cs) d̲iv k) r̲eshape cs } 54 55⍝━━ The panel in 3D 56 57⍝│ Keys: the keys as they sit, a row of the panel each from the top: the 58⍝│ hexadecimal pad (C D E F, 8 9 A B, 4 5 6 7, 0 1 2 3) and the operator 59⍝│ column (+ - * =), by their codes (hx:m_atrix's). 60⍝│ >> "pn:" u_se< "Panel" 61⍝│ >> pn:Keys 62⍝│ 12 13 14 15 16 63⍝│ 8 9 10 11 17 64⍝│ 4 5 6 7 18 65⍝│ 0 1 2 3 19 66ˡKeys ← 4 5 r̲eshape 12 13 14 15 16 8 9 10 11 17 4 5 6 7 18 0 1 2 3 19 67 68⍝│ k_eyAt c: where the middle of each key c is, x y (scene units), a row 69⍝│ each: 0.95 apart, the operator column 0.2 further right. 70⍝│ >> "pn:" u_se< "Panel" 71⍝│ >> pn:k_eyAt 12 19 72⍝│ -2.0 0.55 73⍝│ 2.0 -2.3 74ˡk̲eyAt ← { c → 75 i ← ((r̲avel ˡKeys) i̲ndexOf c) − 1 76 col ← i m̲od 5 77 x ← ((0.95 × f̲loat col) + 0.2 × f̲loat col = 4) − 2.0 78 y ← 0.55 − 0.95 × f̲loat i d̲iv 5 79 2 ʰr̲ows x c̲at y 80} 81 82⍝│ k_eyBoxes p: the 20 keys as boxes, by code from 0, a row each (x y z 83⍝│ of the middle, width, height, depth): 0.78 square, standing 0.3 off 84⍝│ the face, key p (-1: none) pressed down to 0.1. 85⍝│ >> "pn:" u_se< "Panel" 86⍝│ >> s_hape pn:k_eyBoxes 1 87⍝│ 20 6 88ˡk̲eyBoxes ← { p → 89 c ← o̲ffsets 20 90 xy ← ˡk̲eyAt c 91 d ← 0.3 − 0.2 × f̲loat c = p 92 6 ʰr̲ows (1 ʰc̲ol xy) c̲at (2 ʰc̲ol xy) c̲at (d ÷ 2) c̲at (40 r̲eshape 0.78) c̲at d 93} 94 95⍝│ Case: the case and the display's window, as boxes (x y z, width, 96⍝│ height, depth); the face of the case is z = 0. 97⍝│ >> "pn:" u_se< "Panel" 98⍝│ >> 1 t_ake pn:Case 99⍝│ 0.0 0.25 -0.2 5.5 6.7 0.4 100ˡCase ← 2 6 r̲eshape 0.0 0.25 -0.2 5.5 6.7 0.4 0.0 2.3 0.01 4.9 1.1 0.02 101 102⍝│ Led: the ready light's box, top right above the display (lit while 103⍝│ R15 says the program waits for a key). 104⍝│ >> "pn:" u_se< "Panel" 105⍝│ >> pn:Led 106⍝│ 2.2 3.2 0.05 0.16 0.16 0.1 107ˡLed ← 1 6 r̲eshape 2.2 3.2 0.05 0.16 0.16 0.1 108 109⍝│ Corners of a unit cube (signs) and its six faces, the corners round 110⍝│ each from 1. 111Signs ← f̲loat 8 3 r̲eshape -1 -1 -1 1 -1 -1 -1 1 -1 1 1 -1 -1 -1 1 1 -1 1 -1 1 1 1 1 1 112Faces ← 1 3 4 2 5 6 8 7 1 5 7 3 2 4 8 6 1 2 6 5 3 7 8 4 113 114⍝│ b_oxes b: boxes (a row each: x y z of the middle, width, height, 115⍝│ depth) as quads, six faces each, 4 corners a face (24 rows x y z a 116⍝│ box): every box's corners at once, an outer product of the halves and 117⍝│ a unit cube's signs. 118⍝│ >> "pn:" u_se< "Panel" 119⍝│ >> s_hape pn:b_oxes 2 6 r_eshape 0 0 0 1 1 1 2 0 0 1 2 1 120⍝│ 48 3 121ˡb̲oxes ← { b → 122 n ← f̲irst s̲hape b 123 c̲o ← { k → r̲avel ((k ʰc̲ol b) '+ t̲able 8 r̲eshape 0) + (0.5 × (k + 3) ʰc̲ol b) '× t̲able k ʰc̲ol Signs } 124 v ← 3 ʰr̲ows (c̲o 1) c̲at (c̲o 2) c̲at c̲o 3 125 (r̲avel (8 × (r̲ange n) − 1) '+ t̲able Faces) s̲elect v 126} 127 128⍝│ s_trokes (s, z, t): strokes as thin quads facing +z, t wide, at depth 129⍝│ z (one for all, or one a stroke): s a row each (x1 y1 x2 y2); 4 130⍝│ corners a stroke. 131⍝│ >> "pn:" u_se< "Panel" 132⍝│ >> pn:s_trokes (1 4 r_eshape 0 0 1 0, 0.5, 0.2) 133⍝│ 0.0 0.1 0.5 134⍝│ 1.0 0.1 0.5 135⍝│ 1.0 -0.1 0.5 136⍝│ 0.0 -0.1 0.5 137ˡs̲trokes ← { (s, z, t) → 138 n ← f̲irst s̲hape s 139 x1 ← 1 ʰc̲ol s 140 y1 ← 2 ʰc̲ol s 141 x2 ← 3 ʰc̲ol s 142 y2 ← 4 ʰc̲ol s 143 l ← (((x2 − x1) ^ 2.0) + (y2 − y1) ^ 2.0) ^ 0.5 144 nx ← (t × y1 − y2) ÷ 2.0 × l 145 ny ← (t × x2 − x1) ÷ 2.0 × l 146 x ← r̲avel 4 ʰr̲ows (x1 + nx) c̲at (x2 + nx) c̲at (x2 − nx) c̲at x1 − nx 147 y ← r̲avel 4 ʰr̲ows (y1 + ny) c̲at (y2 + ny) c̲at (y2 − ny) c̲at y1 − ny 148 zs ← n r̲eshape z 149 3 ʰr̲ows x c̲at y c̲at r̲avel 4 ʰr̲ows zs c̲at zs c̲at zs c̲at zs 150} 151 152⍝│ Strokes s (a row each x1 y1 x2 y2) moved by xy (a row each x y). 153ʰm̲ove ← { (s, xy) → 154 cx ← 1 ʰc̲ol xy 155 cy ← 2 ʰc̲ol xy 156 4 ʰr̲ows ((1 ʰc̲ol s) + cx) c̲at ((2 ʰc̲ol s) + cy) c̲at ((3 ʰc̲ol s) + cx) c̲at (4 ʰc̲ol s) + cy 157} 158 159⍝│ l_egends p: every key's legend as strokes on its top, with key p 160⍝│ (-1: none) pressed: (the strokes, a row each x1 y1 x2 y2; their 161⍝│ depths). The digits are the display's font at 0.8 size; the 162⍝│ operators are Ops. 163⍝│ >> "pn:" u_se< "Panel" 164⍝│ >> (s, z) := pn:l_egends -1 165⍝│ >> t_ally z 166⍝│ 85 167ˡl̲egends ← { p → 168 digit ← r̲avel (o̲ffsets 16) '+ t̲able 7 r̲eshape 0 169 on ← r̲avel ˡs̲egs 16 + o̲ffsets 16 170 ds ← ʰm̲ove (112 4 r̲eshape 0.8 × r̲avel Seg, ˡk̲eyAt digit) 171 oc ← f̲loor 1 ʰc̲ol Ops 172 os ← ʰm̲ove (4 ʰr̲ows (2 ʰc̲ol Ops) c̲at (3 ʰc̲ol Ops) c̲at (4 ʰc̲ol Ops) c̲at 5 ʰc̲ol Ops, ˡk̲eyAt oc) 173 codes ← (on r̲eplicate digit) c̲at oc 174 ((on r̲eplicate ds) c̲at os, 0.305 − 0.2 × f̲loat codes = p) 175} 176 177⍝│ d_igits d: the display's eight digits for the digit latches d (digit 178⍝│ 0, the rightmost, first) as strokes: (those lit, those dark), a row 179⍝│ each x1 y1 x2 y2. 180⍝│ >> "pn:" u_se< "Panel" 181⍝│ >> (lit, dark) := pn:d_igits 16 17 0 0 0 0 0 0 182⍝│ >> (t_ally lit) c_at t_ally dark 183⍝│ 8 48 184ˡd̲igits ← { d → 185 xp ← 0.58 × 3.5 − f̲loat o̲ffsets 8 186 all ← ʰm̲ove (56 4 r̲eshape r̲avel Seg, 2 ʰr̲ows (r̲avel xp '+ t̲able 7 r̲eshape 0) c̲at 56 r̲eshape 2.3) 187 on ← r̲avel ˡs̲egs d 188 (on r̲eplicate all, (1 − on) r̲eplicate all) 189} 190 191⍝━━ The camera and the window 192 193⍝│ View: the window, width and height (logical pixels). 194⍝│ >> "pn:" u_se< "Panel" 195⍝│ >> pn:View 196⍝│ 560 760 197ˡView ← 560 760 198 199⍝│ Eye: the fixed camera, x y z then its pitch (radians, down negative), 200⍝│ facing -z: in front of the panel and a little above it. 201⍝│ >> "pn:" u_se< "Panel" 202⍝│ >> 3 t_ake pn:Eye 203⍝│ 0.0 1.2 5.1 204ˡEye ← 0.0 1.2 5.1 -0.2 205 206⍝│ p_roject pts: points (a row each x y z) where the camera draws them in 207⍝│ the window, x y in logical pixels from the top left: the scene 208⍝│ extension's first-person view (sc:e_ye!: a 70 degree field of view 209⍝│ across the window's smaller side). 210⍝│ >> "pn:" u_se< "Panel" 211⍝│ >> f_loor pn:p_roject 1 3 r_eshape 0.0 1.5 0.0 212⍝│ 280 274 213ˡp̲roject ← { pts → 214 rx ← (1 ʰc̲ol pts) − 1 s̲elect ˡEye 215 ry ← (2 ʰc̲ol pts) − 2 s̲elect ˡEye 216 rz ← (3 ʰc̲ol pts) − 3 s̲elect ˡEye 217 a ← 4 s̲elect ˡEye 218 sp ← s̲in a 219 cp ← c̲os a 220 yc ← (cp × ry) + sp × rz 221 zc ← (sp × ry) − cp × rz 222 half ← 0.6108652381980153 223 v ← f̲loat ˡView 224 f ← ('m̲in r̲/ v) ÷ 2.0 × (s̲in half) ÷ c̲os half 225 sx ← (0.5 × f̲irst v) + f × rx ÷ zc 226 sy ← (0.5 × 2 s̲elect v) − f × yc ÷ zc 227 2 ʰr̲ows sx c̲at sy 228} 229 230⍝│ h_it xy: the key a click at x y (logical pixels) lands on, its 231⍝│ code, or -1 for none: each key's top face projected, its corners' 232⍝│ box on the screen. 233⍝│ >> "pn:" u_se< "Panel" 234⍝│ >> c := pn:p_roject 1 3 r_eshape (pn:k_eyAt 19) c_at 0.3 235⍝│ >> pn:h_it r_avel c 236⍝│ 19 237ˡh̲it ← { xy0 → 238 x ← f̲irst xy0 239 y ← 2 s̲elect xy0 240 xy ← ˡk̲eyAt o̲ffsets 20 241 kx ← 1 ʰc̲ol xy 242 ky ← 2 ʰc̲ol xy 243 tl ← ˡp̲roject 3 ʰr̲ows (kx − 0.39) c̲at (ky + 0.39) c̲at 20 r̲eshape 0.3 244 br ← ˡp̲roject 3 ʰr̲ows (kx + 0.39) c̲at (ky − 0.39) c̲at 20 r̲eshape 0.3 245 in ← (x ≥ 1 ʰc̲ol tl) ∧ (x ≤ 1 ʰc̲ol br) ∧ (y ≥ 2 ʰc̲ol tl) ∧ y ≤ 2 ʰc̲ol br 246 i ← in i̲ndexOf 1 247 i > 20 ? -1 248 i − 1 249} 250 251⍝━━ The machine behind the panel 252 253⍝│ p_ress (st, d, c): key c pressed at the machine running the calculator 254⍝│ ROM, from state st with the display showing the digit latches d (digit 255⍝│ 0 first), through its own key scan (hx:t_ypeOn): (the state, the 256⍝│ digit latches, the instructions it took). A digit is given 2000 257⍝│ instructions, an operator 20000 (a multiply runs when the next 258⍝│ operator or = settles it). 259⍝│ >> "pn:" u_se< "Panel" 260⍝│ >> "hx:" u_se< "Hex1000" 261⍝│ >> (st, d, took) := pn:p_ress (hx:Reset, 8 r_eshape 0, 7) 262⍝│ >> hx:s_creen r_ev d 263⍝│ 7 264ˡp̲ress ← { (st, d, c) → 265 (t, d1, done, took) ← ʰˣCalcRom ʰˣt̲ypeOn (st, d, 1 r̲eshape c, 2000 + 18000 × c ≥ 16) 266 (t, d1, took) 267} 268 269⍝│ r_eady st: 1 while the program waits for a key (R15), else 0. 270⍝│ >> "pn:" u_se< "Panel" 271⍝│ >> "hx:" u_se< "Hex1000" 272⍝│ >> pn:r_eady hx:Reset 273⍝│ 0 274ˡr̲eady ← { st → 16 s̲elect ʰˣl̲atches st }