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

Add EEPROM encapsulation #9

Merged
merged 6 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
source 'https://rubygems.org'
gem 'arduino_ci', git: 'https://github.com/ianfixes/arduino_ci.git', branch: '2020-10-16_suggestions'
gem 'arduino_ci', git: 'https://github.com/arduino-ci/arduino_ci.git', branch: 'tdd'
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/sh
bundle config --local path vendor/bundle
bundle install
bundle exec arduino_ci_remote.rb --skip-examples-compilation
bundle exec arduino_ci_remote.rb --skip-compilation
101 changes: 101 additions & 0 deletions src/Devices/EEPROM_TC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

#include "EEPROM_TC.h"

void EEPROM_TC::getMacAddress(byte mac[]) {
// Store MAC address in EEPROM
if (EEPROM.read(MAC_ADDRESS) == '#') {
for (int i = 3; i < 6; i++) {
mac[i] = EEPROM.read(i + MAC_ADDRESS);
}
Serial.println(F("MAC Address found in EEPROM and read"));
} else {
Serial.println(F("No MAC Address Found in EEPROM. Generating New MAC."));
for (int i = 3; i < 6; i++) {
mac[i] = i; // TrueRandom.randomByte();
EEPROM.write(i + MAC_ADDRESS, mac[i]);
}
EEPROM.write(MAC_ADDRESS, '#');
}
}

double EEPROM_TC::readTempSetPoint() {
return readDouble(TEMP_ADDRESS, 20);
}

void EEPROM_TC::writeTempSetPoint(double value) {
writeDouble(TEMP_ADDRESS, value);
}

double EEPROM_TC::readpHSetPoint() {
return readDouble(PH_ADDRESS, 8.1);
}

void EEPROM_TC::writepHSetPoint(double value) {
writeDouble(PH_ADDRESS, value);
}

double EEPROM_TC::readKpSetPoint() {
return readDouble(KP_ADDRESS, 100000);
}

void EEPROM_TC::writeKpSetPoint(double value) {
writeDouble(KP_ADDRESS, value);
}

double EEPROM_TC::readKiSetPoint() {
return readDouble(KI_ADDRESS, 0);
}

void EEPROM_TC::writeKiSetPoint(double value) {
writeDouble(KP_ADDRESS, value);
}

double EEPROM_TC::readKdSetPoint() {
return readDouble(KD_ADDRESS, 0);
}

void EEPROM_TC::writeKdSetPoint(double value) {
writeDouble(KD_ADDRESS, value);
}

double EEPROM_TC::readHeatSetPoint() {
return readDouble(HEAT_ADDRESS, 0);
}

void EEPROM_TC::writeHeatSetPoint(double value) {
writeDouble(HEAT_ADDRESS, value);
}

double EEPROM_TC::readAmplitudeSetPoint() {
return readDouble(AMPLITUDE_ADDRESS, 0);
}

void EEPROM_TC::writeAmplitudeSetPoint(double value) {
writeDouble(AMPLITUDE_ADDRESS, value);
}

double EEPROM_TC::readFrequencySetPoint() {
return readDouble(FREQUENCY_ADDRESS, 0);
}

void EEPROM_TC::writeFrequencySetPoint(double value) {
writeDouble(FREQUENCY_ADDRESS, value);
}

void EEPROM_TC::writeDouble(int address, double value) {
if (value != readDouble(address, 1.0/0.0)) {
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++) {
EEPROM.write(address++, *p++);
}
}
}

double EEPROM_TC::readDouble(int address, double defaultValue) {
double value = 0.0;
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++) {
*p++ = EEPROM.read(address++);
}
return isnan(value) ? defaultValue : value;
}
67 changes: 67 additions & 0 deletions src/Devices/EEPROM_TC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* This class provides a wraper for the EEPROM functionality.
* The EEPROM provides memory whose values are kept when the
* board is turned off (like a tiny hard drive).
*
* This device is used by the tank controller to save configuration information.
* - MAC address
* - pH set point
* - auto-tune parameters
* - temperature set point
* - load parameters into EEPROM
* - Write floating point values
* - Read floating point values
*/

#pragma once

#include <Arduino.h>
#include <EEPROM.h>

class EEPROM_TC {
public:
EEPROM_TC();
~EEPROM_TC();
void getMacAddress(byte mac[]);
void writeTempSetPoint(double value);
void writepHSetPoint(double value);
void writeKpSetPoint(double value);
void writeKiSetPoint(double value);
void writeKdSetPoint(double value);
void writeHeatSetPoint(double value);
void writeAmplitudeSetPoint(double vlaue);
void writeFrequencySetPoint(double value);
double readTempSetPoint();
double readpHSetPoint();
double readKpSetPoint();
double readKiSetPoint();
double readKdSetPoint();
double readHeatSetPoint();
double readAmplitudeSetPoint();
double readFrequencySetPoint();

private:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add private methods for read/write double.

void writeDouble(int address, double value);
double readDouble(int address, double defaultValue);
const int PH_ADDRESS = 0;
const int TEMP_ADDRESS = 4;
const int TANKID_ADDRESS = 8;
const int TEMP_CORR_ADDRESS = 12;
const int KP_ADDRESS = 20;
const int KI_ADDRESS = 28;
const int KD_ADDRESS = 36;
const int MAC_ADDRESS = 44;
const int HEAT_ADDRESS = 52;
const int AMPLITUDE_ADDRESS = 56;
const int FREQUENCY_ADDRESS = 60;
const int GRANULARITY_ADDRESS = 64; // granularity for SD logging interval
const int MAX_DATA_AGE_ADDRESS = 68; // max data age for SD card
const int PH_SERIES_SIZE_ADDRESS = 72;
const int PH_SERIES_POINTER_ADDRESS = 76;
const int TEMP_SERIES_SIZE_ADDRESS = 80;
const int TEMP_SERIES_POINTER_ADDRESS = 84;
const int PH_INTERVAL_ADDRESS = 88;
const int PH_DELAY_ADDRESS = 92;
const int TEMP_INTERVAL_ADDRESS = 96;
const int TEMP_DELAY_ADDRESS = 100;
};
56 changes: 56 additions & 0 deletions test/EEPROM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <Arduino.h>
#include <ArduinoUnitTests.h>

#include "EEPROM_TC.h"

EEPROM_TC eepromTC;

unittest(pHSetPoint) {
eepromTC.writepHSetPoint(7.1);
double value = eepromTC.readpHSetPoint();
assertEqual(7.1, value);
}

unittest(TempSetPoint) {
eepromTC.writeTempSetPoint(3.1);
double value = eepromTC.readTempSetPoint();
assertEqual(3.1, value);
}

unittest(KpSetPoint) {
eepromTC.writeKpSetPoint(500);
double value = eepromTC.readKpSetPoint();
assertEqual(500, value);
}

unittest(KiSetPoint) {
eepromTC.writeKiSetPoint(10.1);
double value = eepromTC.readKiSetPoint();
assertEqual(10.1, value);
}

unittest(KdSetPoint) {
eepromTC.writeKdSetPoint(11.2);
double value = eepromTC.readKdSetPoint();
assertEqual(11.2, value);
}

unittest(HeatSetPoint) {
eepromTC.writeHeatSetPoint(7.9);
double value = eepromTC.readHeatSetPoint();
assertEqual(7.9, 0);
}

unittest(AmplitudeSetPoint) {
eepromTC.writeAmplitudeSetPoint(3.14);
double value = eepromTC.readAmplitudeSetPoint();
assertEqual(3.14, value);
}

unittest(FrequencySetPoint) {
eepromTC.writeFrequencySetPoint(5.5);
double value = eepromTC.readFrequencySetPoint();
assertEqual(5.5, value);
}

unittest_main()