librarywork/hex1000/libs/Panel/src/Panel.xtl

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 Import it with an alias of your choice: "pn:" u_se< "Panel". Put libs/Panel/src on XETAL_PATH ("just path"); the reference is libs/Panel/docs. Names with l: are exported; the others are private to this file.

The physical calculator (docs/goals.md): a 4x4 hexadecimal pad, a column of four operator keys (+ - * =) and an 8-digit 7-segment display, around HEX1000 running the calculator ROM. Everything here is pure arrays: the segments a digit lights (the font the display driver will get), the panel's boxes and strokes in scene units (the face in the plane z = 0, facing +z, y up), the projection of a fixed camera onto a window (so a click can be mapped back to a key), and a key pressed at the machine through its own key scan (hx:t_ypeOn). A window or a renderer only draws what these give (mockup/).

source · imports hx: work/fpga/libs/Hex1000/src/Hex1000.xtl

The font

ˡFont : Int

value · line 26

Font: the segments of the hexadecimal digits 0 to F, a number each: bit 0 is segment a (the top), then b to f clockwise from the top right, bit 6 g (the middle). A b C d E F: lowercase b and d tell them from 8 and 0.

      "pn:" u̲se< "Panel"
      4 t̲ake ᵖⁿFont
63 6 91 79
ˡFont ← 63 6 91 79 102 109 125 7 127 111 119 124 57 94 121 113
Used in: ˡs̲egs, ˡs̲eg

ˡs̲egs : Int -> Int

function · line 36

s_egs d: the segments lit for each digit latch d (O as the program latched it: the digit in bits 0 to 3, bit 4 set to show it), a row a to g each; a latch with bit 4 clear is blank.

      "pn:" u̲se< "Panel"
      ᵖⁿs̲egs 16 17 1
1 1 1 1 1 1 0
0 1 1 0 0 0 0
0 0 0 0 0 0 0
ˡs̲egs ← { d →
  p ← (d ≥ 16) × (1 + d m̲od 16) s̲elect ˡFont
  (p 'd̲iv t̲able 1 2 4 8 16 32 64) m̲od 2
}

Seg : Float

value (private) · line 43

Seg: a digit's seven segments as strokes, a row each (x1 y1 x2 y2), in a digit 0.3 wide and 0.5 tall centered on 0 0, a to g.

Seg ← 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

Ops : Float

value (private) · line 47

Ops: the operator keys' legends as strokes, a row each (the key's code, x1 y1 x2 y2): + - * (a cross) and =.

Ops ← 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
Used in: ˡl̲egends

ʰc̲ol : Int -> a -> a

function (private) · line 50

Column k of a matrix m, as a vector.

ʰc̲ol ← { k m → r̲avel k s̲elect 2 1 t̲ranspose m }

ʰr̲ows : Int -> a -> a

function (private) · line 53

k columns, each a vector of n, as an n by k matrix.

ʰr̲ows ← { k cs → 2 1 t̲ranspose (k c̲at (t̲ally cs) d̲iv k) r̲eshape cs }

The panel in 3D

ˡKeys : Int

value · line 66

