que hace este código?? #include "TiltaSerialProtocol.h"

TiltaSerialProtocol::TiltaSerialProtocol() : m_debug(false)
{
    // Initialize all values to "-1"
    m_numPkt.model = -1;
    m_numPkt.command = -1;
    m_numPkt.param = -1;
    m_numPkt.data = -1;
}

uint8_t     TiltaSerialProtocol::getModel() const
{
    return m_numPkt.model;
}

uint8_t     TiltaSerialProtocol::getCommand() const
{
    return m_numPkt.command;
}

uint16_t    TiltaSerialProtocol::getParam() const
{
    return (m_numPkt.param);
}

uint16_t    TiltaSerialProtocol::getData() const
{
    return (m_numPkt.data);
}

bool TiltaSerialProtocol::parseNewSerialData(char newMessage[], int messageLength)
{
    if(messageLength != RAW_MSG_LENGTH)
    {
        if(m_debug)
        {
            Serial.println("Serial Message length error : " + String(messageLength));
        }
        return false;
    }
    else
    {
        if(newMessage[0] != TITLA_INIT_CHAR)
        {
            if(m_debug)
            {
                Serial.println("Wrong init char : " + String(newMessage[0]));
            }
            return false;
        }
        else if(newMessage[RAW_MSG_LENGTH - 2] != TILTA_CR_CHAR || newMessage[RAW_MSG_LENGTH - 1] != TILTA_LF_CHAR)
        {
            if(m_debug)
            {
                Serial.println("Wrong end chars : " + String(newMessage[RAW_MSG_LENGTH - 2], HEX) + " | " + String(newMessage[RAW_MSG_LENGTH - 1], HEX));
            }
            return false;
        }
        else
        {
            for(int i = 0; i < RAW_MSG_LENGTH - 3; i++)
            {
                m_asciiPkt.raw[i] = newMessage[i+1];
            }

            // Convert the ascii data to its equivalent numerical data
            m_convertAsciiToHex();
            
            // for (int j = 0; j < 7; j++)
            // {
            //     Serial.print(String(m_numPkt.raw[j], HEX) + "|");
            // }
            // Serial.println();

            uint8_t checksum = m_computeChecksum();

            if(checksum != (uint8_t)m_numPkt.checksum)
            {
                if(m_debug)
                {
                    Serial.println("Wrong checksum : " + String(m_numPkt.checksum, HEX) + " vs " + String(checksum, HEX));
                }
                return false;
            }

            return true;
        }
    }
}

uint8_t* TiltaSerialProtocol::encodeNewMessage(uint8_t model, uint8_t command, uint16_t param, uint16_t data)
{
    // Store the data to be sent in the numerical packet
    m_numPkt.model     = model;
    m_numPkt.command   = command;
    m_numPkt.param     = param;
    m_numPkt.data      = data;
    m_numPkt.checksum  = this->m_computeChecksum();

    static uint8_t dataToSend[RAW_MSG_LENGTH];
    dataToSend[0] = TITLA_INIT_CHAR; // First char is ":"
    dataToSend[RAW_MSG_LENGTH - 2] = TILTA_CR_CHAR; // <CR>
    dataToSend[RAW_MSG_LENGTH - 1] = TILTA_LF_CHAR; // <LF>

    // Convert the numeriral packet to the ascii packet
    m_convertHexToAscii();

    for(int i = 0; i < RAW_MSG_LENGTH - 3; i++)
    {
        dataToSend[i + 1] = m_asciiPkt.raw[i];
    }
    
    // Serial.print("New Message : ");
    // for (int j = 0; j < 17; j++)
    // {
    //     Serial.write(dataToSend[j]);
    // }
    // Serial.println();

    return dataToSend;
}

uint8_t* TiltaSerialProtocol::encodeNewFocus(uint16_t data)
{
    return this->encodeNewMessage(TILTA_FOCUS_MODEL, TILTA_COMMAND, TILTA_PARAM, data);
}

uint8_t* TiltaSerialProtocol::encodeNewIris(uint16_t data)
{
    return this->encodeNewMessage(TILTA_IRIS_MODEL, TILTA_COMMAND, TILTA_PARAM, data);
}

uint8_t* TiltaSerialProtocol::encodeNewZoom(uint16_t data)
{
    return this->encodeNewMessage(TILTA_ZOOM_MODEL, TILTA_COMMAND, TILTA_PARAM, data);
}

