Misc bug fixes, minor code cleanup, initial sd card data logging implementation

This commit is contained in:
Mike C 2013-04-25 21:38:21 -04:00
parent c6c7ec4ad5
commit f4f8367236
6 changed files with 84 additions and 42 deletions

View file

@ -238,11 +238,13 @@ void Config::processSerialData() {
int inByte = Serial3.read(); int inByte = Serial3.read();
Serial.write(inByte); Serial.write(inByte);
ui->blinkCancelButton(); ui->blinkCancelButton();
dataFile.print(inByte);
} }
if (Serial.available()) { if (Serial.available()) {
int inByte = Serial.read(); int inByte = Serial.read();
Serial3.write(inByte); Serial3.write(inByte);
ui->blinkOKButton(); ui->blinkOKButton();
dataFile.print(inByte);
} }
break; break;
case 1: // db9_null case 1: // db9_null
@ -250,11 +252,13 @@ void Config::processSerialData() {
int inByte = Serial2.read(); int inByte = Serial2.read();
Serial.write(inByte); Serial.write(inByte);
ui->blinkCancelButton(); ui->blinkCancelButton();
dataFile.print(inByte);
} }
if (Serial.available()) { if (Serial.available()) {
int inByte = Serial.read(); int inByte = Serial.read();
Serial2.write(inByte); Serial2.write(inByte);
ui->blinkOKButton(); ui->blinkOKButton();
dataFile.print(inByte);
} }
break; break;
case 2: // cisco case 2: // cisco
@ -262,12 +266,17 @@ void Config::processSerialData() {
int inByte = Serial1.read(); int inByte = Serial1.read();
Serial.write(inByte); Serial.write(inByte);
ui->blinkCancelButton(); ui->blinkCancelButton();
dataFile.print(inByte);
} }
if (Serial.available()) { if (Serial.available()) {
int inByte = Serial.read(); int inByte = Serial.read();
Serial1.write(inByte); Serial1.write(inByte);
ui->blinkOKButton(); ui->blinkOKButton();
dataFile.print(inByte);
} }
break; break;
} }
// Flush data written to log file
dataFile.flush();
} }

View file

