Debugging output, bug fixes, code cleanup, working serial data transfer implementation

This commit is contained in:
Mike C 2013-04-16 22:02:09 -04:00
parent db6e6b940d
commit 0998cb4785
8 changed files with 268 additions and 94 deletions

View file

@ -14,64 +14,103 @@
#include "Config.h"
Config::Config() {
if (DEBUG) {
Serial.println("Config::Config()");
}
currentMode = none;
currentLineSpeed = zero;
currentVoltage = negOne;
currentTimeout = never;
pinMode(voltagePinOnePointEight, OUTPUT);
pinMode(voltagePinThreePointThree, OUTPUT);
pinMode(voltagePinFivePointZero, OUTPUT);
if (DEBUG) {
Serial.begin(115200);
}
}
bool Config::isUIEnabled() {
if (DEBUG) {
Serial.println("Config::isUIEnabled()");
}
return uiEnabled;
}
void Config::enableUI() {
if (DEBUG) {
Serial.println("Config::enableUI()");
}
uiEnabled = true;
}
void Config::disableUI() {
if (DEBUG) {
Serial.println("Config::disableUI()");
}
uiEnabled = false;
}
serialmode Config::getSerialMode() {
if (DEBUG) {
Serial.println("Config::getSerialMode()");
}
return currentMode;
}
linespeed Config::getLineSpeed() {
if (DEBUG) {
Serial.println("Config::getLineSpeed()");
}
return currentLineSpeed;
}
int Config::getLineSpeedBaud() {
float Config::getLineSpeedBaud() {
if (DEBUG) {
Serial.println("Config::getLineSpeedBaud()");
}
switch (currentLineSpeed) {
case 1: // twentyFourHundredBaud
case 0: // twentyFourHundredBaud
return 2400;
break;
case 2: // ninetySixHundredBaud
case 1: // ninetySixHundredBaud
return 9600;
break;
case 3: // nineteenTwoK
case 2: // nineteenTwoK
return 19200;
break;
case 4: // thirtyeightFourK
case 3: // thirtyeightFourK
return 38400;
break;
case 5: // fiftysevenFiveK
case 4: // fiftysevenFiveK
return 57600;
break;
case 6: // oneNineteenTwoK
return 119200;
case 5: // oneNineteenTwoK
return 115200;
break;
}
}
ttlvoltage Config::getVoltage() {
if (DEBUG) {
Serial.println("Config::getVoltage()");
}
return currentVoltage;
}
timeout Config::getTimeout() {
if (DEBUG) {
Serial.println("Config::getTimeout()");
}
return currentTimeout;
}
int Config::getTimeoutMilis() {
if (DEBUG) {
Serial.println("Config::getTimeoutMilis()");
}
switch (currentTimeout) {
case 0: // tenseconds
return 10000;
@ -86,92 +125,97 @@ int Config::getTimeoutMilis() {
}
void Config::setMode(serialmode mode) {
if (DEBUG) {
Serial.println("Config::setMode()");
Serial.print(" Setting ttl line speed to:");
float baudrate = this->getLineSpeedBaud();
Serial.println(baudrate);
}
switch (currentMode) {
case 1: // ttl
Serial1.end();
Serial3.end();
break;
case 2: // db9_null
Serial2.end();
break;
case 3: // cisco
Serial3.end();
Serial1.end();
break;
}
switch (mode) {
case 1: // ttl
Serial1.begin(getLineSpeedBaud());
case 0: // ttl
Serial3.begin(getLineSpeedBaud());
break;
case 2: // db9_null
case 1: // db9_null
Serial2.begin(getLineSpeedBaud());
break;
case 3: // cisco
Serial3.begin(getLineSpeedBaud());
case 2: // cisco
Serial1.begin(getLineSpeedBaud());
break;
}
currentMode = mode;
}
void Config::setLineSpeed(linespeed speed) {
currentLineSpeed = speed;
void Config::setLineSpeed(linespeed aLineSpeed) {
if (DEBUG) {
Serial.println("Config::setLineSpeed()");
}
currentLineSpeed = aLineSpeed;
Serial.end();
Serial.begin(getLineSpeedBaud());
setMode(currentMode);
}
void Config::setVoltage(ttlvoltage voltage) {
if (DEBUG) {
Serial.println("Config::setVoltage()");
}
currentVoltage = voltage;
int shiftNumber = shiftOff;
digitalWrite(voltagePinOnePointEight, LOW);
digitalWrite(voltagePinThreePointThree, LOW);
digitalWrite(voltagePinFivePointZero, LOW);
switch (voltage) {
case 1: // onePointEight
shiftNumber = shift18V;
case 0: // onePointEight
digitalWrite(voltagePinOnePointEight, HIGH);
break;
case 2: // threePointThree
shiftNumber = shift33V;
case 1: // threePointThree
digitalWrite(voltagePinThreePointThree, HIGH);
break;
case 3: // five
shiftNumber = shift50V;
case 2: // five
digitalWrite(voltagePinFivePointZero, HIGH);
break;
}
// Use bit shifter to activate the proper voltage regulator
digitalWrite(shifterLatchPin, LOW);
shiftOut(shifterDataPin, shifterClockPin, MSBFIRST, shiftNumber);
digitalWrite(shifterLatchPin, HIGH);
}
void Config::setTimeout(timeout aTimeout) {
void Config::setLCDTimeout(timeout aTimeout) {
if (DEBUG) {
Serial.println("Config::setTimeout()");
}
currentTimeout = aTimeout;
}
void Config::setDefaults() {
if (DEBUG) {
Serial.println("Config::setDefaults()");
}
setVoltage(onePointEight);
setLineSpeed(oneNineteenTwoK);
setLineSpeed(oneFifteenTwoK);
setMode(ttl);
setTimeout(thirtyseconds);
setLCDTimeout(thirtyseconds);
}
void Config::processSerialData() {
//if (DEBUG) {
// Serial.println("Config::processSerialData()");
//}
switch (currentMode) {
case 1: // ttl
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
break;
case 2: // db9_null
if (Serial2.available()) {
int inByte = Serial2.read();
Serial.write(inByte);
}
if (Serial.available()) {
int inByte = Serial.read();
Serial2.write(inByte);
}
break;
case 3: // cisco
case 0: // ttl
if (Serial3.available()) {
int inByte = Serial3.read();
Serial.write(inByte);
@ -181,5 +225,25 @@ void Config::processSerialData() {
Serial3.write(inByte);
}
break;
case 1: // db9_null
if (Serial2.available()) {
int inByte = Serial2.read();
Serial.write(inByte);
}
if (Serial.available()) {
int inByte = Serial.read();
Serial2.write(inByte);
}
break;
case 2: // cisco
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
break;
}
}
}

View file

@ -22,21 +22,16 @@ private:
timeout currentTimeout;
bool uiEnabled;
const static int shiftOff = 0;
const static int shift18V = 1;
const static int shift33V = 2;
const static int shift50V = 4;
public:
Config();
void setDefaults();
void setMode(serialmode mode);
void setLineSpeed(linespeed speed);
void setVoltage(ttlvoltage voltage);
void setTimeout(timeout aTimeout);
void setLCDTimeout(timeout aTimeout);
serialmode getSerialMode();
linespeed getLineSpeed();
int getLineSpeedBaud();
float getLineSpeedBaud(); // MUST be an unsigned int for >= 38400 and float for >=115200
ttlvoltage getVoltage();
timeout getTimeout();
int getTimeoutMilis();

View file

@ -28,10 +28,10 @@
#define LCD_DC 9 // Data/command line for TFT
#define LCD_RST 8 // Reset line for TFT (or connect to +5V)
// 74HC595 Bit Shifter Pinout
#define shifterLatchPin 2 //Pin connected to ST_CP of 74HC595
#define shifterClockPin 5 //Pin connected to SH_CP of 74HC595
#define shifterDataPin 4 //Pin connected to DS of 74HC595
// Voltage Pinouts
#define voltagePinOnePointEight 40 // Pin controlling 1.8V TTL logic
#define voltagePinThreePointThree 41 // Pin controlling 3.3V TTL logic
#define voltagePinFivePointZero 42 // Pin controlling 5V TTL logic
// Colors / theme of UI
#define SPLASH_BACKGROUND ST7735_WHITE
@ -66,7 +66,7 @@ enum linespeed {
nineteenTwoK,
thirtyeightFourK,
fiftysevenFiveK,
oneNineteenTwoK,
oneFifteenTwoK,
maxlinespeed,
zero=-1
};

View file

@ -14,6 +14,9 @@
#include "UI.h"
UI::UI(Config* aConfig) {
if (DEBUG) {
Serial.println("Config::UI()");
}
config = aConfig;
okButton = new UIButton(okButtonPin, okButtonLed);
@ -28,11 +31,17 @@ UI::UI(Config* aConfig) {
}
void UI::startUI() {
if (DEBUG) {
Serial.println("Config::startUI()");
}
enableUI();
lcd->start();
}
void UI::disableUI() {
if (DEBUG) {
Serial.println("Config::disableUI()");
}
config->disableUI();
lcd->turnOff();
okButton->turnOffLed();
@ -40,6 +49,9 @@ void UI::disableUI() {
}
void UI::enableUI() {
if (DEBUG) {
Serial.println("Config::enableUI()");
}
config->enableUI();
lcd->turnOn();
okButton->turnOnLed();

View file

@ -13,18 +13,25 @@
#include "Project.h"
UIButton::UIButton(int buttonPin, int ledPin) {
if (DEBUG) {
Serial.println("UIButton::UIButton()");
}
this->buttonPin = buttonPin;
this->ledPin = ledPin;
setup();
}
void UIButton::setup() {
if (DEBUG) {
Serial.println("UIButton::setup()");
}
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void UIButton::turnOnLed() {
if (DEBUG) {
Serial.println("UIButton::turnOnLed()");
Serial.print("Turning on pin: ");
Serial.println(ledPin);
}
@ -33,6 +40,7 @@ void UIButton::turnOnLed() {
void UIButton::turnOffLed() {
if (DEBUG) {
Serial.println("UIButton::turnOffLed()");
Serial.print("Turning off pin: ");
Serial.println(ledPin);
}
@ -40,6 +48,9 @@ void UIButton::turnOffLed() {
}
bool UIButton::isPressed() {
// if (DEBUG) {
//Serial.println("UIButton::isPressed()");
// }
bool pressed = digitalRead(buttonPin);
while (digitalRead(buttonPin)); // Wait for release
return pressed;

View file

@ -13,6 +13,9 @@ Serial Adapter Project: Dynamic serial TTY passthroughs
#include "UIJoystickPSP.h"
UIJoystickPSP::UIJoystickPSP(int xAxisPin, int yAxisPin) {
if (DEBUG) {
Serial.println("UIJoystickPSP::UIJoystickPSP()");
}
this->xAxisPin = xAxisPin;
this->yAxisPin = yAxisPin;
@ -25,6 +28,9 @@ UIJoystickPSP::UIJoystickPSP(int xAxisPin, int yAxisPin) {
}
joyDirection UIJoystickPSP::direction() {
// if (DEBUG) {
//Serial.println("UIJoystickPSP::direction()");
// }
// Read the x/y values from the joystick
xAxisValue = map(analogRead(xAxisPin), 0, 1023, 0, 10);
yAxisValue = map(analogRead(yAxisPin), 0, 1023, 0, 10);

View file

@ -20,6 +20,9 @@
#include "UIJoystickPSP.h"
UILCD::UILCD(Config* config) {
if (DEBUG) {
Serial.println("UILCD::UILCD()");
}
pinMode(LCD_LITE, OUTPUT);
this->config = config;
@ -28,26 +31,36 @@ UILCD::UILCD(Config* config) {
tft->initR(INITR_BLACKTAB);
tft->setRotation(3);
if (!SD.begin(SD_CS)) {
if (!SD.begin(SD_CS) && DEBUG) {
Serial.println("SD.begin(SD_CS) -- failed!");
}
}
void UILCD::start() {
if (DEBUG) {
Serial.println("UILCD::start()");
}
drawSplashScreen();
drawMainScreen();
}
void UILCD::turnOn() {
if (DEBUG) {
Serial.println("UILCD::turnOn()");
}
digitalWrite(LCD_LITE, HIGH);
}
void UILCD::turnOff() {
if (DEBUG) {
Serial.println("UILCD::turnOff()");
}
digitalWrite(LCD_LITE, LOW);
}
void UILCD::handleJoystickEvent(joyDirection direction) {
if (DEBUG) {
Serial.println("UILCD::handleJoystickEvent()");
Serial.println("begin UILCD::handleJoystickEvent");
Serial.print("Current Screen: ");
Serial.println(currentScreen);
@ -64,7 +77,8 @@ void UILCD::handleJoystickEvent(joyDirection direction) {
}
void UILCD::handleOkButtonEvent() {
if (DEBUG) {
if (DEBUG) {
Serial.println("UILCD::handleOkButtonEvent()");
Serial.println("begin UILCD::handleOkButtonEvent");
Serial.print("Current Screen: ");
Serial.println(currentScreen);
@ -87,14 +101,15 @@ void UILCD::handleOkButtonEvent() {
drawVoltageScreen(true);
break;
case 5: // timeoutScreen
config->setTimeout((timeout)(currentLine - 3));
config->setLCDTimeout((timeout)(currentLine - 3));
drawTimeoutScreen(true);
break;
}
}
void UILCD::handleCancelButtonEvent() {
if (DEBUG) {
if (DEBUG) {
Serial.println("UILCD::handleCancelButtonEvent");
Serial.println("begin UILCD::handleCancelButtonEvent");
Serial.print("Current Screen: ");
Serial.println(currentScreen);
@ -111,6 +126,9 @@ void UILCD::handleCancelButtonEvent() {
}
void UILCD::mainScreenOkButton() {
if (DEBUG) {
Serial.println("UILCD::mainScreenOkButton");
}
switch(currentLine) {
case 0: // Connection Type
drawConnectionScreen(false);
@ -134,21 +152,33 @@ void UILCD::mainScreenOkButton() {
}
void UILCD::mainScreenCancelButton() {
if (DEBUG) {
Serial.println("UILCD::mainScreenCancelButton()");
}
// Do nothing for now
}
void UILCD::unHilightLine(int line) {
if (DEBUG) {
Serial.println("UILCD::unHilightLine()");
}
tft->setCursor(0, line * FONT_HEIGHT);
tft->fillRect(0, line * FONT_HEIGHT, FONT_WIDTH, FONT_HEIGHT, BACKGROUND);
}
void UILCD::hilightLine(int line) {
if (DEBUG) {
Serial.println("UILCD::hilightLine()");
}
tft->setCursor(0, line * FONT_HEIGHT);
tft->setTextColor(HILIGHT);
tft->print("*");
}
void UILCD::mainScreenHilight(joyDirection direction) {
if (DEBUG) {
Serial.println("UILCD::mainScreenHilight()");
}
if (direction == joyUp) {
// Don't go up past the 1st line
if (currentLine == 0) {
@ -160,13 +190,17 @@ void UILCD::mainScreenHilight(joyDirection direction) {
// Skip blank lines
if (config->getSerialMode() == ttl) {
Serial.println("Serial ttl blank line skip");
if (DEBUG) {
Serial.println("Serial ttl blank line skip");
}
if (currentLine == 3 || currentLine == 6) {
currentLine -= 1;
}
}
else {
Serial.println("Non-serial ttl blank line skip");
if (DEBUG) {
Serial.println("Non-serial ttl blank line skip");
}
if (currentLine == 2 || currentLine == 5) {
currentLine -= 1;
}
@ -193,13 +227,17 @@ void UILCD::mainScreenHilight(joyDirection direction) {
// Skip blank lines
if (config->getSerialMode() == ttl) {
Serial.println("Serial ttl blank line skip");
if (DEBUG) {
Serial.println("Serial ttl blank line skip");
}
if (currentLine == 3 || currentLine == 6) {
currentLine += 1;
}
}
else {
Serial.println("Non-serial ttl blank line skip");
if (DEBUG) {
Serial.println("Non-serial ttl blank line skip");
}
if (currentLine == 2 || currentLine == 5) {
currentLine += 1;
}
@ -210,6 +248,9 @@ void UILCD::mainScreenHilight(joyDirection direction) {
}
void UILCD::configScreenHighlight(joyDirection direction) {
if (DEBUG) {
Serial.println("UILCD::configScreenhilight()");
}
if (direction == joyUp) {
// Don't go up past the 1st line
if (currentLine == 3) {
@ -256,6 +297,9 @@ void UILCD::configScreenHighlight(joyDirection direction) {
}
void UILCD::drawTimeoutScreen(bool keepCurrentLine) {
if (DEBUG) {
Serial.println("UILCD::drawTimeoutScreen()");
}
currentScreen = timeoutscreen;
if (!keepCurrentLine) {
currentLine = 3;
@ -288,6 +332,9 @@ void UILCD::drawTimeoutScreen(bool keepCurrentLine) {
}
void UILCD::drawConnectionScreen(bool keepCurrentLine) {
if (DEBUG) {
Serial.println("UILCD::drawConnectionScreen()");
}
currentScreen = connectionScreen;
if (!keepCurrentLine) {
currentLine = 3;
@ -320,6 +367,9 @@ void UILCD::drawConnectionScreen(bool keepCurrentLine) {
}
void UILCD::drawLineSpeedScreen(bool keepCurrentLine) {
if (DEBUG) {
Serial.println("UILCD::drawLineSpeedScreen()");
}
currentScreen = lineSpeedScreen;
if (!keepCurrentLine) {
currentLine = 3;
@ -352,6 +402,9 @@ void UILCD::drawLineSpeedScreen(bool keepCurrentLine) {
}
void UILCD::drawVoltageScreen(bool keepCurrentLine) {
if (DEBUG) {
Serial.println("UILCD::drawVoltageScreen()");
}
currentScreen = voltageScreen;
if (!keepCurrentLine) {
currentLine = 3;
@ -384,6 +437,9 @@ void UILCD::drawVoltageScreen(bool keepCurrentLine) {
}
void UILCD::drawMainScreen() {
if (DEBUG) {
Serial.println("UILCD::drawMainScreen()");
}
currentScreen = mainScreen;
currentLine = 0;
@ -413,6 +469,9 @@ void UILCD::drawMainScreen() {
}
void UILCD::drawSplashScreen() {
if (DEBUG) {
Serial.println("UILCD::drawSplashScreen()");
}
tft->setCursor(0,0);
tft->fillScreen(SPLASH_BACKGROUND);
bmpDraw("splash.bmp", 13, 0);
@ -424,6 +483,9 @@ void UILCD::drawSplashScreen() {
}
void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
if (DEBUG) {
Serial.println("UILCD::bmpDraw()");
}
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
@ -439,41 +501,55 @@ void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
if((x >= tft->width()) || (y >= tft->height())) return;
Serial.println();
Serial.print("Loading image '");
Serial.print(filename);
Serial.println('\'');
if (DEBUG) {
Serial.println();
Serial.print("Loading image '");
Serial.print(filename);
Serial.println('\'');
}
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
Serial.print("File not found");
if (DEBUG) {
Serial.print("File not found");
}
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print("File size: ");
Serial.println(read32(bmpFile));
if (DEBUG) {
Serial.print("File size: ");
Serial.println(read32(bmpFile));
}
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print("Image Offset: ");
Serial.println(bmpImageoffset, DEC);
if (DEBUG) {
Serial.print("Image Offset: ");
Serial.println(bmpImageoffset, DEC);
}
// Read DIB header
Serial.print("Header size: ");
Serial.println(read32(bmpFile));
if (DEBUG) {
Serial.print("Header size: ");
Serial.println(read32(bmpFile));
}
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1) { // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
Serial.print("Bit Depth: ");
Serial.println(bmpDepth);
if (DEBUG) {
Serial.print("Bit Depth: ");
Serial.println(bmpDepth);
}
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
Serial.print("Image size: ");
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
if (DEBUG) {
Serial.print("Image size: ");
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
}
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
@ -526,15 +602,17 @@ void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
} // end pixel
delay(5);
} // end scanline
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
if (DEBUG) {
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
}
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) Serial.println("BMP format not recognized.");
if(!goodBmp && DEBUG) Serial.println("BMP format not recognized.");
}
// These read 16- and 32-bit types from the SD card file.
@ -542,6 +620,9 @@ void UILCD::bmpDraw(char *filename, uint8_t x, uint8_t y) {
// May need to reverse subscript order if porting elsewhere.
uint16_t UILCD::read16(File f) {
if (DEBUG) {
Serial.println("UILCD::read16()");
}
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
@ -549,6 +630,9 @@ uint16_t UILCD::read16(File f) {
}
uint32_t UILCD::read32(File f) {
if (DEBUG) {
Serial.println("UILCD::read32()");
}
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();

View file

@ -26,6 +26,8 @@ UI* ui;
Config* config;
void setup() {
Serial.begin(115200);
config = new Config();
config->setDefaults();