Changeset - 5b244673cf7f
[Not reviewed]
default
0 2 0
matthewreed - 7 years ago 2017-07-04 21:45:12

Implemented internal get data function and updated protocol definition
2 files changed with 27 insertions and 2 deletions:
0 comments (0 inline, 0 general)
inc/protocol.h
Show inline comments
 
#ifndef _PROTOCOL_H_
 
#define _PROTOCOL_H_
 
 
#include <stdbool.h>
 
 
#include "config.h"
 
#include "can.h"
 
#include "flash.h"
 
 
#define DEFAULT_BROADCAST_ID 0x00000000
 
 
typedef struct {
 
    bool command;
 
    uint8_t id;
 
    uint16_t key;
 
    uint8_t sensor;
 
    union {
 
        float float_data;
 
        uint8_t byte_data[4];
 
    } data;
 
}  protocol_message_t;
 
 
typedef enum {
 
    ESTOP = 0x00,
 
    SILENCE_BUS = 0x01,
 
    SET_OUTPUT = 0X10,
 
    GET_DATA = 0x11,
 
    CONFIG = 0x12,
 
    CALIBRATE = 0x13,
 
} protocol_command_t;
 
 
typedef enum {
 
    MASTER = 0x00,
 
    AIRSENSE = 0x01,
 
    RELAYDRIVE = 0X02,
 
    WATERSENSE = 0x03,
 
    PROTOMODULE = 0x04,
 
} protocol_device_t;
 
 
typedef enum {
 
    NONE = 0x0000,
 
    DIGITAL_INPUT = 0x0001,
 
    FREQ_INPUT = 0x0002,
 
    ANALOG_INPUT = 0x0003,
 
    DIGITAL_OUTPUT = 0x0004,
 
    PWM_OUTPUT = 0x0005,
 
    ANALOG_OUTPUT = 0x0006,
 
    AIR_TEMP = 0x0007,
 
    AIR_HUMIDITY = 0x0008,
 
    AIR_PRESSURE = 0x0009,
 
    AMBIENT_LIGHT = 0x000A,
 
    WATER_TEMP = 0x000B,
 
    WATER_LEVEL = 0x000C,
 
    WATER_CONDUCTIVITY = 0x000D,
 
    WATER_PH = 0x000E,
 
    CAN_ID = 0x0100,
 
    DATA_RATE = 0x0101,
 
    GPIO = 0x0102,
 
    LED_BRIGHTNESS = 0x0103,
 
    LED_BRIGHTNESS = 0x0102,
 
    INPUT = 0x0103,
 
    OUTPUT = 0x0104,
 
} protocol_data_key_t;
 
 
void protocol_init(protocol_device_t device);
 
flash_settings_t* protocol_get_settings(void);
 
bool protocol_receive_message(CanRxMsgTypeDef* can_message);
 
bool protocol_send_message(protocol_message_t* message);
 
bool protocol_process_message(protocol_message_t* message);
 
bool protocol_send_data(protocol_data_key_t key, uint8_t sensor, float data);
 
bool protocol_send_test();
 
bool _protocol_config(protocol_message_t* message);
 
 
bool protocol_estop(bool value);
 
bool protocol_set_output(protocol_message_t* message);
 
bool protocol_get_data(protocol_message_t* message);
 
bool protocol_config(protocol_message_t* message);
 
 
#endif //_PROTOCOL_H_
src/protocol.c
Show inline comments
 
@@ -31,146 +31,170 @@ bool protocol_receive_message(CanRxMsgTy
 
    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];
 
    data[5] = message->data.byte_data[1];
 
    data[6] = message->data.byte_data[2];
 
    data[7] = message->data.byte_data[3];
 
    
 
    can_send(protocol_settings.val.can_id | 0x00000001, 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:
 
                {
 
                    result = _protocol_config(message);
 
                }
 
                break;
 
            default:
 
                result = false;
 
                break;
 
        }
 
    }
 
    
 
    return result;
 
}
 
 
bool protocol_send_data(protocol_data_key_t key, uint8_t sensor, float data)
 
{
 
    bool result = true;
 
 
    protocol_message_t message;
 
    message.command = false;
 
    message.id = protocol_device;
 
    message.key = key;
 
    message.sensor = sensor;
 
    message.data.float_data = data;
 
 
    result = protocol_send_message(&message);
 
 
    return result;
 
}
 
 
bool _protocol_get_data(protocol_message_t* message)
 
{
 
    bool result = false;
 
 
    if (message->key == CAN_ID)
 
    {
 
        protocol_send_data(message->key, message->sensor, protocol_settings.val.can_id);
 
    }
 
    else if (message->key == DATA_RATE)
 
    {
 
        protocol_send_data(message->key, message->sensor, ((float)protocol_settings.val.data_rate)/1000);
 
    }
 
    else if (message->key == LED_BRIGHTNESS)
 
    {
 
        protocol_send_data(message->key, message->sensor, protocol_settings.val.led_brightness);
 
    }
 
    else
 
    {
 
        //call config weak function
 
        result = protocol_get_data(message);
 
    }
 
    return result;
 
}
 
 
bool _protocol_config(protocol_message_t* message)
 
{
 
    bool result = false;
 
 
    if (message->key == CAN_ID)
 
    {
 
        uint32_t can_id = (uint16_t) message->data.float_data;
 
        protocol_settings.val.can_id = can_id;
 
        flash_save(&protocol_settings);
 
    }
 
    else if (message->key == DATA_RATE)
 
    {
 
        uint16_t data_rate = (uint16_t) message->data.float_data;
 
        protocol_settings.val.data_rate = data_rate * 1000;
 
        flash_save(&protocol_settings);
 
    }
 
    else if (message->key == LED_BRIGHTNESS)
 
    {
 
        uint8_t brightness = (uint8_t)message->data.float_data;
 
        result = led_set_brightness(brightness);
 
        protocol_settings.val.led_brightness = brightness;
 
        flash_save(&protocol_settings);
 
    }
 
    else
 
    {
 
        //call config weak function
 
        result = protocol_config(message);
 
    }
 
    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;
 
}
0 comments (0 inline, 0 general)