-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.ino
More file actions
73 lines (59 loc) · 2.59 KB
/
Settings.ino
File metadata and controls
73 lines (59 loc) · 2.59 KB
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
// The ATMEGA32U4 has 1024 bytes of EEPROM. We use some of it to store settings so that
// the oven doesn't need to be reconfigured every time it is turned on. Uninitialzed
// EEPROM is set to 0xFF (255). One of the first things done when powering up is to
// see if the EEPROM is uninitialized - and initialize it if that is the case.
//
// All settings are stored as bytes (unsigned 8-bit values). This presents a problem
// for the maximum temperature which can be as high as 280C - which doesn't fit in a
// 8-bit variable. With the goal of making getSetting/setSetting as simple as possible,
// we could've saved all values as 16-bit values, using consecutive EEPROM locations. We
// instead chose to just offset the temperature by 150C, making the range 25 to 130 instead
// of 175 to 280.
#include <EEPROM.h>
// Get the setting from EEPROM
int getSetting(int settingNum) {
int val = EEPROM.read(settingNum);
// The maximum temperature has an offset to allow it to be saved in 8-bits (0 - 255)
if (settingNum == SETTING_MAX_TEMPERATURE)
return val + SETTING_TEMPERATURE_OFFSET;
return val;
}
// Save the setting to EEPROM. EEPROM has limited write cycles, so don't save it if the
// value hasn't changed.
void setSetting(int settingNum, int value) {
// Do nothing if the value hasn't changed (don't wear out the EEPROM)
if (getSetting(settingNum) == value)
return;
switch (settingNum) {
case SETTING_D4_TYPE:
case SETTING_D5_TYPE:
case SETTING_D6_TYPE:
case SETTING_D7_TYPE:
// The element has been reconfigured so reset the duty cycles and restart learning
EEPROM.write(SETTING_SETTINGS_CHANGED, true);
EEPROM.write(SETTING_LEARNING_MODE, true);
EEPROM.write(settingNum, value);
Serial.println(F("Settings changed! Duty cycles have been reset and learning mode has been enabled"));
break;
case SETTING_MAX_TEMPERATURE:
// Enable learning mode if the maximum temperature has changed a lot
if (abs(getSetting(settingNum) - value) > 5)
EEPROM.write(SETTING_LEARNING_MODE, true);
// Write the new maximum temperature
EEPROM.write(settingNum, value - SETTING_TEMPERATURE_OFFSET);
break;
default:
EEPROM.write(settingNum, value);
break;
}
}
void InitializeSettingsIfNeccessary() {
// Does the EEPROM need to be initialized?
if (getSetting(SETTING_EEPROM_NEEDS_INIT)) {
// Initialize all the settings to 0 (false)
for (int i=0; i<=SETTING_LAST; i++)
EEPROM.write(i, 0);
// Set a reasonable max temperature
setSetting(SETTING_MAX_TEMPERATURE, 240);
}
}