Keys: the keys as they sit, a row of the panel each from the top: the hexadecimal pad (C D E F, 8 9 A B, 4 5 6 7, 0 1 2 3) and the operator column (+ - * =), by their codes (hx:m_atrix's).

      "pn:" u̲se< "Panel"
      ᵖⁿKeys
12 13 14 15 16
 8  9 10 11 17
 4  5  6  7 18
 0  1  2  3 19
ˡ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
Used in: ˡk̲eyAt

ˡk̲eyAt : Int -> Float

function · line 74

k_eyAt c: where the middle of each key c is, x y (scene units), a row each: 0.95 apart, the operator column 0.2 further right.

      "pn:" u̲se< "Panel"
      ᵖⁿk̲eyAt 12 19
-2.0 0.55
 2.0 -2.3
ˡk̲eyAt ← { c →
  i ← ((r̲avel ˡKeys) i̲ndexOf c) − 1
  col ← i m̲od 5
  x ← ((0.95 × f̲loat col) + 0.2 × f̲loat col = 4) − 2.0
  y ← 0.55 − 0.95 × f̲loat i d̲iv 5
  2 ʰr̲ows x c̲at y
}

ˡk̲eyBoxes : Int -> Float

function · line 88

k_eyBoxes p: the 20 keys as boxes, by code from 0, a row each (x y z of the middle, width, height, depth): 0.78 square, standing 0.3 off the face, key p (-1: none) pressed down to 0.1.

      "pn:" u̲se< "Panel"
      s̲hape ᵖⁿk̲eyBoxes 1
20 6
ˡk̲eyBoxes ← { p →
  c ← o̲ffsets 20
  xy ← ˡk̲eyAt c
  d ← 0.3 − 0.2 × f̲loat c = p
  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
}

ˡCase : Float

value · line 100

Case: the case and the display's window, as boxes (x y z, width, height, depth); the face of the case is z = 0.

      "pn:" u̲se< "Panel"
      1 t̲ake ᵖⁿCase
0.0 0.25 -0.2 5.5 6.7 0.4
ˡ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

ˡLed : Float

value · line 107

Led: the ready light's box, top right above the display (lit while R15 says the program waits for a key).

      "pn:" u̲se< "Panel"
      ᵖⁿLed
2.2 3.2 0.05 0.16 0.16 0.1
ˡLed ← 1 6 r̲eshape 2.2 3.2 0.05 0.16 0.16 0.1

Signs : Float

value (private) · line 111

Corners of a unit cube (signs) and its six faces, the corners round each from 1.

Signs ← 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
Used in: ˡb̲oxes

Faces : Int

value (private) · line 112
Faces ← 1 3 4 2  5 6 8 7  1 5 7 3  2 4 8 6  1 2 6 5  3 7 8 4
Used in: ˡb̲oxes

ˡb̲oxes : Float -> Float

function · line 121

b_oxes b: boxes (a row each: x y z of the middle, width, height, depth) as quads, six faces each, 4 corners a face (24 rows x y z a box): every box's corners at once, an outer product of the halves and a unit cube's signs.

      "pn:" u̲se< "Panel"
      s̲hape ᵖⁿb̲oxes 2 6 r̲eshape 0 0 0 1 1 1  2 0 0 1 2 1
48 3
ˡb̲oxes ← { b →
  n ← f̲irst s̲hape b
  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 }
  v ← 3 ʰr̲ows (c̲o 1) c̲at (c̲o 2) c̲at c̲o 3
  (r̲avel (8 × (r̲ange n) − 1) '+ t̲able Faces) s̲elect v
}

ˡs̲trokes : (Float, Float, Float) -> Float

function · line 137

s_trokes (s, z, t): strokes as thin quads facing +z, t wide, at depth z (one for all, or one a stroke): s a row each (x1 y1 x2 y2); 4 corners a stroke.

      "pn:" u̲se< "Panel"
      ᵖⁿs̲trokes (1 4 r̲eshape 0 0 1 0, 0.5, 0.2)
0.0  0.1 0.5
1.0  0.1 0.5
1.0 -0.1 0.5
0.0 -0.1 0.5
ˡs̲trokes ← { (s, z, t) →
  n ← f̲irst s̲hape s
  x1 ← 1 ʰc̲ol s
  y1 ← 2 ʰc̲ol s
  x2 ← 3 ʰc̲ol s
  y2 ← 4 ʰc̲ol s
  l ← (((x2 − x1) ^ 2.0) + (y2 − y1) ^ 2.0) ^ 0.5
  nx ← (t × y1 − y2) ÷ 2.0 × l
  ny ← (t × x2 − x1) ÷ 2.0 × l
  x ← r̲avel 4 ʰr̲ows (x1 + nx) c̲at (x2 + nx) c̲at (x2 − nx) c̲at x1 − nx
  y ← r̲avel 4 ʰr̲ows (y1 + ny) c̲at (y2 + ny) c̲at (y2 − ny) c̲at y1 − ny
  zs ← n r̲eshape z
  3 ʰr̲ows x c̲at y c̲at r̲avel 4 ʰr̲ows zs c̲at zs c̲at zs c̲at zs
}

ʰm̲ove : Num a => (a, a) -> a

function (private) · line 153

Strokes s (a row each x1 y1 x2 y2) moved by xy (a row each x y).

ʰm̲ove ← { (s, xy) →
  cx ← 1 ʰc̲ol xy
  cy ← 2 ʰc̲ol xy
  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
}

ˡl̲egends : Int -> (Float, Float)

function · line 167

l_egends p: every key's legend as strokes on its top, with key p (-1: none) pressed: (the strokes, a row each x1 y1 x2 y2; their depths). The digits are the display's font at 0.8 size; the operators are Ops.

      "pn:" u̲se< "Panel"
      (s, z) ← ᵖⁿl̲egends -1
      t̲ally z
85
ˡl̲egends ← { p →
  digit ← r̲avel (o̲ffsets 16) '+ t̲able 7 r̲eshape 0
  on ← r̲avel ˡs̲egs 16 + o̲ffsets 16
  ds ← ʰm̲ove (112 4 r̲eshape 0.8 × r̲avel Seg, ˡk̲eyAt digit)
  oc ← f̲loor 1 ʰc̲ol Ops
  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)
  codes ← (on r̲eplicate digit) c̲at oc
  ((on r̲eplicate ds) c̲at os, 0.305 − 0.2 × f̲loat codes = p)
}

ˡd̲igits : Int -> (Float, Float)

function · line 184

d_igits d: the display's eight digits for the digit latches d (digit 0, the rightmost, first) as strokes: (those lit, those dark), a row each x1 y1 x2 y2.

      "pn:" u̲se< "Panel"
      (lit, dark) ← ᵖⁿd̲igits 16 17 0 0 0 0 0 0
      (t̲ally lit) c̲at t̲ally dark
