Permalink
Browse files

refactoring classes to remove floating point logic, decreasing progra…

…m footprint in flash memory
  • Loading branch information...
simonrupf committed Aug 1, 2017
1 parent 94c5172 commit ad5ef84358f70dc76810cef95a641ef96d5bbb4d
View
@@ -1,3 +1,4 @@
*.pdf
*.png
*.jpg
*.JPG
@@ -30,7 +30,7 @@
/**
* Returns the last sensor reading.
*/
float IMonSens_Sensor::getReading() {
int16_t IMonSens_Sensor::getReading() {
return reading;
}
@@ -55,11 +55,16 @@ void IMonSens_Communicator::askSensors(const char* input) {
// returning the measurement if it does
for (uint8_t i = 0; i < sensorCount; ++i) {
if (sensors[i]->measure(input)) {
char measurement[MONSENS_MAX_MEASUREMENT_WIDTH];
dtostrf(sensors[i]->getReading(), 0, 2, measurement);
for (uint8_t j = 0; measurement[j] && j < MONSENS_MAX_MEASUREMENT_WIDTH; ++j) {
write(measurement[j]);
int16_t reading = sensors[i]->getReading();
if (reading < 0 && reading > -99) {
write('-');
}
writeInt(reading / 100);
write('.');
if (reading < 10 && reading > -10) {
write('0');
}
writeInt(((reading < 0) ? - (unsigned) reading : reading) % 100);
write('\r');
write('\n');
return;
@@ -91,3 +96,14 @@ void IMonSens_Communicator::askSensors(const char* input) {
}
}
/**
* Write integer to the MCUs interface.
*/
void IMonSens_Communicator::writeInt(const int16_t reading) {
char measurement[MONSENS_MAX_MEASUREMENT_WIDTH];
snprintf(measurement, MONSENS_MAX_MEASUREMENT_WIDTH, "%d", reading);
for (uint8_t i = 0; measurement[i] && i < MONSENS_MAX_MEASUREMENT_WIDTH; ++i) {
write(measurement[i]);
}
}
@@ -58,7 +58,7 @@ class IMonSens_Sensor {
/**
* Returns the last sensor reading.
*/
float getReading();
int16_t getReading();
/**
* If no sensor supports the input value, usage instructions are collected.
@@ -69,7 +69,7 @@ class IMonSens_Sensor {
/**
* Last measured sensor reading.
*/
float reading = 0.0;
int16_t reading = 0;
};
/**
@@ -108,6 +108,11 @@ class IMonSens_Communicator {
*/
void askSensors(const char* input);
/**
* Write integer to the MCUs interface.
*/
void writeInt(const int16_t reading);
/**
* Write output to the MCUs interface.
*/
@@ -59,23 +59,23 @@ bool MonSens_BME280::measure(const char* input) {
if (strstr(input, "C") != NULL) {
// read twice to avoid cached value
sensor.readTemperature();
reading = sensor.readTemperature();
reading = sensor.readTemperature() * 100;
} else if (strstr(input, "K") != NULL) {
// read twice to avoid cached value
sensor.readTemperature();
reading = sensor.readTemperature() + 273.15F;
reading = (sensor.readTemperature() * 100) + 27315;
} else if (strstr(input, "hPa") != NULL) {
// read twice to avoid cached value
sensor.readPressure();
reading = sensor.readPressure() / 100.0F;
reading = sensor.readPressure();
} else if (strstr(input, "m") != NULL) {
// read twice to avoid cached value
sensor.readAltitude(SEALEVELPRESSURE_HPA);
reading = sensor.readAltitude(SEALEVELPRESSURE_HPA);
reading = sensor.readAltitude(SEALEVELPRESSURE_HPA) * 100;
} else if (strstr(input, "RH") != NULL) {
// read twice to avoid cached value
sensor.readHumidity();
reading = sensor.readHumidity();
reading = sensor.readHumidity() * 100;
} else {
return false;
}
@@ -59,19 +59,19 @@ bool MonSens_BMP280::measure(const char* input) {
if (strstr(input, "C") != NULL) {
// read twice to avoid cached value
sensor.readTemperature();
reading = sensor.readTemperature();
reading = sensor.readTemperature() * 100;
} else if (strstr(input, "K") != NULL) {
// read twice to avoid cached value
sensor.readTemperature();
reading = sensor.readTemperature() + 273.15F;
reading = (sensor.readTemperature() * 100) + 27315;
} else if (strstr(input, "hPa") != NULL) {
// read twice to avoid cached value
sensor.readPressure();
reading = sensor.readPressure() / 100.0F;
reading = sensor.readPressure();
} else if (strstr(input, "m") != NULL) {
// read twice to avoid cached value
sensor.readAltitude(SEALEVELPRESSURE_HPA);
reading = sensor.readAltitude(SEALEVELPRESSURE_HPA);
reading = sensor.readAltitude(SEALEVELPRESSURE_HPA) * 100;
} else {
return false;
}
@@ -55,10 +55,10 @@ void MonSens_DallasTemperature::init() {
bool MonSens_DallasTemperature::measure(const char* input) {
if (strstr(input, "C") != NULL) {
sensor.requestTemperaturesByIndex(sensorIndex);
reading = sensor.getTempCByIndex(sensorIndex);
reading = sensor.getTempCByIndex(sensorIndex) * 100;
} else if (strstr(input, "K") != NULL) {
sensor.requestTemperaturesByIndex(sensorIndex);
reading = sensor.getTempCByIndex(sensorIndex) + 273.15F;
reading = (sensor.getTempCByIndex(sensorIndex) * 100) + 27315;
} else {
return false;
}
@@ -47,7 +47,7 @@ void MonSens_Photoresistor::init() {
*/
bool MonSens_Photoresistor::measure(const char* input) {
if (strstr(input, "R") != NULL) {
reading = (float) analogRead(sensorPin);
reading = analogRead(sensorPin) * 100;
return true;
}
return false;
@@ -55,11 +55,11 @@ void MonSens_Si7021::init() {
*/
bool MonSens_Si7021::measure(const char* input) {
if (strstr(input, "C") != NULL) {
reading = sensor.readTemperature();
reading = sensor.readTemperature() * 100;
} else if (strstr(input, "K") != NULL) {
reading = sensor.readTemperature() + 273.15F;
reading = (sensor.readTemperature() * 100) + 27315;
} else if (strstr(input, "RH") != NULL) {
reading = sensor.readHumidity();
reading = sensor.readHumidity() * 100;
} else {
return false;
}
@@ -39,7 +39,7 @@ void MonSens_DigiSpark_Temperature::init() {
*/
bool MonSens_DigiSpark_Temperature::measure(const char* input) {
if (strstr(input, "C") != NULL) {
reading = (float) (analogRead(A0+15) - 273);
reading = (analogRead(A0+15) - 273) * 100;
return true;
}
return false;
@@ -61,7 +61,7 @@ bool MonSens_DigiSpark_Vcc::measure(const char* input) {
uint8_t high = ADCH; // unlock both
uint16_t result = (high << 8) | low;
reading = (1.1 * 1023.0) / (float) result; // Calculate Vcc
reading = 112530 / result; // Calculate Vcc
return true;
}
return false;
@@ -39,7 +39,7 @@ void MonSens_ESP8266_Vcc::init() {
*/
bool MonSens_ESP8266_Vcc::measure(const char* input) {
if (strstr(input, "V") != NULL) {
reading = (float) ESP.getVcc() / 1000.0;
reading = ESP.getVcc() / 10;
return true;
}
return false;
@@ -5,14 +5,27 @@
* serial interface.
*/
#include <DigiCDC.h>
// (optional) conserve memory by reducing the reserved space for the following
// variables
// maximum sensors supported, defaults to 10 (this example only uses 2)
#define MONSENS_MAX_SENSORS 10
// maximum length of measurement strings, defaults to 9 (5 digits, 1 decimal
// point, 2 digits after the point and 1 string termination character)
#define MONSENS_MAX_MEASUREMENT_WIDTH 9
#include <MonSens_DigiSpark.h>
MonSens_DigiSpark mcu;
#include <MonSens_DigiSpark_Temperature.h>
MonSens_DigiSpark_Temperature temp;
/**
* initialize USB interface
*/
void setup() {
SerialUSB.begin();
analogReference(INTERNAL1V1);
mcu.init();
mcu.addSensor(temp);
}
/**
@@ -22,22 +35,6 @@ void setup() {
* according result
*/
void loop() {
if (SerialUSB.available()) {
char c;
c = SerialUSB.read(); // gets one byte from serial buffer
SerialUSB.delay(50); // wait for the shell to be ready to read
if (c == 'C' || c == 'K') {
int temp = analogRead(A0+15);
SerialUSB.refresh();
if (c == 'C') {
temp -= 273;
}
char val[5];
sprintf(val, "%d", temp + 8);
SerialUSB.print(val);
SerialUSB.print("\n");
}
}
SerialUSB.delay(3);
mcu.communicate();
}

0 comments on commit ad5ef84

Please sign in to comment.