2013-04-07 03:20:17 +00:00
|
|
|
/*
|
|
|
|
Serial Adapter Project: Dynamic serial TTY passthroughs
|
|
|
|
by: Mike Crosson
|
|
|
|
Nusku Networks
|
|
|
|
date: 2013/03/09
|
|
|
|
license: CC-BY SA 3.0 - Creative commons share-alike 3.0
|
|
|
|
use this code however you'd like, just keep this license and
|
|
|
|
attribute.
|
|
|
|
*/
|
|
|
|
|
2013-04-09 21:40:12 +00:00
|
|
|
#include "Arduino.h"
|
|
|
|
|
2013-04-07 03:20:17 +00:00
|
|
|
#include "Project.h"
|
|
|
|
#include "Config.h"
|
2013-04-17 23:59:03 +00:00
|
|
|
#include "UI.h"
|
2013-04-07 03:20:17 +00:00
|
|
|
|
|
|
|
Config::Config() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::Config()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-17 23:59:03 +00:00
|
|
|
|
2013-04-07 03:20:17 +00:00
|
|
|
currentMode = none;
|
|
|
|
currentLineSpeed = zero;
|
|
|
|
currentVoltage = negOne;
|
2013-04-07 04:49:23 +00:00
|
|
|
currentTimeout = never;
|
2013-04-17 02:02:09 +00:00
|
|
|
|
|
|
|
pinMode(voltagePinOnePointEight, OUTPUT);
|
|
|
|
pinMode(voltagePinThreePointThree, OUTPUT);
|
|
|
|
pinMode(voltagePinFivePointZero, OUTPUT);
|
|
|
|
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.begin(115200);
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 03:20:17 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 05:32:42 +00:00
|
|
|
bool Config::isUIEnabled() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::isUIEnabled()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 05:32:42 +00:00
|
|
|
return uiEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::enableUI() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::enableUI()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 05:32:42 +00:00
|
|
|
uiEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::disableUI() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::disableUI()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 05:32:42 +00:00
|
|
|
uiEnabled = false;
|
|
|
|
}
|
|
|
|
|
2013-04-07 03:20:17 +00:00
|
|
|
serialmode Config::getSerialMode() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::getSerialMode()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 03:20:17 +00:00
|
|
|
return currentMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
linespeed Config::getLineSpeed() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::getLineSpeed()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 03:20:17 +00:00
|
|
|
return currentLineSpeed;
|
|
|
|
}
|
|
|
|
|
2013-04-17 02:02:09 +00:00
|
|
|
float Config::getLineSpeedBaud() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::getLineSpeedBaud()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-17 02:02:09 +00:00
|
|
|
|
2013-04-09 22:05:22 +00:00
|
|
|
switch (currentLineSpeed) {
|
2013-04-17 02:02:09 +00:00
|
|
|
case 0: // twentyFourHundredBaud
|
2013-04-09 22:05:22 +00:00
|
|
|
return 2400;
|
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 1: // ninetySixHundredBaud
|
2013-04-09 22:05:22 +00:00
|
|
|
return 9600;
|
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 2: // nineteenTwoK
|
2013-04-09 22:05:22 +00:00
|
|
|
return 19200;
|
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 3: // thirtyeightFourK
|
2013-04-09 22:05:22 +00:00
|
|
|
return 38400;
|
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 4: // fiftysevenFiveK
|
2013-04-09 22:05:22 +00:00
|
|
|
return 57600;
|
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 5: // oneNineteenTwoK
|
|
|
|
return 115200;
|
2013-04-09 22:05:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-07 03:20:17 +00:00
|
|
|
ttlvoltage Config::getVoltage() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::getVoltage()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 03:20:17 +00:00
|
|
|
return currentVoltage;
|
|
|
|
}
|
|
|
|
|
2013-04-07 04:49:23 +00:00
|
|
|
timeout Config::getTimeout() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::getTimeout()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 04:49:23 +00:00
|
|
|
return currentTimeout;
|
|
|
|
}
|
|
|
|
|
2013-04-09 21:18:43 +00:00
|
|
|
int Config::getTimeoutMilis() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::getTimeoutMilis()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 05:32:42 +00:00
|
|
|
switch (currentTimeout) {
|
|
|
|
case 0: // tenseconds
|
|
|
|
return 10000;
|
|
|
|
case 1: // thirtyseconds
|
|
|
|
return 30000;
|
|
|
|
case 2: // oneminute
|
|
|
|
return 60000;
|
|
|
|
case 3: // fiveminutes
|
|
|
|
return 300000;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-07 03:20:17 +00:00
|
|
|
void Config::setMode(serialmode mode) {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::setMode()");
|
|
|
|
serialPort0.print(" Setting ttl line speed to:");
|
2013-04-17 02:02:09 +00:00
|
|
|
float baudrate = this->getLineSpeedBaud();
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println(baudrate);
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-17 02:02:09 +00:00
|
|
|
|
2013-04-09 22:05:22 +00:00
|
|
|
switch (currentMode) {
|
|
|
|
case 1: // ttl
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort3.end();
|
2013-04-09 22:05:22 +00:00
|
|
|
break;
|
|
|
|
case 2: // db9_null
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort2.end();
|
2013-04-09 22:05:22 +00:00
|
|
|
break;
|
|
|
|
case 3: // cisco
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort1.end();
|
2013-04-09 22:05:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mode) {
|
2013-04-17 02:02:09 +00:00
|
|
|
case 0: // ttl
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort3.begin(getLineSpeedBaud());
|
2013-04-09 22:05:22 +00:00
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 1: // db9_null
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort2.begin(getLineSpeedBaud());
|
2013-04-09 22:05:22 +00:00
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 2: // cisco
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort1.begin(getLineSpeedBaud());
|
2013-04-09 22:05:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-07 03:20:17 +00:00
|
|
|
currentMode = mode;
|
|
|
|
}
|
|
|
|
|
2013-04-17 02:02:09 +00:00
|
|
|
void Config::setLineSpeed(linespeed aLineSpeed) {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::setLineSpeed()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-17 02:02:09 +00:00
|
|
|
currentLineSpeed = aLineSpeed;
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.end();
|
|
|
|
serialPort0.begin(getLineSpeedBaud());
|
2013-04-17 02:02:09 +00:00
|
|
|
setMode(currentMode);
|
2013-04-07 03:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setVoltage(ttlvoltage voltage) {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::setVoltage()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 03:20:17 +00:00
|
|
|
currentVoltage = voltage;
|
2013-04-17 02:02:09 +00:00
|
|
|
|
|
|
|
digitalWrite(voltagePinOnePointEight, LOW);
|
|
|
|
digitalWrite(voltagePinThreePointThree, LOW);
|
|
|
|
digitalWrite(voltagePinFivePointZero, LOW);
|
|
|
|
|
2013-04-09 21:40:12 +00:00
|
|
|
switch (voltage) {
|
2013-04-17 02:02:09 +00:00
|
|
|
case 0: // onePointEight
|
|
|
|
digitalWrite(voltagePinOnePointEight, HIGH);
|
2013-04-09 21:40:12 +00:00
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 1: // threePointThree
|
|
|
|
digitalWrite(voltagePinThreePointThree, HIGH);
|
2013-04-09 21:40:12 +00:00
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 2: // five
|
|
|
|
digitalWrite(voltagePinFivePointZero, HIGH);
|
2013-04-09 21:40:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-07 03:20:17 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 02:02:09 +00:00
|
|
|
void Config::setLCDTimeout(timeout aTimeout) {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::setTimeout()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 04:49:23 +00:00
|
|
|
currentTimeout = aTimeout;
|
2013-04-17 23:59:03 +00:00
|
|
|
ui->setLCDTimeout();
|
2013-04-07 04:49:23 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 03:20:17 +00:00
|
|
|
void Config::setDefaults() {
|
2013-04-25 23:29:14 +00:00
|
|
|
#if DEBUG == 2
|
2013-04-26 18:17:21 +00:00
|
|
|
serialPort0.println("Config::setDefaults()");
|
2013-04-25 23:29:14 +00:00
|
|
|
#endif
|
2013-04-07 03:20:17 +00:00
|
|
|
setVoltage(onePointEight);
|
2013-04-17 02:02:09 +00:00
|
|
|
setLineSpeed(oneFifteenTwoK);
|
2013-04-09 22:05:22 +00:00
|
|
|
setMode(ttl);
|
2013-04-17 02:02:09 +00:00
|
|
|
setLCDTimeout(thirtyseconds);
|
2013-04-07 05:32:42 +00:00
|
|
|
}
|
2013-04-09 22:11:08 +00:00
|
|
|
|
|
|
|
void Config::processSerialData() {
|
2013-04-17 02:02:09 +00:00
|
|
|
//if (DEBUG) {
|
|
|
|
// Serial.println("Config::processSerialData()");
|
|
|
|
//}
|
2013-04-25 23:53:40 +00:00
|
|
|
//#if DEBUG == 2
|
|
|
|
//DateTime now = rtc.now();
|
|
|
|
//Serial.print(now.year(), DEC);
|
|
|
|
//Serial.print('/');
|
|
|
|
//Serial.print(now.month(), DEC);
|
|
|
|
//Serial.print('/');
|
|
|
|
//Serial.print(now.day(), DEC);
|
|
|
|
//Serial.print(' ');
|
|
|
|
//Serial.print(now.hour(), DEC);
|
|
|
|
//Serial.print(':');
|
|
|
|
//Serial.print(now.minute(), DEC);
|
|
|
|
//Serial.print(':');
|
|
|
|
//Serial.print(now.second(), DEC);
|
|
|
|
//Serial.println();
|
|
|
|
//#endif
|
2013-04-09 22:11:08 +00:00
|
|
|
switch (currentMode) {
|
2013-04-17 02:02:09 +00:00
|
|
|
case 0: // ttl
|
2013-04-26 18:17:21 +00:00
|
|
|
if (serialPort3.available()) {
|
|
|
|
int inByte = serialPort3.read();
|
|
|
|
serialPort0.write(inByte);
|
2013-04-25 23:53:40 +00:00
|
|
|
ui->blinkCancelButton();
|
2013-04-27 18:57:28 +00:00
|
|
|
//dataFile.print(inByte);
|
2013-04-09 22:11:08 +00:00
|
|
|
}
|
2013-04-26 18:17:21 +00:00
|
|
|
if (serialPort0.available()) {
|
|
|
|
int inByte = serialPort0.read();
|
|
|
|
serialPort3.write(inByte);
|
2013-04-25 23:53:40 +00:00
|
|
|
ui->blinkOKButton();
|
2013-04-27 18:57:28 +00:00
|
|
|
//dataFile.print(inByte);
|
2013-04-09 22:11:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 1: // db9_null
|
2013-04-26 18:17:21 +00:00
|
|
|
if (serialPort2.available()) {
|
|
|
|
int inByte = serialPort2.read();
|
|
|
|
serialPort0.write(inByte);
|
2013-04-25 23:53:40 +00:00
|
|
|
ui->blinkCancelButton();
|
2013-04-27 18:57:28 +00:00
|
|
|
//dataFile.print(inByte);
|
2013-04-09 22:11:08 +00:00
|
|
|
}
|
2013-04-26 18:17:21 +00:00
|
|
|
if (serialPort0.available()) {
|
|
|
|
int inByte = serialPort0.read();
|
|
|
|
serialPort2.write(inByte);
|
2013-04-25 23:53:40 +00:00
|
|
|
ui->blinkOKButton();
|
2013-04-27 18:57:28 +00:00
|
|
|
//dataFile.print(inByte);
|
2013-04-09 22:11:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-04-17 02:02:09 +00:00
|
|
|
case 2: // cisco
|
2013-04-26 18:17:21 +00:00
|
|
|
if (serialPort1.available()) {
|
|
|
|
int inByte = serialPort1.read();
|
|
|
|
serialPort0.write(inByte);
|
2013-04-25 23:53:40 +00:00
|
|
|
ui->blinkCancelButton();
|
2013-04-27 18:57:28 +00:00
|
|
|
//dataFile.print(inByte);
|
2013-04-09 22:11:08 +00:00
|
|
|
}
|
2013-04-26 18:17:21 +00:00
|
|
|
if (serialPort0.available()) {
|
|
|
|
int inByte = serialPort0.read();
|
|
|
|
serialPort1.write(inByte);
|
2013-04-25 23:53:40 +00:00
|
|
|
ui->blinkOKButton();
|
2013-04-27 18:57:28 +00:00
|
|
|
//dataFile.print(inByte);
|
|
|
|
}
|
2013-04-09 22:11:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-04-26 01:38:21 +00:00
|
|
|
|
|
|
|
// Flush data written to log file
|
2013-04-27 18:57:28 +00:00
|
|
|
//dataFile.sync();
|
2013-04-17 02:02:09 +00:00
|
|
|
}
|