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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
package main
import (
"bufio"
"image"
"image/color"
"log"
"os"
"strconv"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"janouch.name/desktop-tools/liust-50/charset"
)
const (
displayWidth = 20
displayHeight = 2
charWidth = 5 + 1
charHeight = 7 + 1
magnification = 5
)
// TODO(p): See how this works exactly, and implement it.
const (
cursorModeOff = iota
cursorModeBlink
cursorModeLightUp
)
type Display struct {
chars [displayHeight][displayWidth]uint8
charset uint8
cursorX int
cursorY int
cursorMode int
}
func NewDisplay() *Display {
return &Display{charset: 2}
}
func (d *Display) Clear() {
for y := 0; y < displayHeight; y++ {
for x := 0; x < displayWidth; x++ {
d.chars[y][x] = 0x20 // space
}
}
}
func (d *Display) ClearToEnd() {
for x := d.cursorX; x < displayWidth; x++ {
d.chars[d.cursorY][x] = 0x20 // space
}
}
func (d *Display) drawCharacter(
img *image.RGBA, charImg image.Image, cx, cy int) {
if charImg == nil {
return
}
bounds := charImg.Bounds()
width, height := bounds.Dx(), bounds.Dy()
for dy := 0; dy < height; dy++ {
for dx := 0; dx < width; dx++ {
var c color.RGBA
if r, _, _, _ := charImg.At(
bounds.Min.X+dx, bounds.Min.Y+dy).RGBA(); r >= 0x8000 {
c = color.RGBA{0x00, 0xFF, 0xC0, 0xFF}
} else {
c = color.RGBA{0x20, 0x20, 0x20, 0xFF}
}
img.SetRGBA(1+cx*charWidth+dx, 1+cy*charHeight+dy, c)
}
}
}
func (d *Display) Render() image.Image {
// XXX: The VFD display doesn't have rectangular pixels,
// they are rather elongated in a 3:4 ratio.
width := 1 + displayWidth*charWidth
height := 1 + displayHeight*charHeight
// XXX: Not sure if we rather don't want to provide double buffering,
// meaning we would cycle between two internal buffers.
img := image.NewRGBA(image.Rect(0, 0, width, height))
black := [4]uint8{0x00, 0x00, 0x00, 0xFF}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
copy(img.Pix[img.PixOffset(x, y):], black[:])
}
}
for cy := 0; cy < displayHeight; cy++ {
for cx := 0; cx < displayWidth; cx++ {
charImg := charset.ResolveCharToImage(d.chars[cy][cx], d.charset)
d.drawCharacter(img, charImg, cx, cy)
}
}
return img
}
func (d *Display) PutChar(ch uint8) {
if d.cursorX >= displayWidth || d.cursorY >= displayHeight {
return
}
d.chars[d.cursorY][d.cursorX] = ch
d.cursorX++
if d.cursorX >= displayWidth {
d.cursorX = displayWidth - 1
}
}
func (d *Display) LineFeed() {
d.cursorY++
if d.cursorY >= displayHeight {
d.cursorY = displayHeight - 1
// XXX: This relies on displayHeight being exactly 2.
d.chars[0] = d.chars[1]
for x := 0; x < displayWidth; x++ {
d.chars[1][x] = 0x20
}
}
}
func (d *Display) CarriageReturn() {
d.cursorX = 0
}
func (d *Display) Backspace() {
if d.cursorX > 0 {
d.cursorX--
}
}
func (d *Display) SetCursor(x, y int) {
if x >= 0 && x < displayWidth {
d.cursorX = x
}
if y >= 0 && y < displayHeight {
d.cursorY = y
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
func parseANSI(input string) (command string, params []int) {
if !strings.HasPrefix(input, "\x1b[") {
return "", nil
}
input = input[2:]
if len(input) == 0 {
return "", nil
}
cmdIdx := len(input) - 1
command = string(input[cmdIdx])
paramStr := input[:cmdIdx]
if paramStr == "" {
return command, []int{}
}
parts := strings.Split(paramStr, ";")
for _, p := range parts {
p = strings.TrimSpace(p)
if val, err := strconv.Atoi(p); err == nil {
params = append(params, val)
}
}
return command, params
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type protocolParser struct {
seq strings.Builder
inEsc bool
inCSI bool
display *Display
}
func newProtocolParser(d *Display) *protocolParser {
return &protocolParser{display: d}
}
func (pp *protocolParser) reset() {
pp.inEsc = false
pp.inCSI = false
pp.seq.Reset()
}
func (pp *protocolParser) handleCSICommand() bool {
cmd, params := parseANSI(pp.seq.String())
switch cmd {
case "J": // Clear display
// XXX: The no params case is unverified.
if len(params) == 0 || params[0] == 2 {
pp.display.Clear()
}
case "K": // Delete to end of line
// XXX: The no params case is unverified (but it should work).
if len(params) == 0 || params[0] == 0 {
pp.display.ClearToEnd()
}
case "H": // Cursor position
y, x := 0, 0
if len(params) >= 1 {
y = params[0] - 1 // 1-indexed to 0-indexed
}
if len(params) >= 2 {
x = params[1] - 1
}
pp.display.SetCursor(x, y)
}
return true
}
func (pp *protocolParser) handleEscapeSequence(b byte) bool {
pp.seq.WriteByte(b)
if pp.seq.Len() == 2 && b == '[' {
pp.inCSI = true
return false
}
if pp.seq.Len() == 3 && pp.seq.String()[1] == 'R' {
pp.display.charset = b
pp.reset()
return true
}
if pp.inCSI && (b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z') {
refresh := pp.handleCSICommand()
pp.reset()
return refresh
}
if pp.seq.Len() == 6 && pp.seq.String()[1:5] == "\\?LC" {
pp.display.cursorMode = int(pp.seq.String()[5])
return true
}
return false
}
func (pp *protocolParser) handleCharacter(b byte) bool {
switch b {
case 0x0A: // LF
pp.display.LineFeed()
return true
case 0x0D: // CR
pp.display.CarriageReturn()
return true
case 0x08: // BS
pp.display.Backspace()
return true
default:
if b >= 0x20 {
pp.display.PutChar(b)
return true
}
}
return false
}
func (pp *protocolParser) handleByte(b byte) (needsRefresh bool) {
if b == 0x1b { // ESC
pp.reset()
pp.inEsc = true
pp.seq.WriteByte(b)
return false
}
if pp.inEsc {
return pp.handleEscapeSequence(b)
}
return pp.handleCharacter(b)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type DisplayWidget struct {
widget.BaseWidget
display *Display
}
type DisplayRenderer struct {
image *canvas.Image
label *canvas.Text
objects []fyne.CanvasObject
displayWidget *DisplayWidget
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
func main() {
a := app.New()
a.Settings().SetTheme(theme.DarkTheme())
window := a.NewWindow("Toshiba Tec LIUST-50 Simulator")
display := NewDisplay()
display.Clear()
// TODO(p): It makes the most sense to create a custom widget.
//
// - How does Fyne render text?
// - Do I need to give it a left top position and pixel size, or?
// - canvas.NewText → canvas.Text
//
// - How does the Fyne model work?
// - All Windows have a Canvas.
// - Everything drawn, including Widgets, is a CanvasObject.
// - After CanvasObject updates, call Refresh on them.
// - It seems like this does not work hierarchically,
// and there's a generic canvas.Refresh() function,
// and Refresh() methods generally just invoke that.
// - window.SetContent() takes a CanvasObject.
// - How do WidgetRenderers work?
// - How would a widget that renders itself stretched work?
//
// - What the heck do I need?
// - *canvas.Image, *canvas.Text
//
// - Behaviour:
// - Requesting a minimum size:
// - Return the pixel size of the display,
// and at the bottom add the label.
// - For a character 84 pixels tall,
// the TOSHIBA label is 35 pixels tall.
// That means the TOSHIBA label is 3 pixels tall.
// - Given a size allocation:
// - How does the API work?
// - Rendering:
// - This should really be the work of two CanvasObject subobjects.
// - The bitmap will be rendered pixel-stretched.
// - Below it, the text will be rendered.
//
// _______________________
// | |
// | VFD 2x20 |
// |______________________|
// |________Label_________|
//
// -
//
img := canvas.NewImageFromImage(display.Render())
img.FillMode = canvas.ImageFillOriginal
img.ScaleMode = canvas.ImageScalePixels
c := container.New(layout.NewVBoxLayout(), layout.NewSpacer(), img, layout.NewSpacer())
window.SetContent(c)
window.Resize(fyne.NewSize(600, 100))
go func() {
reader := bufio.NewReader(os.Stdin)
parser := newProtocolParser(display)
for {
b, err := reader.ReadByte()
if err != nil {
log.Println(err)
return
}
if parser.handleByte(b) {
fyne.DoAndWait(func() {
img.Image = display.Render()
img.Refresh()
})
}
}
}()
window.ShowAndRun()
}
|