#ifndef DMX_DISABLE

#include "modules/out_dmx.h"
#include "Arduino.h"
#include "options.h"
#include "Streaming.h"

#include "item.h"
#include "main.h"
#include "dmx.h"

static int driverStatus = CST_UNKNOWN;

int  out_dmx::Setup()
{
debugSerial<<F("DMX-Out Init")<<endl;
driverStatus = CST_INITIALIZED;
return 1;
}

int  out_dmx::Stop()
{
debugSerial<<F("DMX-Out stop")<<endl;
driverStatus = CST_UNKNOWN;
return 1;
}

int  out_dmx::Status()
{
return driverStatus;
}

int out_dmx::isActive()
{
itemArgStore st;
st.aslong = item->getVal(); //Restore old params
debugSerial<< F(" val:")<<st.v<<endl;
return st.v;
}

int out_dmx::Poll(short cause)
{
return 0;
};

int out_dmx::getChanType()
{
  if (item)
  {
  switch (numArgs)
  {
    case 3:
      return CH_RGB;
    case 4:
      return CH_RGBW;
    default:
      return item->itemType;
  }
  return 0;
 }
}

int out_dmx::PixelCtrl(itemCmd cmd, char* subItem, bool show)
//int out_dmx::PixelCtrl(itemCmd cmd)
{
if (!item || !show) return 0;

short   cType=getChanType();
uint8_t storageType;

/*
switch (cmd.getCmd()){
  case CMD_OFF:
    cmd.Percents(0);
  break;
}
*/


debugSerial<<F("DMX ctrl: "); cmd.debugOut();


if (cType==CH_DIMMER) //Single channel
  {
    DmxWrite(iaddr, cmd.getPercents255());
    return 1;
  }

  switch (cType)
  {
    case CH_RGB:
     storageType=ST_RGB;
     break;
    case CH_RGBW:
     storageType=ST_RGBW;
     break;
    default:
     storageType=ST_PERCENTS;
   }

itemCmd st(storageType,CMD_VOID);
st.assignFrom(cmd);

debugSerial<<F("Assigned:");st.debugOut();
    switch (cType)
    {
      case CH_RGBW:
      DmxWrite(getChannelAddr(3), st.param.w);
      case CH_RGB:
      DmxWrite(iaddr, st.param.r);
      DmxWrite(getChannelAddr(1), st.param.g);
      DmxWrite(getChannelAddr(2), st.param.b);
      break;
      default: ;
    }
return 1;
}


#endif
