Add Toshiba Tec LIUST-A00 utilities
This commit is contained in:
242
liust-50/cmd/liustatus/kaomoji.go
Normal file
242
liust-50/cmd/liustatus/kaomoji.go
Normal file
@@ -0,0 +1,242 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type kaomojiKind int
|
||||
|
||||
const (
|
||||
kaomojiKindAwake kaomojiKind = iota
|
||||
kaomojiKindBlink
|
||||
kaomojiKindFace
|
||||
kaomojiKindChase
|
||||
kaomojiKindHappy
|
||||
kaomojiKindSleep
|
||||
kaomojiKindSnore
|
||||
kaomojiKindPeek
|
||||
)
|
||||
|
||||
type kaomojiState struct {
|
||||
kind kaomojiKind
|
||||
face string
|
||||
message string
|
||||
delay int
|
||||
}
|
||||
|
||||
func (ks *kaomojiState) Format() string {
|
||||
line := []rune(strings.Repeat(" ", displayWidth))
|
||||
|
||||
face := []rune(ks.face)
|
||||
if x := (len(line) - len(face) + 1) / 2; x < 0 {
|
||||
copy(line, face)
|
||||
} else {
|
||||
copy(line[x:], face)
|
||||
}
|
||||
|
||||
if ks.message != "" {
|
||||
copy(line[14:], []rune(ks.message))
|
||||
}
|
||||
return string(line)
|
||||
}
|
||||
|
||||
func (ks *kaomojiState) Duration() time.Duration {
|
||||
return time.Millisecond * time.Duration(ks.delay)
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
func kaomojiNewAwake() kaomojiState {
|
||||
return kaomojiState{
|
||||
kind: kaomojiKindAwake,
|
||||
face: "(o_o)",
|
||||
message: "",
|
||||
delay: 2_000 + rand.Intn(4_000),
|
||||
}
|
||||
}
|
||||
|
||||
func kaomojiNewBlink() kaomojiState {
|
||||
return kaomojiState{
|
||||
kind: kaomojiKindBlink,
|
||||
face: "(-_-)",
|
||||
message: "",
|
||||
delay: 100 + rand.Intn(50),
|
||||
}
|
||||
}
|
||||
|
||||
func kaomojiNewFace() kaomojiState {
|
||||
faces := []struct {
|
||||
face, message string
|
||||
}{
|
||||
{"(x_x)", "ズキズキ"},
|
||||
{"(T_T)", "ズーン"},
|
||||
{"=^.^=", "ニャー"},
|
||||
{"(>_<)", "ゲップ"},
|
||||
{"(O_O)", "ジー"},
|
||||
}
|
||||
|
||||
x := faces[rand.Intn(len(faces))]
|
||||
return kaomojiState{
|
||||
kind: kaomojiKindFace,
|
||||
face: x.face,
|
||||
message: x.message,
|
||||
delay: 10_000,
|
||||
}
|
||||
}
|
||||
|
||||
func kaomojiNewChase() kaomojiState {
|
||||
faces := []string{"(゚ロ゚)", "(゚∩゚)"}
|
||||
return kaomojiState{
|
||||
kind: kaomojiKindChase,
|
||||
face: faces[rand.Intn(len(faces))],
|
||||
message: "",
|
||||
delay: 125,
|
||||
}
|
||||
}
|
||||
|
||||
func kaomojiNewHappy() kaomojiState {
|
||||
return kaomojiState{
|
||||
kind: kaomojiKindHappy,
|
||||
face: "(^_^)",
|
||||
message: "",
|
||||
delay: 500,
|
||||
}
|
||||
}
|
||||
|
||||
func kaomojiNewSleep() kaomojiState {
|
||||
return kaomojiState{
|
||||
kind: kaomojiKindSleep,
|
||||
face: "(-_-)",
|
||||
message: "",
|
||||
delay: 10_000,
|
||||
}
|
||||
}
|
||||
|
||||
func kaomojiNewSnore() kaomojiState {
|
||||
return kaomojiState{
|
||||
kind: kaomojiKindSnore,
|
||||
face: "(-_-)",
|
||||
message: "グーグー",
|
||||
delay: 10_000,
|
||||
}
|
||||
}
|
||||
|
||||
func kaomojiNewPeek() kaomojiState {
|
||||
faces := []string{"(o_-)", "(-_o)"}
|
||||
return kaomojiState{
|
||||
kind: kaomojiKindPeek,
|
||||
face: faces[rand.Intn(len(faces))],
|
||||
message: "",
|
||||
delay: 3_000,
|
||||
}
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
func kaomojiAnimateChase(state kaomojiState) (lines []string) {
|
||||
// The main character is fixed and of fixed width.
|
||||
var (
|
||||
normal = []rune("(o_o)")
|
||||
alert = []rune("(O_O)")
|
||||
centre = (displayWidth - 4) / 2
|
||||
chaserLen = len([]rune(state.face))
|
||||
)
|
||||
|
||||
// For simplicity, let the animation run off-screen.
|
||||
for chaserX := chaserLen + displayWidth; chaserX >= 0; chaserX-- {
|
||||
line := []rune(strings.Repeat(" ", chaserLen+displayWidth))
|
||||
|
||||
chased, chasedX := normal, chaserLen+centre
|
||||
if chasedX > chaserX-7 {
|
||||
chased, chasedX = alert, chaserX-7
|
||||
}
|
||||
if chasedX >= 0 {
|
||||
copy(line[chasedX:], chased)
|
||||
}
|
||||
|
||||
copy(line[chaserX:], []rune(state.face))
|
||||
lines = append(lines, string(line[chaserLen:]))
|
||||
}
|
||||
|
||||
// Return our main character back.
|
||||
for chasedX := displayWidth; chasedX >= centre; chasedX-- {
|
||||
line := []rune(strings.Repeat(" ", displayWidth))
|
||||
copy(line[chasedX:], normal)
|
||||
lines = append(lines, string(line))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func kaomojiProducer(lines chan<- string) {
|
||||
state := kaomojiNewAwake()
|
||||
execute := func() {
|
||||
lines <- state.Format()
|
||||
time.Sleep(state.Duration())
|
||||
}
|
||||
|
||||
for {
|
||||
switch state.kind {
|
||||
case kaomojiKindAwake:
|
||||
execute()
|
||||
switch f := rand.Float32(); {
|
||||
case f < 0.025:
|
||||
state = kaomojiNewFace()
|
||||
case f < 0.050:
|
||||
state = kaomojiNewChase()
|
||||
case f < 0.075:
|
||||
state = kaomojiNewHappy()
|
||||
case f < 0.100:
|
||||
state = kaomojiNewSleep()
|
||||
default:
|
||||
state = kaomojiNewBlink()
|
||||
}
|
||||
|
||||
case kaomojiKindBlink, kaomojiKindFace:
|
||||
execute()
|
||||
state = kaomojiNewAwake()
|
||||
|
||||
case kaomojiKindHappy:
|
||||
face := state.face
|
||||
execute()
|
||||
state.face = " " + face
|
||||
execute()
|
||||
state.face = face
|
||||
execute()
|
||||
state.face = face + " "
|
||||
execute()
|
||||
state.face = face
|
||||
execute()
|
||||
state = kaomojiNewAwake()
|
||||
|
||||
case kaomojiKindChase:
|
||||
for _, line := range kaomojiAnimateChase(state) {
|
||||
lines <- line
|
||||
time.Sleep(state.Duration())
|
||||
}
|
||||
state = kaomojiNewAwake()
|
||||
|
||||
case kaomojiKindSleep:
|
||||
execute()
|
||||
switch f := rand.Float32(); {
|
||||
case f < 0.10:
|
||||
state = kaomojiNewAwake()
|
||||
case f < 0.20:
|
||||
state = kaomojiNewPeek()
|
||||
case f < 0.60:
|
||||
state = kaomojiNewSnore()
|
||||
default:
|
||||
state = kaomojiNewSleep()
|
||||
}
|
||||
|
||||
case kaomojiKindSnore:
|
||||
execute()
|
||||
state = kaomojiNewSleep()
|
||||
|
||||
case kaomojiKindPeek:
|
||||
execute()
|
||||
state = kaomojiNewSleep()
|
||||
}
|
||||
}
|
||||
}
|
||||
146
liust-50/cmd/liustatus/status.go
Normal file
146
liust-50/cmd/liustatus/status.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"janouch.name/desktop-tools/liust-50/charset"
|
||||
)
|
||||
|
||||
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++ {
|
||||
start := -1
|
||||
for x := 0; x < displayWidth; x++ {
|
||||
if t.Current.Display[y][x] != t.Last.Display[y][x] {
|
||||
start = x
|
||||
break
|
||||
}
|
||||
}
|
||||
if start >= 0 {
|
||||
fmt.Printf("\x1b[%d;%dH%s",
|
||||
y+1, start+1, []byte(t.Current.Display[y][start:]))
|
||||
copy(t.Last.Display[y][start:], t.Current.Display[y][start:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
terminal := NewDisplay()
|
||||
|
||||
kaomojiChan := make(chan string, 1)
|
||||
statusChan := make(chan string, 1)
|
||||
go func() {
|
||||
kaomojiChan <- strings.Repeat(" ", displayWidth)
|
||||
statusChan <- strings.Repeat(" ", displayWidth)
|
||||
}()
|
||||
|
||||
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
|
||||
|
||||
for {
|
||||
select {
|
||||
case line := <-kaomojiChan:
|
||||
terminal.SetLine(0, line)
|
||||
case line := <-statusChan:
|
||||
terminal.SetLine(1, line)
|
||||
}
|
||||
if terminal.HasChanges() {
|
||||
terminal.Update()
|
||||
}
|
||||
}
|
||||
}
|
||||
132
liust-50/cmd/liustatus/weather.go
Normal file
132
liust-50/cmd/liustatus/weather.go
Normal file
@@ -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()
|
||||
}
|
||||
}
|
||||
411
liust-50/cmd/liustsim/simulator.go
Normal file
411
liust-50/cmd/liustsim/simulator.go
Normal file
@@ -0,0 +1,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()
|
||||
}
|
||||
Reference in New Issue
Block a user