forked from nRF24/RF24Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sync.cpp
93 lines (81 loc) · 2.26 KB
/
Sync.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Copyright (C) 2011 J. Coliz <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
// STL headers
// C headers
#include <stdlib.h>
// Framework headers
// Library headers
#include <RF24Network.h>
// Project headers
// This component's header
#include <Sync.h>
/****************************************************************************/
void Sync::update(void)
{
// Pump the network
network.update();
// Look for changes to the data
uint8_t message[32];
uint8_t *mptr = message;
unsigned at = 0;
while ( at < len )
{
if ( app_data && internal_data && app_data[at] != internal_data[at] )
{
// Compose a message with the deltas
*mptr++ = at + 1;
*mptr++ = app_data[at];
// Update our internal view
internal_data[at] = app_data[at];
}
++at;
}
// Zero out the remainder
while ( at++ < sizeof(message) )
*mptr++ = 0;
// If changes, send a message
if ( *message )
{
// TODO handle the case where this has to be broken into
// multiple messages
RF24NetworkHeader header(/*to node*/ to_node, /*type*/ 'S' /*Sync*/);
network.write(header,message,sizeof(message));
}
// Look for messages from the network
// Is there anything ready for us?
if ( network.available() )
{
// If so, take a look at it
RF24NetworkHeader header;
network.peek(header);
switch (header.type)
{
case 'S':
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: SYN Received sync message\n\r"),millis()));
network.read(header,&message,sizeof(message));
// Parse the message and update the vars
mptr = message;
at = 0;
while ( mptr < message + sizeof(message) )
{
// A '0' in the first position means we are done
if ( !*mptr )
break;
uint8_t pos = (*mptr++) - 1;
uint8_t val = *mptr++;
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: SYN Updated position %u to value %u\n\r"),millis(),pos,val));
app_data[pos] = val;
internal_data[pos] = val;
}
break;
default:
// Leave other messages for the app
break;
};
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp