diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2025-10-05 17:02:02 +0200 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2025-10-06 20:59:19 +0200 |
commit | 5a3d96bd284f479a895c44855c71f19c5a62df9f (patch) | |
tree | 7e489a800acfd17f29c941de5bc1a4b12e54858e /liust-50 | |
parent | f26cfd3bb51023cf4c4ff23acd192125ed04881e (diff) | |
download | desktop-tools-5a3d96bd284f479a895c44855c71f19c5a62df9f.tar.gz desktop-tools-5a3d96bd284f479a895c44855c71f19c5a62df9f.tar.xz desktop-tools-5a3d96bd284f479a895c44855c71f19c5a62df9f.zip |
WIP: Add Toshiba Tec LIUST-A00 utilities
WIP: It still needs some cleanup.
Diffstat (limited to 'liust-50')
-rw-r--r-- | liust-50/README.adoc | 18 | ||||
-rw-r--r-- | liust-50/charset/charset.go | 211 | ||||
-rw-r--r-- | liust-50/charset/germany.png | bin | 0 -> 1204 bytes | |||
-rw-r--r-- | liust-50/charset/international.png | bin | 0 -> 805 bytes | |||
-rw-r--r-- | liust-50/charset/japan.png | bin | 0 -> 1194 bytes | |||
-rw-r--r-- | liust-50/cmd/liustatus/status.go | 208 | ||||
-rw-r--r-- | liust-50/cmd/liustatus/weather.go | 132 | ||||
-rw-r--r-- | liust-50/cmd/liustsim/simulator.go | 325 | ||||
-rw-r--r-- | liust-50/go.mod | 42 | ||||
-rw-r--r-- | liust-50/go.sum | 67 |
10 files changed, 1003 insertions, 0 deletions
diff --git a/liust-50/README.adoc b/liust-50/README.adoc new file mode 100644 index 0000000..0ffeaaf --- /dev/null +++ b/liust-50/README.adoc @@ -0,0 +1,18 @@ +LIUST-50 +======== + +Included here are a simulator for the Toshiba Tec LIUST-A00 (LIUST-50) +VFD line display, and a status program sending data to the device. + +For device documentation, see https://github.com/boricha/M202MD12D which is +seemingly a project for the later LIUST-A10. + +Device setup +------------ + # stty -F /dev/ttyS0 9600 parenb oddp -crtscts -cstopb cs8 + +Running +------- + # liustatus > /dev/ttyS0 + + $ liustatus | liustsim diff --git a/liust-50/charset/charset.go b/liust-50/charset/charset.go new file mode 100644 index 0000000..61196d6 --- /dev/null +++ b/liust-50/charset/charset.go @@ -0,0 +1,211 @@ +package charset + +import ( + "bytes" + _ "embed" + "image" + _ "image/png" + "log" +) + +// Charsets are loosely based on CP 437 and JIS X 0201. + +var runesJapan2 = [256]rune{ + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5A, 0x5B, '¥', 0x5D, 0x5E, 0x5F, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, '⌂', + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + '▒', '。', '「', '」', '、', '・', 'ヲ', 'ァ', + 'ィ', 'ゥ', 'ェ', 'ォ', 'ャ', 'ュ', 'ョ', 'ッ', + 'ー', 'ア', 'イ', 'ウ', 'エ', 'オ', 'カ', 'キ', + 'ク', 'ケ', 'コ', 'サ', 'シ', 'ス', 'セ', 'ソ', + 'タ', 'チ', 'ツ', 'テ', 'ト', 'ナ', 'ニ', 'ヌ', + 'ネ', 'ノ', 'ハ', 'ヒ', 'フ', 'ヘ', 'ホ', 'マ', + 'ミ', 'ム', 'メ', 'モ', 'ヤ', 'ユ', 'ヨ', 'ラ', + 'リ', 'ル', 'レ', 'ロ', 'ワ', 'ン', '゙', '゚', + 'α', 'ß', 'Γ', 'π', 'Σ', 'σ', 'µ', 'τ', + 'Φ', 'Θ', 'Ω', 'δ', '∞', 'φ', 'ε', '∩', + '→', '←', '↓', '↑', '½', '¼', '★', '◊', + '㎏', '℔', '�', '×', '▾', '▴', '日', ' ', +} + +var runesInternational = [256]rune{ + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, '⌂', + 'Ç', 'ü', 'é', 'â', 'ä', 'à', 'å', 'ç', + 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å', + 'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', + 'ÿ', 'Ö', 'Ü', '¢', '£', '¥', '₧', 'ƒ', + 'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', + '¿', '⌐', '¬', '½', '¼', '¡', '«', '»', + '░', '▒', '▓' - 1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, '█', '▄', '▌', '▐', '▀', + 'α', 'ß', 'Γ', 'π', 'Σ', 'σ', 'µ', 'τ', + 'Φ', 'Θ', 'Ω', 'δ', '∞', 'φ', 'ε', '∩', + '≡', '±', '≥', '≤', '⌠', '⌡', '÷', '≈', + '°', '∙', '·', '√', 'ⁿ', '²', '■', ' ', +} + +var runesInternationalVariants = []string{ + "#$@[\\]^`{|}~", // USA + "#$à·ç§^`éùè╍", // France + "#$§ÄÖÜ^`äöüß", // Germany + "£$@[\\]^`{|}~", // UK + "#$@ÆØÅ^`æøå~", // Denmark 1 + "#¤ÉÄÖÅÜéäöåü", // Sweden + "#$@·\\é^ùàòèì", // Italy + "₧$@¡Ñ¿^`╍ñ}~", // Spain + "#$@[¥]^`{|}~", // Japan + "#¤ÉÆØÅÜéæøåü", // Norway + "#$ÉÆØÅÜéæøåü", // Denmark 2 + "#$á¡Ñ¿é`íñóú", // Spain 2 + "#$á¡Ñ¿éüíñóú", // Latin America +} + +var internationalVariantsChars = []byte{ + 0x23, 0x24, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x60, 0x7B, 0x7C, 0x7D, 0x7E} + +// ResolveCharToRune tries to decode a character into a Unicode rune. +// It may return rune(-1) if the character is deemed to have no representation. +func ResolveCharToRune(char, charset uint8) rune { + if charset == 0x63 { + return runesJapan2[char] + } + if int(charset) >= len(runesInternationalVariants) { + return -1 + } + + for i, b := range internationalVariantsChars { + if char == b { + return []rune(runesInternationalVariants[charset])[i] + } + } + return runesInternational[char] +} + +// ResolveRune tries to find a corresponding character for a Unicode rune. +func ResolveRune(r rune, charset uint8) (uint8, bool) { + if charset == 0x63 { + for i, ch := range runesJapan2 { + if ch == r { + return uint8(i), true + } + } + return 0, false + } + if int(charset) >= len(runesInternationalVariants) { + return 0, false + } + + variantRunes := []rune(runesInternationalVariants[charset]) + for i, ch := range variantRunes { + if ch == r { + return internationalVariantsChars[i], true + } + } + for i, ch := range runesInternational { + if ch == r { + return uint8(i), true + } + } + return 0, false +} + +//go:embed japan.png +var pngJapan2 []byte +var imageJapan2 image.Image + +//go:embed germany.png +var pngGermany []byte +var imageGermany image.Image + +//go:embed international.png +var pngInternational []byte +var imageInternational image.Image + +func init() { + var err error + imageJapan2, _, err = image.Decode(bytes.NewReader(pngJapan2)) + if err != nil { + log.Fatalln(err) + } + + imageGermany, _, err = image.Decode(bytes.NewReader(pngGermany)) + if err != nil { + log.Fatalln(err) + } + + imageInternational, _, err = image.Decode(bytes.NewReader(pngInternational)) + if err != nil { + log.Fatalln(err) + } +} + +// ResolveCharToImage tries to decode a character into a 5x7 bitmap image +// (white on black). +func ResolveCharToImage(char, charset uint8) image.Image { + const ( + gridWidth = 6 + gridHeight = 8 + ) + + var src image.Image + var col, row int + if charset == 0x63 { + src, col, row = imageJapan2, int(char)/16, int(char)%16 + } else if int(charset) < len(runesInternationalVariants) { + src, col, row = imageGermany, int(char)/16, int(char)%16 + for i, b := range internationalVariantsChars { + if char == b { + src, col, row = imageInternational, i, int(charset) + } + } + } else { + return nil + } + + x0 := col * gridWidth + y0 := row * gridHeight + return src.(interface { + SubImage(r image.Rectangle) image.Image + }).SubImage(image.Rect( + x0, + y0, + x0+gridWidth-1, + y0+gridHeight-1, + )) +} diff --git a/liust-50/charset/germany.png b/liust-50/charset/germany.png Binary files differnew file mode 100644 index 0000000..851f065 --- /dev/null +++ b/liust-50/charset/germany.png diff --git a/liust-50/charset/international.png b/liust-50/charset/international.png Binary files differnew file mode 100644 index 0000000..b230a94 --- /dev/null +++ b/liust-50/charset/international.png diff --git a/liust-50/charset/japan.png b/liust-50/charset/japan.png Binary files differnew file mode 100644 index 0000000..48260d8 --- /dev/null +++ b/liust-50/charset/japan.png diff --git a/liust-50/cmd/liustatus/status.go b/liust-50/cmd/liustatus/status.go new file mode 100644 index 0000000..2c41e71 --- /dev/null +++ b/liust-50/cmd/liustatus/status.go @@ -0,0 +1,208 @@ +package main + +import ( + "fmt" + "os" + "strings" + "time" + + "janouch.name/desktop-tools/liust-50/charset" +) + +// TODO(p): Make more elaberate animations with these. +var kaomoji = []struct { + kao, message string +}{ + {"(o_o)", ""}, + {"(゚ロ゚)", ""}, + {"(゚∩゚)", ""}, + {"(^_^)", ""}, + {" (^_^)", ""}, + {"(^_^) ", ""}, + {"(x_x)", "ズキズキ"}, + {"(T_T)", "ズーン"}, + {"=^.^=", "ニャー"}, + {"(>_<)", "ゲップ"}, + {"(O_O)", "ジー"}, + {"(-_-)", ""}, + {"(-_-)", "グーグー"}, + {"(o_-)", ""}, + {"(-_o)", ""}, +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +const ( + displayWidth = 20 + displayHeight = 2 + targetCharset = 0x63 +) + +type DisplayState struct { + Display [displayHeight][displayWidth]uint8 +} + +type Display struct { + Current, Last DisplayState +} + +func NewDisplay() *Display { + t := &Display{} + for y := 0; y < displayHeight; y++ { + for x := 0; x < displayWidth; x++ { + t.Current.Display[y][x] = ' ' + t.Last.Display[y][x] = ' ' + } + } + return t +} + +func (t *Display) SetLine(row int, content string) { + if row < 0 || row >= displayHeight { + return + } + + runes := []rune(content) + for x := 0; x < displayWidth; x++ { + if x < len(runes) { + b, ok := charset.ResolveRune(runes[x], targetCharset) + if ok { + t.Current.Display[row][x] = b + } else { + t.Current.Display[row][x] = '?' + } + } else { + t.Current.Display[row][x] = ' ' + } + } +} + +func (t *Display) HasChanges() bool { + for y := 0; y < displayHeight; y++ { + for x := 0; x < displayWidth; x++ { + if t.Current.Display[y][x] != t.Last.Display[y][x] { + return true + } + } + } + return false +} + +func (t *Display) Update() { + for y := 0; y < displayHeight; y++ { + x := 0 + for x < displayWidth { + if t.Current.Display[y][x] == t.Last.Display[y][x] { + x++ + continue + } + + startX := x + var changes strings.Builder + for x < displayWidth && + t.Current.Display[y][x] != t.Last.Display[y][x] { + changes.WriteByte(t.Current.Display[y][x]) + t.Last.Display[y][x] = t.Current.Display[y][x] + x++ + } + + fmt.Printf("\x1b[%d;%dH%s", y+1, startX+1, changes.String()) + } + } + + os.Stdout.Sync() +} + +func centerText(text string, width int) string { + textLen := len([]rune(text)) + if textLen >= width { + return text[:width] + } + leftPad := (width - textLen + 1) / 2 + rightPad := width - textLen - leftPad + return strings.Repeat(" ", leftPad) + text + strings.Repeat(" ", rightPad) +} + +func kaomojiProducer(lines chan<- string) { + ticker := time.NewTicker(30_000 * time.Millisecond) + defer ticker.Stop() + + idx := 0 + for { + km := kaomoji[idx] + + line := centerText(km.kao, displayWidth) + if km.message != "" { + line = line[:14] + km.message + line += strings.Repeat(" ", displayWidth-len([]rune(line))) + } + + lines <- line + + idx = (idx + 1) % len(kaomoji) + <-ticker.C + } +} + +func statusProducer(lines chan<- string) { + ticker := time.NewTicker(1 * time.Second) + defer ticker.Stop() + + temperature, fetcher := "", NewWeatherFetcher() + temperatureChan := make(chan string) + go fetcher.Run(5*time.Minute, temperatureChan) + + for { + select { + case newTemperature := <-temperatureChan: + temperature = newTemperature + default: + } + + now := time.Now() + status := fmt.Sprintf("%s %3s %s", + now.Format("Mon 2 Jan"), temperature, now.Format("15:04")) + + // Ensure exactly 20 characters. + runes := []rune(status) + if len(runes) > displayWidth { + status = string(runes[:displayWidth]) + } else if len(runes) < displayWidth { + status = status + strings.Repeat(" ", displayWidth-len(runes)) + } + + lines <- status + <-ticker.C + } +} + +func main() { + terminal := NewDisplay() + + kaomojiChan := make(chan string, 1) + statusChan := make(chan string, 1) + + go kaomojiProducer(kaomojiChan) + go statusProducer(statusChan) + + // TODO(p): And we might want to disable cursor visibility as well. + fmt.Printf("\x1bR%c", targetCharset) + fmt.Print("\x1b[2J") // Clear display + + go func() { + kaomojiChan <- centerText(kaomoji[0].kao, displayWidth) + statusChan <- strings.Repeat(" ", displayWidth) + }() + + for { + select { + case line := <-kaomojiChan: + terminal.SetLine(0, line) + case line := <-statusChan: + terminal.SetLine(1, line) + } + if terminal.HasChanges() { + terminal.Update() + } + } +} diff --git a/liust-50/cmd/liustatus/weather.go b/liust-50/cmd/liustatus/weather.go new file mode 100644 index 0000000..c744f2f --- /dev/null +++ b/liust-50/cmd/liustatus/weather.go @@ -0,0 +1,132 @@ +package main + +import ( + "encoding/xml" + "fmt" + "io" + "log" + "net/http" + "strconv" + "time" +) + +const ( + baseURL = "https://api.met.no/weatherapi" + userAgent = "liustatus/1.0" + + // Prague coordinates. + lat = 50.08804 + lon = 14.42076 + altitude = 202 +) + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +type Weatherdata struct { + XMLName xml.Name `xml:"weatherdata"` + Product Product `xml:"product"` +} + +type Product struct { + Times []Time `xml:"time"` +} + +type Time struct { + From string `xml:"from,attr"` + To string `xml:"to,attr"` + Location Location `xml:"location"` +} + +type Location struct { + Temperature *Temperature `xml:"temperature"` +} + +type Temperature struct { + Unit string `xml:"unit,attr"` + Value string `xml:"value,attr"` +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +// WeatherFetcher handles weather data retrieval. +type WeatherFetcher struct { + client *http.Client +} + +// NewWeatherFetcher creates a new weather fetcher instance. +func NewWeatherFetcher() *WeatherFetcher { + return &WeatherFetcher{ + client: &http.Client{Timeout: 30 * time.Second}, + } +} + +// fetchWeather retrieves the current temperature from the API. +func (w *WeatherFetcher) fetchWeather() (string, error) { + url := fmt.Sprintf( + "%s/locationforecast/2.0/classic?lat=%.5f&lon=%.5f&altitude=%d", + baseURL, lat, lon, altitude) + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return "", err + } + + req.Header.Set("User-Agent", userAgent) + + resp, err := w.client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("API returned status %d", resp.StatusCode) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var weatherData Weatherdata + if err := xml.Unmarshal(body, &weatherData); err != nil { + return "", err + } + + now := time.Now().UTC() + for _, t := range weatherData.Product.Times { + toTime, err := time.Parse("2006-01-02T15:04:05Z", t.To) + if err != nil || toTime.Before(now) { + continue + } + if t.Location.Temperature != nil { + temp, err := strconv.ParseFloat(t.Location.Temperature.Value, 64) + if err != nil { + continue + } + return fmt.Sprintf("%d゚", int(temp)), nil + } + } + + return "", fmt.Errorf("no usable temperature data found") +} + +// update fetches new weather data and returns it. +func (w *WeatherFetcher) update() string { + temp, err := w.fetchWeather() + if err != nil { + log.Printf("Error fetching weather: %v", err) + } + return temp +} + +// Run runs as a goroutine to periodically fetch weather data. +func (w *WeatherFetcher) Run(interval time.Duration, output chan<- string) { + ticker := time.NewTicker(interval) + defer ticker.Stop() + + output <- w.update() + for range ticker.C { + output <- w.update() + } +} diff --git a/liust-50/cmd/liustsim/simulator.go b/liust-50/cmd/liustsim/simulator.go new file mode 100644 index 0000000..43e06d9 --- /dev/null +++ b/liust-50/cmd/liustsim/simulator.go @@ -0,0 +1,325 @@ +package main + +import ( + "bufio" + "image" + "image/color" + "os" + "strconv" + "strings" + + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/app" + "fyne.io/fyne/v2/canvas" + + "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 { + image *canvas.Image + 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++ { + c := charImg.At(bounds.Min.X+dx, bounds.Min.Y+dy) + if r, _, _, _ := c.RGBA(); r >= 0x8000 { + c = color.RGBA{0x00, 0xFF, 0xA0, 0xFF} + } else { + c = color.RGBA{0x20, 0x20, 0x20, 0xFF} + } + img.Set(1+cx*charWidth+dx, 1+cy*charHeight+dy, c) + } + } +} + +func (d *Display) Render() image.Image { + width := 1 + displayWidth*charWidth + height := 1 + displayHeight*charHeight + + img := image.NewRGBA(image.Rect(0, 0, width, height)) + for y := 0; y < height; y++ { + for x := 0; x < width; x++ { + img.Set(x, y, color.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 escapeParser struct { + seq strings.Builder + inEsc bool + inCSI bool + display *Display +} + +func newEscapeParser(d *Display) *escapeParser { + return &escapeParser{display: d} +} + +func (ep *escapeParser) reset() { + ep.inEsc = false + ep.inCSI = false + ep.seq.Reset() +} + +func (ep *escapeParser) handleCSICommand() bool { + cmd, params := parseANSI(ep.seq.String()) + + switch cmd { + case "J": // Clear display + // XXX: No params case is unverified. + if len(params) == 0 || params[0] == 2 { + ep.display.Clear() + } + case "K": // Delete to end of line + // XXX: No params case is unverified (but it should work). + if len(params) == 0 || params[0] == 0 { + ep.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 + } + ep.display.SetCursor(x, y) + } + return true +} + +func (ep *escapeParser) handleEscapeSequence(b byte) bool { + ep.seq.WriteByte(b) + + if ep.seq.Len() == 2 && b == '[' { + ep.inCSI = true + return false + } + + if ep.seq.Len() == 3 && ep.seq.String()[1] == 'R' { + ep.display.charset = b + ep.reset() + return true + } + + if ep.inCSI && (b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z') { + refresh := ep.handleCSICommand() + ep.reset() + return refresh + } + + if ep.seq.Len() == 6 && ep.seq.String()[1:5] == "\\?LC" { + ep.display.cursorMode = int(ep.seq.String()[5]) + return true + } + + return false +} + +func (ep *escapeParser) handleControlChar(b byte) bool { + switch b { + case 0x0A: // LF + ep.display.LineFeed() + return true + case 0x0D: // CR + ep.display.CarriageReturn() + return true + case 0x08: // BS + ep.display.Backspace() + return true + default: + if b >= 0x20 { + ep.display.PutChar(b) + return true + } + } + return false +} + +func (ep *escapeParser) handleByte(b byte) (needsRefresh bool) { + if b == 0x1b { // ESC + ep.reset() + ep.inEsc = true + ep.seq.WriteByte(b) + return false + } + + if ep.inEsc { + return ep.handleEscapeSequence(b) + } + + return ep.handleControlChar(b) +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +func main() { + myApp := app.New() + window := myApp.NewWindow("Toshiba Tec LIUST-50 Simulator") + + display := NewDisplay() + display.Clear() + + img := canvas.NewImageFromImage(display.Render()) + img.FillMode = canvas.ImageFillOriginal + img.ScaleMode = canvas.ImageScalePixels + + window.SetContent(img) + + window.Resize(fyne.NewSize(600, 100)) + + go func() { + reader := bufio.NewReader(os.Stdin) + parser := newEscapeParser(display) + + for { + b, err := reader.ReadByte() + if err != nil { + fyne.DoAndWait(func() { + window.SetTitle(err.Error()) + }) + return + } + + if parser.handleByte(b) { + fyne.DoAndWait(func() { + img.Image = display.Render() + img.Refresh() + }) + } + } + }() + + window.ShowAndRun() +} diff --git a/liust-50/go.mod b/liust-50/go.mod new file mode 100644 index 0000000..d2b87a2 --- /dev/null +++ b/liust-50/go.mod @@ -0,0 +1,42 @@ +module janouch.name/desktop-tools/liust-50 + +go 1.25.1 + +require ( + fyne.io/fyne v1.4.3 + fyne.io/fyne/v2 v2.6.3 +) + +require ( + fyne.io/systray v1.11.0 // indirect + github.com/BurntSushi/toml v1.4.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fredbi/uri v1.1.0 // indirect + github.com/fsnotify/fsnotify v1.9.0 // indirect + github.com/fyne-io/gl-js v0.2.0 // indirect + github.com/fyne-io/glfw-js v0.3.0 // indirect + github.com/fyne-io/image v0.1.1 // indirect + github.com/fyne-io/oksvg v0.1.0 // indirect + github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 // indirect + github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a // indirect + github.com/go-text/render v0.2.0 // indirect + github.com/go-text/typesetting v0.2.1 // indirect + github.com/godbus/dbus/v5 v5.1.0 // indirect + github.com/hack-pad/go-indexeddb v0.3.2 // indirect + github.com/hack-pad/safejs v0.1.0 // indirect + github.com/jeandeaual/go-locale v0.0.0-20250612000132-0ef82f21eade // indirect + github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 // indirect + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect + github.com/nicksnyder/go-i18n/v2 v2.5.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rymdport/portal v0.4.1 // indirect + github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirect + github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect + github.com/stretchr/testify v1.10.0 // indirect + github.com/yuin/goldmark v1.7.8 // indirect + golang.org/x/image v0.24.0 // indirect + golang.org/x/net v0.35.0 // indirect + golang.org/x/sys v0.30.0 // indirect + golang.org/x/text v0.22.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/liust-50/go.sum b/liust-50/go.sum new file mode 100644 index 0000000..4de0a8e --- /dev/null +++ b/liust-50/go.sum @@ -0,0 +1,67 @@ +fyne.io/fyne v1.4.3 h1:356CnXCiYrrfaLGsB7qLK3c6ktzyh8WR05v/2RBu51I= +fyne.io/fyne v1.4.3/go.mod h1:8kiPBNSDmuplxs9WnKCkaWYqbcXFy0DeAzwa6PBO9Z8= +fyne.io/fyne/v2 v2.6.3 h1:cvtM2KHeRuH+WhtHiA63z5wJVBkQ9+Ay0UMl9PxFHyA= +fyne.io/fyne/v2 v2.6.3/go.mod h1:NGSurpRElVoI1G3h+ab2df3O5KLGh1CGbsMMcX0bPIs= +fyne.io/systray v1.11.0 h1:D9HISlxSkx+jHSniMBR6fCFOUjk1x/OOOJLa9lJYAKg= +fyne.io/systray v1.11.0/go.mod h1:RVwqP9nYMo7h5zViCBHri2FgjXF7H2cub7MAq4NSoLs= +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fredbi/uri v1.1.0 h1:OqLpTXtyRg9ABReqvDGdJPqZUxs8cyBDOMXBbskCaB8= +github.com/fredbi/uri v1.1.0/go.mod h1:aYTUoAXBOq7BLfVJ8GnKmfcuURosB1xyHDIfWeC/iW4= +github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= +github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/fyne-io/gl-js v0.2.0 h1:+EXMLVEa18EfkXBVKhifYB6OGs3HwKO3lUElA0LlAjs= +github.com/fyne-io/gl-js v0.2.0/go.mod h1:ZcepK8vmOYLu96JoxbCKJy2ybr+g1pTnaBDdl7c3ajI= +github.com/fyne-io/glfw-js v0.3.0 h1:d8k2+Y7l+zy2pc7wlGRyPfTgZoqDf3AI4G+2zOWhWUk= +github.com/fyne-io/glfw-js v0.3.0/go.mod h1:Ri6te7rdZtBgBpxLW19uBpp3Dl6K9K/bRaYdJ22G8Jk= +github.com/fyne-io/image v0.1.1 h1:WH0z4H7qfvNUw5l4p3bC1q70sa5+YWVt6HCj7y4VNyA= +github.com/fyne-io/image v0.1.1/go.mod h1:xrfYBh6yspc+KjkgdZU/ifUC9sPA5Iv7WYUBzQKK7JM= +github.com/fyne-io/oksvg v0.1.0 h1:7EUKk3HV3Y2E+qypp3nWqMXD7mum0hCw2KEGhI1fnBw= +github.com/fyne-io/oksvg v0.1.0/go.mod h1:dJ9oEkPiWhnTFNCmRgEze+YNprJF7YRbpjgpWS4kzoI= +github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 h1:5BVwOaUSBTlVZowGO6VZGw2H/zl9nrd3eCZfYV+NfQA= +github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a h1:vxnBhFDDT+xzxf1jTJKMKZw3H0swfWk9RpWbBbDK5+0= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-text/render v0.2.0 h1:LBYoTmp5jYiJ4NPqDc2pz17MLmA3wHw1dZSVGcOdeAc= +github.com/go-text/render v0.2.0/go.mod h1:CkiqfukRGKJA5vZZISkjSYrcdtgKQWRa2HIzvwNN5SU= +github.com/go-text/typesetting v0.2.1 h1:x0jMOGyO3d1qFAPI0j4GSsh7M0Q3Ypjzr4+CEVg82V8= +github.com/go-text/typesetting v0.2.1/go.mod h1:mTOxEwasOFpAMBjEQDhdWRckoLLeI/+qrQeBCTGEt6M= +github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= +github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/hack-pad/go-indexeddb v0.3.2 h1:DTqeJJYc1usa45Q5r52t01KhvlSN02+Oq+tQbSBI91A= +github.com/hack-pad/go-indexeddb v0.3.2/go.mod h1:QvfTevpDVlkfomY498LhstjwbPW6QC4VC/lxYb0Kom0= +github.com/hack-pad/safejs v0.1.0 h1:qPS6vjreAqh2amUqj4WNG1zIw7qlRQJ9K10eDKMCnE8= +github.com/hack-pad/safejs v0.1.0/go.mod h1:HdS+bKF1NrE72VoXZeWzxFOVQVUSqZJAG0xNCnb+Tio= +github.com/jeandeaual/go-locale v0.0.0-20250612000132-0ef82f21eade h1:FmusiCI1wHw+XQbvL9M+1r/C3SPqKrmBaIOYwVfQoDE= +github.com/jeandeaual/go-locale v0.0.0-20250612000132-0ef82f21eade/go.mod h1:ZDXo8KHryOWSIqnsb/CiDq7hQUYryCgdVnxbj8tDG7o= +github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 h1:YLvr1eE6cdCqjOe972w/cYF+FjW34v27+9Vo5106B4M= +github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= +github.com/nicksnyder/go-i18n/v2 v2.5.1 h1:IxtPxYsR9Gp60cGXjfuR/llTqV8aYMsC472zD0D1vHk= +github.com/nicksnyder/go-i18n/v2 v2.5.1/go.mod h1:DrhgsSDZxoAfvVrBVLXoxZn/pN5TXqaDbq7ju94viiQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rymdport/portal v0.4.1 h1:2dnZhjf5uEaeDjeF/yBIeeRo6pNI2QAKm7kq1w/kbnA= +github.com/rymdport/portal v0.4.1/go.mod h1:kFF4jslnJ8pD5uCi17brj/ODlfIidOxlgUDTO5ncnC4= +github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c h1:km8GpoQut05eY3GiYWEedbTT0qnSxrCjsVbb7yKY1KE= +github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q= +github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef h1:Ch6Q+AZUxDBCVqdkI8FSpFyZDtCVBc2VmejdNrm5rRQ= +github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef/go.mod h1:nXTWP6+gD5+LUJ8krVhhoeHjvHTutPxMYl5SvkcnJNE= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic= +github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= +golang.org/x/image v0.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ= +golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8= +golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= +golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |