summaryrefslogtreecommitdiff
path: root/pixelhub.cpp
blob: f876e08dbe5b3473ef940cad7a77e3afbda14f27 (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
/*
 * 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;
const int pixels_pin = 2;
const int num_pixels = 8;
const PIO led_pio = pio0;
uint led_pio_sm;


static inline void put_pixel(uint32_t pixel_grb) {
	pio_sm_put_blocking(led_pio, led_pio_sm, pixel_grb << 8u);
}


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


int main()
{
	DmxInput dmx_in;
	uint offset;
	uint8_t dmxbuf[512];
	uint32_t pixelbuf[num_pixels];
	int i;
	int blink = 1;

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

	/* Initialise Neopixels */
	led_pio_sm = pio_claim_unused_sm(led_pio, true);
    	offset = pio_add_program(led_pio, &ws2812_program);
	ws2812_program_init(led_pio, led_pio_sm, offset, pixels_pin, 800000, false);

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

	for ( i=0; i<num_pixels; i++ ) {
		pixelbuf[i] = 0x00000000;  /* (W)GRB */
	}

	while (1) {

		dmx_in.read(dmxbuf);

		for ( i=0; i<num_pixels; i++) {
			pixelbuf[i] = urgb_u32(dmxbuf[1+3*i+0],
			                       dmxbuf[1+3*i+1],
			                       dmxbuf[1+3*i+2]);
		}

		for ( i=0; i<num_pixels; i++) {
			put_pixel(pixelbuf[i]);
		}

		gpio_put(PICO_DEFAULT_LED_PIN, blink);
		blink = 1 - blink;
		//sleep_us(100000);
	}
}