Simple ring buffer

Committer:
jont
Date:
Mon Jul 13 07:54:11 2015 +0000
Revision:
0:c050eb7b0c10
First pre-release version without documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jont 0:c050eb7b0c10 1 #include "mbed.h"
jont 0:c050eb7b0c10 2 #include "record_ring.h"
jont 0:c050eb7b0c10 3 /*
jont 0:c050eb7b0c10 4 J.J.Trinder, based on original code for midi projects 199X
jont 0:c050eb7b0c10 5 updateded sometime to the MBED
jont 0:c050eb7b0c10 6 Yeh its atad klunky, it works and is useful to explain things to people...
jont 0:c050eb7b0c10 7 */
jont 0:c050eb7b0c10 8 /**
jont 0:c050eb7b0c10 9 Ring Buffer init
jont 0:c050eb7b0c10 10 */
jont 0:c050eb7b0c10 11 JRecordRing::JRecordRing()
jont 0:c050eb7b0c10 12 {
jont 0:c050eb7b0c10 13 int bufsize = buff_size;
jont 0:c050eb7b0c10 14 buf = new log_record [bufsize+1];
jont 0:c050eb7b0c10 15 // sp = ep = buf;
jont 0:c050eb7b0c10 16 memset(buf,0,bufsize);
jont 0:c050eb7b0c10 17 }
jont 0:c050eb7b0c10 18
jont 0:c050eb7b0c10 19 JRecordRing::~JRecordRing()
jont 0:c050eb7b0c10 20 {
jont 0:c050eb7b0c10 21 //not convinced this cleans up properly
jont 0:c050eb7b0c10 22 delete [] buf;
jont 0:c050eb7b0c10 23 }
jont 0:c050eb7b0c10 24 /*======================================================================*/
jont 0:c050eb7b0c10 25 /* RingWriteToBuffer */
jont 0:c050eb7b0c10 26 /*======================================================================*/
jont 0:c050eb7b0c10 27 //add critical section ie disable interrupts while fiddling with buffers
jont 0:c050eb7b0c10 28 /** Write a record of data to buffer
jont 0:c050eb7b0c10 29 */
jont 0:c050eb7b0c10 30 int JRecordRing::RingWriteToBuffer(log_record n)
jont 0:c050eb7b0c10 31 {
jont 0:c050eb7b0c10 32 // printf("WriteInBuffer%d",inCount);
jont 0:c050eb7b0c10 33 __disable_irq(); // Disable Interrupts
jont 0:c050eb7b0c10 34
jont 0:c050eb7b0c10 35 // do something that can't be interrupted
jont 0:c050eb7b0c10 36 opBuffer[inIndex].timestamp = n.timestamp;
jont 0:c050eb7b0c10 37 opBuffer[inIndex].ad1 = n.ad1;
jont 0:c050eb7b0c10 38 opBuffer[inIndex].ad2 = n.ad2;
jont 0:c050eb7b0c10 39 opBuffer[inIndex].ad3 = n.ad3;
jont 0:c050eb7b0c10 40 opBuffer[inIndex].ad4 = n.ad4;
jont 0:c050eb7b0c10 41 opBuffer[inIndex].ad5 = n.ad5;
jont 0:c050eb7b0c10 42 opBuffer[inIndex].ad6 = n.ad6;
jont 0:c050eb7b0c10 43 opBuffer[inIndex].count = n.count;
jont 0:c050eb7b0c10 44 opBuffer[inIndex].count2 = n.count2;
jont 0:c050eb7b0c10 45 opBuffer[inIndex].record_type = n.record_type;
jont 0:c050eb7b0c10 46 opBuffer[inIndex].temperature = n.temperature;
jont 0:c050eb7b0c10 47 inCount++;
jont 0:c050eb7b0c10 48 inIndex++;
jont 0:c050eb7b0c10 49 if (inIndex >= buff_size)
jont 0:c050eb7b0c10 50 inIndex = 0;
jont 0:c050eb7b0c10 51 __enable_irq(); // Enable Interrupts
jont 0:c050eb7b0c10 52 //printf("ZWriteInBuffer%d\n",inCount);
jont 0:c050eb7b0c10 53 return 0;
jont 0:c050eb7b0c10 54 }
jont 0:c050eb7b0c10 55
jont 0:c050eb7b0c10 56
jont 0:c050eb7b0c10 57 /*======================================================================*/
jont 0:c050eb7b0c10 58 /* Output whats remaining.... */
jont 0:c050eb7b0c10 59 /*======================================================================*/
jont 0:c050eb7b0c10 60 long JRecordRing::get_next_record(log_record * lr)
jont 0:c050eb7b0c10 61 {
jont 0:c050eb7b0c10 62
jont 0:c050eb7b0c10 63 // long toSend;
jont 0:c050eb7b0c10 64 #ifdef _PC
jont 0:c050eb7b0c10 65 printf("\nCall in %x out %x count %x",inIndex,outIndex, inCount);
jont 0:c050eb7b0c10 66 #endif
jont 0:c050eb7b0c10 67 if (inCount == 0)
jont 0:c050eb7b0c10 68 return -1; /* nowt to send */
jont 0:c050eb7b0c10 69 __disable_irq(); // Disable Interrupts
jont 0:c050eb7b0c10 70 //lr. = opBuffer[outIndex];
jont 0:c050eb7b0c10 71 lr->timestamp = opBuffer[outIndex].timestamp;
jont 0:c050eb7b0c10 72 lr->ad1 = opBuffer[outIndex].ad1;
jont 0:c050eb7b0c10 73 lr->ad2 = opBuffer[outIndex].ad2;
jont 0:c050eb7b0c10 74 lr->ad3 = opBuffer[outIndex].ad3;
jont 0:c050eb7b0c10 75 lr->ad4 = opBuffer[outIndex].ad4;
jont 0:c050eb7b0c10 76 lr->ad5 = opBuffer[outIndex].ad5;
jont 0:c050eb7b0c10 77 lr->ad6 = opBuffer[outIndex].ad6;
jont 0:c050eb7b0c10 78 lr->count = opBuffer[outIndex].count;
jont 0:c050eb7b0c10 79 lr->count2 = opBuffer[outIndex].count2;
jont 0:c050eb7b0c10 80 lr->temperature = opBuffer[outIndex].temperature;
jont 0:c050eb7b0c10 81 lr->record_type = opBuffer[outIndex].record_type;
jont 0:c050eb7b0c10 82
jont 0:c050eb7b0c10 83 #ifdef _PC
jont 0:c050eb7b0c10 84 printf(" <%s> ",lr->count);
jont 0:c050eb7b0c10 85 #endif
jont 0:c050eb7b0c10 86 inCount--;
jont 0:c050eb7b0c10 87 outIndex++;
jont 0:c050eb7b0c10 88 if (outIndex >=buff_size)
jont 0:c050eb7b0c10 89 outIndex = 0;
jont 0:c050eb7b0c10 90 __enable_irq(); // Enable Interrupts
jont 0:c050eb7b0c10 91 return 0; //check what we should return as now record in the arg
jont 0:c050eb7b0c10 92 }