summaryrefslogtreecommitdiff
path: root/pixelhub.cpp
blob: 5ce4f64ff0e06dc0e3f7eb79ec576f64a9c50d53 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * 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;
	uint dma_chan;
};


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);
}


uint dma_chan;
dma_channel_config dma_conf;
struct pixel_strip pxs[8];
int n_pxs;
int cur_strip = 0;


static void set_dma()
{
	channel_config_set_dreq(&dma_conf,
	                        pio_get_dreq(pxs[cur_strip].pio, pxs[cur_strip].pio_sm, true));
	dma_channel_configure(dma_chan, &dma_conf,
	                      &pxs[cur_strip].pio->txf[pxs[cur_strip].pio_sm],
	                      pxs[cur_strip].pixelbuf,
	                      pxs[cur_strip].num_pixels,
	                      true);
}


static int64_t next_strip(alarm_id_t id, void *vp)
{
	cur_strip++;
	if ( cur_strip >= n_pxs ) cur_strip = 0;
	set_dma();
	return 0;
}


static void dma_complete()
{
	if ( dma_hw->ints0 & 1<<dma_chan ) {
		dma_hw->ints0 = 1<<dma_chan;
		add_alarm_in_us(400, next_strip, NULL, true);
	}
}


static void init_pixel_strip(struct pixel_strip *p, PIO pio, int sm)
{
	int i;

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

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

	pio_sm_set_enabled(pio, sm, false);
	pio_sm_clear_fifos(pio, sm);
	pio_sm_restart(pio, sm);
	pio_sm_set_enabled(pio, sm, true);
}


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

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

	pxs[0].pin = 2;
	pxs[0].num_pixels = 8;
	pxs[0].rgbw = false;

	pxs[1].pin = 3;
	pxs[1].num_pixels = 1;
	pxs[1].rgbw = true;

	n_pxs = 2;

	/* Initialise Neopixels */
	init_pixel_strip(&pxs[0], pio0, 0);
	init_pixel_strip(&pxs[1], pio0, 1);

	/* Start DMA */
	dma_chan = dma_claim_unused_channel(true);
	dma_conf = dma_channel_get_default_config(dma_chan);
	channel_config_set_read_increment(&dma_conf, true);
	channel_config_set_write_increment(&dma_conf, false);
	irq_set_exclusive_handler(DMA_IRQ_0, dma_complete);
	dma_channel_set_irq0_enabled(dma_chan, true);
	irq_set_enabled(DMA_IRQ_0, true);
	set_dma();

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

	int loop = 0;
	while (1) {

		//dmx_in.read(dmxbuf);

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

		gpio_put(PICO_DEFAULT_LED_PIN, blink);
		loop++;
		if ( loop > 1000000 ) {
			loop = 0;
			blink = 1 - blink;
		}
	}
}