@ -13,6 +13,7 @@
#include "Project.h" #include "Project.h"
#include "UI.h" #include "UI.h"
#include <SD.h>
// Forward declaration of UI // Forward declaration of UI
class UI; class UI;
@ -20,6 +21,9 @@ class UI;
// Use global config and ui objects defined / initialized in main ino file // Use global config and ui objects defined / initialized in main ino file
extern UI* ui; extern UI* ui;
// File to log data to
extern File dataFile;
class Config { class Config {
private: private:
// Mode info needed // Mode info needed

View file

@ -36,6 +36,9 @@
// Timer pins // Timer pins
#define timerThreePin 2 // Maps to Mega pin 12 #define timerThreePin 2 // Maps to Mega pin 12
// Splash screen related
#define splashScreenFileName "splash.bmp"
// Colors / theme of UI // Colors / theme of UI
#define SPLASH_BACKGROUND ST7735_WHITE #define SPLASH_BACKGROUND ST7735_WHITE
#define BACKGROUND ST7735_BLACK #define BACKGROUND ST7735_BLACK

View file

@ -30,14 +30,22 @@ UI::UI() {
} }
void UI::blinkOKButton() { void UI::blinkOKButton() {
if (!config->isUIEnabled()) { if (config->isUIEnabled()) {
okButton->turnOffLed();
okButton->turnOnLed();
}
else {
okButton->turnOnLed(); okButton->turnOnLed();
okButton->turnOffLed(); okButton->turnOffLed();
} }
} }
void UI::blinkCancelButton() { void UI::blinkCancelButton() {
if (!config->isUIEnabled()) { if (config->isUIEnabled()) {
cancelButton->turnOffLed();
cancelButton->turnOnLed();
}
else {
cancelButton->turnOnLed(); cancelButton->turnOnLed();
cancelButton->turnOffLed(); cancelButton->turnOffLed();
} }

View file

@ -29,7 +29,7 @@ UILCD::UILCD() {
tft->initR(INITR_BLACKTAB); tft->initR(INITR_BLACKTAB);
tft->setRotation(3); tft->setRotation(3);
if (!SD.begin(SD_CS) && DEBUG) { if (!SD.begin(SD_CS)) {
Serial.println("SD.begin(SD_CS) -- failed!"); Serial.println("SD.begin(SD_CS) -- failed!");
} }
} }
@ -188,17 +188,17 @@ void UILCD::mainScreenHilight(joyDirection direction) {
// Skip blank lines // Skip blank lines
if (config->getSerialMode() == ttl) { if (config->getSerialMode() == ttl) {
#if DEBUG == 2 #if DEBUG == 2
Serial.println("Serial ttl blank line skip"); Serial.println("Serial ttl blank line skip");
#endif #endif
if (currentLine == 3 || currentLine == 6) { if (currentLine == 3 || currentLine == 6) {
currentLine -= 1; currentLine -= 1;
} }
} }
else { else {
#if DEBUG == 2 #if DEBUG == 2
Serial.println("Non-serial ttl blank line skip"); Serial.println("Non-serial ttl blank line skip");
#endif #endif
if (currentLine == 2 || currentLine == 5) { if (currentLine == 2 || currentLine == 5) {
currentLine -= 1; currentLine -= 1;
} }
@ -225,17 +225,17 @@ void UILCD::mainScreenHilight(joyDirection direction) {
// Skip blank lines // Skip blank lines
if (config->getSerialMode() == ttl) { if (config->getSerialMode() == ttl) {
#if DEBUG == 2 #if DEBUG == 2
Serial.println("Serial ttl blank line skip"); Serial.println("Serial ttl blank line skip");
#endif #endif
if (currentLine == 3 || currentLine == 6) { if (currentLine == 3 || currentLine == 6) {
currentLine += 1; currentLine += 1;
} }
} }
else { else {
#if DEBUG == 2 #if DEBUG == 2
Serial.println("Non-serial ttl blank line skip"); Serial.println("Non-serial ttl blank line skip");
#endif #endif
if (currentLine == 2 || currentLine == 5) { if (currentLine == 2 || currentLine == 5) {
currentLine += 1; currentLine += 1;
} }
@ -472,7 +472,7 @@ void UILCD::drawSplashScreen() {
#endif #endif
tft->setCursor(0,0); tft->setCursor(0,0);
tft->fillScreen(SPLASH_BACKGROUND); tft->fillScreen(SPLASH_BACKGROUND);
bmpDraw("splash.bmp", 13, 0); bmpDraw(splashScreenFileName, 13, 0);
delay(1250); delay(1250);
for (int16_t y=0; y < tft->height(); y+=1) { for (int16_t y=0; y < tft->height(); y+=1) {
tft->drawFastHLine(0, y, tft->width(), BACKGROUND); tft->drawFastHLine(0, y, tft->width(), BACKGROUND);
@ -481,9 +481,10 @@ void UILCD::drawSplashScreen() {
} }
void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) { void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
#if DEBUG == 2 #if DEBUG == 2
Serial.println("UILCD::bmpDraw()"); Serial.println("UILCD::bmpDraw()");
#endif #endif
File bmpFile; File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24) uint8_t bmpDepth; // Bit depth (currently must be 24)
@ -497,40 +498,47 @@ void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
uint8_t r, g, b; uint8_t r, g, b;
uint32_t pos = 0, startTime = millis(); uint32_t pos = 0, startTime = millis();
if((x >= tft->width()) || (y >= tft->height())) return; if((x >= tft->width()) || (y >= tft->height())) {
return;
}
#if DEBUG == 2 #if DEBUG == 2
Serial.println(); Serial.println();
Serial.print("Loading image '"); Serial.print("Loading image '");
Serial.print(filename); Serial.print(filename);
Serial.println('\''); Serial.println('\'');
#endif #endif
// Open requested file on SD card // Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) { if ((bmpFile = SD.open(filename)) == NULL) {
#if DEBUG == 2
Serial.print("File not found"); Serial.print("File not found");
#endif
return; return;
} }
// Parse BMP header // Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature if(read16(bmpFile) == 0x4D42) { // BMP signature
long fileSize = read32(bmpFile);
#if DEBUG == 2 #if DEBUG == 2
Serial.print("File size: "); Serial.print("File size: ");
Serial.println(read32(bmpFile)); Serial.println(fileSize);
#endif #endif
(void)read32(bmpFile); // Read & ignore creator bytes (void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data bmpImageoffset = read32(bmpFile); // Start of image data
#if DEBUG == 2 #if DEBUG == 2
Serial.print("Image Offset: "); Serial.print("Image Offset: ");
Serial.println(bmpImageoffset, DEC); Serial.println(bmpImageoffset, DEC);
#endif #endif
// Read DIB header
float headerSize = read32(bmpFile);
#if DEBUG == 2 #if DEBUG == 2
// Read DIB header
Serial.print("Header size: "); Serial.print("Header size: ");
Serial.println(read32(bmpFile)); Serial.println(headerSize);
#endif #endif
bmpWidth = read32(bmpFile); bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile); bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1) { // # planes -- must be '1' if(read16(bmpFile) == 1) { // # planes -- must be '1'
@ -540,7 +548,6 @@ void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
Serial.println(bmpDepth); Serial.println(bmpDepth);
#endif #endif
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed! goodBmp = true; // Supported BMP format -- proceed!
#if DEBUG == 2 #if DEBUG == 2
Serial.print("Image size: "); Serial.print("Image size: ");
@ -562,8 +569,12 @@ void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
// Crop area to be loaded // Crop area to be loaded
w = bmpWidth; w = bmpWidth;
h = bmpHeight; h = bmpHeight;
if((x+w-1) >= tft->width()) w = tft->width() - x; if((x+w-1) >= tft->width()) {
if((y+h-1) >= tft->height()) h = tft->height() - y; w = tft->width() - x;
}
if((y+h-1) >= tft->height()) {
h = tft->height() - y;
}
// Set TFT address window to clipped image bounds // Set TFT address window to clipped image bounds
tft->setAddrWindow(x, y, x+w-1, y+h-1); tft->setAddrWindow(x, y, x+w-1, y+h-1);
@ -576,10 +587,12 @@ void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
// and scanline padding. Also, the seek only takes // and scanline padding. Also, the seek only takes
// place if the file position actually needs to change // place if the file position actually needs to change
// (avoids a lot of cluster math in SD library). // (avoids a lot of cluster math in SD library).
if(flip) // Bitmap is stored bottom-to-top order (normal BMP) if(flip) { // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize; pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom }
else { // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize; pos = bmpImageoffset + row * rowSize;
}
if(bmpFile.position() != pos) { // Need seek? if(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos); bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload buffidx = sizeof(sdbuffer); // Force buffer reload
@ -610,7 +623,9 @@ void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
} }
bmpFile.close(); bmpFile.close();
if(!goodBmp && DEBUG == 2) Serial.println("BMP format not recognized."); if(!goodBmp) {
Serial.println("BMP format not recognized.");
}
} }
// These read 16- and 32-bit types from the SD card file. // These read 16- and 32-bit types from the SD card file.

View file

@ -29,6 +29,7 @@
UI* ui; UI* ui;
Config* config; Config* config;
RTC_DS1307 rtc; RTC_DS1307 rtc;
File dataFile;
// helper for interrupt method call // helper for interrupt method call
void processSerial() { void processSerial() {
@ -63,6 +64,8 @@ void setup() {
Timer3.initialize(100); // initialize timer3, value in micro seconds Timer3.initialize(100); // initialize timer3, value in micro seconds
Timer3.pwm(timerThreePin, 512); // setup pwm on appropriate pin, 50% duty cycle Timer3.pwm(timerThreePin, 512); // setup pwm on appropriate pin, 50% duty cycle
Timer3.attachInterrupt(processSerial); // attaches method as a timer overflow interrupt Timer3.attachInterrupt(processSerial); // attaches method as a timer overflow interrupt
dataFile = SD.open("datalog.txt", FILE_WRITE);
} }
void loop() { void loop() {