You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rgb-led-board/main.go

147 lines
3.9 KiB
Go

package main
import (
"flag"
"image"
"image/color"
_ "image/jpeg"
"log"
"os"
"time"
rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix"
"github.com/disintegration/imaging"
"github.com/fogleman/gg"
)
var (
rows = flag.Int("led-rows", 32, "number of rows supported")
cols = flag.Int("led-cols", 32, "number of columns supported")
parallel = flag.Int("led-parallel", 1, "number of daisy-chained panels")
chain = flag.Int("led-chain", 2, "number of displays daisy-chained")
brightness = flag.Int("brightness", 100, "brightness (0-100)")
hardware_mapping = flag.String("led-gpio-mapping", "regular", "Name of GPIO mapping used.")
show_refresh = flag.Bool("led-show-refresh", false, "Show refresh rate.")
inverse_colors = flag.Bool("led-inverse", false, "Switch if your matrix has inverse colors on.")
disable_hardware_pulsing = flag.Bool("led-no-hardware-pulse", false, "Don't use hardware pin-pulse generation.")
)
func main() {
config := &rgbmatrix.DefaultConfig
config.Rows = *rows
config.Cols = *cols
config.Parallel = *parallel
config.ChainLength = *chain
config.Brightness = *brightness
config.HardwareMapping = *hardware_mapping
config.ShowRefreshRate = *show_refresh
config.InverseColors = *inverse_colors
config.DisableHardwarePulsing = *disable_hardware_pulsing
/*bounds := mario.Bounds()
var histogram [16][4]int
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := mario.At(x, y).RGBA()
// A color's RGBA method returns values in the range [0, 65535].
// Shifting by 12 reduces this to the range [0, 15].
histogram[r>>12][0]++
histogram[g>>12][1]++
histogram[b>>12][2]++
histogram[a>>12][3]++
}
}*/
m, err := rgbmatrix.NewRGBLedMatrix(config)
fatal(err)
tk := rgbmatrix.NewToolKit(m)
defer tk.Close()
tk.PlayAnimation(NewAnimation(image.Point{64, 32}))
}
func init() {
flag.Parse()
}
func fatal(err error) {
if err != nil {
panic(err)
}
}
type Animation struct {
ctx *gg.Context
position image.Point
dir image.Point
height int
width int
stroke int
image []image.Image
updown int
}
func NewAnimation(sz image.Point) *Animation {
reader, err := os.Open("marioUp.png")
if err != nil {
log.Fatal(err)
}
rawMario, _, err := image.Decode(reader)
//marioUp := imaging.FlipH(imaging.Resize(rawMario, 16, 16, imaging.Lanczos))
marioUp := imaging.Resize(rawMario, 16, 16, imaging.Lanczos)
reader, err = os.Open("marioDown.png")
if err != nil {
log.Fatal(err)
}
rawMario, _, err = image.Decode(reader)
//marioDown := imaging.FlipH(imaging.Resize(rawMario, 16, 16, imaging.Lanczos))
marioDown := imaging.Resize(rawMario, 16, 16, imaging.Lanczos)
images := []image.Image{marioUp, marioDown}
return &Animation{
ctx: gg.NewContext(sz.X, sz.Y),
dir: image.Point{1, 1},
height: 8,
width: 8,
stroke: 8,
image: images,
updown: 0,
}
}
func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
defer a.updatePosition()
a.ctx.SetColor(color.Black)
a.ctx.Clear()
//a.ctx.SetColor(color.RGBA{0, 255, 0, 255})
//a.ctx.DrawCircle(float64(8), float64(8), float64(a.stroke))
//a.ctx.Fill()
if a.dir.X == 1 {
a.ctx.DrawImageAnchored(a.image[a.updown], a.position.X, a.position.Y, 0.5, 0.5)
} else {
a.ctx.DrawImageAnchored(imaging.FlipH(a.image[a.updown]), a.position.X, a.position.Y, 0.5, 0.5)
}
a.ctx.DrawString("mario", a.position.X, a.position.Y)
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
}
func (a *Animation) updatePosition() {
a.position.X += 1 * a.dir.X
a.position.Y += 1 * a.dir.Y
if a.position.Y+a.height > a.ctx.Height() {
a.dir.Y = -1
a.updown = 0
} else if a.position.Y-a.height < 0 {
a.updown = 1
a.dir.Y = 1
}
if a.position.X+a.width > a.ctx.Width() {
a.dir.X = -1
} else if a.position.X-a.width < 0 {
a.dir.X = 1
}
}