Skip to content
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: 2 additions & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Dnominal
docstring
Doxyfile
Doxygen
dtostrf
dylib
eep
eeprom
Expand Down Expand Up @@ -131,6 +132,7 @@ sizeof
snprintf
solonoids
src
stdlib
strcpy
strlen
strncmp
Expand Down
2 changes: 1 addition & 1 deletion scripts/testAndBuild.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#! /bin/sh
bundle install --path vendor/bundle
bundle exec arduino_ci.rb --min-free-space=5800
bundle exec arduino_ci.rb --min-free-space=6000
2 changes: 1 addition & 1 deletion src/Devices/EthernetServer_TC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void EthernetServer_TC::echo() {
} else {
buffer[i - 3] = '\0';
serial(F("echo \"%s\""), buffer + 19);
sendHeadersWithSize(strnlen(buffer + 19, sizeof(buffer) - 20));
sendHeadersWithSize(strnlen(buffer, sizeof(buffer)) - 19);
client.write(buffer + 19);
client.stop();
state = NOT_CONNECTED;
Expand Down
2 changes: 1 addition & 1 deletion src/Devices/EthernetServer_TC.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class EthernetServer_TC : public EthernetServer {
// instance variables
EthernetClient client;
serverState_t state = NOT_CONNECTED;
char buffer[1024];
char buffer[512];
int bufferContentsSize = 0;
unsigned long connectedAt = 0;

Expand Down
12 changes: 10 additions & 2 deletions src/Devices/PHControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ PHControl::PHControl() {
targetPh = DEFAULT_PH;
EEPROM_TC::instance()->setPH(targetPh);
}
serial(F("PHControl with target pH = %5.3f"), targetPh);
char buffer[40];
strncpy_P(buffer, (PGM_P)F("PHControl with target pH = "), sizeof(buffer));
dtostrf(targetPh, 5, 3, buffer + strnlen(buffer, sizeof(buffer)));
serial(buffer);
}

void PHControl::setTargetPh(float newPh) {
if (targetPh != newPh) {
serial(F("change target pH from %6.4f to %6.4f"), targetPh, newPh);
char buffer[40];
strncpy_P(buffer, (PGM_P)F("change target pH from "), sizeof(buffer));
dtostrf(targetPh, 5, 3, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(" to "));
dtostrf(newPh, 5, 3, buffer + strnlen(buffer, sizeof(buffer)));
serial(buffer);
targetPh = newPh;
EEPROM_TC::instance()->setPH(newPh);
}
Expand Down
15 changes: 10 additions & 5 deletions src/Devices/PHProbe.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Devices/PHProbe.h"

#include <avr/wdt.h>
#include <stdlib.h>

#include "Devices/Serial_TC.h"

Expand Down Expand Up @@ -73,7 +74,10 @@ void PHProbe::serialEvent1() {
// treat 0 as valid since probe might not be connected
wdt_disable();
wdt_enable(WDTO_120MS); // allow enough time to print message
serial(F("pH value dropped to %5.3f so trigger a reset!"), value);
char buffer[50];
strncpy_P(buffer, (PGM_P)F("Triggering a reset because pH dropped to "), sizeof(buffer));
dtostrf(value, 5, 3, buffer + strnlen(buffer, sizeof(buffer)));
serial(buffer);
while (true) {
}
}
Expand All @@ -94,7 +98,7 @@ void PHProbe::serialEvent1() {
void PHProbe::setTemperatureCompensation(float temperature) {
char buffer[10];
if (temperature > 0 && temperature < 100) {
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("T,%.2f\r"), temperature);
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("T,%i.%i\r"), (int)temperature, (int)(temperature * 100 + 0.5) % 100);
} else {
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("T,20\r"));
}
Expand All @@ -104,18 +108,19 @@ void PHProbe::setTemperatureCompensation(float temperature) {

void PHProbe::setHighpointCalibration(float highpoint) {
char buffer[17];
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Cal,High,%.3f\r"), highpoint);
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Cal,High,%i.%i\r"), (int)highpoint,
(int)(highpoint * 1000 + 0.5) % 1000);
Serial1.print(buffer); // send that string to the Atlas Scientific product
}

void PHProbe::setLowpointCalibration(float lowpoint) {
char buffer[16];
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Cal,low,%.3f\r"), lowpoint);
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Cal,low,%i.%i\r"), (int)lowpoint, (int)(lowpoint * 1000 + 0.5) % 1000);
Serial1.print(buffer); // send that string to the Atlas Scientific product
}

void PHProbe::setMidpointCalibration(float midpoint) {
char buffer[16];
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Cal,mid,%.3f\r"), midpoint);
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Cal,mid,%i.%i\r"), (int)midpoint, (int)(midpoint * 1000 + 0.5) % 1000);
Serial1.print(buffer); // send that string to the Atlas Scientific product
}
12 changes: 10 additions & 2 deletions src/Devices/PID_TC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ float PID_TC::computeOutput(float target, float current) {
}

void PID_TC::logToSerial() {
serial(F("Kp: %6.1f Ki: %6.1f Kd: %6.1f\r\nPID output (s):%4.1f"), pPID->GetKp(), pPID->GetKi(), pPID->GetKd(),
static_cast<float>(output) / 1000);
char buffer[70];
strncpy_P(buffer, (PGM_P)F("Kp: "), sizeof(buffer));
dtostrf(pPID->GetKp(), 6, 1, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(" Ki: "));
dtostrf(pPID->GetKi(), 6, 1, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(" Kd: "));
dtostrf(pPID->GetKd(), 6, 1, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F("\r\nPID output in seconds:"));
dtostrf(static_cast<float>(output) / 1000, 4, 1, buffer + strnlen(buffer, sizeof(buffer)));
serial(buffer);
}

void PID_TC::setKd(float Kd) {
Expand Down
5 changes: 3 additions & 2 deletions src/Devices/PushingBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ void PushingBox::sendData() {
snprintf_P(buffer, sizeof(buffer), (PGM_P)format, DevID, tankID);
} else {
static const char format[] PROGMEM =
"GET /pushingbox?devid=%s&tankid=%i&tempData=%.2f&pHdata=%.3f HTTP/1.1\r\n"
"GET /pushingbox?devid=%s&tankid=%i&tempData=%i.%i&pHdata=%i.%i HTTP/1.1\r\n"
"Host: api.pushingbox.com\r\n"
"Connection: close\r\n"
"\r\n";
// look up tankid, temperature, ph
float temperature = TempProbe_TC::instance()->getRunningAverage();
float pH = PHProbe::instance()->getPh();
snprintf_P(buffer, sizeof(buffer), (PGM_P)format, DevID, tankID, temperature, pH);
snprintf_P(buffer, sizeof(buffer), (PGM_P)format, DevID, tankID, (int)temperature,
(int)(temperature * 100 + 0.5) % 100, (int)pH, (int)(pH * 1000) % 1000);
}
size_t i = 0;
for (; i < sizeof(buffer); ++i) {
Expand Down
4 changes: 4 additions & 0 deletions src/Devices/Serial_TC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ void serial(const __FlashStringHelper *format...) {
va_end(args);
}

void serial(const char *buffer) {
serial(F("%s"), buffer);
}

/**
* static variable for singleton
*/
Expand Down
1 change: 1 addition & 0 deletions src/Devices/Serial_TC.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Arduino.h>

void serial(const __FlashStringHelper *format...);
void serial(const char *buffer);

class Serial_TC {
public:
Expand Down
5 changes: 4 additions & 1 deletion src/Devices/TempProbe_TC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ TempProbe_TC::TempProbe_TC() {
correction = 0;
EEPROM_TC::instance()->setCorrectedTemp(correction);
}
serial(F("Temperature probe with correction of %6.3f"), correction);
char buffer[50];
strncpy(buffer, (PGM_P)F("Temperature probe with correction of "), sizeof(buffer));
dtostrf(correction, 5, 2, buffer + strnlen(buffer, sizeof(buffer)));
serial(buffer);
}

/**
Expand Down
21 changes: 16 additions & 5 deletions src/Devices/TemperatureControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ void TemperatureControl::enableHeater(bool flag) {
if (_instance && (_instance->isHeater() != flag)) {
delete _instance;
_instance = nullptr;
serial(F("TemperatureControl::enableHeater(%s)"), flag ? "true" : "false");
instance();
char buffer[50];
strncpy_P(buffer, (PGM_P)F("TemperatureControl::enableHeater("), sizeof(buffer));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), flag ? (PGM_P)F("true)") : (PGM_P)F("false)"));
serial(buffer);
}
}

Expand All @@ -50,8 +52,12 @@ TemperatureControl::TemperatureControl() {
}
pinMode(TEMP_CONTROL_PIN, OUTPUT);
digitalWrite(TEMP_CONTROL_PIN, TURN_SOLENOID_OFF);
serial(F("%s starts with solenoid off with target temperature of %5.2f C"), this->isHeater() ? "Heater" : "Chiller",
targetTemperature);
char buffer[70];
strcpy_P(buffer, this->isHeater() ? (PGM_P)F("Heater") : (PGM_P)F("Chiller"));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(" starts with solenoid off with target temperature of "));
dtostrf(targetTemperature, 5, 2, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(" C"));
serial(buffer);
}

/**
Expand All @@ -70,7 +76,12 @@ bool TemperatureControl::isOn() {
*/
void TemperatureControl::setTargetTemperature(float newTemperature) {
if (targetTemperature != newTemperature) {
serial(F("Change target temperature from %5.2f to %5.2f"), targetTemperature, newTemperature);
char buffer[50];
strncpy_P(buffer, (PGM_P)F("change target temperature from "), sizeof(buffer));
dtostrf(targetTemperature, 5, 2, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(" to "));
dtostrf(newTemperature, 5, 2, buffer + strnlen(buffer, sizeof(buffer)));
serial(buffer);
EEPROM_TC::instance()->setTemp(newTemperature);
targetTemperature = newTemperature;
}
Expand Down
31 changes: 21 additions & 10 deletions src/TankControllerLib.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "TankControllerLib.h"

#include <avr/wdt.h>
#include <stdlib.h>

#include "Devices/DateTime_TC.h"
#include "Devices/EEPROM_TC.h"
Expand Down Expand Up @@ -254,22 +255,28 @@ void TankControllerLib::writeDataToSD() {
snprintf_P(currentTemp, sizeof(currentTemp), (PGM_P)F("C"));
snprintf_P(currentPh, sizeof(currentPh), (PGM_P)F("C"));
} else {
snprintf_P(currentTemp, sizeof(currentTemp), (PGM_P)F("%4.2f"),
(float)TempProbe_TC::instance()->getRunningAverage());
snprintf_P(currentPh, sizeof(currentPh), (PGM_P)F("%5.3f"), (float)PHProbe::instance()->getPh());
dtostrf((float)TempProbe_TC::instance()->getRunningAverage(), 4, 2, currentTemp);
dtostrf((float)PHProbe::instance()->getPh(), 5, 3, currentPh);
}
char targetTemp[10];
char targetPh[10];
dtostrf(TemperatureControl::instance()->getTargetTemperature(), 4, 2, targetTemp);
dtostrf(PHControl::instance()->getTargetPh(), 5, 3, targetPh);
static const char header[] = "time,tankid,temp,temp setpoint,pH,pH setpoint,onTime,Kp,Ki,Kd";
static const char format[] PROGMEM =
"%02i/%02i/%4i %02i:%02i:%02i, %3i, %s, %4.2f, %s, %5.3f, %4lu, %8.1f, %8.1f, %8.1f";
static const char format[] PROGMEM = "%02i/%02i/%4i %02i:%02i:%02i, %3i, %s, %s, %s, %s, %4lu";
char buffer[128];
DateTime_TC dtNow = DateTime_TC::now();
PID_TC *pPID = PID_TC::instance();
uint16_t tankId = EEPROM_TC::instance()->getTankID();
snprintf_P(buffer, sizeof(buffer), (PGM_P)format, (uint16_t)dtNow.month(), (uint16_t)dtNow.day(),
(uint16_t)dtNow.year(), (uint16_t)dtNow.hour(), (uint16_t)dtNow.minute(), (uint16_t)dtNow.second(),
(uint16_t)tankId, currentTemp, (float)TemperatureControl::instance()->getTargetTemperature(), currentPh,
(float)PHControl::instance()->getTargetPh(), (unsigned long)(millis() / 1000), (float)pPID->getKp(),
(float)pPID->getKi(), (float)pPID->getKd());
(uint16_t)tankId, currentTemp, targetTemp, currentPh, targetPh, (unsigned long)(millis() / 1000));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(", "));
dtostrf(pPID->getKp(), 8, 1, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(", "));
dtostrf(pPID->getKi(), 8, 1, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(", "));
dtostrf(pPID->getKd(), 8, 1, buffer + strnlen(buffer, sizeof(buffer)));
SD_TC::instance()->appendData(header, buffer);
nextWriteTime = msNow / 1000 * 1000 + 1000; // round up to next second
COUT(buffer);
Expand All @@ -283,8 +290,12 @@ void TankControllerLib::writeDataToSerial() {
uint32_t msNow = millis();
if (nextWriteTime <= msNow) {
DateTime_TC dtNow = DateTime_TC::now();
serial(F("%02d:%02d pH=%5.3f temp=%5.2f"), (uint16_t)dtNow.hour(), (uint16_t)dtNow.minute(),
(float)PHProbe::instance()->getPh(), (float)TempProbe_TC::instance()->getRunningAverage());
char buffer[30];
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("%02d:%02d pH="), (uint16_t)dtNow.hour(), (uint16_t)dtNow.minute());
dtostrf((float)PHProbe::instance()->getPh(), 5, 3, buffer + strnlen(buffer, sizeof(buffer)));
strcpy_P(buffer + strnlen(buffer, sizeof(buffer)), (PGM_P)F(" temp="));
dtostrf((float)TempProbe_TC::instance()->getRunningAverage(), 5, 2, buffer + strnlen(buffer, sizeof(buffer)));
serial(buffer);
nextWriteTime = msNow / 60000 * 60000 + 60000; // round up to next minute
COUT(buffer);
}
Expand Down
26 changes: 18 additions & 8 deletions src/UIState/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,19 @@ void MainMenu::selectSet() {
}
}

// show current temp and pH
// pH=7.325 B 7.125
// T=12.25 H 12.75
void MainMenu::idle() {
char output[17];
PHControl *phControl = PHControl::instance();
char equals = millis() / 1000 % 2 ? '=' : ' ';
snprintf_P(output, sizeof(output), (PGM_P)F("pH%c%5.3f %c %5.3f"), equals, PHProbe::instance()->getPh(),
(phControl->isOn() ? 'B' : ' '), PHControl::instance()->getTargetPh());
char output[17];
output[0] = 'p';
output[1] = 'H';
output[2] = millis() / 1000 % 2 ? '=' : ' ';
dtostrf(PHProbe::instance()->getPh(), 5, 3, output + 3);
output[8] = ' ';
output[9] = phControl->isOn() ? 'B' : ' ';
output[10] = ' ';
dtostrf(PHControl::instance()->getTargetPh(), 5, 3, output + 11);
LiquidCrystal_TC::instance()->writeLine(output, 0);
TemperatureControl *tempControl = TemperatureControl::instance();
TempProbe_TC *tempProbe = TempProbe_TC::instance();
Expand All @@ -234,9 +240,13 @@ void MainMenu::idle() {
if (tempControl->isOn()) {
status = toupper(status);
}

snprintf_P(output, sizeof(output), (PGM_P)F("T%c%5.2f %c %5.2f"), equals, temp, status,
tempControl->getTargetTemperature());
output[0] = 'T';
output[1] = millis() / 1000 % 2 ? '=' : ' ';
dtostrf(temp, 5, 2, output + 2);
output[7] = ' ';
output[8] = status;
output[9] = ' ';
dtostrf(tempControl->getTargetTemperature(), 5, 2, output + 10);
LiquidCrystal_TC::instance()->writeLine(output, 1);
}

Expand Down
8 changes: 4 additions & 4 deletions src/UIState/PHCalibrationHigh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

void PHCalibrationHigh::setValue(float value) {
PHProbe::instance()->setHighpointCalibration(value);

char output[17];
snprintf_P(output, sizeof(output), (PGM_P)F("New High=%5.3f"), value); // "New High=12.345"
LiquidCrystal_TC::instance()->writeLine(output, 1);
char buffer[17];
strncpy_P(buffer, (PGM_P)F("New High="), sizeof(buffer));
dtostrf(value, 5, 3, buffer + strnlen(buffer, sizeof(buffer))); // "New High=12.345"
LiquidCrystal_TC::instance()->writeLine(buffer, 1);
returnToMainMenu(3000);
}
8 changes: 4 additions & 4 deletions src/UIState/PHCalibrationLow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

void PHCalibrationLow::setValue(float value) {
PHProbe::instance()->setLowpointCalibration(value);

char output[17];
snprintf_P(output, sizeof(output), (PGM_P)F("New Low = %5.3f"), value); // "New Low = 12.345"
LiquidCrystal_TC::instance()->writeLine(output, 1);
char buffer[17];
strncpy_P(buffer, (PGM_P)F("New Low = "), sizeof(buffer));
dtostrf(value, 5, 3, buffer + strnlen(buffer, sizeof(buffer))); // "New Low = 12.345"
LiquidCrystal_TC::instance()->writeLine(buffer, 1);
this->setNextState((UIState*)new Wait(tc, 3000, new PHCalibrationHigh(tc)));
}
8 changes: 4 additions & 4 deletions src/UIState/PHCalibrationMid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

void PHCalibrationMid::setValue(float value) {
PHProbe::instance()->setMidpointCalibration(value);

char output[17];
snprintf_P(output, sizeof(output), (PGM_P)F("New Mid = %5.3f"), value); // "New Mid = 12.345"
LiquidCrystal_TC::instance()->writeLine(output, 1);
char buffer[17];
strncpy_P(buffer, (PGM_P)F("New Mid = "), sizeof(buffer));
dtostrf(value, 5, 3, buffer + strnlen(buffer, sizeof(buffer))); // "New Mid = 12.345"
LiquidCrystal_TC::instance()->writeLine(buffer, 1);
this->setNextState((UIState*)new Wait(tc, 3000, new PHCalibrationLow(tc)));
}
9 changes: 6 additions & 3 deletions src/UIState/SeePIDConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ void SeePIDConstants::loop() {

void SeePIDConstants::loadKp(uint16_t line) {
char buffer[17];
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Kp: %.1f"), PID_TC::instance()->getKp());
float value = PID_TC::instance()->getKp();
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Kp: %i.%i"), (int)value, (int)(value * 10 + 0.5) % 10);
LiquidCrystal_TC::instance()->writeLine(buffer, line);
}

void SeePIDConstants::loadKi(uint16_t line) {
char buffer[17];
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Ki: %.1f"), PID_TC::instance()->getKi());
float value = PID_TC::instance()->getKi();
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Ki: %i.%i"), (int)value, (int)(value * 10 + 0.5) % 10);
LiquidCrystal_TC::instance()->writeLine(buffer, line);
}

void SeePIDConstants::loadKd(uint16_t line) {
char buffer[17];
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Kd: %.1f"), PID_TC::instance()->getKd());
float value = PID_TC::instance()->getKd();
snprintf_P(buffer, sizeof(buffer), (PGM_P)F("Kd: %i.%i"), (int)value, (int)(value * 10 + 0.5) % 10);
LiquidCrystal_TC::instance()->writeLine(buffer, line);
}

Expand Down
Loading