Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 69874ee

Browse files
Lizj96James FosterElizabeth Ventura
authored
Add EEPROM encapsulation (#9)
* Add Devices directory with stub of LiquidCrystal_TC (#6) * Add LiquidCrystal_TC stub class and devices directory. * Add EEPROM encapsulation * Organized functions and added testing * continuing work, still have warnings and errors Co-authored-by: James Foster <[email protected]> Co-authored-by: Elizabeth Ventura <[email protected]>
1 parent 7baa788 commit 69874ee

File tree

5 files changed

+226
-2
lines changed

5 files changed

+226
-2
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
source 'https://rubygems.org'
2-
gem 'arduino_ci', git: 'https://github.com/ianfixes/arduino_ci.git', branch: '2020-10-16_suggestions'
2+
gem 'arduino_ci', git: 'https://github.com/arduino-ci/arduino_ci.git', branch: 'tdd'

scripts/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#! /bin/sh
22
bundle config --local path vendor/bundle
33
bundle install
4-
bundle exec arduino_ci_remote.rb --skip-examples-compilation
4+
bundle exec arduino_ci_remote.rb --skip-compilation

src/Devices/EEPROM_TC.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
#include "EEPROM_TC.h"
3+
4+
void EEPROM_TC::getMacAddress(byte mac[]) {
5+
// Store MAC address in EEPROM
6+
if (EEPROM.read(MAC_ADDRESS) == '#') {
7+
for (int i = 3; i < 6; i++) {
8+
mac[i] = EEPROM.read(i + MAC_ADDRESS);
9+
}
10+
Serial.println(F("MAC Address found in EEPROM and read"));
11+
} else {
12+
Serial.println(F("No MAC Address Found in EEPROM. Generating New MAC."));
13+
for (int i = 3; i < 6; i++) {
14+
mac[i] = i; // TrueRandom.randomByte();
15+
EEPROM.write(i + MAC_ADDRESS, mac[i]);
16+
}
17+
EEPROM.write(MAC_ADDRESS, '#');
18+
}
19+
}
20+
21+
double EEPROM_TC::readTempSetPoint() {
22+
return readDouble(TEMP_ADDRESS, 20);
23+
}
24+
25+
void EEPROM_TC::writeTempSetPoint(double value) {
26+
writeDouble(TEMP_ADDRESS, value);
27+
}
28+
29+
double EEPROM_TC::readpHSetPoint() {
30+
return readDouble(PH_ADDRESS, 8.1);
31+
}
32+
33+
void EEPROM_TC::writepHSetPoint(double value) {
34+
writeDouble(PH_ADDRESS, value);
35+
}
36+
37+
double EEPROM_TC::readKpSetPoint() {
38+
return readDouble(KP_ADDRESS, 100000);
39+
}
40+
41+
void EEPROM_TC::writeKpSetPoint(double value) {
42+
writeDouble(KP_ADDRESS, value);
43+
}
44+
45+
double EEPROM_TC::readKiSetPoint() {
46+
return readDouble(KI_ADDRESS, 0);
47+
}
48+
49+
void EEPROM_TC::writeKiSetPoint(double value) {
50+
writeDouble(KP_ADDRESS, value);
51+
}
52+
53+
double EEPROM_TC::readKdSetPoint() {
54+
return readDouble(KD_ADDRESS, 0);
55+
}
56+
57+
void EEPROM_TC::writeKdSetPoint(double value) {
58+
writeDouble(KD_ADDRESS, value);
59+
}
60+
61+
double EEPROM_TC::readHeatSetPoint() {
62+
return readDouble(HEAT_ADDRESS, 0);
63+
}
64+
65+
void EEPROM_TC::writeHeatSetPoint(double value) {
66+
writeDouble(HEAT_ADDRESS, value);
67+
}
68+
69+
double EEPROM_TC::readAmplitudeSetPoint() {
70+
return readDouble(AMPLITUDE_ADDRESS, 0);
71+
}
72+
73+
void EEPROM_TC::writeAmplitudeSetPoint(double value) {
74+
writeDouble(AMPLITUDE_ADDRESS, value);
75+
}
76+
77+
double EEPROM_TC::readFrequencySetPoint() {
78+
return readDouble(FREQUENCY_ADDRESS, 0);
79+
}
80+
81+
void EEPROM_TC::writeFrequencySetPoint(double value) {
82+
writeDouble(FREQUENCY_ADDRESS, value);
83+
}
84+
85+
void EEPROM_TC::writeDouble(int address, double value) {
86+
if (value != readDouble(address, 1.0/0.0)) {
87+
byte* p = (byte*)(void*)&value;
88+
for (int i = 0; i < sizeof(value); i++) {
89+
EEPROM.write(address++, *p++);
90+
}
91+
}
92+
}
93+
94+
double EEPROM_TC::readDouble(int address, double defaultValue) {
95+
double value = 0.0;
96+
byte* p = (byte*)(void*)&value;
97+
for (int i = 0; i < sizeof(value); i++) {
98+
*p++ = EEPROM.read(address++);
99+
}
100+
return isnan(value) ? defaultValue : value;
101+
}

src/Devices/EEPROM_TC.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* This class provides a wraper for the EEPROM functionality.
3+
* The EEPROM provides memory whose values are kept when the
4+
* board is turned off (like a tiny hard drive).
5+
*
6+
* This device is used by the tank controller to save configuration information.
7+
* - MAC address
8+
* - pH set point
9+
* - auto-tune parameters
10+
* - temperature set point
11+
* - load parameters into EEPROM
12+
* - Write floating point values
13+
* - Read floating point values
14+
*/
15+
16+
#pragma once
17+
18+
#include <Arduino.h>
19+
#include <EEPROM.h>
20+
21+
class EEPROM_TC {
22+
public:
23+
EEPROM_TC();
24+
~EEPROM_TC();
25+
void getMacAddress(byte mac[]);
26+
void writeTempSetPoint(double value);
27+
void writepHSetPoint(double value);
28+
void writeKpSetPoint(double value);
29+
void writeKiSetPoint(double value);
30+
void writeKdSetPoint(double value);
31+
void writeHeatSetPoint(double value);
32+
void writeAmplitudeSetPoint(double vlaue);
33+
void writeFrequencySetPoint(double value);
34+
double readTempSetPoint();
35+
double readpHSetPoint();
36+
double readKpSetPoint();
37+
double readKiSetPoint();
38+
double readKdSetPoint();
39+
double readHeatSetPoint();
40+
double readAmplitudeSetPoint();
41+
double readFrequencySetPoint();
42+
43+
private:
44+
void writeDouble(int address, double value);
45+
double readDouble(int address, double defaultValue);
46+
const int PH_ADDRESS = 0;
47+
const int TEMP_ADDRESS = 4;
48+
const int TANKID_ADDRESS = 8;
49+
const int TEMP_CORR_ADDRESS = 12;
50+
const int KP_ADDRESS = 20;
51+
const int KI_ADDRESS = 28;
52+
const int KD_ADDRESS = 36;
53+
const int MAC_ADDRESS = 44;
54+
const int HEAT_ADDRESS = 52;
55+
const int AMPLITUDE_ADDRESS = 56;
56+
const int FREQUENCY_ADDRESS = 60;
57+
const int GRANULARITY_ADDRESS = 64; // granularity for SD logging interval
58+
const int MAX_DATA_AGE_ADDRESS = 68; // max data age for SD card
59+
const int PH_SERIES_SIZE_ADDRESS = 72;
60+
const int PH_SERIES_POINTER_ADDRESS = 76;
61+
const int TEMP_SERIES_SIZE_ADDRESS = 80;
62+
const int TEMP_SERIES_POINTER_ADDRESS = 84;
63+
const int PH_INTERVAL_ADDRESS = 88;
64+
const int PH_DELAY_ADDRESS = 92;
65+
const int TEMP_INTERVAL_ADDRESS = 96;
66+
const int TEMP_DELAY_ADDRESS = 100;
67+
};

test/EEPROM.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <Arduino.h>
2+
#include <ArduinoUnitTests.h>
3+
4+
#include "EEPROM_TC.h"
5+
6+
EEPROM_TC eepromTC;
7+
8+
unittest(pHSetPoint) {
9+
eepromTC.writepHSetPoint(7.1);
10+
double value = eepromTC.readpHSetPoint();
11+
assertEqual(7.1, value);
12+
}
13+
14+
unittest(TempSetPoint) {
15+
eepromTC.writeTempSetPoint(3.1);
16+
double value = eepromTC.readTempSetPoint();
17+
assertEqual(3.1, value);
18+
}
19+
20+
unittest(KpSetPoint) {
21+
eepromTC.writeKpSetPoint(500);
22+
double value = eepromTC.readKpSetPoint();
23+
assertEqual(500, value);
24+
}
25+
26+
unittest(KiSetPoint) {
27+
eepromTC.writeKiSetPoint(10.1);
28+
double value = eepromTC.readKiSetPoint();
29+
assertEqual(10.1, value);
30+
}
31+
32+
unittest(KdSetPoint) {
33+
eepromTC.writeKdSetPoint(11.2);
34+
double value = eepromTC.readKdSetPoint();
35+
assertEqual(11.2, value);
36+
}
37+
38+
unittest(HeatSetPoint) {
39+
eepromTC.writeHeatSetPoint(7.9);
40+
double value = eepromTC.readHeatSetPoint();
41+
assertEqual(7.9, 0);
42+
}
43+
44+
unittest(AmplitudeSetPoint) {
45+
eepromTC.writeAmplitudeSetPoint(3.14);
46+
double value = eepromTC.readAmplitudeSetPoint();
47+
assertEqual(3.14, value);
48+
}
49+
50+
unittest(FrequencySetPoint) {
51+
eepromTC.writeFrequencySetPoint(5.5);
52+
double value = eepromTC.readFrequencySetPoint();
53+
assertEqual(5.5, value);
54+
}
55+
56+
unittest_main()

0 commit comments

Comments
 (0)