ISEN Nimes CSI3 / RF24Network

Dependents:   ISEN_RF24Network_Node_01 ISEN_RF24Network_Node_02

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sync.h Source File

Sync.h

00001 /*
00002  Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
00003 
00004  This program is free software; you can redistribute it and/or
00005  modify it under the terms of the GNU General Public License
00006  version 2 as published by the Free Software Foundation.
00007  */
00008 
00009 #ifndef __SYNC_H__
00010 #define __SYNC_H__
00011 
00012 // STL headers
00013 // C headers
00014 #include <stdlib.h>
00015 #include <string.h>
00016 // Framework headers
00017 // Library headers
00018 #include <RF24Network_config.h>
00019 // Project headers
00020 
00021 class RF24Network;
00022 
00023 /**
00024  * Synchronizes a shared set of variables between multiple nodes
00025  */
00026 
00027 class Sync
00028 {
00029 private:
00030   RF24Network& network;
00031   uint8_t* app_data; /**< Application's copy of the data */
00032   uint8_t* internal_data; /**< Our copy of the data */
00033   size_t len; /**< Length of the data in bytes */
00034   uint16_t to_node; /**< The other node we're syncing with */
00035 
00036 protected:
00037 public:
00038   /**
00039    * Constructor
00040    *
00041    * @param _network Which network to syncrhonize over
00042    */
00043   Sync(RF24Network& _network): network(_network), app_data(NULL),
00044     internal_data(NULL), len(0), to_node(0)
00045   {
00046   }
00047   /**
00048    * Begin the object
00049    *
00050    * @param _to_node Which node we are syncing with
00051    */
00052   void begin(uint16_t _to_node)
00053   {
00054     to_node = _to_node;
00055   }
00056   /**
00057    * Declare the shared data set
00058    *
00059    * @param _data Location of shared data to be syncrhonized
00060    */
00061   template <class T>
00062   void register_me(T& _data)
00063   {
00064     app_data = reinterpret_cast<uint8_t*>(&_data);
00065     len = sizeof(_data);
00066     internal_data = reinterpret_cast<uint8_t*>(malloc(len));
00067     reset();
00068   }
00069 
00070   /**
00071    * Reset the internal copy of the shared data set 
00072    */
00073   void reset(void)
00074   {
00075     memcpy(internal_data,app_data,len);
00076   }
00077   
00078   /**
00079    * Update the network and the shared data set
00080    */
00081   void update(void);
00082 };
00083 
00084 #endif // __SYNC_H__
00085 // vim:cin:ai:sts=2 sw=2 ft=cpp