Created
July 8, 2022 15:06
-
-
Save goran-mahovlic/6f173cfc83d0db7202b8254fd14a96da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <cmath> | |
#include "pico/stdlib.h" | |
#include "hardware/watchdog.h" | |
#include "hardware/structs/dma.h" | |
#include "WS2812.hpp" | |
//#define USE_DMX_OUTPUT | |
#define USE_DMX_INPUT | |
#define USE_ASYNC_INPUT | |
//#define USE_WATCHDOG | |
#ifdef USE_DMX_INPUT | |
#include "DmxInput.h" | |
#endif | |
#ifdef USE_DMX_OUTPUT | |
#include "DmxOutput.h" | |
#endif | |
#define LED_PIN 25 | |
#define STRIP_PIN 14 | |
#define DMA_RX_PIN 12 | |
#define DMA_TX_PIN 13 | |
#define BRIGHTNESS 20 | |
#define START_CHANNEL 1 // first DMX channel | |
#define LED_LENGTH 171 // number of RGB LEDs on strip | |
#define NUM_CHANNELS (LED_LENGTH*3) // number of DMX channels used (3*24 LEDs) | |
bool DMX_buffer_ready = false; | |
volatile uint8_t led_buffer[NUM_CHANNELS+1]; | |
#ifdef USE_DMX_OUTPUT | |
DmxOutput dmxOutput; | |
uint8_t universe[NUM_CHANNELS + 1]; | |
int DMX_loop_count; | |
#endif | |
// 0. Initialize LED strip | |
// printf("0. Initialize LED strip"); | |
WS2812 ledStrip( | |
STRIP_PIN, // Data line is connected to pin 0. (GP0) | |
LED_LENGTH, // Strip is 6 LEDs long. | |
pio1_hw, // Use PIO 0 for creating the state machine. | |
0, // Index of the state machine that will be created for controlling the LED strip | |
// You can have 4 state machines per PIO-Block up to 8 overall. | |
// See Chapter 3 in: https://datasheets.raspberrypi.org/rp2040/rp2040-datasheet.pdf | |
WS2812::FORMAT_RGB // Pixel format used by the LED strip | |
); | |
#ifdef USE_DMX_INPUT | |
DmxInput dmxInput; | |
uint8_t *DMX_buffer; | |
volatile uint8_t buffer[DMXINPUT_BUFFER_SIZE(START_CHANNEL, NUM_CHANNELS)]; | |
void __isr dmxDataRecevied(DmxInput* instance) { | |
// A DMX frame has been received :-) | |
// Toggle some LED, depending on which pin the data arrived | |
DMX_buffer_ready = true; | |
} | |
#endif | |
int main() | |
{ | |
stdio_init_all(); | |
#ifdef USE_WATCHDOG | |
watchdog_enable(10000, 1); | |
#endif | |
gpio_set_dir(STRIP_PIN, GPIO_OUT); | |
#ifdef USE_DMX_OUTPUT | |
// Start the DMX Output on GPIO-pin 0 | |
dmxOutput.begin(DMA_TX_PIN); | |
// Set all channels in the universe to the max allowed value (512) | |
for (int i = 1; i < (NUM_CHANNELS + 1); i++) | |
{ | |
universe[i] = BRIGHTNESS; | |
} | |
#endif | |
#ifdef USE_DMX_INPUT | |
dmxInput.begin(DMA_RX_PIN, START_CHANNEL, NUM_CHANNELS); | |
#ifdef USE_ASYNC_INPUT | |
dmxInput.read_async(buffer); | |
#endif | |
#endif | |
// Clean strip | |
ledStrip.fill( WS2812::RGB(0, 0, 0) ); | |
ledStrip.show(); | |
while(true){ | |
#ifdef USE_WATCHDOG | |
watchdog_update(); | |
#endif | |
#ifdef USE_DMX_OUTPUT | |
// Send out universe on GPIO-pin 1 | |
dmxOutput.write(universe, NUM_CHANNELS); | |
while (dmxOutput.busy()) | |
{ | |
/* Do nothing while the DMX frame transmits */ | |
} | |
DMX_loop_count++; | |
if(DMX_loop_count<100){ | |
for (int i = 1; i < NUM_CHANNELS + 1; i++) | |
{ | |
universe[i] = BRIGHTNESS; | |
} | |
} | |
else if(DMX_loop_count>=100 && DMX_loop_count<=200){ | |
for (int i = 1; i < NUM_CHANNELS + 1; i++) | |
{ | |
universe[i] = 0; | |
} | |
if(DMX_loop_count==200){ | |
DMX_loop_count=0; | |
} | |
} | |
#endif | |
#ifdef USE_DMX_INPUT | |
#ifndef USE_ASYNC_INPUT | |
dmxInput.read(buffer); | |
DMX_buffer_ready = true; | |
#endif | |
if(DMX_buffer_ready){ | |
for(int i=0;i<NUM_CHANNELS;i++){ | |
led_buffer[i] = buffer[i+1]; | |
} | |
ledStrip.fill_buffer(led_buffer); | |
ledStrip.show(); | |
DMX_buffer_ready = false; | |
} | |
#endif | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment