Maniacbug's RF24 arduino library ported to mbed. Tested, it works for Nucleo F411

Dependents:   RF24Network_Send RF24Network_Receive maple_chotobot_rf_motores Thesis_Verzender ... more

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