diff --git a/matrix.go b/matrix.go index 5447de6..26daaac 100644 --- a/matrix.go +++ b/matrix.go @@ -59,6 +59,11 @@ var DefaultConfig = HardwareConfig{ ScanMode: Progressive, } +type RuntimeOptions struct { + // Control speed of GPIO updates. Valid range is 0..3 + GPIOSlowdown int +} + // HardwareConfig rgb-led-matrix configuration type HardwareConfig struct { // Rows the number of rows supported by the display, so 32 or 16. @@ -104,6 +109,17 @@ func (c *HardwareConfig) geometry() (width, height int) { return c.Cols * c.ChainLength, c.Rows * c.Parallel } +func (r *RuntimeOptions) toC() *C.struct_RuntimeOptions { + o := &C.struct_RuntimeOptions{} + o.gpio_slowdown = C.int(r.GPIOSlowdown) + + if c.GPIOSlowdown > 0 || c.GPIOSlowdown > 4 { + o.gpio_slowdown = 1 + } + + return o +} + func (c *HardwareConfig) toC() *C.struct_RGBLedMatrixOptions { o := &C.struct_RGBLedMatrixOptions{} o.rows = C.int(c.Rows) @@ -190,6 +206,39 @@ func NewRGBLedMatrix(config *HardwareConfig) (c Matrix, err error) { return c, nil } +// NewRGBLedMatrix returns a new matrix using the given size and config +func NewRGBLedMatrixWithOptions(config *HardwareConfig, run *RuntimeOptions) (c Matrix, err error) { + defer func() { + if r := recover(); r != nil { + var ok bool + err, ok = r.(error) + if !ok { + err = fmt.Errorf("error creating matrix: %v", r) + } + } + }() + + if isMatrixEmulator() { + return buildMatrixEmulator(config), nil + } + + w, h := config.geometry() + m := C.from_matrix(C.CreateMatrixFromOptions(config.toC(), run.toC())) + b := C.led_matrix_create_offscreen_canvas(m) + c = &RGBLedMatrix{ + Config: config, + width: w, height: h, + matrix: m, + buffer: b, + leds: make([]C.uint32_t, w*h), + } + if m == nil { + return nil, fmt.Errorf("unable to allocate memory") + } + + return c, nil +} + func isMatrixEmulator() bool { if os.Getenv(MatrixEmulatorENV) == "1" { return true