summaryrefslogtreecommitdiff
path: root/pixelhub.cpp
blob: 506e1a107e5e6a099665237a2caa0bb76f948b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * pixelhub.c
 *
 * DMX to neopixel interface
 *
 * Copyright © 2023 Thomas White <taw@physics.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with This program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <pico/stdlib.h>
#include <DmxInput.h>
#include <ws2812.pio.h>

const int dmx_rx_pin = 16;

struct pixel_strip
{
	int pin;
	int num_pixels;
	PIO pio;
	uint pio_sm;
	uint offset;
	uint32_t pixelbuf[256];
	int rgbw;
};


static inline void put_pixel(struct pixel_strip *p, uint32_t pixel_grb) {
	pio_sm_put_blocking(p->pio, p->pio_sm, pixel_grb);
}


static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
	return ((uint32_t)(g)<<24) | ((uint32_t)(r)<<16) | ((uint32_t)(b)<<8);
}


static inline uint32_t wrgb_u32(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
	return ((uint32_t)(g)<<24) | ((uint32_t)(r)<<16) | ((uint32_t)(b)<<8) | (uint32_t)(w);
}


static void init_pixel_strip(struct pixel_strip *p)
{
	int i;

	p->pio_sm = pio_claim_unused_sm(p->pio, true);
	p->offset = pio_add_program(p->pio, &ws2812_program);
	ws2812_program_init(p->pio, p->pio_sm, p->offset, p->pin, 800000, p->rgbw);

	for ( i=0; i<p->num_pixels; i++ ) {
		p->pixelbuf[i] = 0;
	}

}


int main()
{
	DmxInput dmx_in;
	struct pixel_strip p1;
	struct pixel_strip p2;
	uint8_t dmxbuf[512];
	int i;
	int blink = 1;

	p1.pin = 2;
	p1.num_pixels = 8;
	p1.rgbw = false;
	p1.pio = pio0;

	p2.pin = 3;
	p2.num_pixels = 1;
	p2.rgbw = true;
	p2.pio = pio0;

	/* Initialise DMX */
	dmx_in.begin(dmx_rx_pin, 1, 512);

	/* Initialise Neopixels */
	init_pixel_strip(&p1);
	init_pixel_strip(&p2);
	int npx = 8;

	/* Status LED */
	gpio_init(PICO_DEFAULT_LED_PIN);
	gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);

	while (1) {

		dmx_in.read(dmxbuf);

		int dmxpos = 1;  /* Numbering starts at 1 */
		for ( i=0; i<p1.num_pixels; i++) {
			p1.pixelbuf[i] = urgb_u32(dmxbuf[dmxpos++],
			                          dmxbuf[dmxpos++],
			                          dmxbuf[dmxpos++]);
		}
		for ( i=0; i<p2.num_pixels; i++) {
			p2.pixelbuf[i] = wrgb_u32(dmxbuf[dmxpos++],
			                          dmxbuf[dmxpos++],
			                          dmxbuf[dmxpos++],
			                          dmxbuf[dmxpos++]);
		}

		for ( i=0; i<npx; i++) {
			put_pixel(&p1, p1.pixelbuf[i]);
			put_pixel(&p2, p2.pixelbuf[i]);
		}

		gpio_put(PICO_DEFAULT_LED_PIN, blink);
		blink = 1 - blink;
	}
}