Skip to content

Instantly share code, notes, and snippets.

@pingswept
Created November 15, 2012 22:34
Show Gist options
  • Select an option

  • Save pingswept/4081912 to your computer and use it in GitHub Desktop.

Select an option

Save pingswept/4081912 to your computer and use it in GitHub Desktop.
First cut at DMX receiving code for Leonardo / Saikoduino
#define RX_STATUS_PIN 13
#define RX_ENABLE_PIN 2
#define TX_ENABLE_PIN 3
volatile uint8_t DmxRxField[8]; //array of DMX vals (raw)
volatile uint16_t DmxAddress; //start address
enum {IDLE, BREAK, STARTB, STARTADR}; //DMX states
volatile uint8_t gDmxState;
int red = 9;
int green = 10;
int blue = 11;
int bmix = 0; // blue brightness
int fadeAmount = 1; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
Serial.begin(250000); //Enable serial reception with a 250k rate
pinMode(RX_STATUS_PIN, OUTPUT); //RX STATUS LED pin, blinks on incoming DMX data
pinMode(RX_ENABLE_PIN, OUTPUT);
pinMode(TX_ENABLE_PIN, OUTPUT);
digitalWrite(RX_ENABLE_PIN, LOW);
digitalWrite(TX_ENABLE_PIN, LOW);
gDmxState= IDLE; // initial state
DmxAddress = 3; // The desired DMX Start Adress
interrupts();
}
// the loop routine runs over and over again forever:
void loop() {
analogWrite(blue, bmix);
// change the brightness for next time through the loop:
bmix = bmix + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (bmix == 0 || bmix == 20) {
fadeAmount = -fadeAmount ;
}
// wait for 60 milliseconds to see the dimming effect
delay(60);
}
ISR(USART1_RX_vect) {
digitalWrite(RX_STATUS_PIN, HIGH);
static uint16_t DmxCount;
uint8_t USARTstate= UCSR1A; //get state before data!
uint8_t DmxByte = UDR1; //get data
uint8_t DmxState = gDmxState; //just load once from SRAM to increase speed
if (USARTstate &(1<<FE1)) //check for break
{
DmxCount = DmxAddress; //reset channel counter (count channels before start address)
gDmxState= BREAK;
}
else if (DmxState == BREAK)
{
if (DmxByte == 0) gDmxState= STARTB; //normal start code detected
else gDmxState= IDLE;
}
else if (DmxState == STARTB)
{
if (--DmxCount == 0) //start address reached?
{
DmxCount= 1; //set up counter for required channels
DmxRxField[0]= DmxByte; //get 1st DMX channel of device
gDmxState= STARTADR;
}
}
else if (DmxState == STARTADR)
{
DmxRxField[DmxCount++]= DmxByte; //get channel
if (DmxCount >= sizeof(DmxRxField)) //all ch received?
{
gDmxState= IDLE; //wait for next break
}
}
digitalWrite(RX_STATUS_PIN, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment