Permalink
Browse files

Added flag for enable/disable of Serial output. Disable will sabe 192…

… bytes of flash.
  • Loading branch information...
charlespax committed Mar 8, 2016
1 parent 58df605 commit 72b80b68738a71a26d132df44d6c93bba68b69cd
Showing with 48 additions and 93 deletions.
  1. +18 −43 t400/sd_log.cpp
  2. +3 −3 t400/t400.h
  3. +27 −47 t400/t400.ino
@@ -1,15 +1,11 @@
#include "Arduino.h" // for boolean type
#include "t400.h"
#include "sd_log.h"
#if SD_LOGGING_ENABLED
#include <SdFat.h>
#endif
extern uint8_t temperatureUnit;
namespace sd {
@@ -21,14 +17,6 @@ SdFile file;
uint32_t syncTime = 0; // time of last sync(), in millis()
//void error_P(const char* str) {
// Serial.print("error: ");
// Serial.print(str);
//
// // Stop the SD card
// close();
//}
void init() {
close();
#if SD_LOGGING_ENABLED
@@ -59,77 +47,64 @@ bool open(char* fileName) {
return false;
}
file.clearWriteError();
// write data header
file.print("time (s)");
#endif
#if SERIAL_OUTPUT_ENABLED
Serial.print("v");
Serial.println(FIRMWARE_VERSION);
Serial.print("File: ");
Serial.println(fileName);
// write data header
Serial.print("time (s)");
/* We are no longer using the junction temperature in our data output.
// write data header
file.print("time (s), ambient");
Serial.print("time (s), ambient");
switch(temperatureUnit) {
case TEMPERATURE_UNITS_C:
file.print(" (C)");
Serial.print(" (C)");
break;
case TEMPERATURE_UNITS_F:
file.print(" (F)");
Serial.print(" (F)");
break;
case TEMPERATURE_UNITS_K:
file.print(" (K)");
Serial.print(" (K)");
break;
}*/
#endif
for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
#if SD_LOGGING_ENABLED
file.print(", temp_");
file.print(i, DEC);
#endif
#if SERIAL_OUTPUT_ENABLED
Serial.print(", temp_");
Serial.print(i, DEC);
#endif
switch(temperatureUnit) {
case TEMPERATURE_UNITS_C:
#if SD_LOGGING_ENABLED
file.print(" (C)");
#endif
#if SERIAL_OUTPUT_ENABLED
Serial.print(" (C)");
#endif
break;
case TEMPERATURE_UNITS_F:
#if SD_LOGGING_ENABLED
file.print(" (F)");
#endif
#if SERIAL_OUTPUT_ENABLED
Serial.print(" (F)");
#endif
break;
case TEMPERATURE_UNITS_K:
#if SD_LOGGING_ENABLED
file.print(" (K)");
#endif
#if SERIAL_OUTPUT_ENABLED
Serial.print(" (K)");
#endif
break;
}
}
#if SD_LOGGING_ENABLED
file.println();
file.flush();
#endif
#if SERIAL_OUTPUT_ENABLED
Serial.println();
#endif
#if SD_LOGGING_ENABLED
return (file.getWriteError() == false);
@@ -146,7 +121,7 @@ void close() {
bool log(char* message) {
// TODO: Test if file is open first
// log time to file
#if SD_LOGGING_ENABLED
file.println(message);
@@ -163,13 +138,13 @@ bool log(char* message) {
void sync(boolean force) {
// TODO: Test if file is open first?
//don't sync too often - requires 2048 bytes of I/O to SD card
if (!force && (millis() - syncTime) < SYNC_INTERVAL) {
return;
}
syncTime = millis();
#if SD_LOGGING_ENABLED
file.flush();
@@ -2,7 +2,8 @@
#define __AVR_ATmega32U4__ 1
// Feature settings
#define SD_LOGGING_ENABLED 1 // Enable/disable all SD card functionality
#define SD_LOGGING_ENABLED 1 // Enable/disable all SD card functionality. Saves 8,696 bytes
#define SERIAL_OUTPUT_ENABLED 1 // Enable/disable serial output functionality. Saves 192 bytes
// Calibration values
#define MCP3424_CALIBRATION_MULTIPLY 1.00713
@@ -34,7 +35,7 @@
// Pin definitions for Electronics version 0.13
#define pcbVersion ".13" // Electronics version 0.12 milestone.
#define FIRMWARE_VERSION "0.14-beta"
#define FIRMWARE_VERSION "0.15"
// Pin definitions
#define BUTTON_B_PIN 1 // 0.12-mod 7, 0.13 1
@@ -62,4 +63,3 @@
#define LCD_RST A3
#define LCD_CS A4
#define LCD_BACKLIGHT_PIN A5 // LCD backlight on pin
@@ -77,7 +77,7 @@ void rotateTemperatureUnit() {
// Reset the graph so we don't have to worry about scaling it
Display::resetGraph();
// TODO: Convert the current data to new units?
}
@@ -98,17 +98,18 @@ double convertTemperature(double Celcius) {
void setup(void) {
Power::setup();
ChargeStatus::setup();
#if SERIAL_OUTPUT_ENABLED
Serial.begin(9600);
#endif
Wire.begin(); // Start using the Wire library; does the i2c communication.
Backlight::setup();
Backlight::set(backlightEnabled);
Display::setup();
Display::resetGraph();
thermocoupleAdc.begin();
ambientSensor.begin();
@@ -117,7 +118,7 @@ void setup(void) {
// Set up the RTC to generate a 1 Hz signal
pinMode(RTC_INT, INPUT);
DS3231_init(0);
// And configure the atmega to interrupt on falling edge of the 1 Hz signal
EICRA |= _BV(ISC21); // Configure INT2 to trigger on falling edge
EIMSK |= _BV(INT2); // and enable the INT2 interrupt
@@ -138,19 +139,19 @@ void stopLogging() {
if(!logging) {
return;
}
logging = false;
sd::close();
}
static void readTemperatures() {
int32_t measuredVoltageUv;
int32_t compensatedVoltage;
double temperature;
ambient = ambientSensor.readTempC16(AMBIENT) / 16.0; // Read ambient temperature in C
// ADC read loop: Start a measurement, wait until it is finished, then record it
for(uint8_t channel = 0; channel < SENSOR_COUNT; channel++) {
thermocoupleAdc.startMeasurement(temperatureChannels[channel]);
@@ -174,38 +175,18 @@ static void readTemperatures() {
// This is some debugging code. Use it to display various values to the LCD.
#if DEBUG_JUNCTION_TEMPERATURE
// Display debugging value on channel
// temperatures[0] = ambient; // Display the back-calculated junction temperature voltage
// temperatures[1] = GetJunctionVoltage(&ambient); // Display the back-calculated junction temperature voltage
if(channel == 2 ){// != OUT_OF_RANGE){
// temperatures[channel] = ambient; // Display measured ambient temperature. Everything looks good here.
temperatures[channel] = (double)measuredVoltageUv; // Display measured voltage across thermocouple. Everything looks good here.
// temperatures[channel] = GetJunctionVoltage(ambient); // Display the back-calculated junction temperature voltage
// temperatures[channel] = compensatedVoltage/1000; // Display junction-temperature-compensated thermocouple voltage
// temperatures[channel] = temperature; // Display the calculated temperature in C
// temperatures[channel] = convertTemperature(temperature + ambient); // Display the final temperature in the appropriate units
}
#endif
}
// Finally, convert ambient to display units
// ambient = convertTemperature(ambient); // No need to convert. This does not appear on the display or serial outout.
}
static void writeOutputs() {
static char updateBuffer[BUFF_MAX]; // Scratch buffer to write serial/sd output into
// Avoid snprintf() to save 1.4k space
// snprintf(updateBuffer, BUFF_MAX, "%02d:%02d:%02d, ", rtcTime.hour, rtcTime.min, rtcTime.sec);
dtostrf(logTimeSeconds, 8,0, updateBuffer + 0);
// dtostrf(rtcTime.hour, 2, 0, updateBuffer + 0);
// dtostrf(rtcTime.min, 2, 0, updateBuffer + 3);
// dtostrf(rtcTime.sec, 2, 0, updateBuffer + 6);
// dtostrf(ambient, 1, 2, updateBuffer + 10);
// updateBuffer[2] = updateBuffer[5] = ':';
// updateBuffer[8] = ',';
// updateBuffer[9] = ' ';
for(uint8_t i = 0; i < SENSOR_COUNT; i++) {
if(temperatures[i] == OUT_OF_RANGE) {
strcpy(updateBuffer+strlen(updateBuffer), ", -");
@@ -215,8 +196,9 @@ static void writeOutputs() {
dtostrf(temperatures[i], 0, 2, updateBuffer+strlen(updateBuffer));
}
}
#if SERIAL_OUTPUT_ENABLED
Serial.println(updateBuffer);
#endif
if(logging) {
logging = sd::log(updateBuffer);
@@ -236,7 +218,7 @@ void resetTicks() {
// Updating the screen
void loop() {
bool needsRefresh = false; // If true, the display needs to be updated
if(timeToSample) {
timeToSample = false;
@@ -246,16 +228,15 @@ void loop() {
writeOutputs();
Display::updateGraph(temperatures);
needsRefresh = true;
}
if(Buttons::pending()) {
uint8_t button = Buttons::getPending();
if(button == Buttons::BUTTON_POWER) { // Disable power
if(!logging) {
// Serial.print("Powering off!\n");
Display::clear();
Backlight::set(0);
Power::shutdown();
@@ -277,7 +258,7 @@ void loop() {
if(!logging) {
logInterval = (logInterval + 1) % LOG_INTERVAL_COUNT;
resetTicks();
Display::resetGraph(); // Reset the graph, to keep the x axis consistent
resetTicks();
needsRefresh = true;
@@ -302,16 +283,16 @@ void loop() {
Backlight::set(backlightEnabled);
}
}
// If we are charging, refresh the display every second to make the charging animation
if(ChargeStatus::get() == ChargeStatus::CHARGING) {
if(lastIsrTick != isrTick) {
needsRefresh = true;
lastIsrTick = isrTick;
}
}
if(needsRefresh) {
if(needsRefresh) {
if(logging) {
Display::draw(
temperatures,
@@ -326,7 +307,7 @@ void loop() {
}
else {
Display::draw(
temperatures,
temperatures,
// ambient,
graphChannel,
temperatureUnit,
@@ -337,7 +318,7 @@ void loop() {
);
}
}
// Sleep if we are on battery power
// Note: Don't sleep if there is power, in case we need to communicate over USB
if(ChargeStatus::get() == ChargeStatus::DISCHARGING) {
@@ -351,9 +332,8 @@ ISR(INT2_vect)
{
isrTick = (isrTick + 1)%(logIntervals[logInterval]);
logTimeSeconds++;
if(isrTick == 0) {
timeToSample = true;
}
}

0 comments on commit 72b80b6

Please sign in to comment.