Skip to content

Commit 1343f23

Browse files
committed
Attempt at adding color order support to Hub75
1 parent 884722d commit 1343f23

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

drivers/hub75/hub75.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,48 @@
66

77
namespace pimoroni {
88

9+
void Hub75::swap_pin(unsigned int &pin_a, unsigned int &pin_b) {
10+
unsigned int swap;
11+
swap = pin_a;
12+
pin_a = pin_b;
13+
pin_b = swap;
14+
}
915

10-
11-
Hub75::Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb)
16+
Hub75::Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb, COLOR_ORDER color_order)
1217
: width(width), height(height), panel_type(panel_type), inverted_stb(inverted_stb)
1318
{
19+
// If the colour order is not RGB, swap the colour pins.
20+
switch(color_order) {
21+
case COLOR_ORDER::RBG:
22+
swap_pin(pin_g0, pin_b0);
23+
swap_pin(pin_g1, pin_b1);
24+
break;
25+
case COLOR_ORDER::GRB:
26+
swap_pin(pin_r0, pin_g0);
27+
swap_pin(pin_r1, pin_g1);
28+
break;
29+
case COLOR_ORDER::GBR:
30+
swap_pin(pin_r0, pin_g0);
31+
swap_pin(pin_r1, pin_g1);
32+
33+
swap_pin(pin_r0, pin_b0);
34+
swap_pin(pin_r1, pin_b1);
35+
break;
36+
case COLOR_ORDER::BRG:
37+
swap_pin(pin_r0, pin_b0);
38+
swap_pin(pin_r1, pin_b1);
39+
40+
swap_pin(pin_r0, pin_g0);
41+
swap_pin(pin_r1, pin_g1);
42+
break;
43+
case COLOR_ORDER::BGR:
44+
swap_pin(pin_r0, pin_b0);
45+
swap_pin(pin_r1, pin_b1);
46+
break;
47+
default:
48+
break;
49+
}
50+
1451
// Set up allllll the GPIO
1552
gpio_init(pin_r0); gpio_set_function(pin_r0, GPIO_FUNC_SIO); gpio_set_dir(pin_r0, true); gpio_put(pin_r0, 0);
1653
gpio_init(pin_g0); gpio_set_function(pin_g0, GPIO_FUNC_SIO); gpio_set_dir(pin_g0, true); gpio_put(pin_g0, 0);

drivers/hub75/hub75.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ Pixel hsv_to_rgb(float h, float s, float v);
5252

5353
class Hub75 {
5454
public:
55+
enum class COLOR_ORDER {
56+
RGB,
57+
RBG,
58+
GRB,
59+
GBR,
60+
BRG,
61+
BGR
62+
};
5563
uint width;
5664
uint height;
5765
Pixel *back_buffer;
@@ -112,7 +120,7 @@ class Hub75 {
112120
Hub75(uint width, uint height) : Hub75(width, height, nullptr) {};
113121
Hub75(uint width, uint height, Pixel *buffer) : Hub75(width, height, buffer, PANEL_GENERIC) {};
114122
Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type) : Hub75(width, height, buffer, panel_type, false) {};
115-
Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb);
123+
Hub75(uint width, uint height, Pixel *buffer, PanelType panel_type, bool inverted_stb, COLOR_ORDER color_order=COLOR_ORDER::RGB);
116124
~Hub75();
117125

118126
void FM6126A_write_register(uint16_t value, uint8_t position);
@@ -126,5 +134,7 @@ class Hub75 {
126134
void stop(irq_handler_t handler);
127135
void dma_complete();
128136
void update(PicoGraphics *graphics);
137+
private:
138+
void swap_pin(unsigned int &pin_a, unsigned int &pin_b);
129139
};
130140
}

micropython/modules/hub75/hub75.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ STATIC const mp_map_elem_t hub75_globals_table[] = {
6161
{ MP_ROM_QSTR(MP_QSTR_PIN_SDA), MP_ROM_INT(20) },
6262
{ MP_ROM_QSTR(MP_QSTR_PIN_SCL), MP_ROM_INT(21) },
6363
{ MP_ROM_QSTR(MP_QSTR_CURRENT_SENSE), MP_ROM_INT(29) },
64+
65+
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) },
66+
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RBG), MP_ROM_INT(0x01) },
67+
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_GRB), MP_ROM_INT(0x02) },
68+
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_GBR), MP_ROM_INT(0x03) },
69+
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_BRG), MP_ROM_INT(0x04) },
70+
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_BGR), MP_ROM_INT(0x05) },
6471
};
6572
STATIC MP_DEFINE_CONST_DICT(mp_module_hub75_globals, hub75_globals_table);
6673

micropython/modules/hub75/hub75.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@ mp_obj_t Hub75_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c
8585
ARG_height,
8686
ARG_buffer,
8787
ARG_panel_type,
88-
ARG_stb_invert
88+
ARG_stb_invert,
89+
ARG_color_order
8990
};
9091
static const mp_arg_t allowed_args[] = {
9192
{ MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT },
9293
{ MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT },
9394
{ MP_QSTR_buffer, MP_ARG_OBJ, {.u_obj = nullptr} },
9495
{ MP_QSTR_panel_type, MP_ARG_INT, {.u_int = 0} },
9596
{ MP_QSTR_stb_invert, MP_ARG_INT, {.u_int = 0} },
97+
{ MP_QSTR_color_order, MP_ARG_INT, {.u_int = (uint8_t)Hub75::COLOR_ORDER::RGB} },
9698
};
9799

98100
// 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
103105
int height = args[ARG_height].u_int;
104106
PanelType paneltype = (PanelType)args[ARG_panel_type].u_int;
105107
bool stb_invert = args[ARG_stb_invert].u_int;
108+
Hub75::COLOR_ORDER color_order = (Hub75::COLOR_ORDER)args[ARG_color_order].u_int;
106109

107110
Pixel *buffer = nullptr;
108111

@@ -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
120123
hub75_obj = m_new_obj_with_finaliser(_Hub75_obj_t);
121124
hub75_obj->base.type = &Hub75_type;
122125
hub75_obj->buf = buffer;
123-
hub75_obj->hub75 = m_new_class(Hub75, width, height, buffer, paneltype, stb_invert);
126+
hub75_obj->hub75 = m_new_class(Hub75, width, height, buffer, paneltype, stb_invert, color_order);
124127

125128
return MP_OBJ_FROM_PTR(hub75_obj);
126129
}

0 commit comments

Comments
 (0)