From 3b69399be226a170db20991dc95e3c950612b711 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Wed, 15 Aug 2018 18:23:06 -0500 Subject: [PATCH 01/19] Added GPIO Slowdown --- matrix.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/matrix.go b/matrix.go index 8b9ddf1..9ecc432 100644 --- a/matrix.go +++ b/matrix.go @@ -95,6 +95,9 @@ type HardwareConfig struct { ShowRefreshRate bool InverseColors bool + // Control speed of GPIO updates. Valid range is 0..3 + GPIOSlowdown int + // Name of GPIO mapping used HardwareMapping string } @@ -114,6 +117,7 @@ func (c *HardwareConfig) toC() *C.struct_RGBLedMatrixOptions { o.brightness = C.int(c.Brightness) o.scan_mode = C.int(c.ScanMode) o.hardware_mapping = C.CString(c.HardwareMapping) + o.gpio_slowdown = C.int(c.GPIOSlowdown) if c.ShowRefreshRate == true { C.set_show_refresh_rate(o, C.int(1)) From 1338f761458c00775275c9fd889d2f950c827270 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Wed, 15 Aug 2018 18:23:45 -0500 Subject: [PATCH 02/19] Added GPIO Slowdown default --- matrix.go | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix.go b/matrix.go index 9ecc432..ab0045a 100644 --- a/matrix.go +++ b/matrix.go @@ -56,6 +56,7 @@ var DefaultConfig = HardwareConfig{ PWMLSBNanoseconds: 130, Brightness: 100, ScanMode: Progressive, + GPIOSlowdown: 1, } // HardwareConfig rgb-led-matrix configuration From 1563b8531e40f6675f5be06edbe3821f04e1b7de Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Wed, 15 Aug 2018 18:42:16 -0500 Subject: [PATCH 03/19] fixing gpio slowdown --- matrix.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/matrix.go b/matrix.go index ab0045a..439a34e 100644 --- a/matrix.go +++ b/matrix.go @@ -118,7 +118,10 @@ func (c *HardwareConfig) toC() *C.struct_RGBLedMatrixOptions { o.brightness = C.int(c.Brightness) o.scan_mode = C.int(c.ScanMode) o.hardware_mapping = C.CString(c.HardwareMapping) - o.gpio_slowdown = C.int(c.GPIOSlowdown) + + if c.GPIOSlowdown >= 0 && c.GPIOSlowdown < 3 { + C.gpio_slowdown(gpio_slowdown) + } if c.ShowRefreshRate == true { C.set_show_refresh_rate(o, C.int(1)) From e46ef1c378a30f21530558c10c317642d3425948 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Wed, 15 Aug 2018 18:44:43 -0500 Subject: [PATCH 04/19] fixing gpio slowdown --- matrix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix.go b/matrix.go index 439a34e..1a0f089 100644 --- a/matrix.go +++ b/matrix.go @@ -120,7 +120,7 @@ func (c *HardwareConfig) toC() *C.struct_RGBLedMatrixOptions { o.hardware_mapping = C.CString(c.HardwareMapping) if c.GPIOSlowdown >= 0 && c.GPIOSlowdown < 3 { - C.gpio_slowdown(gpio_slowdown) + C.gpio_slowdown(c.GPIOSlowdown) } if c.ShowRefreshRate == true { From 59ef5ae4add6eaa8b32e4679e91446bbf338622c Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 11:35:22 -0500 Subject: [PATCH 05/19] trying to fix gpio slowdown --- matrix.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/matrix.go b/matrix.go index 1a0f089..1d14044 100644 --- a/matrix.go +++ b/matrix.go @@ -56,7 +56,11 @@ var DefaultConfig = HardwareConfig{ PWMLSBNanoseconds: 130, Brightness: 100, ScanMode: Progressive, - GPIOSlowdown: 1, +} + +type RuntimeOptions struct { + // Control speed of GPIO updates. Valid range is 0..3 + GPIOSlowdown int } // HardwareConfig rgb-led-matrix configuration @@ -96,9 +100,6 @@ type HardwareConfig struct { ShowRefreshRate bool InverseColors bool - // Control speed of GPIO updates. Valid range is 0..3 - GPIOSlowdown int - // Name of GPIO mapping used HardwareMapping string } @@ -107,6 +108,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) @@ -119,10 +131,6 @@ func (c *HardwareConfig) toC() *C.struct_RGBLedMatrixOptions { o.scan_mode = C.int(c.ScanMode) o.hardware_mapping = C.CString(c.HardwareMapping) - if c.GPIOSlowdown >= 0 && c.GPIOSlowdown < 3 { - C.gpio_slowdown(c.GPIOSlowdown) - } - if c.ShowRefreshRate == true { C.set_show_refresh_rate(o, C.int(1)) } else { @@ -197,6 +205,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 From 6c4e89d4225915865b9e8fa55d5ff24b64d51241 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 12:24:10 -0500 Subject: [PATCH 06/19] trying to fix gpio slowdown --- matrix.go | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix.go b/matrix.go index 8b9ddf1..5447de6 100644 --- a/matrix.go +++ b/matrix.go @@ -4,6 +4,7 @@ package rgbmatrix #cgo CFLAGS: -std=c99 -I${SRCDIR}/vendor/rpi-rgb-led-matrix/include -DSHOW_REFRESH_RATE #cgo LDFLAGS: -lrgbmatrix -L${SRCDIR}/vendor/rpi-rgb-led-matrix/lib -lstdc++ -lm #include +#include void led_matrix_swap(struct RGBLedMatrix *matrix, struct LedCanvas *offscreen_canvas, int width, int height, const uint32_t pixels[]) { From 3780e4ab4146300f4b7b04b6ad51b92d3b5f7e4f Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 12:29:52 -0500 Subject: [PATCH 07/19] trying to fix gpio slowdown --- matrix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix.go b/matrix.go index 26daaac..dfa181a 100644 --- a/matrix.go +++ b/matrix.go @@ -4,7 +4,7 @@ package rgbmatrix #cgo CFLAGS: -std=c99 -I${SRCDIR}/vendor/rpi-rgb-led-matrix/include -DSHOW_REFRESH_RATE #cgo LDFLAGS: -lrgbmatrix -L${SRCDIR}/vendor/rpi-rgb-led-matrix/lib -lstdc++ -lm #include -#include +#include "led-matrix.h" void led_matrix_swap(struct RGBLedMatrix *matrix, struct LedCanvas *offscreen_canvas, int width, int height, const uint32_t pixels[]) { From bccf120d9343529286896e3823f4554180d206a6 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:21:43 -0500 Subject: [PATCH 08/19] trying to fix gpio_slowdown --- matrix.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/matrix.go b/matrix.go index dfa181a..512de9d 100644 --- a/matrix.go +++ b/matrix.go @@ -4,7 +4,6 @@ package rgbmatrix #cgo CFLAGS: -std=c99 -I${SRCDIR}/vendor/rpi-rgb-led-matrix/include -DSHOW_REFRESH_RATE #cgo LDFLAGS: -lrgbmatrix -L${SRCDIR}/vendor/rpi-rgb-led-matrix/lib -lstdc++ -lm #include -#include "led-matrix.h" void led_matrix_swap(struct RGBLedMatrix *matrix, struct LedCanvas *offscreen_canvas, int width, int height, const uint32_t pixels[]) { @@ -173,8 +172,18 @@ type RGBLedMatrix struct { const MatrixEmulatorENV = "MATRIX_EMULATOR" +func stringsToC(s []string) **C.char { + cArray := C.malloc(C.size_t(len(s)) * C.size_t(unsafe.Sizeof(uintptr(0)))) + + for idx, substring := range s { + a[idx] = C.CString(substring) + } + + return (**C.char)(cArray) +} + // NewRGBLedMatrix returns a new matrix using the given size and config -func NewRGBLedMatrix(config *HardwareConfig) (c Matrix, err error) { +func NewRGBLedMatrix(config *HardwareConfig, argc *int, argv *[]string) (c Matrix, err error) { defer func() { if r := recover(); r != nil { var ok bool @@ -190,7 +199,7 @@ func NewRGBLedMatrix(config *HardwareConfig) (c Matrix, err error) { } w, h := config.geometry() - m := C.led_matrix_create_from_options(config.toC(), nil, nil) + m := C.led_matrix_create_from_options(config.toC(), argc, &stringsToC(*argv)) b := C.led_matrix_create_offscreen_canvas(m) c = &RGBLedMatrix{ Config: config, From f93d9c14ecd05aa5b4e6465b20629149240d23e7 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:22:51 -0500 Subject: [PATCH 09/19] trying to fix gpio_slowdown --- matrix.go | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/matrix.go b/matrix.go index 512de9d..82961fc 100644 --- a/matrix.go +++ b/matrix.go @@ -215,39 +215,6 @@ func NewRGBLedMatrix(config *HardwareConfig, argc *int, argv *[]string) (c Matri 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 From 333bbaf8670e6b3d2f226ed3b3a8bff619ffe5e0 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:29:20 -0500 Subject: [PATCH 10/19] trying to fix gpio_slowdown --- matrix.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/matrix.go b/matrix.go index 82961fc..ea9f635 100644 --- a/matrix.go +++ b/matrix.go @@ -58,11 +58,6 @@ 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. @@ -175,6 +170,8 @@ const MatrixEmulatorENV = "MATRIX_EMULATOR" func stringsToC(s []string) **C.char { cArray := C.malloc(C.size_t(len(s)) * C.size_t(unsafe.Sizeof(uintptr(0)))) + a := (*[len(s) - 1]*C.char)(cArray) + for idx, substring := range s { a[idx] = C.CString(substring) } From c33ed9ec1774c55ab97825df38de9357fead9bc7 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:30:29 -0500 Subject: [PATCH 11/19] trying to fix gpio_slowdown --- matrix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix.go b/matrix.go index ea9f635..1087642 100644 --- a/matrix.go +++ b/matrix.go @@ -196,7 +196,7 @@ func NewRGBLedMatrix(config *HardwareConfig, argc *int, argv *[]string) (c Matri } w, h := config.geometry() - m := C.led_matrix_create_from_options(config.toC(), argc, &stringsToC(*argv)) + m := C.led_matrix_create_from_options(config.toC(), &C.int(*argc), &stringsToC(*argv)) b := C.led_matrix_create_offscreen_canvas(m) c = &RGBLedMatrix{ Config: config, From 6e70d484fbc4dd1145fdf498aa868f4b60a00527 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:31:55 -0500 Subject: [PATCH 12/19] trying to fix gpio_slowdown --- matrix.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/matrix.go b/matrix.go index 1087642..f3f9fce 100644 --- a/matrix.go +++ b/matrix.go @@ -196,7 +196,9 @@ func NewRGBLedMatrix(config *HardwareConfig, argc *int, argv *[]string) (c Matri } w, h := config.geometry() - m := C.led_matrix_create_from_options(config.toC(), &C.int(*argc), &stringsToC(*argv)) + cargc := C.int(*argc) + cargv := stringsToC(*argv) + m := C.led_matrix_create_from_options(config.toC(), &cargc, &cargv) b := C.led_matrix_create_offscreen_canvas(m) c = &RGBLedMatrix{ Config: config, From 3c1c20ef3612ad2bde1bcf9ccc3e9bd33f980056 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:33:04 -0500 Subject: [PATCH 13/19] trying to fix gpio_slowdown --- matrix.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/matrix.go b/matrix.go index f3f9fce..2d3a3fd 100644 --- a/matrix.go +++ b/matrix.go @@ -103,17 +103,6 @@ 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) From 39fb76bcc6694e7d9c0267b4067c3c0e434c9598 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:34:43 -0500 Subject: [PATCH 14/19] trying to fix gpio_slowdown --- matrix.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matrix.go b/matrix.go index 2d3a3fd..2d80aeb 100644 --- a/matrix.go +++ b/matrix.go @@ -159,7 +159,8 @@ const MatrixEmulatorENV = "MATRIX_EMULATOR" func stringsToC(s []string) **C.char { cArray := C.malloc(C.size_t(len(s)) * C.size_t(unsafe.Sizeof(uintptr(0)))) - a := (*[len(s) - 1]*C.char)(cArray) + arglen := len(s) - 1 + a := (*[arglen]*C.char)(cArray) for idx, substring := range s { a[idx] = C.CString(substring) From 417f033a0ec2e256786407d71fa0a12876650ac1 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:36:08 -0500 Subject: [PATCH 15/19] trying to fix gpio_slowdown --- matrix.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/matrix.go b/matrix.go index 2d80aeb..67468e6 100644 --- a/matrix.go +++ b/matrix.go @@ -159,8 +159,7 @@ const MatrixEmulatorENV = "MATRIX_EMULATOR" func stringsToC(s []string) **C.char { cArray := C.malloc(C.size_t(len(s)) * C.size_t(unsafe.Sizeof(uintptr(0)))) - arglen := len(s) - 1 - a := (*[arglen]*C.char)(cArray) + a := (*[1<<30 - 1]*C.char)(cArray) for idx, substring := range s { a[idx] = C.CString(substring) From c1ae882c7c81fe3e542fd7fd2fcc8f3f322ed943 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:37:21 -0500 Subject: [PATCH 16/19] trying to fix gpio_slowdown --- matrix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix.go b/matrix.go index 67468e6..368ed5f 100644 --- a/matrix.go +++ b/matrix.go @@ -159,7 +159,7 @@ const MatrixEmulatorENV = "MATRIX_EMULATOR" func stringsToC(s []string) **C.char { cArray := C.malloc(C.size_t(len(s)) * C.size_t(unsafe.Sizeof(uintptr(0)))) - a := (*[1<<30 - 1]*C.char)(cArray) + a := (*[2000]*C.char)(cArray) for idx, substring := range s { a[idx] = C.CString(substring) From 0174d3dae6f741837573c95068a1819d3057e3af Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 13:54:43 -0500 Subject: [PATCH 17/19] trying to fix gpio slowdown --- matrix.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/matrix.go b/matrix.go index 368ed5f..26e2112 100644 --- a/matrix.go +++ b/matrix.go @@ -187,6 +187,8 @@ func NewRGBLedMatrix(config *HardwareConfig, argc *int, argv *[]string) (c Matri w, h := config.geometry() cargc := C.int(*argc) cargv := stringsToC(*argv) + fmt.Printf("ArgV: %#v", *argv) + fmt.Printf("CArgV: %#v", cargv) m := C.led_matrix_create_from_options(config.toC(), &cargc, &cargv) b := C.led_matrix_create_offscreen_canvas(m) c = &RGBLedMatrix{ From 8cacfbb810ab660005973c6687561bce3ee50124 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 14:17:46 -0500 Subject: [PATCH 18/19] trying to fix gpio slowdown --- matrix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix.go b/matrix.go index 26e2112..f602b15 100644 --- a/matrix.go +++ b/matrix.go @@ -188,7 +188,7 @@ func NewRGBLedMatrix(config *HardwareConfig, argc *int, argv *[]string) (c Matri cargc := C.int(*argc) cargv := stringsToC(*argv) fmt.Printf("ArgV: %#v", *argv) - fmt.Printf("CArgV: %#v", cargv) + fmt.Printf("CArgV: %#v", **cargv) m := C.led_matrix_create_from_options(config.toC(), &cargc, &cargv) b := C.led_matrix_create_offscreen_canvas(m) c = &RGBLedMatrix{ From 5ac43e9b46b740dbb51a6e0bcfc77ea66ca82210 Mon Sep 17 00:00:00 2001 From: Brendan Porter Date: Fri, 17 Aug 2018 15:10:48 -0500 Subject: [PATCH 19/19] GPIO slowdown fix in place, cleaning up changes --- matrix.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/matrix.go b/matrix.go index f602b15..5ed9abb 100644 --- a/matrix.go +++ b/matrix.go @@ -169,7 +169,7 @@ func stringsToC(s []string) **C.char { } // NewRGBLedMatrix returns a new matrix using the given size and config -func NewRGBLedMatrix(config *HardwareConfig, argc *int, argv *[]string) (c Matrix, err error) { +func NewRGBLedMatrix(config *HardwareConfig) (c Matrix, err error) { defer func() { if r := recover(); r != nil { var ok bool @@ -185,10 +185,8 @@ func NewRGBLedMatrix(config *HardwareConfig, argc *int, argv *[]string) (c Matri } w, h := config.geometry() - cargc := C.int(*argc) - cargv := stringsToC(*argv) - fmt.Printf("ArgV: %#v", *argv) - fmt.Printf("CArgV: %#v", **cargv) + cargc := C.int(len(os.Args)) + cargv := stringsToC(os.Args) m := C.led_matrix_create_from_options(config.toC(), &cargc, &cargv) b := C.led_matrix_create_offscreen_canvas(m) c = &RGBLedMatrix{