src/slcan_encode.c
/**
 * 
 * This decodes a data packet
 * 
 * @param buf    The buffer to use
 * @param length The length of the buffer
 * @param frame  The frame struct to put the data into
 * 
 * @return The number of bytes encoded
 */
This decodes a data packet

@param buf    The buffer to use
@param length The length of the buffer
@param frame  The frame struct to put the data into

@return The number of bytes encoded
This decodes a data packet @param buf The buffer to use @param length The length of the buffer @param frame The frame struct to put the data into @return The number of bytes encoded
uint16_t encodeSLCANFrame(uint8_t *buf, uint8_t length, SLCANFrame *frame)
{
    uint8_t index = 0;
    uint8_t i;
    uint32_t id;
    if (frame->rtr) {
        if (frame->ext) {
            buf[index++] = 'R';
        } else {
            buf[index++] = 'r';
        }
    } else {
        if (frame->ext) {
            buf[index++] = 'T';
        } else {
            buf[index++] = 't';
        }
    }
    if (frame->ext) {
        id = frame->id & SLCAN_EXT_ID_MASK;
        for (i = 0; i < 8; i++) {
            buf[index + 7 - i] = encodeDigit(id);
            id >>= 4;
        }
        index += 8;
    } else {
        id = frame->id & SLCAN_ID_MASK;
        for (i = 0; i < 3; i++) {
            buf[index + 2 - i] = encodeDigit(id);
            id >>= 4;
        }
        index += 3;
    }
    buf[index++] = encodeDigit(frame->length & 0xF);
    if (!frame->rtr) {
        for (i = 0; i < frame->length; i++) {
            buf[index++] = encodeDigit(frame->data[i] >> 4);
            buf[index++] = encodeDigit(frame->data[i]);
        }
    }
    buf[index++] = '\r';
    return index;
}
uint16_t encodeSLCANFrame ( uint8_t * buf , uint8_t length , SLCANFrame * frame ) { uint8_t index = 0 ; uint8_t i ; uint32_t id ; if ( frame -> rtr ) { if ( frame -> ext ) { buf [ index ++ ] = ' ' ; } else { buf [ index ++ ] = ' ' ; } } else { if ( frame -> ext ) { buf [ index ++ ] = ' ' ; } else { buf [ index ++ ] = ' ' ; } } if ( frame -> ext ) { id = frame -> id & SLCAN_EXT_ID_MASK ; for ( i = 0 ; i < 8 ; i ++ ) { buf [ index + 7 - i ] = encodeDigit ( id ) ; id >>= 4 ; } index += 8 ; } else { id = frame -> id & SLCAN_ID_MASK ; for ( i = 0 ; i < 3 ; i ++ ) { buf [ index + 2 - i ] = encodeDigit ( id ) ; id >>= 4 ; } index += 3 ; } buf [ index ++ ] = encodeDigit ( frame -> length & 0xF ) ; if ( ! frame -> rtr ) { for ( i = 0 ; i < frame -> length ; i ++ ) { buf [ index ++ ] = encodeDigit ( frame -> data [ i ] >> 4 ) ; buf [ index ++ ] = encodeDigit ( frame -> data [ i ] ) ; } } buf [ index ++ ] = ' \r ' ; return index ; }
This decodes a data packet
This decodes a data packet