I've run into a strange problem where if I load my data structure from EEPROM it casts it incorrectly. But, if I have both my function calls to the function responsible for saving the data structure and the function responsible for reading the data structure it casts the data into my structure successfully.
I've played around with the problem a bit and I've noticed that the data saved and the data read from the EEPROM is always correct if you read it as uint8_t
. But for some reason it fails to cast the data into my data structure (BareKeyboardKey2
) when the save function is not used in the program and we only read the data from the EEPROM.
The following structure is what I believe causes the problem:
// testStruct.h
struct IKey2
{
int pin;
};
struct BareKeyboardKey2 : virtual IKey2
{
int keyCode;
BareKeyboardKey2() {}
BareKeyboardKey2(int _pin, int _keyCode)
{
pin = _pin;
keyCode = _keyCode;
}
};
Example output of when I first call SaveStruct then LoadStruct. (The data I save to EEPROM is a BareKeyboardKey2(2, 4)
:
Reading: 0x68 0x1 0x4 0x0 0x2 0x0
pin: 2, keycode: 4
as you can see it casts the BareKeyboardKey2
correctly, but... Here is an example output of when ONLY LoadStruct is called (and the same data from the previous example is stored on the EEPROM):
Reading: 0x68 0x1 0x4 0x0 0x2 0x0
pin: -18248, keycode: 4
As you can see the data read from the EEPROM (i.e 0x68 0x1 0x4 0x0 0x2 0x0
) is the same between both examples, but in the latter example it fails to properly cast the data into a BareKeyboardKey2 (pin: -18248, keycode: 4
).
I have found that if I change the structure into the following it works regardless if I use SaveStruct
and LoadStruct
consequently or only use LoadStruct
:
// testStruct.h
struct IKey2
{
};
struct BareKeyboardKey2 : virtual IKey2
{
int pin;
int keyCode;
BareKeyboardKey2() {}
BareKeyboardKey2(int _pin, int _keyCode)
{
pin = _pin;
keyCode = _keyCode;
}
};
I also found that if I move both variables into the IKey2 like this...:
struct IKey2
{
int pin;
int keyCode;
};
struct BareKeyboardKey2 : virtual IKey2
{
BareKeyboardKey2() {}
BareKeyboardKey2(int _pin, int _keyCode)
{
pin = _pin;
keyCode = _keyCode;
}
};
... this causes the program to cast both variables wrong. Example output: pin: -18248, keycode: -18248
What is causing this behavior and what can I do to make it consistent?
Aucun commentaire:
Enregistrer un commentaire