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@4:d675ad9c57ff, 2015-10-13 (annotated)
- Committer:
- Overdrivr
- Date:
- Tue Oct 13 12:20:44 2015 +0000
- Revision:
- 4:d675ad9c57ff
- Parent:
- 3:135f55b5334e
- Child:
- 5:e8936f38a338
Interface cleanup + added documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Overdrivr | 2:f2c816b681e3 | 1 | /* |
Overdrivr | 2:f2c816b681e3 | 2 | * distantio.c |
Overdrivr | 2:f2c816b681e3 | 3 | * |
Overdrivr | 2:f2c816b681e3 | 4 | * Created on: Oct 13, 2014 |
Overdrivr | 2:f2c816b681e3 | 5 | * Author: B48923 |
Overdrivr | 2:f2c816b681e3 | 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 | 2:f2c816b681e3 | 13 | /* |
Overdrivr | 2:f2c816b681e3 | 14 | * WARNING : IMPLEMENTATION FOR LITTLE-ENDIAN PROCESSOR |
Overdrivr | 2:f2c816b681e3 | 15 | * TODO : HANDLE BOTH |
Overdrivr | 2:f2c816b681e3 | 16 | */ |
Overdrivr | 0:c4676d32d381 | 17 | |
Overdrivr | 0:c4676d32d381 | 18 | static log Log; |
Overdrivr | 0:c4676d32d381 | 19 | uint32_t tmp; |
Overdrivr | 0:c4676d32d381 | 20 | |
Overdrivr | 4:d675ad9c57ff | 21 | void _dIO_send_variable(uint16_t index,float extra_identifier_1,uint16_t extra_identifier_2=0); |
Overdrivr | 4:d675ad9c57ff | 22 | uint16_t _dIO_get_size(dio_type type); |
Overdrivr | 4:d675ad9c57ff | 23 | void _dIO_send_descriptor(uint16_t index); |
Overdrivr | 4:d675ad9c57ff | 24 | void _dIO_send_group_descriptor(uint16_t index); |
Overdrivr | 0:c4676d32d381 | 25 | |
Overdrivr | 4:d675ad9c57ff | 26 | |
Overdrivr | 4:d675ad9c57ff | 27 | void dIO_init() |
Overdrivr | 0:c4676d32d381 | 28 | { |
Overdrivr | 0:c4676d32d381 | 29 | uint16_t i; |
Overdrivr | 0:c4676d32d381 | 30 | char default_name[] = {"undef. "}; |
Overdrivr | 0:c4676d32d381 | 31 | Log.amount = 0; |
Overdrivr | 0:c4676d32d381 | 32 | for(i = 0 ; i < VARIABLES_AMOUNT ; i++) |
Overdrivr | 0:c4676d32d381 | 33 | { |
Overdrivr | 0:c4676d32d381 | 34 | Log.variables[i].size = 0; |
Overdrivr | 0:c4676d32d381 | 35 | Log.variables[i].ptr = 0; |
Overdrivr | 0:c4676d32d381 | 36 | Log.variables[i].writeable = 0; |
Overdrivr | 0:c4676d32d381 | 37 | Log.variables[i].id = i; |
Overdrivr | 3:135f55b5334e | 38 | strncpy(Log.variables[i].name,default_name,NAMESIZE); |
Overdrivr | 0:c4676d32d381 | 39 | Log.variables[i].send = 0; |
Overdrivr | 0:c4676d32d381 | 40 | Log.variables[i].groupID = 0; |
Overdrivr | 0:c4676d32d381 | 41 | } |
Overdrivr | 0:c4676d32d381 | 42 | tmp=0; |
Overdrivr | 0:c4676d32d381 | 43 | Log.current_group_id = 0; |
Overdrivr | 3:135f55b5334e | 44 | strncpy(Log.groups[0].name,"default",NAMESIZE); |
Overdrivr | 0:c4676d32d381 | 45 | } |
Overdrivr | 0:c4676d32d381 | 46 | |
Overdrivr | 4:d675ad9c57ff | 47 | uint8_t dIO_var(void* ptr, uint16_t size, dio_type type, uint8_t writeable, char* name, float refresh_rate) |
Overdrivr | 0:c4676d32d381 | 48 | { |
Overdrivr | 0:c4676d32d381 | 49 | // Too many variables, aborting |
Overdrivr | 0:c4676d32d381 | 50 | if(Log.amount >= VARIABLES_AMOUNT) |
Overdrivr | 0:c4676d32d381 | 51 | return 1; |
Overdrivr | 0:c4676d32d381 | 52 | |
Overdrivr | 0:c4676d32d381 | 53 | Log.variables[Log.amount].ptr = (uint8_t*) ptr; |
Overdrivr | 4:d675ad9c57ff | 54 | Log.variables[Log.amount].size = _dIO_get_size(type); |
Overdrivr | 0:c4676d32d381 | 55 | Log.variables[Log.amount].writeable = writeable; |
Overdrivr | 0:c4676d32d381 | 56 | Log.variables[Log.amount].type = type; |
Overdrivr | 0:c4676d32d381 | 57 | Log.variables[Log.amount].groupID = Log.current_group_id; |
Overdrivr | 3:135f55b5334e | 58 | strncpy(Log.variables[Log.amount].name,name,NAMESIZE); |
Overdrivr | 2:f2c816b681e3 | 59 | Log.variables[Log.amount].refresh_rate = refresh_rate; |
Overdrivr | 2:f2c816b681e3 | 60 | Log.variables[Log.amount].last_refreshed = 0; |
Overdrivr | 0:c4676d32d381 | 61 | Log.amount++; |
Overdrivr | 0:c4676d32d381 | 62 | |
Overdrivr | 0:c4676d32d381 | 63 | return 0; |
Overdrivr | 0:c4676d32d381 | 64 | } |
Overdrivr | 0:c4676d32d381 | 65 | |
Overdrivr | 4:d675ad9c57ff | 66 | void dIO_group(char* groupname) |
Overdrivr | 0:c4676d32d381 | 67 | { |
Overdrivr | 0:c4676d32d381 | 68 | Log.current_group_id++; |
Overdrivr | 3:135f55b5334e | 69 | strncpy(Log.groups[Log.current_group_id].name,groupname,NAMESIZE); |
Overdrivr | 0:c4676d32d381 | 70 | } |
Overdrivr | 0:c4676d32d381 | 71 | |
Overdrivr | 4:d675ad9c57ff | 72 | |
Overdrivr | 4:d675ad9c57ff | 73 | |
Overdrivr | 4:d675ad9c57ff | 74 | void dIO_decode(uint8_t* data,uint16_t datasize) |
Overdrivr | 4:d675ad9c57ff | 75 | { |
Overdrivr | 4:d675ad9c57ff | 76 | // First check data size |
Overdrivr | 4:d675ad9c57ff | 77 | if(datasize != FRAMESIZE) |
Overdrivr | 4:d675ad9c57ff | 78 | return; |
Overdrivr | 4:d675ad9c57ff | 79 | |
Overdrivr | 4:d675ad9c57ff | 80 | // Second, check CRC |
Overdrivr | 4:d675ad9c57ff | 81 | uint16_t crc_value = crc16(data,FRAMESIZE-2); |
Overdrivr | 4:d675ad9c57ff | 82 | uint16_t crc_rx = ((uint16_t)data[FRAMESIZE-2] << 8) | data[FRAMESIZE-1]; |
Overdrivr | 4:d675ad9c57ff | 83 | |
Overdrivr | 4:d675ad9c57ff | 84 | if(crc_value != crc_rx) |
Overdrivr | 4:d675ad9c57ff | 85 | return; |
Overdrivr | 4:d675ad9c57ff | 86 | |
Overdrivr | 4:d675ad9c57ff | 87 | // Process frame |
Overdrivr | 4:d675ad9c57ff | 88 | // First, identify command |
Overdrivr | 4:d675ad9c57ff | 89 | uint8_t command = data[0]; |
Overdrivr | 4:d675ad9c57ff | 90 | |
Overdrivr | 4:d675ad9c57ff | 91 | // Second, identify variable ID |
Overdrivr | 4:d675ad9c57ff | 92 | uint16_t ID = data[2] + (data[1] << 8); |
Overdrivr | 4:d675ad9c57ff | 93 | ID = (ID & 0x3FF); |
Overdrivr | 4:d675ad9c57ff | 94 | |
Overdrivr | 4:d675ad9c57ff | 95 | // Third, identify data type |
Overdrivr | 4:d675ad9c57ff | 96 | uint8_t type = data[3]; |
Overdrivr | 4:d675ad9c57ff | 97 | |
Overdrivr | 4:d675ad9c57ff | 98 | switch(command) |
Overdrivr | 4:d675ad9c57ff | 99 | { |
Overdrivr | 4:d675ad9c57ff | 100 | // User requested descriptors |
Overdrivr | 4:d675ad9c57ff | 101 | case 0x02: |
Overdrivr | 4:d675ad9c57ff | 102 | // Send variables |
Overdrivr | 4:d675ad9c57ff | 103 | for(uint16_t i = 0 ; i < Log.amount ; i++) |
Overdrivr | 4:d675ad9c57ff | 104 | _dIO_send_descriptor(i); |
Overdrivr | 4:d675ad9c57ff | 105 | // Send groups |
Overdrivr | 4:d675ad9c57ff | 106 | for(uint16_t i = 0 ; i <= Log.current_group_id ; i++) |
Overdrivr | 4:d675ad9c57ff | 107 | _dIO_send_group_descriptor(i); |
Overdrivr | 4:d675ad9c57ff | 108 | break; |
Overdrivr | 4:d675ad9c57ff | 109 | |
Overdrivr | 4:d675ad9c57ff | 110 | // User provided value to write |
Overdrivr | 4:d675ad9c57ff | 111 | case 0x04: |
Overdrivr | 4:d675ad9c57ff | 112 | if(ID >= Log.amount) |
Overdrivr | 4:d675ad9c57ff | 113 | return; |
Overdrivr | 4:d675ad9c57ff | 114 | |
Overdrivr | 4:d675ad9c57ff | 115 | if(Log.variables[ID].writeable == 0x00) |
Overdrivr | 4:d675ad9c57ff | 116 | return; |
Overdrivr | 4:d675ad9c57ff | 117 | |
Overdrivr | 4:d675ad9c57ff | 118 | if(Log.variables[ID].type != type) |
Overdrivr | 4:d675ad9c57ff | 119 | return; |
Overdrivr | 4:d675ad9c57ff | 120 | |
Overdrivr | 4:d675ad9c57ff | 121 | uint16_t start_address = DATASTART + DATASIZE - 1; |
Overdrivr | 0:c4676d32d381 | 122 | |
Overdrivr | 4:d675ad9c57ff | 123 | // Copy contents directly into variable |
Overdrivr | 4:d675ad9c57ff | 124 | for(uint16_t i = 0 ; i < Log.variables[ID].size ; i++) |
Overdrivr | 4:d675ad9c57ff | 125 | { |
Overdrivr | 4:d675ad9c57ff | 126 | // Packet is big-endian, convert to little-endian |
Overdrivr | 4:d675ad9c57ff | 127 | uint8_t offset = start_address - i; |
Overdrivr | 4:d675ad9c57ff | 128 | *(Log.variables[ID].ptr + i) = *(data + offset); |
Overdrivr | 4:d675ad9c57ff | 129 | } |
Overdrivr | 4:d675ad9c57ff | 130 | break; |
Overdrivr | 4:d675ad9c57ff | 131 | |
Overdrivr | 4:d675ad9c57ff | 132 | // User requested variable read |
Overdrivr | 4:d675ad9c57ff | 133 | case 0x05: |
Overdrivr | 4:d675ad9c57ff | 134 | if(ID >= Log.amount) |
Overdrivr | 4:d675ad9c57ff | 135 | return; |
Overdrivr | 4:d675ad9c57ff | 136 | |
Overdrivr | 4:d675ad9c57ff | 137 | Log.variables[ID].send = 1; |
Overdrivr | 4:d675ad9c57ff | 138 | break; |
Overdrivr | 4:d675ad9c57ff | 139 | |
Overdrivr | 4:d675ad9c57ff | 140 | // User requested stop variable read |
Overdrivr | 4:d675ad9c57ff | 141 | case 0x06: |
Overdrivr | 4:d675ad9c57ff | 142 | if(ID >= Log.amount) |
Overdrivr | 4:d675ad9c57ff | 143 | return; |
Overdrivr | 4:d675ad9c57ff | 144 | Log.variables[ID].send = 0; |
Overdrivr | 4:d675ad9c57ff | 145 | break; |
Overdrivr | 4:d675ad9c57ff | 146 | |
Overdrivr | 4:d675ad9c57ff | 147 | } |
Overdrivr | 4:d675ad9c57ff | 148 | } |
Overdrivr | 4:d675ad9c57ff | 149 | |
Overdrivr | 4:d675ad9c57ff | 150 | void dIO_update(float current_time) |
Overdrivr | 4:d675ad9c57ff | 151 | { |
Overdrivr | 4:d675ad9c57ff | 152 | for(uint16_t i = 0 ; i < Log.amount ; i++) |
Overdrivr | 4:d675ad9c57ff | 153 | { |
Overdrivr | 4:d675ad9c57ff | 154 | if(Log.variables[i].send == 0) |
Overdrivr | 4:d675ad9c57ff | 155 | continue; |
Overdrivr | 4:d675ad9c57ff | 156 | |
Overdrivr | 4:d675ad9c57ff | 157 | if(current_time < Log.variables[i].last_refreshed + Log.variables[i].refresh_rate) |
Overdrivr | 4:d675ad9c57ff | 158 | continue; |
Overdrivr | 4:d675ad9c57ff | 159 | |
Overdrivr | 4:d675ad9c57ff | 160 | _dIO_send_variable(i,current_time); |
Overdrivr | 4:d675ad9c57ff | 161 | Log.variables[i].last_refreshed = current_time; |
Overdrivr | 4:d675ad9c57ff | 162 | } |
Overdrivr | 4:d675ad9c57ff | 163 | } |
Overdrivr | 4:d675ad9c57ff | 164 | |
Overdrivr | 4:d675ad9c57ff | 165 | void dIO_send_alive() |
Overdrivr | 4:d675ad9c57ff | 166 | { |
Overdrivr | 4:d675ad9c57ff | 167 | static uint8_t buffer[FRAMESIZE]; |
Overdrivr | 4:d675ad9c57ff | 168 | buffer[0] = 0x03; |
Overdrivr | 4:d675ad9c57ff | 169 | |
Overdrivr | 4:d675ad9c57ff | 170 | // Compute crc |
Overdrivr | 4:d675ad9c57ff | 171 | uint16_t crc_value = crc16(buffer,FRAMESIZE - 2); |
Overdrivr | 4:d675ad9c57ff | 172 | |
Overdrivr | 4:d675ad9c57ff | 173 | // Write crc into buffer's last byte |
Overdrivr | 4:d675ad9c57ff | 174 | buffer[FRAMESIZE - 1] = crc_value & 0xFF; |
Overdrivr | 4:d675ad9c57ff | 175 | buffer[FRAMESIZE - 2] = (crc_value >> 8) & 0xFF; |
Overdrivr | 4:d675ad9c57ff | 176 | |
Overdrivr | 4:d675ad9c57ff | 177 | // Send frame to encoding |
Overdrivr | 4:d675ad9c57ff | 178 | encode(buffer,FRAMESIZE); |
Overdrivr | 4:d675ad9c57ff | 179 | } |
Overdrivr | 4:d675ad9c57ff | 180 | |
Overdrivr | 4:d675ad9c57ff | 181 | /* ------------------- PRIVATE FUNCTIONS ------------------ */ |
Overdrivr | 4:d675ad9c57ff | 182 | |
Overdrivr | 4:d675ad9c57ff | 183 | void _dIO_send_descriptor(uint16_t index) |
Overdrivr | 0:c4676d32d381 | 184 | { |
Overdrivr | 0:c4676d32d381 | 185 | if(index >= Log.amount) |
Overdrivr | 0:c4676d32d381 | 186 | return; |
Overdrivr | 0:c4676d32d381 | 187 | |
Overdrivr | 3:135f55b5334e | 188 | static uint8_t buffer[FRAMESIZE]; |
Overdrivr | 0:c4676d32d381 | 189 | uint8_t type; |
Overdrivr | 0:c4676d32d381 | 190 | |
Overdrivr | 0:c4676d32d381 | 191 | // Respond returned-descriptor |
Overdrivr | 0:c4676d32d381 | 192 | buffer[0] = 0x00; |
Overdrivr | 0:c4676d32d381 | 193 | |
Overdrivr | 0:c4676d32d381 | 194 | // Write id |
Overdrivr | 0:c4676d32d381 | 195 | uint16_t ID = ((Log.variables[index].groupID & 0x003F) << 10) + (index & 0x3FF); |
Overdrivr | 0:c4676d32d381 | 196 | uint8_t * temp_ptr = (uint8_t*)(&ID); |
Overdrivr | 0:c4676d32d381 | 197 | buffer[1] = *(temp_ptr + 1); |
Overdrivr | 0:c4676d32d381 | 198 | buffer[2] = *(temp_ptr ); |
Overdrivr | 0:c4676d32d381 | 199 | |
Overdrivr | 0:c4676d32d381 | 200 | // Write type & writeable |
Overdrivr | 0:c4676d32d381 | 201 | |
Overdrivr | 0:c4676d32d381 | 202 | type = (uint8_t)(Log.variables[index].type); |
Overdrivr | 0:c4676d32d381 | 203 | |
Overdrivr | 0:c4676d32d381 | 204 | if(Log.variables[index].writeable) |
Overdrivr | 0:c4676d32d381 | 205 | type += 0xF0; |
Overdrivr | 0:c4676d32d381 | 206 | |
Overdrivr | 0:c4676d32d381 | 207 | buffer[3] = type; |
Overdrivr | 0:c4676d32d381 | 208 | |
Overdrivr | 0:c4676d32d381 | 209 | //Write name |
Overdrivr | 0:c4676d32d381 | 210 | uint16_t i = 4; |
Overdrivr | 3:135f55b5334e | 211 | // TODO : Replace with strncpy |
Overdrivr | 3:135f55b5334e | 212 | for(uint16_t k = 0 ; k < NAMESIZE ; k++) |
Overdrivr | 0:c4676d32d381 | 213 | { |
Overdrivr | 0:c4676d32d381 | 214 | if(k < strlen(Log.variables[index].name)) |
Overdrivr | 0:c4676d32d381 | 215 | { |
Overdrivr | 0:c4676d32d381 | 216 | buffer[i] = Log.variables[index].name[k]; |
Overdrivr | 0:c4676d32d381 | 217 | i++; |
Overdrivr | 0:c4676d32d381 | 218 | } |
Overdrivr | 0:c4676d32d381 | 219 | else |
Overdrivr | 0:c4676d32d381 | 220 | buffer[i++] = 0; |
Overdrivr | 0:c4676d32d381 | 221 | } |
Overdrivr | 0:c4676d32d381 | 222 | |
Overdrivr | 0:c4676d32d381 | 223 | // Compute crc |
Overdrivr | 0:c4676d32d381 | 224 | uint16_t crc_value = crc16(buffer,i); |
Overdrivr | 0:c4676d32d381 | 225 | |
Overdrivr | 0:c4676d32d381 | 226 | // Write crc into buffer's last byte |
Overdrivr | 0:c4676d32d381 | 227 | buffer[i++] = (crc_value >> 8) & 0xFF; |
Overdrivr | 0:c4676d32d381 | 228 | buffer[i++] = crc_value & 0xFF; |
Overdrivr | 0:c4676d32d381 | 229 | |
Overdrivr | 0:c4676d32d381 | 230 | // Encode frame |
Overdrivr | 0:c4676d32d381 | 231 | encode(buffer,i); |
Overdrivr | 0:c4676d32d381 | 232 | } |
Overdrivr | 0:c4676d32d381 | 233 | |
Overdrivr | 4:d675ad9c57ff | 234 | void _dIO_send_group_descriptor(uint16_t index) |
Overdrivr | 0:c4676d32d381 | 235 | { |
Overdrivr | 0:c4676d32d381 | 236 | if(index > Log.current_group_id) |
Overdrivr | 0:c4676d32d381 | 237 | return; |
Overdrivr | 0:c4676d32d381 | 238 | |
Overdrivr | 3:135f55b5334e | 239 | static uint8_t buffer[FRAMESIZE]; |
Overdrivr | 0:c4676d32d381 | 240 | |
Overdrivr | 0:c4676d32d381 | 241 | // Respond returned-descriptor |
Overdrivr | 0:c4676d32d381 | 242 | buffer[0] = 0x00; |
Overdrivr | 0:c4676d32d381 | 243 | |
Overdrivr | 0:c4676d32d381 | 244 | // Write id |
Overdrivr | 0:c4676d32d381 | 245 | uint16_t ID = (index & 0x3F) << 10; |
Overdrivr | 0:c4676d32d381 | 246 | uint8_t * temp_ptr = (uint8_t*)(&ID); |
Overdrivr | 0:c4676d32d381 | 247 | buffer[1] = *(temp_ptr + 1); |
Overdrivr | 0:c4676d32d381 | 248 | buffer[2] = *(temp_ptr); |
Overdrivr | 0:c4676d32d381 | 249 | |
Overdrivr | 0:c4676d32d381 | 250 | // Write type |
Overdrivr | 0:c4676d32d381 | 251 | buffer[3] = 0x07; |
Overdrivr | 0:c4676d32d381 | 252 | |
Overdrivr | 0:c4676d32d381 | 253 | //Write name |
Overdrivr | 0:c4676d32d381 | 254 | uint16_t i = 4; |
Overdrivr | 3:135f55b5334e | 255 | for(uint16_t k = 0 ; k < NAMESIZE ; k++) |
Overdrivr | 0:c4676d32d381 | 256 | { |
Overdrivr | 0:c4676d32d381 | 257 | if(k < strlen(Log.groups[index].name)) |
Overdrivr | 0:c4676d32d381 | 258 | { |
Overdrivr | 0:c4676d32d381 | 259 | buffer[i] = Log.groups[index].name[k]; |
Overdrivr | 0:c4676d32d381 | 260 | i++; |
Overdrivr | 0:c4676d32d381 | 261 | } |
Overdrivr | 0:c4676d32d381 | 262 | else |
Overdrivr | 0:c4676d32d381 | 263 | buffer[i++] = 0; |
Overdrivr | 0:c4676d32d381 | 264 | } |
Overdrivr | 0:c4676d32d381 | 265 | |
Overdrivr | 0:c4676d32d381 | 266 | // Compute crc |
Overdrivr | 0:c4676d32d381 | 267 | uint16_t crc_value = crc16(buffer,i); |
Overdrivr | 0:c4676d32d381 | 268 | |
Overdrivr | 0:c4676d32d381 | 269 | // Write crc into buffer's last byte |
Overdrivr | 0:c4676d32d381 | 270 | buffer[i++] = (crc_value >> 8) & 0xFF; |
Overdrivr | 0:c4676d32d381 | 271 | buffer[i++] = crc_value & 0xFF; |
Overdrivr | 0:c4676d32d381 | 272 | |
Overdrivr | 0:c4676d32d381 | 273 | // Encode frame |
Overdrivr | 0:c4676d32d381 | 274 | encode(buffer,i); |
Overdrivr | 0:c4676d32d381 | 275 | } |
Overdrivr | 0:c4676d32d381 | 276 | |
Overdrivr | 0:c4676d32d381 | 277 | |
Overdrivr | 4:d675ad9c57ff | 278 | void _dIO_send_variable(uint16_t index,float extra_identifier_1,uint16_t extra_identifier_2) |
Overdrivr | 0:c4676d32d381 | 279 | { |
Overdrivr | 0:c4676d32d381 | 280 | if(index >= Log.amount) |
Overdrivr | 0:c4676d32d381 | 281 | return; |
Overdrivr | 0:c4676d32d381 | 282 | |
Overdrivr | 3:135f55b5334e | 283 | static uint8_t buffer[FRAMESIZE]; |
Overdrivr | 0:c4676d32d381 | 284 | |
Overdrivr | 0:c4676d32d381 | 285 | // Response code 0x01 |
Overdrivr | 0:c4676d32d381 | 286 | buffer[0] = 0x01; |
Overdrivr | 0:c4676d32d381 | 287 | |
Overdrivr | 0:c4676d32d381 | 288 | // Write variable ID |
Overdrivr | 0:c4676d32d381 | 289 | uint16_t ID = ((Log.variables[index].groupID & 0x003F) << 10) + (index & 0x3FF); |
Overdrivr | 0:c4676d32d381 | 290 | uint8_t * temp_ptr = (uint8_t*)(&ID); |
Overdrivr | 0:c4676d32d381 | 291 | buffer[1] = *(temp_ptr + 1); |
Overdrivr | 0:c4676d32d381 | 292 | buffer[2] = *(temp_ptr); |
Overdrivr | 0:c4676d32d381 | 293 | |
Overdrivr | 0:c4676d32d381 | 294 | // Write variable type |
Overdrivr | 0:c4676d32d381 | 295 | buffer[3] = Log.variables[index].type; |
Overdrivr | 0:c4676d32d381 | 296 | //TODO writeable |
Overdrivr | 3:135f55b5334e | 297 | |
Overdrivr | 3:135f55b5334e | 298 | // Extra identifier 1 |
Overdrivr | 3:135f55b5334e | 299 | temp_ptr = (uint8_t*)(&extra_identifier_1); |
Overdrivr | 3:135f55b5334e | 300 | buffer[4] = *(temp_ptr + 3); |
Overdrivr | 3:135f55b5334e | 301 | buffer[5] = *(temp_ptr + 2); |
Overdrivr | 3:135f55b5334e | 302 | buffer[6] = *(temp_ptr + 1); |
Overdrivr | 3:135f55b5334e | 303 | buffer[7] = *(temp_ptr ); |
Overdrivr | 3:135f55b5334e | 304 | |
Overdrivr | 3:135f55b5334e | 305 | // Extra identifier 2 |
Overdrivr | 3:135f55b5334e | 306 | temp_ptr = (uint8_t*)(&extra_identifier_2); |
Overdrivr | 3:135f55b5334e | 307 | buffer[8] = *(temp_ptr + 1); |
Overdrivr | 3:135f55b5334e | 308 | buffer[9] = *(temp_ptr ); |
Overdrivr | 3:135f55b5334e | 309 | |
Overdrivr | 3:135f55b5334e | 310 | uint16_t i = 10; |
Overdrivr | 0:c4676d32d381 | 311 | |
Overdrivr | 0:c4676d32d381 | 312 | // Write data |
Overdrivr | 3:135f55b5334e | 313 | for(uint16_t k = 0 ; k < DATASIZE ; k++) |
Overdrivr | 0:c4676d32d381 | 314 | { |
Overdrivr | 3:135f55b5334e | 315 | uint16_t off = DATASIZE - 1 - k; |
Overdrivr | 0:c4676d32d381 | 316 | |
Overdrivr | 0:c4676d32d381 | 317 | // Fill buffer with data |
Overdrivr | 0:c4676d32d381 | 318 | if(off < Log.variables[index].size) |
Overdrivr | 0:c4676d32d381 | 319 | { |
Overdrivr | 0:c4676d32d381 | 320 | temp_ptr = Log.variables[index].ptr + off ; |
Overdrivr | 0:c4676d32d381 | 321 | buffer[i++] = *temp_ptr; |
Overdrivr | 0:c4676d32d381 | 322 | } |
Overdrivr | 0:c4676d32d381 | 323 | // Fill remaining bits with 0 |
Overdrivr | 0:c4676d32d381 | 324 | else |
Overdrivr | 0:c4676d32d381 | 325 | { |
Overdrivr | 0:c4676d32d381 | 326 | buffer[i++] = 0; |
Overdrivr | 0:c4676d32d381 | 327 | } |
Overdrivr | 0:c4676d32d381 | 328 | } |
Overdrivr | 0:c4676d32d381 | 329 | |
Overdrivr | 0:c4676d32d381 | 330 | // Compute crc |
Overdrivr | 0:c4676d32d381 | 331 | uint16_t crc_value = crc16(buffer,i); |
Overdrivr | 0:c4676d32d381 | 332 | |
Overdrivr | 0:c4676d32d381 | 333 | // Write crc into buffer's last byte |
Overdrivr | 0:c4676d32d381 | 334 | buffer[i++] = (crc_value >> 8) & 0xFF; |
Overdrivr | 0:c4676d32d381 | 335 | buffer[i++] = crc_value & 0xFF; |
Overdrivr | 0:c4676d32d381 | 336 | |
Overdrivr | 0:c4676d32d381 | 337 | // Encode frame |
Overdrivr | 0:c4676d32d381 | 338 | encode(buffer,i); |
Overdrivr | 0:c4676d32d381 | 339 | } |
Overdrivr | 0:c4676d32d381 | 340 | |
Overdrivr | 0:c4676d32d381 | 341 | |
Overdrivr | 4:d675ad9c57ff | 342 | uint16_t _dIO_get_size(dio_type type) |
Overdrivr | 0:c4676d32d381 | 343 | { |
Overdrivr | 0:c4676d32d381 | 344 | switch(type) |
Overdrivr | 0:c4676d32d381 | 345 | { |
Overdrivr | 0:c4676d32d381 | 346 | case dio_type_FLOAT: |
Overdrivr | 0:c4676d32d381 | 347 | case dio_type_UINT32: |
Overdrivr | 0:c4676d32d381 | 348 | case dio_type_INT32: |
Overdrivr | 0:c4676d32d381 | 349 | return 4; |
Overdrivr | 0:c4676d32d381 | 350 | |
Overdrivr | 0:c4676d32d381 | 351 | case dio_type_UINT16: |
Overdrivr | 0:c4676d32d381 | 352 | case dio_type_INT16: |
Overdrivr | 0:c4676d32d381 | 353 | return 2; |
Overdrivr | 0:c4676d32d381 | 354 | |
Overdrivr | 0:c4676d32d381 | 355 | case dio_type_UINT8: |
Overdrivr | 0:c4676d32d381 | 356 | case dio_type_INT8: |
Overdrivr | 0:c4676d32d381 | 357 | default: |
Overdrivr | 0:c4676d32d381 | 358 | return 1; |
Overdrivr | 0:c4676d32d381 | 359 | } |
Overdrivr | 0:c4676d32d381 | 360 | } |