8 48
ˡd̲igits ← { d →
  xp ← 0.58 × 3.5 − f̲loat o̲ffsets 8
  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)
  on ← r̲avel ˡs̲egs d
  (on r̲eplicate all, (1 − on) r̲eplicate all)
}

The camera and the window

ˡView : Int

value · line 197

View: the window, width and height (logical pixels).

      "pn:" u̲se< "Panel"
      ᵖⁿView
560 760
ˡView ← 560 760
Used in: ˡp̲roject

ˡEye : Float

value · line 204

Eye: the fixed camera, x y z then its pitch (radians, down negative), facing -z: in front of the panel and a little above it.

      "pn:" u̲se< "Panel"
      3 t̲ake ᵖⁿEye
0.0 1.2 5.1
ˡEye ← 0.0 1.2 5.1 -0.2
Used in: ˡp̲roject

ˡp̲roject : Float -> Float

function · line 213

p_roject pts: points (a row each x y z) where the camera draws them in the window, x y in logical pixels from the top left: the scene extension's first-person view (sc:e_ye!: a 70 degree field of view across the window's smaller side).

      "pn:" u̲se< "Panel"
      f̲loor ᵖⁿp̲roject 1 3 r̲eshape 0.0 1.5 0.0
280 274
ˡp̲roject ← { pts →
  rx ← (1 ʰc̲ol pts) − 1 s̲elect ˡEye
  ry ← (2 ʰc̲ol pts) − 2 s̲elect ˡEye
  rz ← (3 ʰc̲ol pts) − 3 s̲elect ˡEye
  a ← 4 s̲elect ˡEye
  sp ← s̲in a
  cp ← c̲os a
  yc ← (cp × ry) + sp × rz
  zc ← (sp × ry) − cp × rz
  half ← 0.6108652381980153
  v ← f̲loat ˡView
  f ← ('m̲in r̲/ v) ÷ 2.0 × (s̲in half) ÷ c̲os half
  sx ← (0.5 × f̲irst v) + f × rx ÷ zc
  sy ← (0.5 × 2 s̲elect v) − f × yc ÷ zc
  2 ʰr̲ows sx c̲at sy
}
Used in: ˡh̲it

ˡh̲it : Float -> Int

function · line 237

h_it xy: the key a click at x y (logical pixels) lands on, its code, or -1 for none: each key's top face projected, its corners' box on the screen.

      "pn:" u̲se< "Panel"
      c ← ᵖⁿp̲roject 1 3 r̲eshape (ᵖⁿk̲eyAt 19) c̲at 0.3
      ᵖⁿh̲it r̲avel c
19
ˡh̲it ← { xy0 →
  x ← f̲irst xy0
  y ← 2 s̲elect xy0
  xy ← ˡk̲eyAt o̲ffsets 20
  kx ← 1 ʰc̲ol xy
  ky ← 2 ʰc̲ol xy
  tl ← ˡp̲roject 3 ʰr̲ows (kx − 0.39) c̲at (ky + 0.39) c̲at 20 r̲eshape 0.3
  br ← ˡp̲roject 3 ʰr̲ows (kx + 0.39) c̲at (ky − 0.39) c̲at 20 r̲eshape 0.3
  in ← (x ≥ 1 ʰc̲ol tl) ∧ (x ≤ 1 ʰc̲ol br) ∧ (y ≥ 2 ʰc̲ol tl) ∧ y ≤ 2 ʰc̲ol br
  i ← in i̲ndexOf 1
  i > 20 ? -1
  i − 1
}

The machine behind the panel

ˡp̲ress : Num a => ((Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int), Int, Int) -> ((Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int), Int, a)

function · line 264

p_ress (st, d, c): key c pressed at the machine running the calculator ROM, from state st with the display showing the digit latches d (digit 0 first), through its own key scan (hx:t_ypeOn): (the state, the digit latches, the instructions it took). A digit is given 2000 instructions, an operator 20000 (a multiply runs when the next operator or = settles it).

      "pn:" u̲se< "Panel"
      "hx:" u̲se< "Hex1000"
      (st, d, took) ← ᵖⁿp̲ress (ʰˣReset, 8 r̲eshape 0, 7)
      ʰˣs̲creen r̲ev d
       7
ˡp̲ress ← { (st, d, c) →
  (t, d1, done, took) ← ʰˣCalcRom ʰˣt̲ypeOn (st, d, 1 r̲eshape c, 2000 + 18000 × c ≥ 16)
  (t, d1, took)
}

ˡr̲eady : (Any a, Any b, Any c, Any d, Any e, Any f, Any g, Any h, Any i, Any j, Any l) => (a, b, c, d, e, f, g, h, i, j, k, l) -> k

function · line 274

r_eady st: 1 while the program waits for a key (R15), else 0.

      "pn:" u̲se< "Panel"
      "hx:" u̲se< "Hex1000"
      ᵖⁿr̲eady ʰˣReset
0
ˡr̲eady ← { st → 16 s̲elect ʰˣl̲atches st }