// Queue Access Library
// 081723 DS

#include "QueueLib.h"

AT200Q at_Q[at_QSIZ];
uint16_t at_Qhead = 0;
uint16_t at_Qtail = 0;

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

void atQ_push(void) // load tempdata struct first..  pointer sits on next one to add
{
  /*
    at_Q[at_Qhead].serial = at_QTempData.serial;
    at_Q[at_Qhead].chan   = at_QTempData.chan;
    at_Q[at_Qhead].type   = at_QTempData.type;
    at_Q[at_Qhead].stat   = at_QTempData.stat;
    at_Q[at_Qhead].ver    = at_QTempData.ver;
    at_Q[at_Qhead].rssi   = at_QTempData.rssi;
    at_Q[at_Qhead].data   = at_QTempData.data;
    at_Q[at_Qhead].etime  = getEpochRtc();
  */

  at_Q[at_Qhead] = at_QTempData;

  if (++at_Qhead >= at_QSIZ)
    at_Qhead = 0; // if at end then wrap

  if (at_Qhead == at_Qtail)
    at_Qtail++; // if hit other pointer then we full
  if (at_Qtail == at_QSIZ)
    at_Qtail = 0; // if at end then wrap

  if (QueueLibVB)
    Serial.printf("Qsize %d\n", atQ_size());
}

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

void atQ_pull_FIFO(void) // dumps into tempdata struct pointer sits on next on to pull
{
  /*
    at_QTempData.serial = at_Q[at_Qtail].serial;
    at_QTempData.type = at_Q[at_Qtail].type;
    at_QTempData.stat = at_Q[at_Qtail].stat;
    at_QTempData.chan = at_Q[at_Qtail].chan;
    at_QTempData.ver = at_Q[at_Qtail].ver;
    at_QTempData.rssi = at_Q[at_Qtail].rssi;
    at_QTempData.data = at_Q[at_Qtail].data;
    at_QTempData.etime = at_Q[at_Qtail].etime;
  */

  at_QTempData = at_Q[at_Qtail];

  if (++at_Qtail == at_QSIZ)
    at_Qtail = 0; // if at end then wrap
}

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

void atQ_pull_LIFO(void) // dumps into tempdata struct pointer sits on next on to pull
{
  if (at_Qhead == at_Qtail)
    return;

  // must have wraped on last push
  if (at_Qhead == 0)
    at_Qhead = (at_QSIZ - 1);
  else
    at_Qhead--;

  at_QTempData = at_Q[at_Qhead]; // copy it

  /*
    at_QTempData.serial = at_Q[at_Qhead].serial;
    at_QTempData.type = at_Q[at_Qhead].type;
    at_QTempData.stat = at_Q[at_Qhead].stat;
    at_QTempData.chan = at_Q[at_Qhead].chan;
    at_QTempData.ver = at_Q[at_Qhead].ver;
    at_QTempData.rssi = at_Q[at_Qhead].rssi;
    at_QTempData.data = at_Q[at_Qhead].data;
    at_QTempData.etime = at_Q[at_Qhead].etime;
  */

  if (QueueLibVB)
    Serial.printf("PULL LIFO :  H: %d T: %d\n", at_Qhead, at_Qtail);
}

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

uint16_t atQ_size(void)
{
  if (at_Qhead >= at_Qtail)
    return (at_Qhead - at_Qtail);
  else
    return ((at_QSIZ - at_Qtail) + at_Qhead);
}

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

void atQ_flush(void)
{
  memset(at_Q, 0, sizeof(at_Q));
  at_Qhead = at_Qtail = 0;
}
