Hi there,
I can successfully write to EEPROM using a struct pointer, but I am stuck with using structs within the main struct to group the data, as I keep hitting a compile error brick wall.
I wanted to be able to point to an inner struct and still read & write data for it's members into the correct locations in EEPROM. I could not find a way to reference the inner struct in a struct pointer and have it compile, using either "->" or ".". Perhaps I am overreaching, any guidance would be greatly appreciated.
Admittedly I am new to C++ so some concepts may have alluded me and I have tried searching for a solution.
Anyway first, here's the code that works for me.
#include <EEPROM.h>
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
struct config_t
{
// Server stuff
char authkey[40];
bool dhcp_enabled;
byte ip[4];
byte gw_ip[4];
byte dns_ip[4];
// Wesbite notify stuff
char wn_host[100];
char wn_path[100];
uint16_t wn_port;
//xmbc stuff
char xmbc_website[50] ;
char xmbc_path[50];
uint16_t xmbc_port;
byte validConfig;
};
config_t * config;
void readConfigParam(char * buffer, const char * param, byte len)
{
for (byte i = 0; i < len; i++)
*buffer++ = (char) EEPROM.read( (uint16_t) param++);
}
void writeConfigParam(char * input, const char * param, byte len)
{
for (byte i = 0; i < len; i++)
EEPROM.write( (uint16_t ) param++,(const byte) *input++);
}
void testConfigWrite()
{
writeConfigParam("www.myexample.com"
, config->wn_host
, sizeof config->wn_host
);
writeConfigParam("shivermeauthy"
, config->authkey
, sizeof config->authkey
);
}
void testConfigRead()
{
char webhost[ sizeof config->wn_host ];
char authKey[ sizeof config->authkey ];
readConfigParam( webhost, config->wn_host, sizeof config->wn_host );
readConfigParam( authKey, config->authkey, sizeof config->authkey );
printf_P( PSTR("Web Host: %s\nAuthorise key: %s\n")
,webhost
,authKey
);
}
void setup() {
Serial.begin(115200);
printf_begin();
// Set config pointer to EEPROM location 0
config = 0;
// Run tests
testConfigWrite();
testConfigRead();
}
void loop() {
}
Now here is where I am struggling to get my mind around these compiler errors and what I am doing wrong.
#include <EEPROM.h>
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
struct config_t
{
struct server_t
{
char authkey[40];
bool dhcp_enabled;
byte ip[4];
byte gw_ip[4];
byte dns_ip[4];
} server;
struct webNotify_t
{
char host[100];
char path[100];
uint16_t port;
} web;
//xmbc stuff
char xmbc_website[50] ;
char xmbc_path[50];
uint16_t xmbc_port;
byte validConfig;
};
config_t * config;
void readConfigParam(char * buffer, const char * param, byte len)
{
for (byte i = 0; i < len; i++)
*buffer++ = (char) EEPROM.read( (uint16_t) param++);
}
void writeConfigParam(char * input, const char * param, byte len)
{
for (byte i = 0; i < len; i++)
EEPROM.write( (uint16_t ) param++,(const byte) *input++);
}
void testConfigWrite()
{
writeConfigParam("www.myexample.com"
, config->web->host
, sizeof config->web->host
);
writeConfigParam("shivermeauthy"
, config->server->authkey
, sizeof config->server->authkey
);
}
void testConfigRead()
{
char webhost[ sizeof config->web->host ];
char authKey[ sizeof config->server->authkey ];
readConfigParam( webhost, config->web->host, sizeof config->web->host );
readConfigParam( authKey, config->server->authkey, sizeof config->server->authkey );
printf_P( PSTR("Web Host: %s\nAuthorise key: %s\n")
,webhost
,authKey
);
}
void setup() {
Serial.begin(115200);
printf_begin();
// Set config pointer to EEPROM location 0
config = 0;
// Run tests
testConfigWrite();
testConfigRead();
}
void loop() {
}
The compile errors I get are as follows:-
Arduino: 1.5.7 (Windows 7), Board: "Arduino Uno"
Using library EEPROM in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM (legacy)
C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=157 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM C:\Users\Del\AppData\Local\Temp\build3074773576706612058.tmp\sketch_dec18a.cpp -o C:\Users\Del\AppData\Local\Temp\build3074773576706612058.tmp\sketch_dec18a.cpp.o
sketch_dec18a.ino: In function 'void testConfigWrite()':
sketch_dec18a.ino:62:38: error: base operand of '->' has non-pointer type 'config_t::webNotify_t'
sketch_dec18a.ino:63:45: error: base operand of '->' has non-pointer type 'config_t::webNotify_t'
sketch_dec18a.ino:67:41: error: base operand of '->' has non-pointer type 'config_t::server_t'
sketch_dec18a.ino:68:48: error: base operand of '->' has non-pointer type 'config_t::server_t'
sketch_dec18a.ino: In function 'void testConfigRead()':
sketch_dec18a.ino:73:37: error: base operand of '->' has non-pointer type 'config_t::webNotify_t'
sketch_dec18a.ino:74:40: error: base operand of '->' has non-pointer type 'config_t::server_t'
sketch_dec18a.ino:76:22: error: 'webhost' was not declared in this scope
sketch_dec18a.ino:76:42: error: base operand of '->' has non-pointer type 'config_t::webNotify_t'
sketch_dec18a.ino:76:68: error: base operand of '->' has non-pointer type 'config_t::webNotify_t'
sketch_dec18a.ino:77:22: error: 'authKey' was not declared in this scope
sketch_dec18a.ino:77:45: error: base operand of '->' has non-pointer type 'config_t::server_t'
sketch_dec18a.ino:77:77: error: base operand of '->' has non-pointer type 'config_t::server_t'
Once again, any guidance on my foolishness or otherwise would be appreciated.
Thanks for reading.