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

LTCDecode.cpp

Committer:
AndyA
Date:
2021-01-15
Revision:
1:dd1f7e162f91
Parent:
0:97661408d0f9
Child:
9:7214e3c3e5f8

File content as of revision 1:dd1f7e162f91:

#include "LTCDecode.h"

const float FrameRates[] = {0,     23.976, 24, 25,    29.97, 30,
                            47.95, 48,     50, 59.94, 60,    10000000
                           };
const double FramePeriods[] = {1000000,         1000000 / 23.976, 1000000 / 24,
                               1000000 / 25,    1000000 / 29.97,  1000000 / 30,
                               1000000 / 47.95, 1000000 / 48,     1000000 / 50,
                               1000000 / 59.94, 1000000 / 60,     10
                              };
const int numberOfRates = 11;


LTCDecode::LTCDecode(const PinName pin) : _LTCIn(pin)
{
    LTCsynced = false;
    newBitsWrite = 0;
    bitCounter = 0;
    dwordCounter = 0;
    newBitsRead = 0;
    markCount = 0;

}

void LTCDecode::setInputTimer(Timer *inputTimer)
{
    _inputTimer = inputTimer;
    _LTCIn.fall(callback(this, &LTCDecode::LTCOnEdge));
    _LTCIn.rise(callback(this, &LTCDecode::LTCOnEdge));
}

void LTCDecode::LTCOnEdge()
{
    uint32_t transitionTime = _inputTimer->read_us();
    uint32_t period = (transitionTime - lastTransition);
    lastTransition = transitionTime;
    if (period >
            maxMidSymbolTimeUS) { // time since last transition is > maximum time for
        // a 1, must be a zero and start of the next bit.
        //        printf("Z");
        bitTimes[newBitsWrite] = lastBitStart;
        lastBitStart = transitionTime;
        newBitBuffer[newBitsWrite++] = false;
        if (newBitsWrite == _newBitBufferSize_)
            newBitsWrite = 0;
        return;
    }
    // if it's not a 0 it must be a 1 but are we at the end of the bit yet?
    if ((transitionTime - lastBitStart)> minBitPeriodUS) { // end of a bit
        //        printf("N");
        bitTimes[newBitsWrite] = lastBitStart;
        lastBitStart = transitionTime;
        newBitBuffer[newBitsWrite++] = true;
        if (newBitsWrite == _newBitBufferSize_)
            newBitsWrite = 0;
        return;
    }
}

bool LTCDecode::searchForSync()
{
    while (!LTCsynced && (newBitsRead != newBitsWrite)) {
        LTCBuffer[0] = LTCBuffer[0] << 1;
        if (newBitBuffer[newBitsRead++])
            LTCBuffer[0] |= 0x01;
        if (_newBitBufferSize_ == newBitsRead)
            newBitsRead = 0;
        if ((LTCBuffer[0] & 0x0000FFFF) == 0x3FFD) {
            LTCsynced = true;
            memset(LTCBuffer, 0, 3 * sizeof(uint32_t));
        }
    }
    return LTCsynced;
}

bool LTCDecode::readWaitingData()
{
    while (newBitsRead != newBitsWrite) {
        timeOfBit = bitTimes[newBitsRead];

        if ((bitCounter == 0) && (dwordCounter == 0))
            frameStartTime = timeOfBit;

        if (newBitBuffer[newBitsRead++])
            LTCBuffer[dwordCounter] |= (1 << bitCounter);

        if (_newBitBufferSize_ == newBitsRead)
            newBitsRead = 0;

        bitCounter++;
        if (32 == bitCounter) {
            dwordCounter++;
            bitCounter = 0;
        }

        if ((2 == dwordCounter) && (16 == bitCounter)) {
            bitCounter = 0;
            dwordCounter = 0;
            if ((LTCBuffer[2] & 0xFFFF) != 0xBFFC) {
                LTCsynced = false;
                return true;
            } else {
                lastFrame.frame = LTCBuffer[0] & 0xf;
                lastFrame.frame += ((LTCBuffer[0] >> 8) & 0x3) * 10;
                lastFrame.seconds = (LTCBuffer[0] >> 16) & 0xf;
                lastFrame.seconds += ((LTCBuffer[0] >> 24) & 0x7) * 10;
                lastFrame.minutes = LTCBuffer[1] & 0xf;
                lastFrame.minutes += ((LTCBuffer[1] >> 8) & 0x7) * 10;
                lastFrame.hours = (LTCBuffer[1] >> 16) & 0xf;
                lastFrame.hours += ((LTCBuffer[1] >> 24) & 0x3) * 10;
                lastFrame.frameDrop = (LTCBuffer[0] & (1 << 10)) == (1 << 10);
                lastFrame.frameStartTime = frameStartTime;
                memset(LTCBuffer, 0, 3 * sizeof(uint32_t));
                return true;
            }
        }
    }
    return false;
}