Files
@ 01ed20819369
Branch filter:
Location: HydroBot/hydrobot-sharedlibs/src/protocol.c
01ed20819369
5.8 KiB
text/plain
Added send bytes function to protocol
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | #include "protocol.h"
protocol_device_t protocol_device;
flash_settings_t protocol_settings;
void protocol_init(protocol_device_t device)
{
protocol_device = device;
flash_restore(&protocol_settings);
led_set_brightness(protocol_settings.val.led_brightness);
can_init(protocol_settings.val.can_id, DEFAULT_BROADCAST_ID);
}
flash_settings_t* protocol_get_settings(void)
{
return &protocol_settings;
}
void protocol_save_settings(void)
{
flash_save(&protocol_settings);
}
bool protocol_send_test()
{
bool result = true;
can_send_test(protocol_settings.val.can_id | 0x00000001);
return result;
}
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];
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_send_data_bytes(protocol_data_key_t key, uint8_t sensor, uint8_t* data)
{
bool result = true;
protocol_message_t message;
message.command = false;
message.id = protocol_device;
message.key = key;
message.sensor = sensor;
message.data.byte_data[0] = data[0];
message.data.byte_data[1] = data[1];
message.data.byte_data[2] = data[2];
message.data.byte_data[3] = data[3];
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;
}
|