Skip to content

Commit

Permalink
led and ledbar widget to visualize binary data
Browse files Browse the repository at this point in the history
  • Loading branch information
persuader72 committed Mar 29, 2024
1 parent 3ba9170 commit 957486e
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cmd/led_demo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"image/color"
"time"

"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
xwidget "fyne.io/x/fyne/widget"
)

var (
counter1 int
ledbar1 *xwidget.LedBar
ledbar2 *xwidget.LedBar
)

func run() {
for {
counter1 += 1
ledbar1.Set(counter1 & 0xFF)
ledbar2.Set(^(counter1 & 0xFF))
time.Sleep(time.Millisecond * 350)
}
}

func main() {
a := app.New()
w := a.NewWindow("Led")

ledbar1 = xwidget.NewLedBar([]string{"BIT7", "BIT6", "BIT5", "BIT4", "BIT3", "BIT2", "BIT1", "BIT0"})
ledbar2 = xwidget.NewLedBar([]string{"BIT7", "BIT6", "BIT5", "BIT4", "BIT3", "BIT2", "BIT1", "BIT0"})
ledbar2.SetOnColor(color.RGBA{0, 255, 0, 255})

c := container.NewVBox(ledbar1, ledbar2)

w.SetContent(c)
go run()
w.ShowAndRun()
}
145 changes: 145 additions & 0 deletions widget/led.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package widget

import (
"image/color"
"log"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)

var defaultLedOnColor color.Color = color.RGBA{255, 0, 0, 255}
var defaultLedOffColor color.Color = color.RGBA{120, 120, 120, 255}

type ledRenderer struct {
led *Led
circle *canvas.Circle
text *canvas.Text
}

func (h *ledRenderer) MinSize() fyne.Size {
log.Println(h.led.Size())
return fyne.NewSize(h.led.Size().Width, h.led.Size().Height)
}

func (h *ledRenderer) Layout(_ fyne.Size) {
h.circle.Resize(fyne.NewSize(h.led.MinSize().Width, h.led.MinSize().Width))
pos := fyne.NewPos(
(h.led.MinSize().Width-h.text.MinSize().Width)/2,
h.led.MinSize().Height-h.text.MinSize().Height,
)
h.text.Move(pos)
}

func (h *ledRenderer) Destroy() {
}

func (h *ledRenderer) Objects() []fyne.CanvasObject {
return []fyne.CanvasObject{h.circle, h.text}
}

func (h *ledRenderer) Refresh() {
h.circle.FillColor = h.led.getLedColor()
canvas.Refresh(h.circle)
}

type Led struct {
widget.BaseWidget
state bool
text string
OnColor color.Color
offColor color.Color
}

// NewMap creates a new instance of the map widget.
func NewLed(text string) *Led {
m := &Led{
state: false,
text: text,
OnColor: defaultLedOnColor,
offColor: defaultLedOffColor,
}
m.ExtendBaseWidget(m)
return m
}

func (w *Led) getLedColor() color.Color {
if w.state {
return w.OnColor
} else {
return w.offColor
}
}

func (w *Led) MinSize() fyne.Size {
return fyne.NewSize(30, 45)
}

func (w *Led) Set(state bool) {
w.state = state
w.Refresh()
}

// CreateRenderer returns the renderer for this widget.
// A map renderer is simply the map Raster with user interface elements overlaid.
func (m *Led) CreateRenderer() fyne.WidgetRenderer {
circ0 := canvas.NewCircle(defaultLedOffColor)
circ0.StrokeColor = color.Black
circ0.StrokeWidth = 3

text0 := canvas.NewText(m.text, color.Black)
text0.TextSize = 10

r := &ledRenderer{led: m, circle: circ0, text: text0}
return r
}

type LedBar struct {
widget.BaseWidget
texts []string
leds []fyne.CanvasObject
}

func NewLedBar(texts []string) *LedBar {
m := &LedBar{texts: texts}
m.ExtendBaseWidget(m)
m.makeLeds()
return m
}

func (m *LedBar) makeLeds() {
m.leds = make([]fyne.CanvasObject, len(m.texts))
for i := 0; i < len(m.texts); i++ {
m.leds[i] = NewLed(m.texts[i])
}
}

func (m *LedBar) CreateRenderer() fyne.WidgetRenderer {
c := container.NewHBox(m.leds...)
return widget.NewSimpleRenderer(c)
}

func (m *LedBar) SetOnColor(c color.Color) {
for i := 0; i < len(m.leds); i++ {
m.leds[i].(*Led).OnColor = c
}
}

func (w *LedBar) Set(state int) {

var l int = len(w.texts) - 1
var b int = 1

for i := 0; i < len(w.texts); i++ {
if (state & b) == b {
w.leds[l-i].(*Led).Set(true)
} else {
w.leds[l-i].(*Led).Set(false)
}
b <<= 1
}

w.Refresh()
}

0 comments on commit 957486e

Please sign in to comment.