Simple ring buffer

Files at this revision

API Documentation at this revision

Comitter:
jont
Date:
Mon Jul 13 07:54:11 2015 +0000
Commit message:
First pre-release version without documentation

Changed in this revision

nine_record_ring/record_ring.cpp Show annotated file Show diff for this revision Revisions of this file
nine_record_ring/record_ring.h Show annotated file Show diff for this revision Revisions of this file
nine_ring/nine_ring.cpp Show annotated file Show diff for this revision Revisions of this file
nine_ring/nine_ring.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r c050eb7b0c10 nine_record_ring/record_ring.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nine_record_ring/record_ring.cpp	Mon Jul 13 07:54:11 2015 +0000
@@ -0,0 +1,92 @@
+#include "mbed.h"
+#include "record_ring.h"
+/*
+J.J.Trinder, based on original code for midi projects 199X
+updateded sometime to the MBED
+Yeh its atad klunky, it works and is useful to explain things to people...
+*/
+/**
+Ring Buffer init
+*/
+JRecordRing::JRecordRing()
+{
+    int bufsize = buff_size;
+    buf = new log_record [bufsize+1];
+    // sp = ep = buf;
+    memset(buf,0,bufsize);
+}
+
+JRecordRing::~JRecordRing()
+{
+//not convinced this cleans up properly
+    delete [] buf;
+}
+/*======================================================================*/
+/* RingWriteToBuffer                     */
+/*======================================================================*/
+//add critical section ie disable interrupts while fiddling with buffers
+/** Write a record of data to buffer
+*/
+int JRecordRing::RingWriteToBuffer(log_record n)
+{
+    // printf("WriteInBuffer%d",inCount);
+    __disable_irq();    // Disable Interrupts
+
+// do something that can't be interrupted
+    opBuffer[inIndex].timestamp = n.timestamp;
+    opBuffer[inIndex].ad1 = n.ad1;
+    opBuffer[inIndex].ad2 = n.ad2;
+    opBuffer[inIndex].ad3 = n.ad3;
+    opBuffer[inIndex].ad4 = n.ad4;
+    opBuffer[inIndex].ad5 = n.ad5;
+    opBuffer[inIndex].ad6 = n.ad6;
+    opBuffer[inIndex].count = n.count;
+    opBuffer[inIndex].count2 = n.count2;
+    opBuffer[inIndex].record_type = n.record_type;
+    opBuffer[inIndex].temperature = n.temperature;
+    inCount++;
+    inIndex++;
+    if (inIndex >= buff_size)
+        inIndex = 0;
+    __enable_irq();     // Enable Interrupts
+    //printf("ZWriteInBuffer%d\n",inCount);
+    return 0;
+}
+
+
+/*======================================================================*/
+/* Output whats remaining....            */
+/*======================================================================*/
+long JRecordRing::get_next_record(log_record * lr)
+{
+
+    //  long toSend;
+#ifdef _PC
+    printf("\nCall in %x out %x count %x",inIndex,outIndex, inCount);
+#endif
+    if (inCount == 0)
+        return -1;     /* nowt to send */
+    __disable_irq();    // Disable Interrupts
+    //lr. = opBuffer[outIndex];
+    lr->timestamp = opBuffer[outIndex].timestamp;
+    lr->ad1 =     opBuffer[outIndex].ad1;
+    lr->ad2 =     opBuffer[outIndex].ad2;
+    lr->ad3 =     opBuffer[outIndex].ad3;
+    lr->ad4 =     opBuffer[outIndex].ad4;
+    lr->ad5 =     opBuffer[outIndex].ad5;
+    lr->ad6 =     opBuffer[outIndex].ad6;
+    lr->count =   opBuffer[outIndex].count;
+    lr->count2 =  opBuffer[outIndex].count2;
+    lr->temperature =  opBuffer[outIndex].temperature;
+    lr->record_type =  opBuffer[outIndex].record_type;
+
+#ifdef _PC
+    printf(" <%s> ",lr->count);
+#endif
+    inCount--;
+    outIndex++;
+    if (outIndex >=buff_size)
+        outIndex = 0;
+    __enable_irq();     // Enable Interrupts
+    return 0; //check what we should return as now record in the arg
+}
\ No newline at end of file
diff -r 000000000000 -r c050eb7b0c10 nine_record_ring/record_ring.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nine_record_ring/record_ring.h	Mon Jul 13 07:54:11 2015 +0000
@@ -0,0 +1,54 @@
+#ifndef _RECORD_RING
+#define _RECORD_RING
+#include "nine_ring.h"
+ 
+/**
+Ring Buffer using records of data rather than simple values
+J.J.Trinder, based on original code for midi projects 199X
+updateded sometime to the MBED
+
+Its a ring buffer for records of data
+Yeh its atad klunky, it works and is useful to explain things to people...
+*/
+
+/*======================================================================*/
+/* Data structures to be used                       */
+/*======================================================================*/
+
+/*
+At the moment an aribtry collection of mostly float values.
+
+If you want different, then fork the library and modify as you see fit.
+
+*/
+typedef struct
+{
+  //  long sampleID;
+    long timestamp; // time stamp
+    float ad1;
+    float ad2;
+    float ad3;
+    float ad4;
+    float ad5;
+    float ad6;
+    int count;
+    int count2;
+    float temperature;
+    char record_type; //J for journal E for event etc
+
+}log_record;
+
+class JRecordRing :  public  NRing {
+public:
+  /** Create JRecordRing instance */
+        JRecordRing();
+        ~JRecordRing();
+        int RingWriteToBuffer(log_record );
+        long get_next_record(log_record*);
+protected:
+log_record opBuffer[buff_size];    /* our Ring out buffer  */
+log_record * buf;
+}; 
+
+
+#endif
diff -r 000000000000 -r c050eb7b0c10 nine_ring/nine_ring.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nine_ring/nine_ring.cpp	Mon Jul 13 07:54:11 2015 +0000
@@ -0,0 +1,94 @@
+#include "mbed.h"
+#include "nine_ring.h"
+/*
+J.J.Trinder, based on original code for midi projects 199X
+updateded sometime to the MBED
+
+Yeh its atad klunky, it works and is useful to explain things to people...
+*/
+#define _PC
+NRing::NRing()
+{
+   ring_init();
+}
+
+
+
+/*======================================================================*/
+/* implementation of Ringbuffer                     */
+/*======================================================================*/
+ /** Init the indexes
+        
+*/
+void NRing::ring_init(void)
+{
+    inIndex = 0;
+    inCount = 0;
+}
+
+/*======================================================================*/
+/* RingWriteToBuffer                     */
+/*======================================================================*/
+/**
+Write value (long) into the ring buffer
+@param <n> A long value to store in the ring buffer
+*/
+//add critical section ie disable interrupts while fiddling with buffers
+int NRing::RingWriteToBuffer(long n)
+{
+   // printf("WriteInBuffer%d",inCount);
+__disable_irq();    // Disable Interrupts
+ 
+// do something that can't be interrupted
+   opBuffer[inIndex] = n;
+    inCount++;
+    inIndex++;
+    if (inIndex >= buff_size)
+        inIndex = 0;
+   
+ 
+__enable_irq();     // Enable Interrupts 
+  //printf("ZWriteInBuffer%d\n",inCount);       
+    return 0;
+}
+
+
+//todo fix this as we dont want to do this
+/*======================================================================*/
+/* return current count of stuff in buffer           */
+/*======================================================================*/
+int NRing::ring_count()
+{
+   // printf("InCount %d",inCount);
+    return (inCount);
+}
+
+
+//todo fix this as we dont want to do this
+/*======================================================================*/
+/* Output whats remaining....            */
+/*======================================================================*/
+long NRing::get_next()
+{
+   
+    long toSend;
+#ifdef _PC
+    printf("\nCall in %x out %x count %x",inIndex,outIndex, inCount);
+#endif
+    if (inCount == 0)
+        return -1;     /* nowt to send */
+    __disable_irq();    // Disable Interrupts    
+    toSend = opBuffer[outIndex];
+
+#ifdef _PC
+    printf(" <%i> ",toSend);
+#endif
+ 
+    inCount--;
+    outIndex++;
+    if (outIndex >=buff_size)
+        outIndex = 0;
+    __enable_irq();     // Enable Interrupts       
+    return toSend;
+}
+
diff -r 000000000000 -r c050eb7b0c10 nine_ring/nine_ring.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nine_ring/nine_ring.h	Mon Jul 13 07:54:11 2015 +0000
@@ -0,0 +1,31 @@
+#ifndef NL_RING
+#define NL_RING
+
+/**
+
+Simple Ring Buffer
+J.J.Trinder, based on original code for midi projects 199X
+updateded sometime to the MBED
+
+Yeh its atad klunky, it works and is useful to explain things to people...
+*/
+#define buff_size 120
+class NRing {
+public:
+        NRing();    
+        
+        void ring_init();
+        int ring_count();
+        long get_next();
+        int RingWriteToBuffer(long);        /* put byte in buffer       */ 
+        
+ protected:          
+        volatile int inIndex; /* where to put next byte           */
+        volatile int inCount; /* how full is the buffer           */
+        volatile int outIndex;   /* for output to sio             */
+        
+
+  long opBuffer[buff_size];    /* our Ring out buffer  */
+}; 
+
+#endif
\ No newline at end of file