2023-07-07 20:26:36 +00:00
|
|
|
#include <hamlib/rig.h>
|
|
|
|
#include <stdio.h>
|
2023-07-21 14:44:24 +00:00
|
|
|
#include <ctype.h>
|
2023-07-07 20:26:36 +00:00
|
|
|
#include "fifo.h"
|
2023-07-30 12:53:17 +00:00
|
|
|
#include "config.h"
|
2023-07-07 20:26:36 +00:00
|
|
|
|
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-30 12:53:17 +00:00
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
static pthread_mutex_t t = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
fifo->mutex = t;
|
|
|
|
#endif
|
2023-07-07 20:26:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 11:57:11 +00:00
|
|
|
void resetFIFO(FIFO_RIG *fifo)
|
2023-07-09 21:51:33 +00:00
|
|
|
{
|
2023-08-19 03:35:21 +00:00
|
|
|
rig_debug(RIG_DEBUG_TRACE, "%s: fifo flushed\n", __func__);
|
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
|
|
|
{
|
2023-07-30 12:53:17 +00:00
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
pthread_mutex_lock(&fifo->mutex);
|
|
|
|
#endif
|
2023-07-07 20:26:36 +00:00
|
|
|
int len = strlen(msg);
|
|
|
|
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
{
|
2023-07-21 14:50:42 +00:00
|
|
|
// FIFO is meant for CW use only
|
|
|
|
// So we skip some chars that don't work with CW
|
2023-12-16 17:37:58 +00:00
|
|
|
if (msg[i] & 0x80) { continue; } // drop any chars that have high bit set
|
|
|
|
|
|
|
|
switch (msg[i])
|
2023-07-21 14:50:42 +00:00
|
|
|
{
|
2023-12-16 17:37:58 +00:00
|
|
|
case 0x0d:
|
|
|
|
case 0x0a:
|
2023-07-21 14:50:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 17:37:58 +00:00
|
|
|
|
2023-07-07 20:26:36 +00:00
|
|
|
fifo->data[fifo->tail] = msg[i];
|
2023-12-16 17:37:58 +00:00
|
|
|
|
2023-07-24 14:44:40 +00:00
|
|
|
if (isalnum(msg[i]))
|
2023-12-16 17:37:58 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: push %c (%d,%d)\n", __func__, msg[i],
|
|
|
|
fifo->head, fifo->tail);
|
2023-07-21 14:44:24 +00:00
|
|
|
else
|
2023-12-16 17:37:58 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: push 0x%02x (%d,%d)\n", __func__, msg[i],
|
|
|
|
fifo->head, fifo->tail);
|
2023-07-09 21:51:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-30 12:53:17 +00:00
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
pthread_mutex_unlock(&fifo->mutex);
|
|
|
|
#endif
|
2023-07-07 20:26:36 +00:00
|
|
|
return RIG_OK;
|
|
|
|
}
|
|
|
|
|
2023-07-17 15:26:47 +00:00
|
|
|
int peek(FIFO_RIG *fifo)
|
|
|
|
{
|
2023-12-16 17:37:58 +00:00
|
|
|
if (fifo == NULL) { return -1; }
|
|
|
|
|
|
|
|
if (fifo->tail < 0 || fifo->head < 0) { return -1; }
|
|
|
|
|
|
|
|
if (fifo->tail > 1023 || fifo->head > 1023) { return -1; }
|
|
|
|
|
2023-07-17 15:26:47 +00:00
|
|
|
if (fifo->tail == fifo->head) { return -1; }
|
2023-12-16 17:37:58 +00:00
|
|
|
|
2023-07-30 12:53:17 +00:00
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
pthread_mutex_lock(&fifo->mutex);
|
|
|
|
#endif
|
2023-07-17 15:26:47 +00:00
|
|
|
char c = fifo->data[fifo->head];
|
2023-09-30 20:43:52 +00:00
|
|
|
|
|
|
|
#if 0
|
2023-12-16 17:37:58 +00:00
|
|
|
|
2023-07-24 14:44:40 +00:00
|
|
|
if (isalnum(c))
|
2023-12-16 17:37:58 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: peek %c (%d,%d)\n", __func__, c, fifo->head,
|
|
|
|
fifo->tail);
|
2023-07-21 14:44:24 +00:00
|
|
|
else
|
2023-12-16 17:37:58 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: peek 0x%02x (%d,%d)\n", __func__, c,
|
|
|
|
fifo->head,
|
|
|
|
fifo->tail);
|
|
|
|
|
2023-09-30 20:43:52 +00:00
|
|
|
#endif
|
2023-07-30 12:53:17 +00:00
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
pthread_mutex_unlock(&fifo->mutex);
|
|
|
|
#endif
|
2023-07-31 14:08:34 +00:00
|
|
|
return c;
|
2023-07-17 15:26:47 +00:00
|
|
|
}
|
|
|
|
|
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; }
|
|
|
|
|
2023-07-30 12:53:17 +00:00
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
pthread_mutex_lock(&fifo->mutex);
|
|
|
|
#endif
|
2023-07-07 20:26:36 +00:00
|
|
|
char c = fifo->data[fifo->head];
|
2023-09-30 20:43:52 +00:00
|
|
|
#if 0
|
2023-12-16 17:37:58 +00:00
|
|
|
|
2023-07-24 14:44:40 +00:00
|
|
|
if (isalnum(c))
|
2023-12-16 17:37:58 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: pop %c (%d,%d)\n", __func__, c, fifo->head,
|
|
|
|
fifo->tail);
|
2023-07-21 14:44:24 +00:00
|
|
|
else
|
2023-12-16 17:37:58 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: pop 0x%02x (%d,%d)\n", __func__, c,
|
|
|
|
fifo->head,
|
|
|
|
fifo->tail);
|
|
|
|
|
2023-09-30 20:43:52 +00:00
|
|
|
#endif
|
2023-07-09 21:51:33 +00:00
|
|
|
fifo->head = (fifo->head + 1) % HAMLIB_FIFO_SIZE;
|
2023-07-30 12:53:17 +00:00
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
pthread_mutex_unlock(&fifo->mutex);
|
|
|
|
#endif
|
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
|