Framework for reading and writing variables in real time on any MBED platform.
DistantIO
This is the C implementation of the DistantIO slave framework.
Library is working but slight API breaks may occur in the future. C++ version is also in development.
To get the master-side implementation, see https://github.com/Overdrivr/DistantIO
distantio.cpp@0:c4676d32d381, 2015-09-15 (annotated)
- Committer:
- Overdrivr
- Date:
- Tue Sep 15 15:27:55 2015 +0000
- Revision:
- 0:c4676d32d381
- Child:
- 1:aaffeb93f99b
Version not bug free (especially names should not be longer thant 8 characters or have spaces), but pretty reliable behavior. See github repo for tickets
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Overdrivr | 0:c4676d32d381 | 1 | /* |
Overdrivr | 0:c4676d32d381 | 2 | * distantio.c |
Overdrivr | 0:c4676d32d381 | 3 | * |
Overdrivr | 0:c4676d32d381 | 4 | * Created on: Oct 13, 2014 |
Overdrivr | 0:c4676d32d381 | 5 | * Author: B48923 |
Overdrivr | 0:c4676d32d381 | 6 | */ |
Overdrivr | 0:c4676d32d381 | 7 | |
Overdrivr | 0:c4676d32d381 | 8 | #include "distantio.h" |
Overdrivr | 0:c4676d32d381 | 9 | #include "crc.h" |
Overdrivr | 0:c4676d32d381 | 10 | #include "string.h" |
Overdrivr | 0:c4676d32d381 | 11 | #include "protocol.h" |
Overdrivr | 0:c4676d32d381 | 12 | |
Overdrivr | 0:c4676d32d381 | 13 | /* |
Overdrivr | 0:c4676d32d381 | 14 | * WARNING : IMPLEMENTATION FOR LITTLE-ENDIAN PROCESSOR |
Overdrivr | 0:c4676d32d381 | 15 | * TODO : HANDLE BOTH |
Overdrivr | 0:c4676d32d381 | 16 | */ |
Overdrivr | 0:c4676d32d381 | 17 | |
Overdrivr | 0:c4676d32d381 | 18 | static log Log; |
Overdrivr | 0:c4676d32d381 | 19 | uint32_t tmp; |
Overdrivr | 0:c4676d32d381 | 20 | |
Overdrivr | 0:c4676d32d381 | 21 | void send_variable(uint16_t index); |
Overdrivr | 0:c4676d32d381 | 22 | uint16_t get_size(dio_type type); |
Overdrivr | 0:c4676d32d381 | 23 | void send_descriptor(uint16_t index); |
Overdrivr | 0:c4676d32d381 | 24 | void send_group_descriptor(uint16_t index); |
Overdrivr | 0:c4676d32d381 | 25 | |
Overdrivr | 0:c4676d32d381 | 26 | /** |
Overdrivr | 0:c4676d32d381 | 27 | * Inits the distant io framework |
Overdrivr | 0:c4676d32d381 | 28 | */ |
Overdrivr | 0:c4676d32d381 | 29 | void init_distantio() |
Overdrivr | 0:c4676d32d381 | 30 | { |
Overdrivr | 0:c4676d32d381 | 31 | uint16_t i; |
Overdrivr | 0:c4676d32d381 | 32 | char default_name[] = {"undef. "}; |
Overdrivr | 0:c4676d32d381 | 33 | Log.amount = 0; |
Overdrivr | 0:c4676d32d381 | 34 | for(i = 0 ; i < VARIABLES_AMOUNT ; i++) |
Overdrivr | 0:c4676d32d381 | 35 | { |
Overdrivr | 0:c4676d32d381 | 36 | Log.variables[i].size = 0; |
Overdrivr | 0:c4676d32d381 | 37 | Log.variables[i].ptr = 0; |
Overdrivr | 0:c4676d32d381 | 38 | Log.variables[i].writeable = 0; |
Overdrivr | 0:c4676d32d381 | 39 | Log.variables[i].id = i; |
Overdrivr | 0:c4676d32d381 | 40 | strcpy(Log.variables[i].name,default_name); |
Overdrivr | 0:c4676d32d381 | 41 | Log.variables[i].send = 0; |
Overdrivr | 0:c4676d32d381 | 42 | Log.variables[i].groupID = 0; |
Overdrivr | 0:c4676d32d381 | 43 | } |
Overdrivr | 0:c4676d32d381 | 44 | tmp=0; |
Overdrivr | 0:c4676d32d381 | 45 | Log.current_group_id = 0; |
Overdrivr | 0:c4676d32d381 | 46 | strcpy(Log.groups[0].name,"default"); |
Overdrivr | 0:c4676d32d381 | 47 | } |
Overdrivr | 0:c4676d32d381 | 48 | |
Overdrivr | 0:c4676d32d381 | 49 | /** |
Overdrivr | 0:c4676d32d381 | 50 | * Register a variable exchanged with the computer |
Overdrivr | 0:c4676d32d381 | 51 | */ |
Overdrivr | 0:c4676d32d381 | 52 | uint8_t register_var(void* ptr, uint16_t size, dio_type type, uint8_t writeable, char* name) |
Overdrivr | 0:c4676d32d381 | 53 | { |
Overdrivr | 0:c4676d32d381 | 54 | // Too many variables, aborting |
Overdrivr | 0:c4676d32d381 | 55 | if(Log.amount >= VARIABLES_AMOUNT) |
Overdrivr | 0:c4676d32d381 | 56 | return 1; |
Overdrivr | 0:c4676d32d381 | 57 | |
Overdrivr | 0:c4676d32d381 | 58 | Log.variables[Log.amount].ptr = (uint8_t*) ptr; |
Overdrivr | 0:c4676d32d381 | 59 | Log.variables[Log.amount].size = get_size(type); |
Overdrivr | 0:c4676d32d381 | 60 | Log.variables[Log.amount].writeable = writeable; |
Overdrivr | 0:c4676d32d381 | 61 | Log.variables[Log.amount].type = type; |
Overdrivr | 0:c4676d32d381 | 62 | Log.variables[Log.amount].groupID = Log.current_group_id; |
Overdrivr | 0:c4676d32d381 | 63 | strcpy(Log.variables[Log.amount].name,name); |
Overdrivr | 0:c4676d32d381 | 64 | |
Overdrivr | 0:c4676d32d381 | 65 | Log.amount++; |
Overdrivr | 0:c4676d32d381 | 66 | |
Overdrivr | 0:c4676d32d381 | 67 | return 0; |
Overdrivr | 0:c4676d32d381 | 68 | } |
Overdrivr | 0:c4676d32d381 | 69 | |
Overdrivr | 0:c4676d32d381 | 70 | void start_group(char* groupname) |
Overdrivr | 0:c4676d32d381 | 71 | { |
Overdrivr | 0:c4676d32d381 | 72 | Log.current_group_id++; |
Overdrivr | 0:c4676d32d381 | 73 | strcpy(Log.groups[Log.current_group_id].name,groupname); |
Overdrivr | 0:c4676d32d381 | 74 | } |
Overdrivr | 0:c4676d32d381 | 75 | |
Overdrivr | 0:c4676d32d381 | 76 | /** |
Overdrivr | 0:c4676d32d381 | 77 | * Send var descriptor |
Overdrivr | 0:c4676d32d381 | 78 | */ |
Overdrivr | 0:c4676d32d381 | 79 | |
Overdrivr | 0:c4676d32d381 | 80 | void send_descriptor(uint16_t index) |
Overdrivr | 0:c4676d32d381 | 81 | { |
Overdrivr | 0:c4676d32d381 | 82 | if(index >= Log.amount) |
Overdrivr | 0:c4676d32d381 | 83 | return; |
Overdrivr | 0:c4676d32d381 | 84 | |
Overdrivr | 0:c4676d32d381 | 85 | static uint8_t buffer[PAYLOAD_SIZE]; |
Overdrivr | 0:c4676d32d381 | 86 | uint8_t type; |
Overdrivr | 0:c4676d32d381 | 87 | |
Overdrivr | 0:c4676d32d381 | 88 | // Respond returned-descriptor |
Overdrivr | 0:c4676d32d381 | 89 | buffer[0] = 0x00; |
Overdrivr | 0:c4676d32d381 | 90 | |
Overdrivr | 0:c4676d32d381 | 91 | // Write id |
Overdrivr | 0:c4676d32d381 | 92 | uint16_t ID = ((Log.variables[index].groupID & 0x003F) << 10) + (index & 0x3FF); |
Overdrivr | 0:c4676d32d381 | 93 | uint8_t * temp_ptr = (uint8_t*)(&ID); |
Overdrivr | 0:c4676d32d381 | 94 | buffer[1] = *(temp_ptr + 1); |
Overdrivr | 0:c4676d32d381 | 95 | buffer[2] = *(temp_ptr ); |
Overdrivr | 0:c4676d32d381 | 96 | |
Overdrivr | 0:c4676d32d381 | 97 | // Write type & writeable |
Overdrivr | 0:c4676d32d381 | 98 | |
Overdrivr | 0:c4676d32d381 | 99 | type = (uint8_t)(Log.variables[index].type); |
Overdrivr | 0:c4676d32d381 | 100 | |
Overdrivr | 0:c4676d32d381 | 101 | if(Log.variables[index].writeable) |
Overdrivr | 0:c4676d32d381 | 102 | type += 0xF0; |
Overdrivr | 0:c4676d32d381 | 103 | |
Overdrivr | 0:c4676d32d381 | 104 | buffer[3] = type; |
Overdrivr | 0:c4676d32d381 | 105 | |
Overdrivr | 0:c4676d32d381 | 106 | //Write name |
Overdrivr | 0:c4676d32d381 | 107 | uint16_t i = 4; |
Overdrivr | 0:c4676d32d381 | 108 | for(uint16_t k = 0 ; k < 8 ; k++) |
Overdrivr | 0:c4676d32d381 | 109 | { |
Overdrivr | 0:c4676d32d381 | 110 | if(k < strlen(Log.variables[index].name)) |
Overdrivr | 0:c4676d32d381 | 111 | { |
Overdrivr | 0:c4676d32d381 | 112 | buffer[i] = Log.variables[index].name[k]; |
Overdrivr | 0:c4676d32d381 | 113 | i++; |
Overdrivr | 0:c4676d32d381 | 114 | } |
Overdrivr | 0:c4676d32d381 | 115 | else |
Overdrivr | 0:c4676d32d381 | 116 | buffer[i++] = 0; |
Overdrivr | 0:c4676d32d381 | 117 | } |
Overdrivr | 0:c4676d32d381 | 118 | |
Overdrivr | 0:c4676d32d381 | 119 | // Compute crc |
Overdrivr | 0:c4676d32d381 | 120 | uint16_t crc_value = crc16(buffer,i); |
Overdrivr | 0:c4676d32d381 | 121 | |
Overdrivr | 0:c4676d32d381 | 122 | // Write crc into buffer's last byte |
Overdrivr | 0:c4676d32d381 | 123 | buffer[i++] = (crc_value >> 8) & 0xFF; |
Overdrivr | 0:c4676d32d381 | 124 | buffer[i++] = crc_value & 0xFF; |
Overdrivr | 0:c4676d32d381 | 125 | |
Overdrivr | 0:c4676d32d381 | 126 | // Encode frame |
Overdrivr | 0:c4676d32d381 | 127 | encode(buffer,i); |
Overdrivr | 0:c4676d32d381 | 128 | } |
Overdrivr | 0:c4676d32d381 | 129 | |
Overdrivr | 0:c4676d32d381 | 130 | void send_group_descriptor(uint16_t index) |
Overdrivr | 0:c4676d32d381 | 131 | { |
Overdrivr | 0:c4676d32d381 | 132 | if(index > Log.current_group_id) |
Overdrivr | 0:c4676d32d381 | 133 | return; |
Overdrivr | 0:c4676d32d381 | 134 | |
Overdrivr | 0:c4676d32d381 | 135 | static uint8_t buffer[PAYLOAD_SIZE]; |
Overdrivr | 0:c4676d32d381 | 136 | |
Overdrivr | 0:c4676d32d381 | 137 | // Respond returned-descriptor |
Overdrivr | 0:c4676d32d381 | 138 | buffer[0] = 0x00; |
Overdrivr | 0:c4676d32d381 | 139 | |
Overdrivr | 0:c4676d32d381 | 140 | // Write id |
Overdrivr | 0:c4676d32d381 | 141 | uint16_t ID = (index & 0x3F) << 10; |
Overdrivr | 0:c4676d32d381 | 142 | uint8_t * temp_ptr = (uint8_t*)(&ID); |
Overdrivr | 0:c4676d32d381 | 143 | buffer[1] = *(temp_ptr + 1); |
Overdrivr | 0:c4676d32d381 | 144 | buffer[2] = *(temp_ptr); |
Overdrivr | 0:c4676d32d381 | 145 | |
Overdrivr | 0:c4676d32d381 | 146 | // Write type |
Overdrivr | 0:c4676d32d381 | 147 | buffer[3] = 0x07; |
Overdrivr | 0:c4676d32d381 | 148 | |
Overdrivr | 0:c4676d32d381 | 149 | //Write name |
Overdrivr | 0:c4676d32d381 | 150 | uint16_t i = 4; |
Overdrivr | 0:c4676d32d381 | 151 | for(uint16_t k = 0 ; k < 8 ; k++) |
Overdrivr | 0:c4676d32d381 | 152 | { |
Overdrivr | 0:c4676d32d381 | 153 | if(k < strlen(Log.groups[index].name)) |
Overdrivr | 0:c4676d32d381 | 154 | { |
Overdrivr | 0:c4676d32d381 | 155 | buffer[i] = Log.groups[index].name[k]; |
Overdrivr | 0:c4676d32d381 | 156 | i++; |
Overdrivr | 0:c4676d32d381 | 157 | } |
Overdrivr | 0:c4676d32d381 | 158 | else |
Overdrivr | 0:c4676d32d381 | 159 | buffer[i++] = 0; |
Overdrivr | 0:c4676d32d381 | 160 | } |
Overdrivr | 0:c4676d32d381 | 161 | |
Overdrivr | 0:c4676d32d381 | 162 | // Compute crc |
Overdrivr | 0:c4676d32d381 | 163 | uint16_t crc_value = crc16(buffer,i); |
Overdrivr | 0:c4676d32d381 | 164 | |
Overdrivr | 0:c4676d32d381 | 165 | // Write crc into buffer's last byte |
Overdrivr | 0:c4676d32d381 | 166 | buffer[i++] = (crc_value >> 8) & 0xFF; |
Overdrivr | 0:c4676d32d381 | 167 | buffer[i++] = crc_value & 0xFF; |
Overdrivr | 0:c4676d32d381 | 168 | |
Overdrivr | 0:c4676d32d381 | 169 | // Encode frame |
Overdrivr | 0:c4676d32d381 | 170 | encode(buffer,i); |
Overdrivr | 0:c4676d32d381 | 171 | } |
Overdrivr | 0:c4676d32d381 | 172 | |
Overdrivr | 0:c4676d32d381 | 173 | void distantio_decode(uint8_t* data,uint16_t datasize) |
Overdrivr | 0:c4676d32d381 | 174 | { |
Overdrivr | 0:c4676d32d381 | 175 | // First check data size |
Overdrivr | 0:c4676d32d381 | 176 | // 1 byte cmd + 2 bytes id + 1 byte type + FRAME_SIZE + 2 byte CRC |
Overdrivr | 0:c4676d32d381 | 177 | if(datasize != PAYLOAD_SIZE) |
Overdrivr | 0:c4676d32d381 | 178 | return; |
Overdrivr | 0:c4676d32d381 | 179 | |
Overdrivr | 0:c4676d32d381 | 180 | // Second, check CRC |
Overdrivr | 0:c4676d32d381 | 181 | uint16_t crc_value = crc16(data,PAYLOAD_SIZE-2); |
Overdrivr | 0:c4676d32d381 | 182 | uint16_t crc_rx = ((uint16_t)data[PAYLOAD_SIZE-2] << 8) | data[PAYLOAD_SIZE-1]; |
Overdrivr | 0:c4676d32d381 | 183 | |
Overdrivr | 0:c4676d32d381 | 184 | if(crc_value != crc_rx) |
Overdrivr | 0:c4676d32d381 | 185 | return; |
Overdrivr | 0:c4676d32d381 | 186 | |
Overdrivr | 0:c4676d32d381 | 187 | // Process frame |
Overdrivr | 0:c4676d32d381 | 188 | // First, identify command |
Overdrivr | 0:c4676d32d381 | 189 | uint8_t command = data[0]; |
Overdrivr | 0:c4676d32d381 | 190 | |
Overdrivr | 0:c4676d32d381 | 191 | // Second, identify variable ID |
Overdrivr | 0:c4676d32d381 | 192 | uint16_t ID = data[2] + (data[1] << 8); |
Overdrivr | 0:c4676d32d381 | 193 | ID = (ID & 0x3FF); |
Overdrivr | 0:c4676d32d381 | 194 | |
Overdrivr | 0:c4676d32d381 | 195 | // Third, identify data type |
Overdrivr | 0:c4676d32d381 | 196 | uint8_t type = data[3]; |
Overdrivr | 0:c4676d32d381 | 197 | |
Overdrivr | 0:c4676d32d381 | 198 | switch(command) |
Overdrivr | 0:c4676d32d381 | 199 | { |
Overdrivr | 0:c4676d32d381 | 200 | // User requested descriptors |
Overdrivr | 0:c4676d32d381 | 201 | case 0x02: |
Overdrivr | 0:c4676d32d381 | 202 | // Send variables |
Overdrivr | 0:c4676d32d381 | 203 | for(uint16_t i = 0 ; i < Log.amount ; i++) |
Overdrivr | 0:c4676d32d381 | 204 | send_descriptor(i); |
Overdrivr | 0:c4676d32d381 | 205 | // Send groups |
Overdrivr | 0:c4676d32d381 | 206 | for(uint16_t i = 0 ; i <= Log.current_group_id ; i++) |
Overdrivr | 0:c4676d32d381 | 207 | send_group_descriptor(i); |
Overdrivr | 0:c4676d32d381 | 208 | break; |
Overdrivr | 0:c4676d32d381 | 209 | |
Overdrivr | 0:c4676d32d381 | 210 | // User provided value to write |
Overdrivr | 0:c4676d32d381 | 211 | case 0x04: |
Overdrivr | 0:c4676d32d381 | 212 | if(ID >= Log.amount) |
Overdrivr | 0:c4676d32d381 | 213 | return; |
Overdrivr | 0:c4676d32d381 | 214 | |
Overdrivr | 0:c4676d32d381 | 215 | if(Log.variables[ID].writeable == 0x00) |
Overdrivr | 0:c4676d32d381 | 216 | return; |
Overdrivr | 0:c4676d32d381 | 217 | |
Overdrivr | 0:c4676d32d381 | 218 | if(Log.variables[ID].type != type) |
Overdrivr | 0:c4676d32d381 | 219 | return; |
Overdrivr | 0:c4676d32d381 | 220 | |
Overdrivr | 0:c4676d32d381 | 221 | uint16_t start_address = 4 + DATA_SIZE - 1; |
Overdrivr | 0:c4676d32d381 | 222 | |
Overdrivr | 0:c4676d32d381 | 223 | // Copy contents directly into variable |
Overdrivr | 0:c4676d32d381 | 224 | for(uint16_t i = 0 ; i < Log.variables[ID].size ; i++) |
Overdrivr | 0:c4676d32d381 | 225 | { |
Overdrivr | 0:c4676d32d381 | 226 | // Packet is big-endian, convert to little-endian |
Overdrivr | 0:c4676d32d381 | 227 | uint8_t offset = start_address - i; |
Overdrivr | 0:c4676d32d381 | 228 | *(Log.variables[ID].ptr + i) = *(data + offset); |
Overdrivr | 0:c4676d32d381 | 229 | } |
Overdrivr | 0:c4676d32d381 | 230 | break; |
Overdrivr | 0:c4676d32d381 | 231 | |
Overdrivr | 0:c4676d32d381 | 232 | // User requested variable read |
Overdrivr | 0:c4676d32d381 | 233 | case 0x05: |
Overdrivr | 0:c4676d32d381 | 234 | if(ID >= Log.amount) |
Overdrivr | 0:c4676d32d381 | 235 | return; |
Overdrivr | 0:c4676d32d381 | 236 | |
Overdrivr | 0:c4676d32d381 | 237 | Log.variables[ID].send = 1; |
Overdrivr | 0:c4676d32d381 | 238 | break; |
Overdrivr | 0:c4676d32d381 | 239 | |
Overdrivr | 0:c4676d32d381 | 240 | // User requested stop variable read |
Overdrivr | 0:c4676d32d381 | 241 | case 0x06: |
Overdrivr | 0:c4676d32d381 | 242 | if(ID >= Log.amount) |
Overdrivr | 0:c4676d32d381 | 243 | return; |
Overdrivr | 0:c4676d32d381 | 244 | Log.variables[ID].send = 0; |
Overdrivr | 0:c4676d32d381 | 245 | break; |
Overdrivr | 0:c4676d32d381 | 246 | |
Overdrivr | 0:c4676d32d381 | 247 | } |
Overdrivr | 0:c4676d32d381 | 248 | } |
Overdrivr | 0:c4676d32d381 | 249 | |
Overdrivr | 0:c4676d32d381 | 250 | void send_variables() |
Overdrivr | 0:c4676d32d381 | 251 | { |
Overdrivr | 0:c4676d32d381 | 252 | for(uint16_t i = 0 ; i < Log.amount ; i++) |
Overdrivr | 0:c4676d32d381 | 253 | { |
Overdrivr | 0:c4676d32d381 | 254 | if(Log.variables[i].send == 0) |
Overdrivr | 0:c4676d32d381 | 255 | continue; |
Overdrivr | 0:c4676d32d381 | 256 | |
Overdrivr | 0:c4676d32d381 | 257 | send_variable(i); |
Overdrivr | 0:c4676d32d381 | 258 | } |
Overdrivr | 0:c4676d32d381 | 259 | } |
Overdrivr | 0:c4676d32d381 | 260 | |
Overdrivr | 0:c4676d32d381 | 261 | void send_variable(uint16_t index) |
Overdrivr | 0:c4676d32d381 | 262 | { |
Overdrivr | 0:c4676d32d381 | 263 | if(index >= Log.amount) |
Overdrivr | 0:c4676d32d381 | 264 | return; |
Overdrivr | 0:c4676d32d381 | 265 | |
Overdrivr | 0:c4676d32d381 | 266 | static uint8_t buffer[PAYLOAD_SIZE]; |
Overdrivr | 0:c4676d32d381 | 267 | |
Overdrivr | 0:c4676d32d381 | 268 | // Response code 0x01 |
Overdrivr | 0:c4676d32d381 | 269 | buffer[0] = 0x01; |
Overdrivr | 0:c4676d32d381 | 270 | |
Overdrivr | 0:c4676d32d381 | 271 | // Write variable ID |
Overdrivr | 0:c4676d32d381 | 272 | uint16_t ID = ((Log.variables[index].groupID & 0x003F) << 10) + (index & 0x3FF); |
Overdrivr | 0:c4676d32d381 | 273 | uint8_t * temp_ptr = (uint8_t*)(&ID); |
Overdrivr | 0:c4676d32d381 | 274 | buffer[1] = *(temp_ptr + 1); |
Overdrivr | 0:c4676d32d381 | 275 | buffer[2] = *(temp_ptr); |
Overdrivr | 0:c4676d32d381 | 276 | |
Overdrivr | 0:c4676d32d381 | 277 | // Write variable type |
Overdrivr | 0:c4676d32d381 | 278 | buffer[3] = Log.variables[index].type; |
Overdrivr | 0:c4676d32d381 | 279 | //TODO writeable |
Overdrivr | 0:c4676d32d381 | 280 | |
Overdrivr | 0:c4676d32d381 | 281 | uint16_t i = 4; |
Overdrivr | 0:c4676d32d381 | 282 | |
Overdrivr | 0:c4676d32d381 | 283 | // Write data |
Overdrivr | 0:c4676d32d381 | 284 | for(uint16_t k = 0 ; k < DATA_SIZE ; k++) |
Overdrivr | 0:c4676d32d381 | 285 | { |
Overdrivr | 0:c4676d32d381 | 286 | uint16_t off = DATA_SIZE - 1 - k; |
Overdrivr | 0:c4676d32d381 | 287 | |
Overdrivr | 0:c4676d32d381 | 288 | // Fill buffer with data |
Overdrivr | 0:c4676d32d381 | 289 | if(off < Log.variables[index].size) |
Overdrivr | 0:c4676d32d381 | 290 | { |
Overdrivr | 0:c4676d32d381 | 291 | temp_ptr = Log.variables[index].ptr + off ; |
Overdrivr | 0:c4676d32d381 | 292 | buffer[i++] = *temp_ptr; |
Overdrivr | 0:c4676d32d381 | 293 | } |
Overdrivr | 0:c4676d32d381 | 294 | // Fill remaining bits with 0 |
Overdrivr | 0:c4676d32d381 | 295 | else |
Overdrivr | 0:c4676d32d381 | 296 | { |
Overdrivr | 0:c4676d32d381 | 297 | buffer[i++] = 0; |
Overdrivr | 0:c4676d32d381 | 298 | } |
Overdrivr | 0:c4676d32d381 | 299 | } |
Overdrivr | 0:c4676d32d381 | 300 | |
Overdrivr | 0:c4676d32d381 | 301 | // Compute crc |
Overdrivr | 0:c4676d32d381 | 302 | uint16_t crc_value = crc16(buffer,i); |
Overdrivr | 0:c4676d32d381 | 303 | |
Overdrivr | 0:c4676d32d381 | 304 | // Write crc into buffer's last byte |
Overdrivr | 0:c4676d32d381 | 305 | buffer[i++] = (crc_value >> 8) & 0xFF; |
Overdrivr | 0:c4676d32d381 | 306 | buffer[i++] = crc_value & 0xFF; |
Overdrivr | 0:c4676d32d381 | 307 | |
Overdrivr | 0:c4676d32d381 | 308 | // Encode frame |
Overdrivr | 0:c4676d32d381 | 309 | encode(buffer,i); |
Overdrivr | 0:c4676d32d381 | 310 | } |
Overdrivr | 0:c4676d32d381 | 311 | |
Overdrivr | 0:c4676d32d381 | 312 | void send_alive() |
Overdrivr | 0:c4676d32d381 | 313 | { |
Overdrivr | 0:c4676d32d381 | 314 | static uint8_t buffer[PAYLOAD_SIZE] = {0x03,0x00,0x10,0x00,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x00,0x00}; |
Overdrivr | 0:c4676d32d381 | 315 | |
Overdrivr | 0:c4676d32d381 | 316 | uint16_t index = 1; |
Overdrivr | 0:c4676d32d381 | 317 | uint16_t group = 0; |
Overdrivr | 0:c4676d32d381 | 318 | uint16_t ID = ((group & 0x003F) << 10) + (index & 0x3FF); |
Overdrivr | 0:c4676d32d381 | 319 | uint8_t * temp_ptr = (uint8_t*)(&ID); |
Overdrivr | 0:c4676d32d381 | 320 | buffer[1] = *(temp_ptr + 1); |
Overdrivr | 0:c4676d32d381 | 321 | buffer[2] = *(temp_ptr ); |
Overdrivr | 0:c4676d32d381 | 322 | |
Overdrivr | 0:c4676d32d381 | 323 | // Compute crc |
Overdrivr | 0:c4676d32d381 | 324 | uint16_t crc_value = crc16(buffer,PAYLOAD_SIZE - 2); |
Overdrivr | 0:c4676d32d381 | 325 | |
Overdrivr | 0:c4676d32d381 | 326 | // Write crc into buffer's last byte |
Overdrivr | 0:c4676d32d381 | 327 | buffer[PAYLOAD_SIZE - 1] = crc_value & 0xFF; |
Overdrivr | 0:c4676d32d381 | 328 | buffer[PAYLOAD_SIZE - 2] = (crc_value >> 8) & 0xFF; |
Overdrivr | 0:c4676d32d381 | 329 | |
Overdrivr | 0:c4676d32d381 | 330 | // Send frame to encoding |
Overdrivr | 0:c4676d32d381 | 331 | encode(buffer,PAYLOAD_SIZE); |
Overdrivr | 0:c4676d32d381 | 332 | } |
Overdrivr | 0:c4676d32d381 | 333 | |
Overdrivr | 0:c4676d32d381 | 334 | |
Overdrivr | 0:c4676d32d381 | 335 | /** |
Overdrivr | 0:c4676d32d381 | 336 | * Returns the size in byte for each variable |
Overdrivr | 0:c4676d32d381 | 337 | */ |
Overdrivr | 0:c4676d32d381 | 338 | |
Overdrivr | 0:c4676d32d381 | 339 | uint16_t get_size(dio_type type) |
Overdrivr | 0:c4676d32d381 | 340 | { |
Overdrivr | 0:c4676d32d381 | 341 | switch(type) |
Overdrivr | 0:c4676d32d381 | 342 | { |
Overdrivr | 0:c4676d32d381 | 343 | case dio_type_FLOAT: |
Overdrivr | 0:c4676d32d381 | 344 | case dio_type_UINT32: |
Overdrivr | 0:c4676d32d381 | 345 | case dio_type_INT32: |
Overdrivr | 0:c4676d32d381 | 346 | return 4; |
Overdrivr | 0:c4676d32d381 | 347 | |
Overdrivr | 0:c4676d32d381 | 348 | case dio_type_UINT16: |
Overdrivr | 0:c4676d32d381 | 349 | case dio_type_INT16: |
Overdrivr | 0:c4676d32d381 | 350 | return 2; |
Overdrivr | 0:c4676d32d381 | 351 | |
Overdrivr | 0:c4676d32d381 | 352 | case dio_type_UINT8: |
Overdrivr | 0:c4676d32d381 | 353 | case dio_type_INT8: |
Overdrivr | 0:c4676d32d381 | 354 | default: |
Overdrivr | 0:c4676d32d381 | 355 | return 1; |
Overdrivr | 0:c4676d32d381 | 356 | } |
Overdrivr | 0:c4676d32d381 | 357 | } |
Overdrivr | 0:c4676d32d381 | 358 |