-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathnodeconfig.cpp
More file actions
109 lines (97 loc) · 3.2 KB
/
nodeconfig.cpp
File metadata and controls
109 lines (97 loc) · 3.2 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
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.
*/
#include "RF24Network_config.h"
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include "nodeconfig.h"
// Where in EEPROM is the address stored?
uint8_t* address_at_eeprom_location = (uint8_t*)10;
// What flag value is stored there so we know the value is valid?
const uint8_t valid_eeprom_flag = 0xde;
eeprom_info_t eeprom_info;
const eeprom_info_t& nodeconfig_read(void)
{
memset(&eeprom_info,0,sizeof(eeprom_info));
// Look for the token in EEPROM to indicate the following value is
// a validly set node address
if ( eeprom_read_byte(address_at_eeprom_location) == valid_eeprom_flag )
{
eeprom_read_block(&eeprom_info,address_at_eeprom_location,sizeof(eeprom_info));
printf_P(PSTR("ADDRESS: %o\n\r"),eeprom_info.address);
printf_P(PSTR("ROLE: %S\n\r"),eeprom_info.relay ? PSTR("Relay") : PSTR("Leaf") );
}
else
{
printf_P(PSTR("*** No valid address found. Send node address via serial of the form 011<cr>\n\r"));
while(1)
{
nodeconfig_listen();
}
}
return eeprom_info;
}
char serialdata[10];
char* nextserialat = serialdata;
const char* maxserial = serialdata + sizeof(serialdata) - 1;
void nodeconfig_listen(void)
{
//
// Listen for serial input, which is how we set the address
//
if (Serial.available())
{
// If the character on serial input is in a valid range...
char c = Serial.read();
if ( c >= '0' && c <= '5' )
{
*nextserialat++ = c;
if ( nextserialat == maxserial )
{
*nextserialat = 0;
printf_P(PSTR("\r\n*** Unknown serial command: %s\r\n"),serialdata);
nextserialat = serialdata;
}
}
else if ( tolower(c) == 'r' )
{
eeprom_info.relay = true;
printf_P(PSTR("ROLE: %S\n\r"),eeprom_info.relay ? PSTR("Relay") : PSTR("Leaf") );
if ( eeprom_info.flag == valid_eeprom_flag )
eeprom_write_block(&eeprom_info,address_at_eeprom_location,sizeof(eeprom_info));
else
printf_P(PSTR("Please assign an address to commit this role to EEPROM\r\n"));
}
else if ( tolower(c) == 'l' )
{
eeprom_info.relay = false;
printf_P(PSTR("ROLE: %S\n\r"),eeprom_info.relay ? PSTR("Relay") : PSTR("Leaf") );
if ( eeprom_info.flag == valid_eeprom_flag )
eeprom_write_block(&eeprom_info,address_at_eeprom_location,sizeof(eeprom_info));
else
printf_P(PSTR("Please assign an address to commit this role to EEPROM\r\n"));
}
else if ( c == 13 )
{
// Convert to octal
char *pc = serialdata;
uint16_t address = 0;
while ( pc < nextserialat )
{
address <<= 3;
address |= (*pc++ - '0');
}
// It is our address
eeprom_info.flag = valid_eeprom_flag;
eeprom_info.address = address;
eeprom_write_block(&eeprom_info,address_at_eeprom_location,sizeof(eeprom_info));
// And we are done right now (no easy way to soft reset)
printf_P(PSTR("\n\rManually set to address 0%o\n\rPress RESET to continue!"),address);
while(1);
}
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp