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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
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/theme"
"fyne.io/fyne/v2/widget"
"janouch.name/desktop-tools/liust-50/charset"
)
// --- Display emulation -------------------------------------------------------
const (
displayWidth = 20
displayHeight = 2
charWidth = 5 + 1
charHeight = 7 + 1
)
// 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, character image.Image, cx, cy int) {
if character == nil {
return
}
bounds := character.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, _, _, _ := character.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 {
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
y := 0
for ; y < displayHeight-1; y++ {
d.chars[y] = d.chars[y+1]
}
for x := 0; x < displayWidth; x++ {
d.chars[y][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
paramStr, command := input[:cmdIdx], input[cmdIdx:]
if paramStr != "" {
for _, p := range strings.Split(paramStr, ";") {
if p = strings.TrimSpace(p); p == "" {
params = append(params, 0)
} else if value, err := strconv.Atoi(p); err == nil {
params = append(params, value)
}
}
}
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)
}
// --- Display widget ----------------------------------------------------------
type DisplayRenderer struct {
image *canvas.Image
label *canvas.Text
objects []fyne.CanvasObject
displayWidget *DisplayWidget
}
func (r *DisplayRenderer) Destroy() {}
func (r *DisplayRenderer) Layout(size fyne.Size) {
minSize := r.MinSize()
aspectRatio := minSize.Width / minSize.Height
var areaX, areaY, areaWidth, areaHeight float32
if size.Width/size.Height > aspectRatio {
areaHeight = size.Height
areaWidth = areaHeight * aspectRatio
areaX = (size.Width - areaWidth) / 2
} else {
areaWidth = size.Width
areaHeight = areaWidth / aspectRatio
areaY = (size.Height - areaHeight) / 2
}
imageHeight := areaHeight * (minSize.Height - 5) / minSize.Height
r.image.Move(fyne.NewPos(areaX, areaY))
r.image.Resize(fyne.NewSize(areaWidth, imageHeight))
// The appropriate TextSize for the desired label height is guesswork.
// In theory, we could figure out the relation between TextSize
// and measured height in our MinSize.
r.label.TextSize = (areaHeight - imageHeight) * 0.75
labelSize := r.label.MinSize()
// The VFD display is not mounted exactly in the centre of the device.
r.label.Move(fyne.NewPos(
areaX+(areaWidth-labelSize.Width)*0.525,
areaY+imageHeight))
r.label.Resize(labelSize)
}
func (r *DisplayRenderer) MinSize() fyne.Size {
// The VFD display doesn't have rectangular pixels,
// they are rather elongated in a roughly 3:4 ratio.
//
// Add space for the bottom label.
bounds := r.image.Image.Bounds()
return fyne.NewSize(float32(bounds.Dx()), float32(bounds.Dy())*1.25).
AddWidthHeight(0, 5)
}
func (r *DisplayRenderer) Objects() []fyne.CanvasObject { return r.objects }
func (r *DisplayRenderer) Refresh() {
r.image.Image = r.displayWidget.display.Render()
r.image.Refresh()
r.label.Refresh()
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type DisplayWidget struct {
widget.BaseWidget
display *Display
}
func NewDisplayWidget(display *Display) *DisplayWidget {
dw := &DisplayWidget{display: display}
dw.ExtendBaseWidget(dw)
return dw
}
func (dw *DisplayWidget) CreateRenderer() fyne.WidgetRenderer {
image := canvas.NewImageFromImage(dw.display.Render())
image.ScaleMode = canvas.ImageScalePixels
label := canvas.NewText("TOSHIBA", color.Gray{0x99})
label.TextStyle.Bold = true
return &DisplayRenderer{
image: image,
label: label,
objects: []fyne.CanvasObject{image, label},
displayWidget: dw,
}
}
// --- Main --------------------------------------------------------------------
func main() {
a := app.New()
a.Settings().SetTheme(theme.DarkTheme())
window := a.NewWindow("Toshiba Tec LIUST-50 Simulator")
display := NewDisplay()
display.Clear()
dw := NewDisplayWidget(display)
window.SetContent(dw)
window.Resize(fyne.NewSize(600, 150))
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() { dw.Refresh() })
}
}
}()
window.ShowAndRun()
}
|