-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
98 lines (91 loc) · 2.71 KB
/
Config.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
94
95
96
97
98
#include <Arduino.h>
#include <EEPROM.h>
#include "ReadWrite.h"
#include "PixelRing.h"
#include "Tape.h"
#include "Machine.h"
#include "Config.h"
Config config;
// First two bytes identify a valid config in EEPROM
const char* Sig = "tm";
// First 8 bytes reserved for settings, the rest is for saved Machines+Tape
const int OffsetToSlot0 = 8;
void Config::Init()
{
// init to defaults, if no stored config, create it
int idx = 0;
int packedbytes;
machine.GetPackedData(packedbytes);
_SlotSize = packedbytes + PixelRing::Count;
_NumberOfMachineSlotsInEEPROM = (EEPROM.length() - OffsetToSlot0)/_SlotSize;
if (!(EEPROM[idx++] == Sig[0] && EEPROM[idx++] == Sig[1]))
{
// config is not there, init/create it
_MachineStepDelayMS = (MaxStepDelayMS + MinStepDelayMS)/2;
_PixelBrightness = StepBrightness;
_AmimateState = true;
// zero the machines in EEPROM
idx = OffsetToSlot0;
for (int mch = 0; mch < _NumberOfMachineSlotsInEEPROM; mch++)
{
for (int packed = 0; packed < packedbytes; packed++)
EEPROM[idx++] = 0;
for (int tape = 0; tape < PixelRing::Count; tape++)
EEPROM[idx++] = 0;
}
Save();
}
}
void Config::Load()
{
// load the config setting(s)
int idx = 0;
if (EEPROM[idx++] == Sig[0] && EEPROM[idx++] == Sig[1])
{
// config is there
_MachineStepDelayMS = EEPROM[idx++];
_PixelBrightness = EEPROM[idx++];
_AmimateState = EEPROM[idx++];
}
}
void Config::Save()
{
// load the config setting(s)
int idx = 0;
EEPROM[idx++] = Sig[0];
EEPROM[idx++] = Sig[1];
EEPROM[idx++] = _MachineStepDelayMS;
EEPROM[idx++] = _PixelBrightness;
EEPROM[idx++] = _AmimateState?1:0;
}
void Config::LoadSlot(int slot)
{
// get the machine and tape from slot in EEPROM
int packedBytes = 0;
byte* pMch = machine.GetPackedData(packedBytes);
int idx = OffsetToSlot0 + _SlotSize*slot;
if (0 <= slot && slot < _NumberOfMachineSlotsInEEPROM && pMch && packedBytes)
{
for (int packed = 0; packed < packedBytes; packed++)
*(pMch++) = EEPROM[idx++];
for (int t = 0; t < PixelRing::Count; t++)
{
byte b = EEPROM[idx++];
tape.Set(t, Label(b));
}
}
}
void Config::SaveSlot(int slot)
{
// put the machine and tape to slot in EEPROM
int packedBytes = 0;
byte* pMch = machine.GetPackedData(packedBytes);
int idx = OffsetToSlot0 + _SlotSize*slot;
if (0 <= slot && slot < _NumberOfMachineSlotsInEEPROM && pMch && packedBytes)
{
for (int packed = 0; packed < packedBytes; packed++)
EEPROM[idx++] = *(pMch++);
for (int t = 0; t < PixelRing::Count; t++)
EEPROM[idx++] = static_cast<byte>(tape.Get(t));
}
}