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