I messed up the merge, so pushing it over to another repo so I don't lose it. Will tidy up and remove later

Dependencies:   BufferedSerial FatFileSystemCpp mbed

Committer:
AndyA
Date:
Fri Jan 15 11:49:01 2021 +0000
Revision:
0:97661408d0f9
Child:
1:dd1f7e162f91
first;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AndyA 0:97661408d0f9 1 #include "LTCDecode.h"
AndyA 0:97661408d0f9 2
AndyA 0:97661408d0f9 3 const float FrameRates[] = {0, 23.976, 24, 25, 29.97, 30,
AndyA 0:97661408d0f9 4 47.95, 48, 50, 59.94, 60, 10000000};
AndyA 0:97661408d0f9 5 const double FramePeriods[] = {1000000, 1000000 / 23.976, 1000000 / 24,
AndyA 0:97661408d0f9 6 1000000 / 25, 1000000 / 29.97, 1000000 / 30,
AndyA 0:97661408d0f9 7 1000000 / 47.95, 1000000 / 48, 1000000 / 50,
AndyA 0:97661408d0f9 8 1000000 / 59.94, 1000000 / 60, 10};
AndyA 0:97661408d0f9 9 const int numberOfRates = 11;
AndyA 0:97661408d0f9 10
AndyA 0:97661408d0f9 11
AndyA 0:97661408d0f9 12 LTCDecode::LTCDecode(const PinName pin) : _LTCIn(pin) {
AndyA 0:97661408d0f9 13 LTCsynced = false;
AndyA 0:97661408d0f9 14 _LTCIn.fall(callback(this, &LTCDecode::LTCOnEdge));
AndyA 0:97661408d0f9 15 _LTCIn.rise(callback(this, &LTCDecode::LTCOnEdge));
AndyA 0:97661408d0f9 16 newBitsWrite = 0;
AndyA 0:97661408d0f9 17 bitCounter = 0;
AndyA 0:97661408d0f9 18 dwordCounter = 0;
AndyA 0:97661408d0f9 19 newBitsRead = 0;
AndyA 0:97661408d0f9 20 markCount = 0;
AndyA 0:97661408d0f9 21
AndyA 0:97661408d0f9 22 }
AndyA 0:97661408d0f9 23
AndyA 0:97661408d0f9 24 void LTCDecode::setInputTimer(Timer *inputTimer) { _inputTimer = inputTimer; }
AndyA 0:97661408d0f9 25
AndyA 0:97661408d0f9 26 void LTCDecode::LTCOnEdge() {
AndyA 0:97661408d0f9 27 uint32_t transitionTime = _inputTimer->read_us();
AndyA 0:97661408d0f9 28 uint32_t period = (transitionTime - lastTransition);
AndyA 0:97661408d0f9 29 lastTransition = transitionTime;
AndyA 0:97661408d0f9 30 if (period >
AndyA 0:97661408d0f9 31 maxMidSymbolTimeUS) { // time since last transition is > maximum time for
AndyA 0:97661408d0f9 32 // a 1, must be a zero and start of the next bit.
AndyA 0:97661408d0f9 33 // printf("Z");
AndyA 0:97661408d0f9 34 bitTimes[newBitsWrite] = lastBitStart;
AndyA 0:97661408d0f9 35 lastBitStart = transitionTime;
AndyA 0:97661408d0f9 36 newBitBuffer[newBitsWrite++] = false;
AndyA 0:97661408d0f9 37 if (newBitsWrite == _newBitBufferSize_)
AndyA 0:97661408d0f9 38 newBitsWrite = 0;
AndyA 0:97661408d0f9 39 return;
AndyA 0:97661408d0f9 40 }
AndyA 0:97661408d0f9 41 // if it's not a 0 it must be a 1 but are we at the end of the bit yet?
AndyA 0:97661408d0f9 42 if ((transitionTime - lastBitStart)> minBitPeriodUS) { // end of a bit
AndyA 0:97661408d0f9 43 // printf("N");
AndyA 0:97661408d0f9 44 bitTimes[newBitsWrite] = lastBitStart;
AndyA 0:97661408d0f9 45 lastBitStart = transitionTime;
AndyA 0:97661408d0f9 46 newBitBuffer[newBitsWrite++] = true;
AndyA 0:97661408d0f9 47 if (newBitsWrite == _newBitBufferSize_)
AndyA 0:97661408d0f9 48 newBitsWrite = 0;
AndyA 0:97661408d0f9 49 return;
AndyA 0:97661408d0f9 50 }
AndyA 0:97661408d0f9 51 }
AndyA 0:97661408d0f9 52
AndyA 0:97661408d0f9 53 bool LTCDecode::searchForSync() {
AndyA 0:97661408d0f9 54 while (!LTCsynced && (newBitsRead != newBitsWrite)) {
AndyA 0:97661408d0f9 55 LTCBuffer[0] = LTCBuffer[0] << 1;
AndyA 0:97661408d0f9 56 if (newBitBuffer[newBitsRead++])
AndyA 0:97661408d0f9 57 LTCBuffer[0] |= 0x01;
AndyA 0:97661408d0f9 58 if (_newBitBufferSize_ == newBitsRead)
AndyA 0:97661408d0f9 59 newBitsRead = 0;
AndyA 0:97661408d0f9 60 if ((LTCBuffer[0] & 0x0000FFFF) == 0x3FFD) {
AndyA 0:97661408d0f9 61 LTCsynced = true;
AndyA 0:97661408d0f9 62 memset(LTCBuffer, 0, 3 * sizeof(uint32_t));
AndyA 0:97661408d0f9 63 }
AndyA 0:97661408d0f9 64 }
AndyA 0:97661408d0f9 65 return LTCsynced;
AndyA 0:97661408d0f9 66 }
AndyA 0:97661408d0f9 67
AndyA 0:97661408d0f9 68 bool LTCDecode::readWaitingData() {
AndyA 0:97661408d0f9 69 while (newBitsRead != newBitsWrite) {
AndyA 0:97661408d0f9 70 timeOfBit = bitTimes[newBitsRead];
AndyA 0:97661408d0f9 71
AndyA 0:97661408d0f9 72 if ((bitCounter == 0) && (dwordCounter == 0))
AndyA 0:97661408d0f9 73 frameStartTime = timeOfBit;
AndyA 0:97661408d0f9 74
AndyA 0:97661408d0f9 75 if (newBitBuffer[newBitsRead++])
AndyA 0:97661408d0f9 76 LTCBuffer[dwordCounter] |= (1 << bitCounter);
AndyA 0:97661408d0f9 77
AndyA 0:97661408d0f9 78 if (_newBitBufferSize_ == newBitsRead)
AndyA 0:97661408d0f9 79 newBitsRead = 0;
AndyA 0:97661408d0f9 80
AndyA 0:97661408d0f9 81 bitCounter++;
AndyA 0:97661408d0f9 82 if (32 == bitCounter) {
AndyA 0:97661408d0f9 83 dwordCounter++;
AndyA 0:97661408d0f9 84 bitCounter = 0;
AndyA 0:97661408d0f9 85 }
AndyA 0:97661408d0f9 86
AndyA 0:97661408d0f9 87 if ((2 == dwordCounter) && (16 == bitCounter)) {
AndyA 0:97661408d0f9 88 bitCounter = 0;
AndyA 0:97661408d0f9 89 dwordCounter = 0;
AndyA 0:97661408d0f9 90 if ((LTCBuffer[2] & 0xFFFF) != 0xBFFC) {
AndyA 0:97661408d0f9 91 LTCsynced = false;
AndyA 0:97661408d0f9 92 return true;
AndyA 0:97661408d0f9 93 } else {
AndyA 0:97661408d0f9 94 lastFrame.frame = LTCBuffer[0] & 0xf;
AndyA 0:97661408d0f9 95 lastFrame.frame += ((LTCBuffer[0] >> 8) & 0x3) * 10;
AndyA 0:97661408d0f9 96 lastFrame.seconds = (LTCBuffer[0] >> 16) & 0xf;
AndyA 0:97661408d0f9 97 lastFrame.seconds += ((LTCBuffer[0] >> 24) & 0x7) * 10;
AndyA 0:97661408d0f9 98 lastFrame.minutes = LTCBuffer[1] & 0xf;
AndyA 0:97661408d0f9 99 lastFrame.minutes += ((LTCBuffer[1] >> 8) & 0x7) * 10;
AndyA 0:97661408d0f9 100 lastFrame.hours = (LTCBuffer[1] >> 16) & 0xf;
AndyA 0:97661408d0f9 101 lastFrame.hours += ((LTCBuffer[1] >> 24) & 0x3) * 10;
AndyA 0:97661408d0f9 102 lastFrame.frameDrop = (LTCBuffer[0] & (1 << 10)) == (1 << 10);
AndyA 0:97661408d0f9 103 lastFrame.frameStartTime = frameStartTime;
AndyA 0:97661408d0f9 104 memset(LTCBuffer, 0, 3 * sizeof(uint32_t));
AndyA 0:97661408d0f9 105 return true;
AndyA 0:97661408d0f9 106 }
AndyA 0:97661408d0f9 107 }
AndyA 0:97661408d0f9 108 }
AndyA 0:97661408d0f9 109 return false;
AndyA 0:97661408d0f9 110 }