Files @ f4e15fde3799
Branch filter:

Location: HydroBot/hydrobot-sharedlibs/src/protocol.c

matthewreed
Moved can interface into shared libs
#include "protocol.h"


bool protocol_receive_message(CanRxMsgTypeDef* can_message)
{
    bool result = true;
    
    protocol_message_t message;
    message.command = can_message->Data[0] & 0x80;
    message.id = can_message->Data[0] & 0x7F;
    message.key = (can_message->Data[1] << 8) & can_message->Data[2];
    message.sensor = can_message->Data[3];
    message.data.byte_data[0] = can_message->Data[4];
    message.data.byte_data[1] = can_message->Data[5];
    message.data.byte_data[2] = can_message->Data[6];
    message.data.byte_data[3] = can_message->Data[7];
    
    protocol_process_message(&message);
    
    return result;
}

bool protocol_send_message(protocol_message_t* message)
{
    bool result = true;
    uint8_t data[8];
    
    data[0] = (message->command & 0x80) & (message->id & 0x7F);
    data[1] = (message->key >> 8) & 0xFF;
    data[2] = (message->key >> 0) & 0xFF;
    data[3] = message->sensor;
    data[4] = (message->data.byte_data[0] >> 24) & 0xFF;
    data[5] = (message->data.byte_data[1] >> 16) & 0xFF;
    data[6] = (message->data.byte_data[2] >> 8) & 0xFF;
    data[7] = (message->data.byte_data[3] >> 0) & 0xFF;
    
    can_send(CAN_SEND_ID, CAN_ID_STD, 8, data);
    
    return result;
}

bool protocol_process_message(protocol_message_t* message)
{
    bool result = true;
    
    if (message->command)
    {
        switch(message->id)
        {
            case ESTOP:
                {
                    //call estop weak function
                    result = protocol_estop(message->data.float_data!=0.0f);
                }
                break;
            case SILENCE_BUS:
                {
                    //silence can bus
                    result = can_silence_bus(message->data.float_data!=0.0f);
                }
                break;
            case SET_OUTPUT:
                {
                    //call set output weak function
                    result = protocol_set_output(message);
                }
                break;
            case GET_DATA:
                {
                    //call get data weak function
                    result = protocol_get_data(message);
                }
                break;
            case CONFIG:
                {
                    //call config weak function
                    result = protocol_config(message);
                }
                break;
        }
    }
    
    return result;
}

__weak bool protocol_estop(bool value)
{
    return false;
}

__weak bool protocol_set_output(protocol_message_t* message)
{
    return false;
}

__weak bool protocol_get_data(protocol_message_t* message)
{
    return false;
}

__weak bool protocol_config(protocol_message_t* message)
{
    return false;
}