read-packet

gabbapeople/simple-byte-protocol/read-packet

Compares the specified "header" byte with the byte received through a UART interface. If a "header" byte is found, read-packet puts the received bytes into a packet of the specified size
read-packet
@/read-packet
Compares the specified "header" byte with the byte received through a UART interface. If a "header" byte is found, read-packet puts the received bytes into a packet of the specified size
IN@/packet
BYTEbyte
An incoming byte
PUSHpulse
Triggers a new "header" match
HEADbyte
A "header" byte value
read-packet
IN
OUT
BYTE
PUSH
HEAD
DONE
DONEpulse
Fires if a "header" is found
OUT@/packet
To use the node in your project you should have the gabbapeople/simple-byte-protocol library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
    uint8_t readCounter = 0;
    bool isHeader = false;
    bool firstTimeHeader = false;
};

{{ GENERATED_CODE }}


void evaluate(Context ctx) {
    auto state = getState(ctx);
    uint8_t head = getValue<input_HEAD>(ctx);

    if(isInputDirty<input_PUSH>(ctx)){

        uint8_t c = getValue<input_BYTE>(ctx);

        if (c == head){
            if(!state->firstTimeHeader){
                state->isHeader = true;
                state->firstTimeHeader = true;
                state->readCounter = 0;
            }
        }

         auto _packet = getValue<input_IN>(ctx);

        _packet.buffer[state->readCounter] = c;
        state->readCounter++;

        if(state->readCounter >= _packet.bufferSize){
            state->readCounter = 0;

            if(state->isHeader){
                emitValue<output_OUT>(ctx, _packet);
                emitValue<output_DONE>(ctx, 1);
                state->isHeader = false;
                state->firstTimeHeader = false;
            }
        }

    }
}