Skip to content

Instantly share code, notes, and snippets.

@jhgorse
Created December 19, 2019 20:21
Show Gist options
  • Save jhgorse/c14b5dd2c27b3bdf14cd902dbd69e363 to your computer and use it in GitHub Desktop.
Save jhgorse/c14b5dd2c27b3bdf14cd902dbd69e363 to your computer and use it in GitHub Desktop.
fifo fum
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <limits.h>
#include "comms_layer.h"
#define MAX_BUFFER_LENGTH 2 //135
#define MAX_TX_FIFO_LENGTH (MAX_BUFFER_LENGTH * 3)
static char tx_fifo_buffer[MAX_TX_FIFO_LENGTH]; // Triple buffer
static int tx_fifo_head = 0;
static int tx_fifo_tail = 0;
static void tx_putc(char);
static char tx_getc(void);
static void tx_putc(char ch) {
tx_fifo_buffer[tx_fifo_tail] = ch;
tx_fifo_tail = (tx_fifo_tail + 1) % MAX_TX_FIFO_LENGTH; // increment and wrap
// TODO: check if fifo_tail == fifo_head
}
static char tx_getc(void) {
char ret = tx_fifo_buffer[tx_fifo_head];
tx_fifo_head = (tx_fifo_head + 1) % MAX_TX_FIFO_LENGTH;
return ret;
}
int main(int argc, char *argv[]) {
char packet[] = {5, 10, 1, 2, 3, 4, 0}; // 7 bytes
printf("hello world, %lu %lu\n", sizeof(packet), strlen(packet)); // 7 6
for (int i=0; i<sizeof(packet); i++) {
printf("%02x ", packet[i]);
tx_putc(packet[i]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment