Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

led and ledbar widget to visualize binary data #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
Comment on lines +12 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use global variables please. Use an anonymous function in the main function instead or update the run function to take in pointer parameters for the things you have to modify.


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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the debug print.

return fyne.NewSize(h.led.Size().Width, h.led.Size().Height)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the current size should be the minimum but this is at least simpler than what is there currently:

Suggested change
return fyne.NewSize(h.led.Size().Width, h.led.Size().Height)
return h.led.Size()

}

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
}
Comment on lines +69 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if w.state {
return w.OnColor
} else {
return w.offColor
}
if w.state {
return w.OnColor
}
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
Comment on lines +131 to +133
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var l int = len(w.texts) - 1
var b int = 1
l := len(w.texts) - 1
b := 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)
}
Comment on lines +136 to +140
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pass in the boolean directly instead of branching.

Suggested change
if (state & b) == b {
w.leds[l-i].(*Led).Set(true)
} else {
w.leds[l-i].(*Led).Set(false)
}
w.leds[l-i].(*Led).Set(state & b == b)

b <<= 1
}

w.Refresh()
}