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.cpp Source File

Sync.cpp

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 // STL headers
00010 // C headers
00011 #include <stdlib.h>
00012 // Framework headers
00013 // Library headers
00014 #include <RF24Network.h>
00015 // Project headers
00016 // This component's header
00017 #include <Sync.h>
00018 
00019 /****************************************************************************/
00020 
00021 void Sync::update(void)
00022 {
00023   // Pump the network
00024   network.update();
00025 
00026   // Look for changes to the data
00027   uint8_t message[32];
00028   uint8_t *mptr = message;
00029   unsigned at = 0;
00030   while ( at < len )
00031   {
00032     if ( app_data && internal_data && app_data[at] != internal_data[at] )
00033     {
00034       // Compose a message with the deltas
00035       *mptr++ = at + 1;
00036       *mptr++ = app_data[at];
00037 
00038       // Update our internal view
00039       internal_data[at] = app_data[at];
00040     }
00041     ++at;
00042   }
00043   // Zero out the remainder
00044   while ( at++ < sizeof(message) )
00045     *mptr++ = 0;
00046 
00047   // If changes, send a message
00048   if ( *message )
00049   {
00050     // TODO handle the case where this has to be broken into
00051     // multiple messages
00052     RF24NetworkHeader header(/*to node*/ to_node, /*type*/ 'S' /*Sync*/);
00053     network.write(header,message,sizeof(message));
00054   }
00055 
00056   // Look for messages from the network
00057   // Is there anything ready for us?
00058   if ( network.available() )
00059   {
00060     // If so, take a look at it
00061     RF24NetworkHeader header;
00062     network.peek(header);
00063 
00064     switch (header.type)
00065     {
00066     case 'S':
00067       //IF_SERIAL_DEBUG(printf_P(PSTR("%lu: SYN Received sync message\n\r"),millis()));
00068 
00069       network.read(header,&message,sizeof(message));
00070       // Parse the message and update the vars
00071       mptr = message;
00072       at = 0;
00073       while ( mptr < message + sizeof(message) )
00074       {
00075         // A '0' in the first position means we are done
00076         if ( !*mptr )
00077           break;
00078         uint8_t pos = (*mptr++) - 1;
00079         uint8_t val = *mptr++;
00080 
00081         //IF_SERIAL_DEBUG(printf_P(PSTR("%lu: SYN Updated position %u to value %u\n\r"),millis(),pos,val));
00082 
00083         app_data[pos] = val;
00084         internal_data[pos] = val;
00085       }
00086       break;
00087     default:
00088       // Leave other messages for the app
00089       break;
00090     };
00091   }
00092 }
00093 // vim:cin:ai:sts=2 sw=2 ft=cpp