-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
85 lines (77 loc) · 2.57 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
#include <EEPROM.h>
#include "constants.hpp"
#include "config.hpp"
/**
* Reads the config stored in EEPROM for the controller
* Format: [ConfigSyncByte: 1 byte][RGBHeaderConfig: 5 bytes][RGBHeaderConfig: 5 bytes][...]
* @param config Array of header configs to write to
* @param address Starting address in EEPROM to read configs from
* @param numHeaders Number of headers this controller has
*/
void HeaderConfig::load(RGBHeaderConfig config[], int address, uint8_t numHeaders)
{
ConfigSyncByte sync = EEPROM.read(address);
if (sync != CONFIG_INITIALIZED)
{
// Initialize the config
init(config, address, numHeaders);
}
else
{
// Skip sync byte
address += 1;
// Load configs into memory struct by struct
for (uint8_t i = 0; i < numHeaders; i++)
{
readFromRom(&config[i], address, i);
}
}
}
/**
* Initializes the config in EEPROM and memory
* All headers start OFF
* @param config Array of header configs to write to
* @param address Starting address in EEPROM to write configs to
* @param numHeaders Number of headers this controller has
*/
void HeaderConfig::init(RGBHeaderConfig config[], int address, uint8_t numHeaders)
{
EEPROM.write(address, CONFIG_INITIALIZED);
address += 1;
for (uint8_t i = 0; i < numHeaders; i++)
{
config[i] = {1, OFF, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
writeToRom(&config[i], address, i);
}
}
/**
* Clears the config from EEPROM
*/
void HeaderConfig::wipe(int address, uint8_t numHeaders)
{
// Replace numHeaders number of RGBHeaderConfig's with 0xFF, plus sync byte
for (unsigned int i = 0; i < (numHeaders * sizeof(RGBHeaderConfig)) + 1; i++)
{
EEPROM.write(address + i, 0xFF);
}
}
/**
* Writes a single instance of RGB Header config to EEPROM from memory
* @param srcConfig Pointer to a RGBHeaderConfig to read from
* @param address Starting address in configs in EEPROM
* @param header Which header to read
*/
void HeaderConfig::writeToRom(RGBHeaderConfig *srcConfig, int address, uint8_t header)
{
EEPROM.put(address + (header * sizeof(RGBHeaderConfig)), *srcConfig);
}
/**
* Reads a single instance of RGB Header config from EEPROM into memory
* @param destConfig Pointer to a RGBHeaderConfig to write to
* @param address Starting address in configs in EEPROM
* @param header Which header to read
*/
void HeaderConfig::readFromRom(RGBHeaderConfig *destConfig, int address, uint8_t header)
{
EEPROM.get(address + (header * sizeof(RGBHeaderConfig)), *destConfig);
}