From 1343f23316f7f7fd429e8ced38e8463e8ef1aed0 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 31 Jan 2023 13:00:22 +0000 Subject: [PATCH 1/3] Attempt at adding color order support to Hub75 --- drivers/hub75/hub75.cpp | 41 +++++++++++++++++++++++++++-- drivers/hub75/hub75.hpp | 12 ++++++++- micropython/modules/hub75/hub75.c | 7 +++++ micropython/modules/hub75/hub75.cpp | 7 +++-- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/drivers/hub75/hub75.cpp b/drivers/hub75/hub75.cpp index 3114210a..84240b50 100644 --- a/drivers/hub75/hub75.cpp +++ b/drivers/hub75/hub75.cpp @@ -6,11 +6,48 @@ namespace pimoroni { +void Hub75::swap_pin(unsigned int &pin_a, unsigned int &pin_b) { + unsigned int swap; + swap = pin_a; + pin_a = pin_b; + pin_b = swap; +} - -Hub75::Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb) +Hub75::Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb, COLOR_ORDER color_order) : width(width), height(height), panel_type(panel_type), inverted_stb(inverted_stb) { + // If the colour order is not RGB, swap the colour pins. + switch(color_order) { + case COLOR_ORDER::RBG: + swap_pin(pin_g0, pin_b0); + swap_pin(pin_g1, pin_b1); + break; + case COLOR_ORDER::GRB: + swap_pin(pin_r0, pin_g0); + swap_pin(pin_r1, pin_g1); + break; + case COLOR_ORDER::GBR: + swap_pin(pin_r0, pin_g0); + swap_pin(pin_r1, pin_g1); + + swap_pin(pin_r0, pin_b0); + swap_pin(pin_r1, pin_b1); + break; + case COLOR_ORDER::BRG: + swap_pin(pin_r0, pin_b0); + swap_pin(pin_r1, pin_b1); + + swap_pin(pin_r0, pin_g0); + swap_pin(pin_r1, pin_g1); + break; + case COLOR_ORDER::BGR: + swap_pin(pin_r0, pin_b0); + swap_pin(pin_r1, pin_b1); + break; + default: + break; + } + // Set up allllll the GPIO gpio_init(pin_r0); gpio_set_function(pin_r0, GPIO_FUNC_SIO); gpio_set_dir(pin_r0, true); gpio_put(pin_r0, 0); gpio_init(pin_g0); gpio_set_function(pin_g0, GPIO_FUNC_SIO); gpio_set_dir(pin_g0, true); gpio_put(pin_g0, 0); diff --git a/drivers/hub75/hub75.hpp b/drivers/hub75/hub75.hpp index 15832ec3..d6bb457c 100644 --- a/drivers/hub75/hub75.hpp +++ b/drivers/hub75/hub75.hpp @@ -52,6 +52,14 @@ Pixel hsv_to_rgb(float h, float s, float v); class Hub75 { public: + enum class COLOR_ORDER { + RGB, + RBG, + GRB, + GBR, + BRG, + BGR + }; uint width; uint height; Pixel *back_buffer; @@ -112,7 +120,7 @@ class Hub75 { Hub75(uint width, uint height) : Hub75(width, height, nullptr) {}; Hub75(uint width, uint height, Pixel *buffer) : Hub75(width, height, buffer, PANEL_GENERIC) {}; Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type) : Hub75(width, height, buffer, panel_type, false) {}; - Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb); + Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb, COLOR_ORDER color_order=COLOR_ORDER::RGB); ~Hub75(); void FM6126A_write_register(uint16_t value, uint8_t position); @@ -126,5 +134,7 @@ class Hub75 { void stop(irq_handler_t handler); void dma_complete(); void update(PicoGraphics *graphics); + private: + void swap_pin(unsigned int &pin_a, unsigned int &pin_b); }; } \ No newline at end of file diff --git a/micropython/modules/hub75/hub75.c b/micropython/modules/hub75/hub75.c index b8993e89..855dc366 100644 --- a/micropython/modules/hub75/hub75.c +++ b/micropython/modules/hub75/hub75.c @@ -61,6 +61,13 @@ STATIC const mp_map_elem_t hub75_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_PIN_SDA), MP_ROM_INT(20) }, { MP_ROM_QSTR(MP_QSTR_PIN_SCL), MP_ROM_INT(21) }, { MP_ROM_QSTR(MP_QSTR_CURRENT_SENSE), MP_ROM_INT(29) }, + + { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) }, + { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RBG), MP_ROM_INT(0x01) }, + { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_GRB), MP_ROM_INT(0x02) }, + { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_GBR), MP_ROM_INT(0x03) }, + { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_BRG), MP_ROM_INT(0x04) }, + { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_BGR), MP_ROM_INT(0x05) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_hub75_globals, hub75_globals_table); diff --git a/micropython/modules/hub75/hub75.cpp b/micropython/modules/hub75/hub75.cpp index 5172dc82..8a8c7624 100644 --- a/micropython/modules/hub75/hub75.cpp +++ b/micropython/modules/hub75/hub75.cpp @@ -85,7 +85,8 @@ mp_obj_t Hub75_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c ARG_height, ARG_buffer, ARG_panel_type, - ARG_stb_invert + ARG_stb_invert, + ARG_color_order }; static const mp_arg_t allowed_args[] = { { MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT }, @@ -93,6 +94,7 @@ mp_obj_t Hub75_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c { MP_QSTR_buffer, MP_ARG_OBJ, {.u_obj = nullptr} }, { MP_QSTR_panel_type, MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_stb_invert, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_color_order, MP_ARG_INT, {.u_int = (uint8_t)Hub75::COLOR_ORDER::RGB} }, }; // Parse args. @@ -103,6 +105,7 @@ mp_obj_t Hub75_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c int height = args[ARG_height].u_int; PanelType paneltype = (PanelType)args[ARG_panel_type].u_int; bool stb_invert = args[ARG_stb_invert].u_int; + Hub75::COLOR_ORDER color_order = (Hub75::COLOR_ORDER)args[ARG_color_order].u_int; Pixel *buffer = nullptr; @@ -120,7 +123,7 @@ mp_obj_t Hub75_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c hub75_obj = m_new_obj_with_finaliser(_Hub75_obj_t); hub75_obj->base.type = &Hub75_type; hub75_obj->buf = buffer; - hub75_obj->hub75 = m_new_class(Hub75, width, height, buffer, paneltype, stb_invert); + hub75_obj->hub75 = m_new_class(Hub75, width, height, buffer, paneltype, stb_invert, color_order); return MP_OBJ_FROM_PTR(hub75_obj); } From eda6e996ceaca79ba85b0e4a6874faec9bea364f Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 31 Jan 2023 13:11:50 +0000 Subject: [PATCH 2/3] Updated interstate module --- micropython/modules_py/interstate75.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/micropython/modules_py/interstate75.py b/micropython/modules_py/interstate75.py index 94d13a12..24bcce79 100644 --- a/micropython/modules_py/interstate75.py +++ b/micropython/modules_py/interstate75.py @@ -32,10 +32,10 @@ class Interstate75: # Count Constants NUM_SWITCHES = 2 - def __init__(self, display, panel_type=hub75.PANEL_GENERIC, stb_invert=False): + def __init__(self, display, panel_type=hub75.PANEL_GENERIC, stb_invert=False, color_order=hub75.COLOR_ORDER_RGB): self.display = PicoGraphics(display=display) self.width, self.height = self.display.get_bounds() - self.hub75 = hub75.Hub75(self.width, self.height, panel_type=panel_type, stb_invert=stb_invert) + self.hub75 = hub75.Hub75(self.width, self.height, panel_type=panel_type, stb_invert=stb_invert, color_order=color_order) self.hub75.start() # Set up the user switches From cacfbd174b6514f520b2aa1890e63356d7fec9c8 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 31 Jan 2023 14:07:14 +0000 Subject: [PATCH 3/3] Previous idea did not work, so now checking color order for every pixel set --- drivers/hub75/hub75.cpp | 62 +++++++++----------------- drivers/hub75/hub75.hpp | 3 +- micropython/modules/hub75/README.md | 19 +++++++- micropython/modules_py/interstate75.py | 6 +++ 4 files changed, 46 insertions(+), 44 deletions(-) diff --git a/drivers/hub75/hub75.cpp b/drivers/hub75/hub75.cpp index 84240b50..1d30c75a 100644 --- a/drivers/hub75/hub75.cpp +++ b/drivers/hub75/hub75.cpp @@ -6,48 +6,9 @@ namespace pimoroni { -void Hub75::swap_pin(unsigned int &pin_a, unsigned int &pin_b) { - unsigned int swap; - swap = pin_a; - pin_a = pin_b; - pin_b = swap; -} - Hub75::Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb, COLOR_ORDER color_order) - : width(width), height(height), panel_type(panel_type), inverted_stb(inverted_stb) + : width(width), height(height), panel_type(panel_type), inverted_stb(inverted_stb), color_order(color_order) { - // If the colour order is not RGB, swap the colour pins. - switch(color_order) { - case COLOR_ORDER::RBG: - swap_pin(pin_g0, pin_b0); - swap_pin(pin_g1, pin_b1); - break; - case COLOR_ORDER::GRB: - swap_pin(pin_r0, pin_g0); - swap_pin(pin_r1, pin_g1); - break; - case COLOR_ORDER::GBR: - swap_pin(pin_r0, pin_g0); - swap_pin(pin_r1, pin_g1); - - swap_pin(pin_r0, pin_b0); - swap_pin(pin_r1, pin_b1); - break; - case COLOR_ORDER::BRG: - swap_pin(pin_r0, pin_b0); - swap_pin(pin_r1, pin_b1); - - swap_pin(pin_r0, pin_g0); - swap_pin(pin_r1, pin_g1); - break; - case COLOR_ORDER::BGR: - swap_pin(pin_r0, pin_b0); - swap_pin(pin_r1, pin_b1); - break; - default: - break; - } - // Set up allllll the GPIO gpio_init(pin_r0); gpio_set_function(pin_r0, GPIO_FUNC_SIO); gpio_set_dir(pin_r0, true); gpio_put(pin_r0, 0); gpio_init(pin_g0); gpio_set_function(pin_g0, GPIO_FUNC_SIO); gpio_set_dir(pin_g0, true); gpio_put(pin_g0, 0); @@ -97,7 +58,26 @@ void Hub75::set_color(uint x, uint y, Pixel c) { } void Hub75::set_pixel(uint x, uint y, uint8_t r, uint8_t g, uint8_t b) { - set_color(x, y, Pixel(r, g, b)); + switch(color_order) { + case COLOR_ORDER::RGB: + set_color(x, y, Pixel(r, g, b)); + break; + case COLOR_ORDER::RBG: + set_color(x, y, Pixel(r, b, g)); + break; + case COLOR_ORDER::GRB: + set_color(x, y, Pixel(g, r, b)); + break; + case COLOR_ORDER::GBR: + set_color(x, y, Pixel(g, b, r)); + break; + case COLOR_ORDER::BRG: + set_color(x, y, Pixel(b, r, g)); + break; + case COLOR_ORDER::BGR: + set_color(x, y, Pixel(b, g, r)); + break; + } } void Hub75::FM6126A_write_register(uint16_t value, uint8_t position) { diff --git a/drivers/hub75/hub75.hpp b/drivers/hub75/hub75.hpp index d6bb457c..412b76b0 100644 --- a/drivers/hub75/hub75.hpp +++ b/drivers/hub75/hub75.hpp @@ -66,6 +66,7 @@ class Hub75 { bool managed_buffer = false; PanelType panel_type; bool inverted_stb = false; + COLOR_ORDER color_order; Pixel background = 0; // DMA & PIO @@ -134,7 +135,5 @@ class Hub75 { void stop(irq_handler_t handler); void dma_complete(); void update(PicoGraphics *graphics); - private: - void swap_pin(unsigned int &pin_a, unsigned int &pin_b); }; } \ No newline at end of file diff --git a/micropython/modules/hub75/README.md b/micropython/modules/hub75/README.md index 27bd1a35..fafb31c9 100644 --- a/micropython/modules/hub75/README.md +++ b/micropython/modules/hub75/README.md @@ -11,6 +11,7 @@ It can, in theory, be used with your own custom wiring, though custom pin assign - [Notes On PIO & DMA Limitations](#notes-on-pio--dma-limitations) - [Getting Started](#getting-started) - [FM6216A Panels](#fm6216a-panels) + - [Setting Colour Order](#setting-colour-order) - [Quick Reference](#quick-reference) - [Set A Pixel](#set-a-pixel) - [Clear The Display](#clear-the-display) @@ -52,9 +53,25 @@ import hub75 WIDTH = 64 HEIGHT = 64 -matrix = hub75.Hub75(WIDTH, HEIGHT,panel_type=hub75.PANEL_FM6126A) +matrix = hub75.Hub75(WIDTH, HEIGHT, panel_type=hub75.PANEL_FM6126A) ``` +### Setting Colour Order + +Some hub 75 panels have varying colour orders. A keyword argument is supplied to configure this: + +```python +matrix = hub75.Hub75(WIDTH, HEIGHT, panel_type=hub75.COLOR_ORDER_RBG) +``` + +The available orders are defined as constants in `hub75`: + +* `COLOR_ORDER_RGB` +* `COLOR_ORDER_RBG` +* `COLOR_ORDER_GRB` +* `COLOR_ORDER_GBR` +* `COLOR_ORDER_BRG` +* `COLOR_ORDER_BGR` ## Quick Reference diff --git a/micropython/modules_py/interstate75.py b/micropython/modules_py/interstate75.py index 24bcce79..e261b4fb 100644 --- a/micropython/modules_py/interstate75.py +++ b/micropython/modules_py/interstate75.py @@ -28,6 +28,12 @@ class Interstate75: PANEL_GENERIC = hub75.PANEL_GENERIC PANEL_FM6126A = hub75.PANEL_FM6126A + COLOR_ORDER_RGB = hub75.COLOR_ORDER_RGB + COLOR_ORDER_RBG = hub75.COLOR_ORDER_RBG + COLOR_ORDER_GRB = hub75.COLOR_ORDER_GRB + COLOR_ORDER_GBR = hub75.COLOR_ORDER_GBR + COLOR_ORDER_BRG = hub75.COLOR_ORDER_BRG + COLOR_ORDER_BGR = hub75.COLOR_ORDER_BGR # Count Constants NUM_SWITCHES = 2