refresh the display with new data
This commit is contained in:
+60
-60
@@ -1,11 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.dvdt.dev/david/bitcoin-ticker-pi/display"
|
||||
"git.dvdt.dev/david/bitcoin-ticker-pi/fonts"
|
||||
rgbmatrix "git.dvdt.dev/david/bitcoin-ticker-pi/go-rpi-rgb-led-matrix"
|
||||
"git.dvdt.dev/david/bitcoin-ticker-pi/mempool"
|
||||
)
|
||||
|
||||
const (
|
||||
RefreshRateHz = 60
|
||||
MempoolURL = "wss://mempool.dvdt.dev/v1/api/ws"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -23,78 +33,68 @@ func main() {
|
||||
display.NewSlot(12, 5, 4, 7, 27),
|
||||
display.NewSlot(8, 5, 4, 28, 27),
|
||||
)
|
||||
err := d.UpdateTopLeft([]display.Segment{
|
||||
{Text: "dollar", Color: fonts.Green},
|
||||
{Text: "4", Color: fonts.White},
|
||||
{Text: "2", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ticker := time.NewTicker(time.Second / RefreshRateHz)
|
||||
tdone := make(chan struct{})
|
||||
mdone := make(chan struct{})
|
||||
blocks := make(chan mempool.Block)
|
||||
errs := make(chan error)
|
||||
|
||||
mp := mempool.New(MempoolURL)
|
||||
err := mp.Init()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = d.UpdateTopRight([]display.Segment{
|
||||
{Text: "satoshi", Color: fonts.Orange},
|
||||
{Text: "2", Color: fonts.White},
|
||||
{Text: ".", Color: fonts.White},
|
||||
{Text: "5", Color: fonts.White},
|
||||
{Text: "K", Color: fonts.White},
|
||||
})
|
||||
go func(done chan struct{}, t *time.Ticker) {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-ticker.C:
|
||||
updateSlots(d, 44000, 821000, 100, 200, 300)
|
||||
}
|
||||
}
|
||||
}(tdone, ticker)
|
||||
}
|
||||
|
||||
func updateSlots(d *display.Display, price, height, lfee, mfee, hfee int) error {
|
||||
priceStr := fmt.Sprintf("%d", price)
|
||||
var priceSegments []display.Segment
|
||||
priceSegments = append(priceSegments, strToDisplaySegments("dollar", fonts.Green)...)
|
||||
priceSegments = append(priceSegments, strToDisplaySegments(fmt.Sprintf("%d", price), fonts.White)...)
|
||||
|
||||
err := d.UpdateTopLeft(priceSegments)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = d.UpdateCenter([]display.Segment{
|
||||
{Text: "height", Color: fonts.Blue},
|
||||
{Text: "8", Color: fonts.White},
|
||||
{Text: "2", Color: fonts.White},
|
||||
{Text: "1", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
})
|
||||
var moscowTimeSegments []display.Segment
|
||||
moscowTimeSegments = append(moscowTimeSegments, strToDisplaySegments("satoshi", fonts.Orange)...)
|
||||
moscowTimeSegments = append(moscowTimeSegments, strToDisplaySegments(fmt.Sprintf("%d", 100000000/price), fonts.White)...)
|
||||
|
||||
err = d.UpdateTopRight(moscowTimeSegments)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
var heightSegments []display.Segment
|
||||
heightSegments = append(heightSegments, strToDisplaySegments("height", fonts.Blue)...)
|
||||
heightSegments = append(heightSegments, strToDisplaySegments(fmt.Sprintf("%d", height), fonts.White)...)
|
||||
|
||||
err = d.UpdateBottomLeft([]display.Segment{
|
||||
{Text: "low", Color: fonts.Green},
|
||||
{Text: "1", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "medium", Color: fonts.Orange},
|
||||
{Text: "2", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "high", Color: fonts.Red},
|
||||
{Text: "3", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
})
|
||||
err = d.UpdateCenter(heightSegments)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = d.UpdateBottomRight([]display.Segment{
|
||||
{Text: "1", Color: fonts.White},
|
||||
{Text: "2", Color: fonts.White},
|
||||
{Text: ":", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: ":", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
{Text: "0", Color: fonts.White},
|
||||
})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err = d.Render()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
func strToDisplaySegments(s string, c color.Color) []display.Segment {
|
||||
var segs []display.Segment
|
||||
for _, r := range s {
|
||||
segs = append(segs, display.Segment{Text: string(r), Color: c})
|
||||
}
|
||||
|
||||
select {}
|
||||
return segs
|
||||
}
|
||||
|
||||
+8
-8
@@ -4,13 +4,13 @@ import (
|
||||
rgbmatrix "git.dvdt.dev/david/bitcoin-ticker-pi/go-rpi-rgb-led-matrix"
|
||||
)
|
||||
|
||||
type display struct {
|
||||
type Display struct {
|
||||
canvas *rgbmatrix.Canvas
|
||||
|
||||
tl, tr, c, bl, br *Slot
|
||||
}
|
||||
|
||||
func New(canvas *rgbmatrix.Canvas, tl, tr, c, bl, br *Slot) *display {
|
||||
func New(canvas *rgbmatrix.Canvas, tl, tr, c, bl, br *Slot) *Display {
|
||||
return &display{
|
||||
canvas: canvas,
|
||||
tl: tl,
|
||||
@@ -21,7 +21,7 @@ func New(canvas *rgbmatrix.Canvas, tl, tr, c, bl, br *Slot) *display {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *display) UpdateTopLeft(text []Segment) error {
|
||||
func (d *Display) UpdateTopLeft(text []Segment) error {
|
||||
err := d.tl.Update(text)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -30,7 +30,7 @@ func (d *display) UpdateTopLeft(text []Segment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *display) UpdateTopRight(text []Segment) error {
|
||||
func (d *Display) UpdateTopRight(text []Segment) error {
|
||||
err := d.tr.Update(text)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -39,7 +39,7 @@ func (d *display) UpdateTopRight(text []Segment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *display) UpdateCenter(text []Segment) error {
|
||||
func (d *Display) UpdateCenter(text []Segment) error {
|
||||
err := d.c.Update(text)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -48,7 +48,7 @@ func (d *display) UpdateCenter(text []Segment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *display) UpdateBottomLeft(text []Segment) error {
|
||||
func (d *Display) UpdateBottomLeft(text []Segment) error {
|
||||
err := d.bl.Update(text)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -57,7 +57,7 @@ func (d *display) UpdateBottomLeft(text []Segment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *display) UpdateBottomRight(text []Segment) error {
|
||||
func (d *Display) UpdateBottomRight(text []Segment) error {
|
||||
err := d.br.Update(text)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -66,6 +66,6 @@ func (d *display) UpdateBottomRight(text []Segment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *display) Render() error {
|
||||
func (d *Display) Render() error {
|
||||
return d.canvas.Render()
|
||||
}
|
||||
|
||||
+1
-1
@@ -140,7 +140,7 @@ var (
|
||||
"0": {
|
||||
{0, 1, 1, 1, 1, 1, 0, 0},
|
||||
{1, 1, 1, 1, 1, 1, 1, 0},
|
||||
{1, 1, 0, 0, 0, 1, 1, 0},
|
||||
{1, 1, 0, 0, 1, 1, 1, 0},
|
||||
{1, 1, 0, 1, 1, 1, 1, 0},
|
||||
{1, 1, 1, 1, 0, 1, 1, 0},
|
||||
{1, 1, 1, 0, 0, 1, 1, 0},
|
||||
|
||||
Reference in New Issue
Block a user