/*
 * Author: klaute -Kai Lauterbach - @kailauterbach - me@klaute.de
 * Date: 08/2016
 * License: GPLv3
 */

/*****************************************************************************/

extern void USB_serialStreamWriteC(char*, uint16_t);

extern uint32_t eep_baudrate;
extern uint32_t baudrate;

/*****************************************************************************/

void cc_setBaudrate()
{
  baudrate  = (uint32_t)cc_read_data[0] << 24;
  baudrate += (uint32_t)cc_read_data[1] << 16;
  baudrate += (uint32_t)cc_read_data[2] <<  8;
  baudrate += (uint32_t)cc_read_data[3];

  eeprom_busy_wait();

  eeprom_write_dword(&eep_baudrate, baudrate);

  eeprom_busy_wait();

  uart_init( UART_BAUD_SELECT(baudrate, F_CPU) ); 

  char* sBody = "     ";
  sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2);
  USB_serialStreamWriteC(sBody, strlen(sBody));
}

/*****************************************************************************/

void cc_getBaudrate()
{
  char* sBody = "         ";
  sprintf(sBody, "%c%c%c%c%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_BAUDRATE,
                               (uint8_t)((baudrate & 0xff000000) >> 24),
                               (uint8_t)((baudrate &   0xff0000) >> 16),
                               (uint8_t)((baudrate &     0xff00) >>  8),
                               (uint8_t) (baudrate & 0xff),
                               MSG_EOM1, MSG_EOM2);
  USB_serialStreamWriteC(sBody, strlen(sBody));
}

/*****************************************************************************/

void cc_startBootloader()
{
  // enable the watchdog without a time interval to use
  wdt_enable(0);

  // trigger watchdog
  while(1) {};
}

/*****************************************************************************/

void cc_getMuxLine()
{
  uint8_t mux_line  = ((PORTB & 0x40) >> 6);
          mux_line += ((PORTB & 0x20) >> 4);
          mux_line += ((PORTB & 0x10) >> 2);

  char* sBody = "      ";
  sprintf(sBody, "%c%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_MUXLINE, mux_line, MSG_EOM1, MSG_EOM2);
  USB_serialStreamWriteC(sBody, strlen(sBody));
}

/*****************************************************************************/

void cc_setMuxLine()
{
  switch(cc_read_data[0])
  {
    case 0:;
      EN_MUX_LINE0;
      break;
    case 1:;
      EN_MUX_LINE1;
      break;
    case 2:;
      EN_MUX_LINE2;
      break;
    case 3:;
      EN_MUX_LINE3;
      break;
    case 4:;
      EN_MUX_LINE4;
      break;
    case 5:;
      EN_MUX_LINE5;
      break;
    case 6:;
      EN_MUX_LINE6;
      break;
    case 7:;
      EN_MUX_LINE7;
      break;
    default:;
      EN_MUX_LINE0;
      char* sBody = "     ";
      sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_NOK, MSG_EOM1, MSG_EOM2);
      USB_serialStreamWriteC(sBody, strlen(sBody));
      return;

  }

  char* sBody = "     ";
  sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2);
  USB_serialStreamWriteC(sBody, strlen(sBody));
}

/*****************************************************************************/

