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
Diff: distantio.cpp
- Revision:
- 2:f2c816b681e3
- Parent:
- 1:aaffeb93f99b
- Child:
- 3:135f55b5334e
--- a/distantio.cpp Thu Oct 08 12:27:20 2015 +0000 +++ b/distantio.cpp Thu Oct 08 13:14:32 2015 +0000 @@ -1,12 +1,19 @@ -// Copyright (C) 2015 Rémi Bèges -// For conditions of distribution and use, see copyright notice in the LICENSE.md file +/* + * distantio.c + * + * Created on: Oct 13, 2014 + * Author: B48923 + */ #include "distantio.h" #include "crc.h" #include "string.h" #include "protocol.h" - +/* + * WARNING : IMPLEMENTATION FOR LITTLE-ENDIAN PROCESSOR + * TODO : HANDLE BOTH + */ static log Log; uint32_t tmp; @@ -30,19 +37,19 @@ Log.variables[i].ptr = 0; Log.variables[i].writeable = 0; Log.variables[i].id = i; - strcpy(Log.variables[i].name,default_name); + strncpy(Log.variables[i].name,default_name,8); Log.variables[i].send = 0; Log.variables[i].groupID = 0; } tmp=0; Log.current_group_id = 0; - strcpy(Log.groups[0].name,"default"); + strncpy(Log.groups[0].name,"default",8); } /** * Register a variable exchanged with the computer */ -uint8_t register_var(void* ptr, uint16_t size, dio_type type, uint8_t writeable, char* name) +uint8_t register_var(void* ptr, uint16_t size, dio_type type, uint8_t writeable, char* name, float refresh_rate) { // Too many variables, aborting if(Log.amount >= VARIABLES_AMOUNT) @@ -53,17 +60,23 @@ Log.variables[Log.amount].writeable = writeable; Log.variables[Log.amount].type = type; Log.variables[Log.amount].groupID = Log.current_group_id; - strcpy(Log.variables[Log.amount].name,name); - + strncpy(Log.variables[Log.amount].name,name,8); + Log.variables[Log.amount].refresh_rate = refresh_rate; + Log.variables[Log.amount].last_refreshed = 0; Log.amount++; return 0; } +uint8_t register_var(void* ptr, uint16_t size, dio_type type, uint8_t writeable, char* name) +{ + register_var(ptr,size,type,writeable,name,0.f); +} + void start_group(char* groupname) { Log.current_group_id++; - strcpy(Log.groups[Log.current_group_id].name,groupname); + strncpy(Log.groups[Log.current_group_id].name,groupname,8); } /** @@ -240,14 +253,18 @@ } } -void send_variables() +void update(float current_time) { for(uint16_t i = 0 ; i < Log.amount ; i++) { if(Log.variables[i].send == 0) continue; - + + if(current_time < Log.variables[i].last_refreshed + Log.variables[i].refresh_rate) + continue; + send_variable(i); + Log.variables[i].last_refreshed = current_time; } } @@ -348,4 +365,3 @@ return 1; } } -