2023-07-07 20:26:36 +00:00
|
|
|
#include <hamlib/rig.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "fifo.h"
|
|
|
|
|
|
|
|
|
2023-07-10 11:57:11 +00:00
|
|
|
void initFIFO(FIFO_RIG *fifo)
|
2023-07-07 20:26:36 +00:00
|
|
|
{
|
|
|
|
fifo->head = 0;
|
|
|
|
fifo->tail = 0;
|
|
|
|
}
|
|
|
|
|
2023-07-10 11:57:11 +00:00
|
|
|
void resetFIFO(FIFO_RIG *fifo)
|
2023-07-09 21:51:33 +00:00
|
|
|
{
|
|
|
|
fifo->head = fifo->tail;
|
2023-07-09 22:04:39 +00:00
|
|
|
fifo->flush = 1;
|
2023-07-09 21:51:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 20:26:36 +00:00
|
|
|
// returns RIG_OK if added
|
|
|
|
// return -RIG error if overflow
|
2023-07-10 11:57:11 +00:00
|
|
|
int push(FIFO_RIG *fifo, const char *msg)
|
2023-07-07 20:26:36 +00:00
|
|
|
{
|
|
|
|
int len = strlen(msg);
|
|
|
|
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
fifo->data[fifo->tail] = msg[i];
|
2023-07-09 21:51:33 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: push %c (%d,%d)\n", __func__, msg[i],
|
|
|
|
fifo->head, fifo->tail);
|
|
|
|
|
|
|
|
if (fifo->tail + 1 == fifo->head) { return -RIG_EDOM; }
|
2023-07-07 20:26:36 +00:00
|
|
|
|
2023-07-09 21:51:33 +00:00
|
|
|
fifo->tail = (fifo->tail + 1) % HAMLIB_FIFO_SIZE;
|
2023-07-07 20:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return RIG_OK;
|
|
|
|
}
|
|
|
|
|
2023-07-12 07:52:42 +00:00
|
|
|
int pop(FIFO_RIG *fifo)
|
2023-07-07 20:26:36 +00:00
|
|
|
{
|
|
|
|
if (fifo->tail == fifo->head) { return -1; }
|
|
|
|
|
|
|
|
char c = fifo->data[fifo->head];
|
2023-07-09 21:51:33 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: pop %c (%d,%d)\n", __func__, c, fifo->head,
|
|
|
|
fifo->tail);
|
|
|
|
fifo->head = (fifo->head + 1) % HAMLIB_FIFO_SIZE;
|
2023-07-07 20:26:36 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
int main()
|
|
|
|
{
|
2023-07-10 11:57:11 +00:00
|
|
|
FIFO_RIG fifo;
|
2023-07-07 20:26:36 +00:00
|
|
|
initFIFO(&fifo);
|
|
|
|
|
|
|
|
const char *str = "Hello, World!\n";
|
|
|
|
|
|
|
|
// Pushing the string onto the FIFO
|
|
|
|
push(&fifo, str);
|
|
|
|
|
|
|
|
// Popping and printing one character at a time
|
|
|
|
int c;
|
2023-07-08 03:23:28 +00:00
|
|
|
|
2023-07-07 20:26:36 +00:00
|
|
|
while ((c = pop(&fifo)) != -1)
|
|
|
|
{
|
|
|
|
printf("%c", c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|