133 lines
2.6 KiB
C++
133 lines
2.6 KiB
C++
/*
|
|
EEPROM Write
|
|
|
|
Stores values read from analog input 0 into the EEPROM.
|
|
These values will stay in the EEPROM when the board is
|
|
turned off and may be retrieved later by another sketch.
|
|
*/
|
|
|
|
|
|
// the current address in the EEPROM (i.e. which byte
|
|
// we're going to write to next)
|
|
#include <EEPROM.h>
|
|
#define EEP_ADDR (0)
|
|
|
|
typedef struct gameState_s {
|
|
uint8_t pionSpelerA[4][2];
|
|
uint8_t pionSpelerB[4][2];
|
|
uint8_t pionSpelerC[4][2];
|
|
uint8_t pionSpelerD[4][2];
|
|
} gameState_t;
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(115200);
|
|
EEPROM.begin(512);
|
|
|
|
gameState_t game;
|
|
read(&game);
|
|
printGameState(game);
|
|
|
|
for (int i=0; i < 4; i++)
|
|
{
|
|
game.pionSpelerA[i][0] = i*2;
|
|
game.pionSpelerA[i][1] = i*2 + 1;
|
|
}
|
|
|
|
for (int i=0; i < 4; i++)
|
|
{
|
|
game.pionSpelerB[i][0] = i*2;
|
|
game.pionSpelerB[i][1] = i*2 + 1;
|
|
}
|
|
|
|
for (int i=0; i < 4; i++)
|
|
{
|
|
game.pionSpelerC[i][0] = i*2;
|
|
game.pionSpelerC[i][1] = i*2 + 1;
|
|
}
|
|
|
|
for (int i=0; i < 4; i++)
|
|
{
|
|
game.pionSpelerD[i][0] = i*2;
|
|
game.pionSpelerD[i][1] = i*2 + 1;
|
|
}
|
|
|
|
write(game);
|
|
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
|
|
}
|
|
|
|
void printGameState(gameState_t game)
|
|
{
|
|
Serial.println("player A:");
|
|
for (int i=0; i < 4; i++)
|
|
{
|
|
Serial.print(" pion ");
|
|
Serial.print(i + 1);
|
|
Serial.print(": (");
|
|
Serial.print(game.pionSpelerA[i][0]);
|
|
Serial.print(",");
|
|
Serial.print(game.pionSpelerA[i][1]);
|
|
Serial.println(")");
|
|
}
|
|
Serial.println("player B:");
|
|
for (int i=0; i < 4; i++)
|
|
{
|
|
Serial.print(" pion ");
|
|
Serial.print(i + 1);
|
|
Serial.print(": (");
|
|
Serial.print(game.pionSpelerB[i][0]);
|
|
Serial.print(",");
|
|
Serial.print(game.pionSpelerB[i][1]);
|
|
Serial.println(")");
|
|
}
|
|
Serial.println("player C:");
|
|
for (int i=0; i < 4; i++)
|
|
{
|
|
Serial.print(" pion ");
|
|
Serial.print(i + 1);
|
|
Serial.print(": (");
|
|
Serial.print(game.pionSpelerC[i][0]);
|
|
Serial.print(",");
|
|
Serial.print(game.pionSpelerC[i][1]);
|
|
Serial.println(")");
|
|
}
|
|
Serial.println("player D:");
|
|
for (int i=0; i < 4; i++)
|
|
{
|
|
Serial.print(" pion ");
|
|
Serial.print(i + 1);
|
|
Serial.print(": (");
|
|
Serial.print(game.pionSpelerD[i][0]);
|
|
Serial.print(",");
|
|
Serial.print(game.pionSpelerD[i][1]);
|
|
Serial.println(")");
|
|
}
|
|
}
|
|
|
|
void write(gameState_t game)
|
|
{
|
|
for (int i = 0; i < sizeof(game); i++)
|
|
{
|
|
EEPROM.write(EEP_ADDR + i, (uint8_t) *(((uint8_t*)&game) + i));
|
|
}
|
|
|
|
if (EEPROM.commit()) {
|
|
Serial.println("EEPROM successfully committed");
|
|
} else {
|
|
Serial.println("ERROR! EEPROM commit failed");
|
|
}
|
|
}
|
|
|
|
void read(gameState_t *game)
|
|
{
|
|
for (int i = 0; i < sizeof(game); i++)
|
|
{
|
|
*(((uint8_t*)&game) + i) = EEPROM.read(EEP_ADDR + i);
|
|
}
|
|
}
|