void TiltaSerialProtocol::setDebug(bool debug)
{
    m_debug = debug;
}

void TiltaSerialProtocol::setModel(uint8_t model)
{
    m_numPkt.model = model;
}

void TiltaSerialProtocol::setCommand(uint8_t command)
{
    m_numPkt.command = command;
}

void TiltaSerialProtocol::setParam(uint16_t param)
{
    m_numPkt.param = param;
}

void TiltaSerialProtocol::setData(uint16_t data)
{
    m_numPkt.data = data;
}

// Calculate the 8-bit checksum using 2's complement
uint8_t TiltaSerialProtocol::m_computeChecksum(uint8_t *data, int len)
{
    int sum = 0x100;

    for (int i = 0; i < len; i++)
    {
        sum -= data[i];
    }
    return (uint8_t)(sum);
}

// Calculate the 8-bit checksum using 2's complement
uint8_t TiltaSerialProtocol::m_computeChecksum()
{
    int sum = 0x100;

    for (int i = 0; i < TOTAL_MSG_BYTES - 1; i++)
    {
        sum -= m_numPkt.raw[i];
    }
    return (uint8_t)(sum);
}

uint16_t TiltaSerialProtocol::m_swapEndian(uint16_t valueToSwap) const
{
    return (valueToSwap << 8) | (valueToSwap >> 8);
}


unsigned TiltaSerialProtocol::m_charToHex(char character) {
    if (character >= '0' && character <= '9')
        return character - '0';
    else if (character >= 'A' && character <= 'F')
        return character - 'A' + 10;
    else if (character >= 'a' && character <= 'f')
        return character - 'a' + 10;
    else
        return 0; // Invalid character
}

unsigned TiltaSerialProtocol::m_charArrayToHex(const char* charArray, size_t size) {
    unsigned hexValue = 0;
    for (size_t i = 0; i < size; ++i) {
        hexValue <<= 4; // Shift left by 4 bits
        hexValue += m_charToHex(charArray[i]);
    }
    return hexValue;
}


void TiltaSerialProtocol::m_convertAsciiToHex()
{
    m_numPkt.model = m_charArrayToHex(m_asciiPkt.model, 2);
    m_numPkt.command = m_charArrayToHex(m_asciiPkt.command, 2);
    m_numPkt.param = m_charArrayToHex(m_asciiPkt.param, 4);
    m_numPkt.data = m_charArrayToHex(m_asciiPkt.data, 4);
    m_numPkt.checksum = m_charArrayToHex(m_asciiPkt.checksum, 2);
}

char TiltaSerialProtocol::m_hexToChar(unsigned hexValue) {
    if (hexValue >= 0 && hexValue <= 9)
        return '0' + hexValue;
    else if (hexValue >= 10 && hexValue <= 15)
        return 'A' + (hexValue - 10);
    else
        return '\0'; // Invalid hexadecimal value
}

void TiltaSerialProtocol::m_hexToCharArray(unsigned hexValue, char* charArray, size_t size) {
    for (size_t i = size; i > 0; --i) {
        charArray[i - 1] = m_hexToChar(hexValue & 0xF); // Extract the last 4 bits
        hexValue >>= 4; // Shift right by 4 bits
    }
}

void TiltaSerialProtocol::m_convertHexToAscii()
{
    char charArray[4];
    m_hexToCharArray(m_numPkt.model, charArray, 2);
    m_asciiPkt.model[0] = charArray[0];
    m_asciiPkt.model[1] = charArray[1];

    m_hexToCharArray(m_numPkt.command, charArray, 2);
    m_asciiPkt.command[0] = charArray[0];
    m_asciiPkt.command[1] = charArray[1];

    m_hexToCharArray(m_numPkt.param, charArray, 4);
    m_asciiPkt.param[0] = charArray[0];
    m_asciiPkt.param[1] = charArray[1];
    m_asciiPkt.param[2] = charArray[2];
    m_asciiPkt.param[3] = charArray[3];

    m_hexToCharArray(m_numPkt.data, charArray, 4);
    m_asciiPkt.data[0] = charArray[0];
    m_asciiPkt.data[1] = charArray[1];
    m_asciiPkt.data[2] = charArray[2];
    m_asciiPkt.data[3] = charArray[3];

    m_hexToCharArray(m_numPkt.checksum, charArray, 2);
    m_asciiPkt.checksum[0] = charArray[0];
    m_asciiPkt.checksum[1] = charArray[1];
}