Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: xtoff2 RF24Network mbed
Fork of xtoff3 by
RF24Network/Sync.cpp@0:3982c0e9eda1, 2015-07-06 (annotated)
- Committer:
- akashvibhute
- Date:
- Mon Jul 06 03:17:33 2015 +0000
- Revision:
- 0:3982c0e9eda1
First mbed-arduino working program!; mbed is able to receive data from arduino nodes 0 or 1 depending on address set
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| akashvibhute | 0:3982c0e9eda1 | 1 | /* |
| akashvibhute | 0:3982c0e9eda1 | 2 | Copyright (C) 2011 J. Coliz <maniacbug@ymail.com> |
| akashvibhute | 0:3982c0e9eda1 | 3 | |
| akashvibhute | 0:3982c0e9eda1 | 4 | This program is free software; you can redistribute it and/or |
| akashvibhute | 0:3982c0e9eda1 | 5 | modify it under the terms of the GNU General Public License |
| akashvibhute | 0:3982c0e9eda1 | 6 | version 2 as published by the Free Software Foundation. |
| akashvibhute | 0:3982c0e9eda1 | 7 | */ |
| akashvibhute | 0:3982c0e9eda1 | 8 | |
| akashvibhute | 0:3982c0e9eda1 | 9 | // STL headers |
| akashvibhute | 0:3982c0e9eda1 | 10 | // C headers |
| akashvibhute | 0:3982c0e9eda1 | 11 | #include <stdlib.h> |
| akashvibhute | 0:3982c0e9eda1 | 12 | // Framework headers |
| akashvibhute | 0:3982c0e9eda1 | 13 | // Library headers |
| akashvibhute | 0:3982c0e9eda1 | 14 | #include <RF24Network.h> |
| akashvibhute | 0:3982c0e9eda1 | 15 | // Project headers |
| akashvibhute | 0:3982c0e9eda1 | 16 | // This component's header |
| akashvibhute | 0:3982c0e9eda1 | 17 | #include <Sync.h> |
| akashvibhute | 0:3982c0e9eda1 | 18 | |
| akashvibhute | 0:3982c0e9eda1 | 19 | /****************************************************************************/ |
| akashvibhute | 0:3982c0e9eda1 | 20 | |
| akashvibhute | 0:3982c0e9eda1 | 21 | void Sync::update(void) |
| akashvibhute | 0:3982c0e9eda1 | 22 | { |
| akashvibhute | 0:3982c0e9eda1 | 23 | // Pump the network |
| akashvibhute | 0:3982c0e9eda1 | 24 | network.update(); |
| akashvibhute | 0:3982c0e9eda1 | 25 | |
| akashvibhute | 0:3982c0e9eda1 | 26 | // Look for changes to the data |
| akashvibhute | 0:3982c0e9eda1 | 27 | uint8_t message[32]; |
| akashvibhute | 0:3982c0e9eda1 | 28 | uint8_t *mptr = message; |
| akashvibhute | 0:3982c0e9eda1 | 29 | unsigned at = 0; |
| akashvibhute | 0:3982c0e9eda1 | 30 | while ( at < len ) |
| akashvibhute | 0:3982c0e9eda1 | 31 | { |
| akashvibhute | 0:3982c0e9eda1 | 32 | if ( app_data && internal_data && app_data[at] != internal_data[at] ) |
| akashvibhute | 0:3982c0e9eda1 | 33 | { |
| akashvibhute | 0:3982c0e9eda1 | 34 | // Compose a message with the deltas |
| akashvibhute | 0:3982c0e9eda1 | 35 | *mptr++ = at + 1; |
| akashvibhute | 0:3982c0e9eda1 | 36 | *mptr++ = app_data[at]; |
| akashvibhute | 0:3982c0e9eda1 | 37 | |
| akashvibhute | 0:3982c0e9eda1 | 38 | // Update our internal view |
| akashvibhute | 0:3982c0e9eda1 | 39 | internal_data[at] = app_data[at]; |
| akashvibhute | 0:3982c0e9eda1 | 40 | } |
| akashvibhute | 0:3982c0e9eda1 | 41 | ++at; |
| akashvibhute | 0:3982c0e9eda1 | 42 | } |
| akashvibhute | 0:3982c0e9eda1 | 43 | // Zero out the remainder |
| akashvibhute | 0:3982c0e9eda1 | 44 | while ( at++ < sizeof(message) ) |
| akashvibhute | 0:3982c0e9eda1 | 45 | *mptr++ = 0; |
| akashvibhute | 0:3982c0e9eda1 | 46 | |
| akashvibhute | 0:3982c0e9eda1 | 47 | // If changes, send a message |
| akashvibhute | 0:3982c0e9eda1 | 48 | if ( *message ) |
| akashvibhute | 0:3982c0e9eda1 | 49 | { |
| akashvibhute | 0:3982c0e9eda1 | 50 | // TODO handle the case where this has to be broken into |
| akashvibhute | 0:3982c0e9eda1 | 51 | // multiple messages |
| akashvibhute | 0:3982c0e9eda1 | 52 | RF24NetworkHeader header(/*to node*/ to_node, /*type*/ 'S' /*Sync*/); |
| akashvibhute | 0:3982c0e9eda1 | 53 | network.write(header,message,sizeof(message)); |
| akashvibhute | 0:3982c0e9eda1 | 54 | } |
| akashvibhute | 0:3982c0e9eda1 | 55 | |
| akashvibhute | 0:3982c0e9eda1 | 56 | // Look for messages from the network |
| akashvibhute | 0:3982c0e9eda1 | 57 | // Is there anything ready for us? |
| akashvibhute | 0:3982c0e9eda1 | 58 | if ( network.available() ) |
| akashvibhute | 0:3982c0e9eda1 | 59 | { |
| akashvibhute | 0:3982c0e9eda1 | 60 | // If so, take a look at it |
| akashvibhute | 0:3982c0e9eda1 | 61 | RF24NetworkHeader header; |
| akashvibhute | 0:3982c0e9eda1 | 62 | network.peek(header); |
| akashvibhute | 0:3982c0e9eda1 | 63 | |
| akashvibhute | 0:3982c0e9eda1 | 64 | switch (header.type) |
| akashvibhute | 0:3982c0e9eda1 | 65 | { |
| akashvibhute | 0:3982c0e9eda1 | 66 | case 'S': |
| akashvibhute | 0:3982c0e9eda1 | 67 | //IF_SERIAL_DEBUG(printf_P(PSTR("%lu: SYN Received sync message\n\r"),millis())); |
| akashvibhute | 0:3982c0e9eda1 | 68 | |
| akashvibhute | 0:3982c0e9eda1 | 69 | network.read(header,&message,sizeof(message)); |
| akashvibhute | 0:3982c0e9eda1 | 70 | // Parse the message and update the vars |
| akashvibhute | 0:3982c0e9eda1 | 71 | mptr = message; |
| akashvibhute | 0:3982c0e9eda1 | 72 | at = 0; |
| akashvibhute | 0:3982c0e9eda1 | 73 | while ( mptr < message + sizeof(message) ) |
| akashvibhute | 0:3982c0e9eda1 | 74 | { |
| akashvibhute | 0:3982c0e9eda1 | 75 | // A '0' in the first position means we are done |
| akashvibhute | 0:3982c0e9eda1 | 76 | if ( !*mptr ) |
| akashvibhute | 0:3982c0e9eda1 | 77 | break; |
| akashvibhute | 0:3982c0e9eda1 | 78 | uint8_t pos = (*mptr++) - 1; |
| akashvibhute | 0:3982c0e9eda1 | 79 | uint8_t val = *mptr++; |
| akashvibhute | 0:3982c0e9eda1 | 80 | |
| akashvibhute | 0:3982c0e9eda1 | 81 | //IF_SERIAL_DEBUG(printf_P(PSTR("%lu: SYN Updated position %u to value %u\n\r"),millis(),pos,val)); |
| akashvibhute | 0:3982c0e9eda1 | 82 | |
| akashvibhute | 0:3982c0e9eda1 | 83 | app_data[pos] = val; |
| akashvibhute | 0:3982c0e9eda1 | 84 | internal_data[pos] = val; |
| akashvibhute | 0:3982c0e9eda1 | 85 | } |
| akashvibhute | 0:3982c0e9eda1 | 86 | break; |
| akashvibhute | 0:3982c0e9eda1 | 87 | default: |
| akashvibhute | 0:3982c0e9eda1 | 88 | // Leave other messages for the app |
| akashvibhute | 0:3982c0e9eda1 | 89 | break; |
| akashvibhute | 0:3982c0e9eda1 | 90 | }; |
| akashvibhute | 0:3982c0e9eda1 | 91 | } |
| akashvibhute | 0:3982c0e9eda1 | 92 | } |
| akashvibhute | 0:3982c0e9eda1 | 93 | // vim:cin:ai:sts=2 sw=2 ft=cpp |
