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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user