/* Copyright 2026 by COG9 Enterprises LLC
 * All rights reserved.
 * Title: queue.ino
 * Author: Jed Marti KI7NNP
 * Description: Queue code for the graphs. Each graph has a queue of values so the full graph
 *   is always displayed with the oldest values on the left. There's one type of queue for each
 *   data type.
 * Created: Wed Jan  7 14:20:46 2026
 * Revision History:
 */

#include <string.h>
#include "powerard.h"
#include "queue.h"


void initQueue8(Queue8 *q, char *name)
/*     Initialize a queue for 8 bit values. The queue size is set at compile time.
 * Parameters:
 *   q: the address of the queue control block (see queue.h).
 *   name: A name - mostly for display and debugging. Maximum characters is qnameSize.
 * External References: none
 * Returned values: none
 * Errors detected: none
 */
{

  strncpy(q->qname, name, qnameSize);
  // qindex is the first free position. (qindex - 1) % queueSize the last.
  q->qindex = 0;
  memset(q->v, queueSize, sizeof(uint8_t));
}



void initQueue16(Queue16 *q, char *name)
/*     Initialize a queue for 16 bit values. The queue size is set at compile time.
 * Parameters:
 *   q: the address of the 16 bit queue control block (see queue.h).
 *   name: A name - mostly for display and debugging. Maximum characters is qnameSize.
 * External References: none
 * Returned values: none
 * Errors detected: none
 */
{
  strncpy(q->qname, name, qnameSize);
  // qindex is the first free position. (qindex - 1) % queueSize the last.
  q->qindex = 0;
  memset(q->v, queueSize, sizeof(uint16_t));
}



void initQueueF(QueueF *q, char *name)
/*     Initialize a queue for 4 byte floating-point values. The queue size is set at compile time.
 * Parameters:
 *   q: the address of the floating-point queue control block (see queue.h).
 *   name: A name - mostly for display and debugging. Maximum characters is qnameSize.
 * External References: none
 * Returned values: none
 * Errors detected: none
 */
{
  strncpy(q->qname, name, qnameSize);
  // qindex is the first free position. (qindex - 1) % queueSize the last.
  q->qindex = 0;
  memset(q->v, queueSize, sizeof(float));
}


void add2Queue8(Queue8 *q, uint8_t v)
/*      Add an unsigned 8 bit value to an 8 bit queue and crank up the index module the queue
 *   size.
 * Parameters:
 *   q: Address of an 8 bit queue control block. Contents modified.
 *   v: Value to store there.
 * External References:
 *   queueSize: Number of queue entries (constant, see queue.h).
 * Returned values: none
 * Errors detected: none
 */
{
  q->v[q->qindex] = v;
  q->qindex = (q->qindex + 1) % queueSize;
}



void add2Queue16(Queue16 *q, uint16_t v)
/*      Add an usigned 16 bit value to a 16 bit queue and crank up the index modulo the queue
 *   size.
 * Parameters:
 *   q: Address of an 16 bit queue control block. Contents modified.
 *   v: Value to store there.
 * External References:
 *   queueSize: Number of queue entries (constant, see queue.h).
 * Returned values: none
 * Errors detected: none
 */
{
  q->v[q->qindex] = v;
  q->qindex = (q->qindex + 1) % queueSize;
}



void add2QueueF(QueueF *q, float v)
/*      Add a single-precision, floating-point value to a queue and crank up the index modulo
 *   the queue size.
 * Parameters:
 *   q: Address of an floating-point queue control block.
 *   v: Value to store there.
 * External References:
 *   queueSize: Number of queue entries (constant, see queue.h).
 * Returned values: none
 * Errors detected: none
 */
{
  q->v[q->qindex] = v;
  q->qindex = (q->qindex + 1) % queueSize;
}



uint8_t getQueue8(Queue8 *q, uint8_t index)
/*     Retrieve a value from the queue. 0 is the oldest entry.
 * Parameters:
 *   q: Address of an eight bit queue control block. Contents modified.
 *   index: index of the entry.
 * External References:
 *   queueSize: Number of queue entries (constant, see queue.h).
 * Returned values: the entry value.
 * Errors detected: none
 */
{
  uint8_t i;
  i = (index + q->qindex) % queueSize;
  return q->v[i];
}



uint16_t getQueue16(Queue16 *q, uint8_t index)
/*     Retrieve a value from the queue. 0 is the oldest entry.
 * Parameters:
 *   q: Address of an eight bit queue control block. Contents modified.
 *   index: index of the entry.
 * External References:
 *   queueSize: Number of queue entries (constant, see queue.h).
 * Returned values: the entry value.
 * Errors detected: none
 */
{
  uint8_t i;
  i = (index + q->qindex) % queueSize;
  return q->v[i];
}



float getQueueF(QueueF *q, uint8_t index)
/*     Retrieve a value from the queue. 0 is the oldest entry.
 * Parameters:
 *   q: Address of an eight bit queue control block. Contents modified.
 *   index: index of the entry.
 * External References:
 *   queueSize: Number of queue entries (constant, see queue.h).
 * Returned values: the entry value.
 * Errors detected: none
 */
{
  uint8_t i;
  i = (index + q->qindex) % queueSize;
  return q->v[i];
}


// EOF
