
/*
 * dmx.c
 *
 *  Created on: 26 Jul 2019
 *      Author: Mike
 */

#include "stdint.h"
#include "string.h"
#include "dmx.h"


// DMX state variables
DMX_State_t DMX_State = DMX_IDLE;
uint8_t DMX_Buffer[512];
uint32_t DMX_Pointer;

void dmx_handle_state(uint8_t* Buf, uint32_t *Len)
{
switch (DMX_State)
{
case	DMX_IDLE:  // nothing happening
	  break;
case DMX_BREAK://
	  if(*Len > 0)
	  {
		  if( Buf[0] != 0)
		  DMX_State = DMX_IDLE;
		  else
		  {
			  memcpy(DMX_Buffer,Buf+1,*Len-1);
			  DMX_Pointer = *Len-1;
			  DMX_State = DMX_BYTES;
		  }
	  }
	  break;
case	DMX_BYTES:
{
    int count = *Len;
    int newPointer  = DMX_Pointer + count;
    if(newPointer >= 512)
  	  count = 512-count;

    memcpy(DMX_Buffer+ DMX_Pointer, Buf, count);
    DMX_Pointer += count;
    if (DMX_Pointer == 512)
    {
       DMX_State = DMX_DATA;
    }
}
break;
case DMX_DATA:
case DMX_DONE:
   break;
}
}

