diff --git a/src/Makefile.am b/src/Makefile.am index 17d5a2701..31fe4b9fe 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ RIGSRC = hamlibdatetime.h rig.c serial.c serial.h misc.c misc.h register.c regis network.c network.h cm108.c cm108.h gpio.c gpio.h idx_builtin.h token.h \ par_nt.h microham.c microham.h amplifier.c amp_reg.c amp_conf.c \ amp_conf.h amp_settings.c extamp.c sleep.c sleep.h sprintflst.c \ - sprintflst.h cache.c cache.h snapshot_data.c snapshot_data.h multicast.c + sprintflst.h cache.c cache.h snapshot_data.c snapshot_data.h multicast.c fifo.c if VERSIONDLL RIGSRC += \ diff --git a/src/fifo.c b/src/fifo.c new file mode 100644 index 000000000..44b00c8a9 --- /dev/null +++ b/src/fifo.c @@ -0,0 +1,58 @@ +#include +#include +#include "fifo.h" + + +void initFIFO(FIFO *fifo) +{ + fifo->head = 0; + fifo->tail = 0; +} + +// returns RIG_OK if added +// return -RIG error if overflow +int push(FIFO *fifo, const char *msg) +{ + int len = strlen(msg); + + for (int i = 0; i < len; ++i) + { + fifo->data[fifo->tail] = msg[i]; + fifo->tail = (fifo->tail + 1) % FIFO_SIZE; + + if (fifo->tail == fifo->head) { return -RIG_EDOM; } + } + + return RIG_OK; +} + +char pop(FIFO *fifo) +{ + if (fifo->tail == fifo->head) { return -1; } + + char c = fifo->data[fifo->head]; + fifo->head = (fifo->head + 1) % FIFO_SIZE; + return c; +} + +#ifdef TEST +int main() +{ + FIFO fifo; + 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; + while ((c = pop(&fifo)) != -1) + { + printf("%c", c); + } + + return 0; +} +#endif diff --git a/src/fifo.h b/src/fifo.h new file mode 100644 index 000000000..0eeaedb99 --- /dev/null +++ b/src/fifo.h @@ -0,0 +1,12 @@ +#define FIFO_SIZE 1024 + +typedef struct +{ + char data[FIFO_SIZE]; + int head; + int tail; +} FIFO; + +void initFIFO(FIFO *fifo); +int push(FIFO *fifo, const char *msg); +char pop(FIFO *fifo);