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
49 changes: 25 additions & 24 deletions src/Devices/PHProbe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,26 @@ void PHProbe::clearCalibration() {
}

void PHProbe::sendSlopeRequest() {
Serial1.print("SLOPE,?\r"); // Sending request for Calibration Slope
slopeResponse = " Slope requested!";
// Sending request for Calibration Slope
Serial1.print("SLOPE,?\r");
strncpy(slopeResponse, " Slope requested!", sizeof(slopeResponse)); // Flawfinder: ignore
}

String PHProbe::getSlope() {
void PHProbe::getSlope(char *buffer, int size) {
// for example "?SLOPE,99.7,100.3, -0.89"
if (slopeResponse.length() < 10) {
return String("");
if (strlen(slopeResponse) > 10) { // Flawfinder: ignore
strncpy(buffer, slopeResponse + 7, size); // Flawfinder: ignore
} else {
buffer[0] = '\0';
}
String slope = slopeResponse.substring(7);
return slope;
}

/**
* interrupt handler for data arriving from probe
*/
void PHProbe::serialEvent1() {
while (Serial1.available() > 0) { // if we see that the Atlas Scientific product has sent a character
// if we see that the Atlas Scientific product has sent a character
while (Serial1.available() > 0) {
String string = Serial1.readStringUntil('\r'); // read the string until we see a <CR>
if (string.length() > 0 && string[string.length() - 1] == '\r') {
// We should not see the CR (https://github.com/Arduino-CI/arduino_ci/pull/302)
Expand All @@ -79,7 +81,7 @@ void PHProbe::serialEvent1() {
serial("PHProbe serialEvent1: \"%s\"", string.c_str());
if (string.length() > 7 && string.substring(0, 7) == "?SLOPE,") {
// for example "?SLOPE,16.1,100.0"
slopeResponse = string;
strncpy(slopeResponse, string.c_str(), sizeof(slopeResponse)); // Flawfinder: ignore
}
}
}
Expand All @@ -90,31 +92,30 @@ void PHProbe::serialEvent1() {
// water becomes more acidic at higher temperatures."
// https://www.westlab.com/blog/2017/11/15/how-does-temperature-affect-ph
void PHProbe::setTemperatureCompensation(float temperature) {
const String PARTIAL_COMMAND = "T,";
String fullCommand;
char buffer[10];
if (temperature > 0 && temperature < 100) {
fullCommand = PARTIAL_COMMAND + String(temperature, 2) + "\r";
snprintf(buffer, sizeof(buffer), "T,%.2f\r", temperature);
} else {
fullCommand = PARTIAL_COMMAND + "20\r";
snprintf(buffer, sizeof(buffer), "T,20\r");
}
serial("PHProbe::setTemperatureCompensation) - %s", fullCommand.c_str());
Serial1.print(fullCommand); // send that string to the Atlas Scientific product
serial("PHProbe::setTemperatureCompensation) - %s", buffer);
Serial1.print(buffer); // send that string to the Atlas Scientific product
}

void PHProbe::setHighpointCalibration(float highpoint) {
String fullCommand;
fullCommand = "Cal,High," + String(highpoint, 3) + "\r";
Serial1.print(fullCommand); // send that string to the Atlas Scientific product
char buffer[17];
snprintf(buffer, sizeof(buffer), "Cal,High,%.3f\r", highpoint);
Serial1.print(buffer); // send that string to the Atlas Scientific product
}

void PHProbe::setLowpointCalibration(float lowpoint) {
String fullCommand;
fullCommand = "Cal,low," + String(lowpoint, 3) + "\r";
Serial1.print(fullCommand); // send that string to the Atlas Scientific product
char buffer[16];
snprintf(buffer, sizeof(buffer), "Cal,low,%.3f\r", lowpoint);
Serial1.print(buffer); // send that string to the Atlas Scientific product
}

void PHProbe::setMidpointCalibration(float midpoint) {
String fullCommand;
fullCommand = "Cal,mid," + String(midpoint, 3) + "\r";
Serial1.print(fullCommand); // send that string to the Atlas Scientific product
char buffer[16];
snprintf(buffer, sizeof(buffer), "Cal,mid,%.3f\r", midpoint);
Serial1.print(buffer); // send that string to the Atlas Scientific product
}
6 changes: 3 additions & 3 deletions src/Devices/PHProbe.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class PHProbe {
float getPh() {
return value;
}
String getSlopeResponse() {
const char* getSlopeResponse() const {
return slopeResponse;
}
void clearCalibration();
String getSlope();
void getSlope(char* buffer, int size);
void sendSlopeRequest();
void serialEvent1();
void setHighpointCalibration(float highpoint);
Expand All @@ -36,7 +36,7 @@ class PHProbe {
static PHProbe* _instance;
// instance variable
float value = 0;
String slopeResponse = "";
char slopeResponse[32] = "";
// Methods
PHProbe();
};
27 changes: 13 additions & 14 deletions src/Devices/SD_TC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,33 @@ SD_TC::SD_TC() {
/**
* append data to a data log file
*/
void SD_TC::appendData(String header, String line) {
String path = todaysDataFileName();
if (!sd.exists(path.c_str())) {
appendDataToPath(header, path.c_str());
void SD_TC::appendData(const char* header, const char* line) {
char path[30];
todaysDataFileName(path, sizeof(path));
if (!sd.exists(path)) {
appendDataToPath(header, path);
COUT(header);
}
appendDataToPath(line, path.c_str());
appendDataToPath(line, path);
COUT(line);
}

/**
* append data to a path
*/
void SD_TC::appendDataToPath(String line, String path) {
void SD_TC::appendDataToPath(const char* line, const char* path) {
COUT(path);
File file = sd.open(path, O_CREAT | O_WRONLY | O_APPEND);
if (file) {
file.write(line.c_str(), line.length());
file.write(line);
file.write("\n", 1);
file.close();
COUT(file);
} else {
if (!hasHadError) {
hasHadError = true;
serial("Unable to open file: \"%s\"", path.c_str());
COUT("Unable to open file: \"" << path.c_str() << "\"");
serial("Unable to open file: \"%s\"", path);
COUT("Unable to open file: \"" << path << "\"");
return;
}
}
Expand All @@ -68,7 +69,7 @@ void SD_TC::appendDataToPath(String line, String path) {
/**
* append data to a serial log file
*/
void SD_TC::appendToLog(String line) {
void SD_TC::appendToLog(const char* line) {
DateTime_TC now = DateTime_TC::now();
char path[30];
snprintf(path, sizeof(path), "%4i%02i%02i.log", now.year(), now.month(), now.day());
Expand Down Expand Up @@ -98,10 +99,8 @@ void SD_TC::printRootDirectory() {
// serial("SD_TC::printRootDirectory()");
}

String SD_TC::todaysDataFileName() {
void SD_TC::todaysDataFileName(char* path, int size) {
DateTime_TC now = DateTime_TC::now();
char path[30];
snprintf(path, sizeof(path), "%4i%02i%02i.csv", now.year(), now.month(), now.day());
snprintf(path, size, "%4i%02i%02i.csv", now.year(), now.month(), now.day());
COUT(path);
return String(path);
}
11 changes: 4 additions & 7 deletions src/Devices/SD_TC.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
#define SS 4
#include <SdFat.h>

typedef void (*visitor)(File* pEntry, String parentPath);

class SD_TC {
public:
// class methods
static SD_TC* instance();

// instance methods
void appendData(String header, String line);
void appendToLog(String line);
void appendData(const char* header, const char* line);
void appendToLog(const char* line);
bool exists(const char* path);
bool format();
bool mkdir(const char* path);
File open(const char* path, oflag_t oflag = 0x00);
String todaysDataFileName();
void todaysDataFileName(char* path, int size);
void printRootDirectory();
void visit(visitor pFunction);

private:
// class variables
Expand All @@ -34,5 +31,5 @@ class SD_TC {

// instance methods
SD_TC();
void appendDataToPath(String data, String path);
void appendDataToPath(const char* data, const char* path);
};
4 changes: 3 additions & 1 deletion src/UIState/SeeLogFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

void SeeLogFile::start() {
LiquidCrystal_TC::instance()->writeLine(prompt(), 0);
LiquidCrystal_TC::instance()->writeLine(SD_TC::instance()->todaysDataFileName().c_str(), 1);
char path[30];
SD_TC::instance()->todaysDataFileName(path, sizeof(path));
LiquidCrystal_TC::instance()->writeLine(path, 1);
}
6 changes: 4 additions & 2 deletions src/UIState/SeePHSlope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#include "Devices/PHProbe.h"

void SeePHSlope::loop() {
LiquidCrystal_TC::instance()->writeLine(PHProbe::instance()->getSlope().c_str(), 1);
char buffer[20];
PHProbe::instance()->getSlope(buffer, sizeof(buffer));
LiquidCrystal_TC::instance()->writeLine(buffer, 1);
}

void SeePHSlope::start() {
LiquidCrystal_TC::instance()->writeLine(prompt(), 0);
LiquidCrystal_TC::instance()->writeLine("requesting slope", 1);
PHProbe::instance()->sendSlopeRequest();
}
}
9 changes: 5 additions & 4 deletions test/PHProbeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ unittest(getSlope) {
PHProbe *pPHProbe = PHProbe::instance();
GODMODE()->serialPort[1].dataIn = "?SLOPE,99.7,100.3,-0.89\r"; // the queue of data waiting to be read
tc->serialEvent1(); // fake interrupt
String slope = pPHProbe->getSlope();
assertEqual("99.7,100.3,-0.89", slope);
char buffer[20];
pPHProbe->getSlope(buffer, sizeof(buffer));
assertEqual("99.7,100.3,-0.89", buffer);
COUT(state->serialPort[0].dataOut.length());
GODMODE()->serialPort[1].dataIn = "?SLOPE,98.7,101.3,-0.89\r"; // the answer to getSlop() waiting to be read
tc->serialEvent1(); // fake interrupt
COUT(state->serialPort[0].dataOut.length());
state->serialPort[0].dataOut = "";
slope = pPHProbe->getSlope();
assertEqual("98.7,101.3,-0.89", slope);
pPHProbe->getSlope(buffer, sizeof(buffer));
assertEqual("98.7,101.3,-0.89", buffer);
}

unittest(getPh) {
Expand Down