emulator: improvements and integration

pull/3/head
Máximo Cuadros 8 years ago
parent 40b62517bc
commit 741ec8a743

@ -81,6 +81,15 @@ The image of the header was recorded using this few lines, the running _Mario_ g
Check the folder [`examples`](https://github.com/mcuadros/go-rpi-rgb-led-matrix/tree/master/examples) folder for more examples
Matrix Emulation
----------------
As part of the library an small Matrix emulator is provided. The emulator renderize a virtual RGB matrix on a window in your desktop, without needing a real RGB matrix connected to your computer.
To execute the emulator set the `MATRIX_EMULATOR` environment variable to `1`, then when `NewRGBLedMatrix` is used, a `emulator.Emulator` is returned instead of a interface the real board.
License
-------

@ -3,10 +3,12 @@ package emulator
import (
"image"
"image/color"
"log"
"os"
"sync"
"fmt"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/imageutil"
"golang.org/x/exp/shiny/screen"
@ -14,10 +16,8 @@ import (
"golang.org/x/mobile/event/size"
)
var (
black = color.RGBA{0x00, 0x00, 0x00, 0xff}
red = color.RGBA{0x7f, 0x00, 0x00, 0x7f}
)
const DefaultPixelPitch = 12
const windowTitle = "RGB led matrix emulator"
var margin = 10
@ -29,24 +29,42 @@ type Emulator struct {
leds []color.Color
w screen.Window
s screen.Screen
wg sync.WaitGroup
isReady bool
}
wg sync.WaitGroup
func NewEmulator(w, h, pixelPitch int, autoInit bool) *Emulator {
e := &Emulator{
PixelPitch: pixelPitch,
Gutter: int(float64(pixelPitch) * 0.66),
Width: w,
Height: h,
}
if autoInit {
e.Init()
}
return e
}
// Init initialize the emulator, creating a new Window and waiting until is
// painted. If something goes wrong the function panics
func (e *Emulator) Init() {
e.leds = make([]color.Color, 2048)
e.wg.Add(1)
go e.init()
go driver.Main(e.mainWindowLoop)
e.wg.Wait()
}
func (e *Emulator) init() {
driver.Main(func(s screen.Screen) {
func (e *Emulator) mainWindowLoop(s screen.Screen) {
var err error
e.s = s
e.w, err = s.NewWindow(&screen.NewWindowOptions{
Title: "Basic Shiny Example",
Title: windowTitle,
})
if err != nil {
@ -60,12 +78,7 @@ func (e *Emulator) init() {
evn := e.w.NextEvent()
switch evn := evn.(type) {
case paint.Event:
for _, r := range imageutil.Border(sz.Bounds(), margin) {
e.w.Fill(r, red, screen.Src)
}
e.w.Fill(sz.Bounds().Inset(margin), black, screen.Src)
e.w.Publish()
e.drawContext(sz)
if e.isReady {
continue
}
@ -77,10 +90,18 @@ func (e *Emulator) init() {
sz = evn
case error:
log.Print(e)
fmt.Fprintln(os.Stderr, e)
}
}
})
}
func (e *Emulator) drawContext(sz size.Event) {
for _, r := range imageutil.Border(sz.Bounds(), margin) {
e.w.Fill(r, color.White, screen.Src)
}
e.w.Fill(sz.Bounds().Inset(margin), color.Black, screen.Src)
e.w.Publish()
}
func (e *Emulator) Geometry() (width, height int) {
@ -98,8 +119,10 @@ func (e *Emulator) Apply(leds []color.Color) error {
x += margin * 2
y += margin * 2
c := e.At(col + (row * e.Width))
e.w.Fill(image.Rect(x, y, x+e.PixelPitch, y+e.PixelPitch), c, screen.Over)
color := e.At(col + (row * e.Width))
led := image.Rect(x, y, x+e.PixelPitch, y+e.PixelPitch)
e.w.Fill(led, color, screen.Over)
}
}

@ -29,7 +29,10 @@ import "C"
import (
"fmt"
"image/color"
"os"
"unsafe"
"github.com/mcuadros/go-rpi-rgb-led-matrix/emulator"
)
// DefaultConfig default WS281x configuration
@ -116,8 +119,14 @@ type RGBLedMatrix struct {
leds []C.uint32_t
}
const MatrixEmulatorENV = "MATRIX_EMULATOR"
// NewRGBLedMatrix returns a new matrix using the given size and config
func NewRGBLedMatrix(config *HardwareConfig) (*RGBLedMatrix, error) {
func NewRGBLedMatrix(config *HardwareConfig) (Matrix, error) {
if isMatrixEmulator() {
return buildMatrixEmulator(config), nil
}
w, h := config.geometry()
c := &RGBLedMatrix{
@ -134,6 +143,19 @@ func NewRGBLedMatrix(config *HardwareConfig) (*RGBLedMatrix, error) {
return c, nil
}
func isMatrixEmulator() bool {
if os.Getenv(MatrixEmulatorENV) == "1" {
return true
}
return false
}
func buildMatrixEmulator(config *HardwareConfig) Matrix {
w, h := config.geometry()
return emulator.NewEmulator(w, h, emulator.DefaultPixelPitch, true)
}
// Initialize initialize library, must be called once before other functions are
// called.
func (c *RGBLedMatrix) Initialize() error {

Loading…
Cancel
Save