/** @package

        g2_slave.c

        Copyright(c) ADI 2000

        Author: Dan Smith
        Created: DS  9/21/2007 7:56:59 AM
   change: DS 9/21/2007 7:59:34 AM
*/

int highestScannedBed = 0;
uint16_t lastchar;

#include "slave.h"
int S232 = 0;
void runSlave(void) /*this runs in the slave unit*/
{
  static uint8_t c, i;
  static int16_t j, cc;

  while (Serial2.available()) // JRB 022823
  {
    cc = Serial2.read(); // get the char
    c = cc & 0xff;
    if ((c == 0xff) && (lastchar == 0xff)) // 2 ffs in a row
    {
      indx = 0;
    }
    else
    {
      instr[++indx] = c;
    }
    lastchar = c;

    if (indx >= 16)
      indx = 0; // oops overrun
    if (indx >= 6)
      break; // should have a command.. outta here *
  };

  /*
    do {
      if (!Serial2.available()) break;
      cc = Serial2.read();                // get the char

      c = cc & 0xff;
      if ((c == 0xff) && (instr[indx] == 0xff))   // 2 ffs in a row
      {
        indx = 0;
      } else
      {
        instr[++indx] = c;
      }

      if (indx >= 16) indx = 0;                      // oops overrun
      if (indx >= 6) break;                          // should have a command.. outta here *
    } while (1);
  */

  // instr[1] = command
  // instr[2] = address

  if (indx == 6) /* we got 6 int8s after 2 ffs so it's a command */
  {
    indx = 0; /*so we dont do this twice on same receive*/

    if (S232)
    {
      Serial.printf("RX data: %3d, %3d, %3d, %3d,%3d, %3d ", instr[0], instr[1], instr[2], instr[3], instr[4], instr[5], instr[6]);
      Serial.printf("SLV CSUM calc: %d sent: %d\n", slav_csum(), instr[6]);
    }

    // request for data
    if (instr[1] == 10)
    {
      if (S232)
        Serial.printf("Slave Command Received\n");
      if (instr[2] == (100))         /* is it us? */
        if (slav_csum() == instr[6]) /*got a good cs*/
        {
          wiFiOK = instr[5];
          if (S232)
            Serial.printf("Slave CSUM OK\n");
          delay(80); /*a little settling time for rs485*/
          if (instr[3] != 0)
            Serial.printf("\n RX Command %d\n", instr[3]);

          // bug screen commands
          if (instr[3] == 1)
            rfSendKey(RF_KEY_DN, myCHAN);
          if (instr[3] == 2)
            rfSendKey(RF_KEY_STOP, myCHAN);
          if (instr[3] == 3)
            rfSendKey(RF_KEY_UP, myCHAN);

          // door commands
          cDOORCLOSE = 0;
          cDOOROPEN = 0;
          cDOORSTOP = 0;

          if (instr[3] == 4)
            cDOORCLOSE = 1;
          if (instr[3] == 5)
            cDOOROPEN = 1;
          if (instr[3] == 6)
            cDOORSTOP = 1;

          instr[3] = 0;

          if (getMotorError())
          {
            faultNum = 4;
            faultAB = 0;
            faultData = getMotorError();
            clearMotorError();
          }

          if (motorFault)
          {
            faultNum = 2;
            faultAB = 0;
            faultData = motorFault;
            motorFault = 0;
          }

          c_send((15 << 2), 0, faultData & 0xff, faultNum, faultData >> 8); /*nothing to report*/
          faultNum = faultData = 0;
        }
    } /*end of cs ok*/

#define VERSION 1

    // ping to us
    if (instr[1] == 0)               // ping
      if (instr[2] == (100))         /* is it to us?? */
        if (slav_csum() == instr[6]) /*checksum ok?*/
        {
          if (S232)
            Serial.printf("RX Ping Slave# %d\n", instr[2]);
          if (S232)
            Serial.printf("It's us! CSUM ok! respond!\n");
          delay(50); /*a little settling time for rs485*/

          c_send(0, 0, 5, VERSION, 0); /*report version*/
        }
  }

} // run_slave

uint16_t slav_csum(void)
{
  uint16_t sum = 0;
  uint16_t i;
  for (i = 1; i < 6; sum += instr[i++])
    ;
  if ((sum & 255) == 255)
    sum = 0;

  return (sum & 255);
}
