Allow left, center, and right alignment of text

This commit is contained in:
2024-01-09 22:48:47 -05:00
parent 6516c40862
commit 5884b37d5a
5 changed files with 50 additions and 18 deletions
+11 -2
View File
@@ -1,8 +1,17 @@
.PHONY: setup run build install
setup: setup:
cd go-rpi-rgb-led-matrix/lib/rpi-rgb-led-matrix && make cd go-rpi-rgb-led-matrix/lib/rpi-rgb-led-matrix && make
run: rundev:
sudo go run cmd/rpi/main.go --led-slowdown-gpio=2 --led-pwm-dither-bits=1 sudo go run cmd/rpi/main.go --led-slowdown-gpio=2 --led-pwm-dither-bits=1
build: build:
go build cmd/rpi/main.go go build -o blockyclock cmd/rpi/main.go
install:
$(MAKE) setup && \
$(MAKE) build && \
sudo mv blockyclock /usr/bin && \
sudo cp blockyclock.service /lib/systemd/system/ && \
sudo systemctl enable --now blockyclock.service
+5 -6
View File
@@ -16,7 +16,7 @@ import (
) )
const ( const (
RefreshRateHz = 1 RefreshRateHz = 120
MempoolURL = "mempool.space" MempoolURL = "mempool.space"
) )
@@ -29,10 +29,10 @@ func main() {
c := rgbmatrix.NewCanvas(m) c := rgbmatrix.NewCanvas(m)
d := display.New(c, d := display.New(c,
display.NewSlot(9, 5, 4, 0, 0), display.NewSlot(display.Left, 9, 5, 4, 0, 0),
display.NewSlot(5, 5, 4, 44, 0), display.NewSlot(display.Right, 5, 5, 4, 44, 0),
display.NewSlot(7, 13, 8, 5, 9), display.NewSlot(display.Center, 8, 13, 8, 0, 9),
display.NewSlot(12, 5, 4, 7, 27), display.NewSlot(display.Center, 16, 5, 4, 0, 27),
) )
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@@ -85,7 +85,6 @@ func main() {
for { for {
select { select {
case m := <-msgs: case m := <-msgs:
fmt.Printf("%+v\n", m)
if m.Fees != nil { if m.Fees != nil {
feesMutex.Lock() feesMutex.Lock()
fees = *m.Fees fees = *m.Fees
+27 -3
View File
@@ -9,6 +9,14 @@ import (
rgbmatrix "git.dvdt.dev/david/bitcoin-ticker-pi/go-rpi-rgb-led-matrix" rgbmatrix "git.dvdt.dev/david/bitcoin-ticker-pi/go-rpi-rgb-led-matrix"
) )
type Alignment uint8
const (
Left Alignment = iota
Center
Right
)
var ( var (
ErrUnsupportedSlotHeight = errors.New("unsupported slot height") ErrUnsupportedSlotHeight = errors.New("unsupported slot height")
) )
@@ -33,15 +41,17 @@ func drawRow(canvas *rgbmatrix.Canvas, row []color.RGBA, x, y int) {
} }
type Slot struct { type Slot struct {
alignment Alignment
segmentLimit int segmentLimit int
segmentHeight int segmentHeight int
segmentWidth int segmentWidth int
x, y int x, xoffset, y int
text []Segment text []Segment
} }
func NewSlot(limit, height, width, x, y int) *Slot { func NewSlot(alignment Alignment, limit, height, width, x, y int) *Slot {
return &Slot{ return &Slot{
alignment: alignment,
segmentLimit: limit, segmentLimit: limit,
segmentHeight: height, segmentHeight: height,
segmentWidth: width, segmentWidth: width,
@@ -61,6 +71,20 @@ func (s *Slot) Update(text []Segment) error {
}...) }...)
} }
// Default to left alignment.
s.xoffset = 0
if s.alignment == Center {
s.xoffset = ((s.segmentLimit * s.segmentWidth) - (len(text) * s.segmentWidth)) / 2
}
if s.alignment == Right {
s.xoffset = (s.segmentLimit * s.segmentWidth) - (len(text) * s.segmentWidth)
}
fmt.Printf("segmentLimit: %d\n", s.segmentLimit)
fmt.Printf("segmentWidth: %d\n", s.segmentWidth)
fmt.Printf("len(text): %d\n", len(text))
fmt.Printf("xoffset value: %d\n", s.xoffset)
var font fonts.Font var font fonts.Font
switch s.segmentHeight { switch s.segmentHeight {
case 5: case 5:
@@ -88,6 +112,6 @@ func (s *Slot) Update(text []Segment) error {
func (s *Slot) Draw(canvas *rgbmatrix.Canvas) { func (s *Slot) Draw(canvas *rgbmatrix.Canvas) {
for i, segment := range s.text { for i, segment := range s.text {
segment.Draw(canvas, s.x+(i*s.segmentWidth), s.y) segment.Draw(canvas, s.x+(i*s.segmentWidth)+s.xoffset, s.y)
} }
} }
+3 -3
View File
@@ -21,7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{0, 0, 1, 0},
{0, 1, 1, 0}, {1, 1, 0, 0},
}, },
"0": { "0": {
{0, 1, 0, 0}, {0, 1, 0, 0},
@@ -73,10 +73,10 @@ var (
{0, 1, 0, 0}, {0, 1, 0, 0},
}, },
"satoshi": { "satoshi": {
{0, 0, 1, 0},
{0, 1, 0, 0}, {0, 1, 0, 0},
{1, 1, 1, 0}, {1, 1, 1, 1},
{1, 1, 1, 0}, {0, 0, 1, 0},
{1, 1, 1, 0},
{0, 1, 0, 0}, {0, 1, 0, 0},
}, },
"dollar": { "dollar": {