This repository has been archived on 2020-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
arduino_universal_serial_ad.../Universal_Serial_Adapter/Config.cpp

283 lines
5.8 KiB
C++
Raw Normal View History

/*
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"
#include "Project.h"
#include "Config.h"
#include "UI.h"
Config::Config() {
#if DEBUG == 2
serialPort0.println("Config::Config()");
#endif
currentMode = none;
currentLineSpeed = zero;
currentVoltage = negOne;
2013-04-07 04:49:23 +00:00
currentTimeout = never;
pinMode(voltagePinOnePointEight, OUTPUT);
pinMode(voltagePinThreePointThree, OUTPUT);
pinMode(voltagePinFivePointZero, OUTPUT);
#if DEBUG == 2
serialPort0.begin(115200);
#endif
}
2013-04-07 05:32:42 +00:00
bool Config::isUIEnabled() {
#if DEBUG == 2
serialPort0.println("Config::isUIEnabled()");
#endif
2013-04-07 05:32:42 +00:00
return uiEnabled;
}
void Config::enableUI() {
#if DEBUG == 2
serialPort0.println("Config::enableUI()");
#endif
2013-04-07 05:32:42 +00:00
uiEnabled = true;
}
void Config::disableUI() {
#if DEBUG == 2
serialPort0.println("Config::disableUI()");
#endif
2013-04-07 05:32:42 +00:00
uiEnabled = false;
}
serialmode Config::getSerialMode() {
#if DEBUG == 2
serialPort0.println("Config::getSerialMode()");
#endif
return currentMode;
}
linespeed Config::getLineSpeed() {
#if DEBUG == 2
serialPort0.println("Config::getLineSpeed()");
#endif
return currentLineSpeed;
}
float Config::getLineSpeedBaud() {
#if DEBUG == 2
serialPort0.println("Config::getLineSpeedBaud()");
#endif
2013-04-09 22:05:22 +00:00
switch (currentLineSpeed) {
case 0: // twentyFourHundredBaud
2013-04-09 22:05:22 +00:00
return 2400;
break;
case 1: // ninetySixHundredBaud
2013-04-09 22:05:22 +00:00
return 9600;
break;
case 2: // nineteenTwoK
2013-04-09 22:05:22 +00:00
return 19200;
break;
case 3: // thirtyeightFourK
2013-04-09 22:05:22 +00:00
return 38400;
break;
case 4: // fiftysevenFiveK
2013-04-09 22:05:22 +00:00
return 57600;
break;
case 5: // oneNineteenTwoK
return 115200;
2013-04-09 22:05:22 +00:00
break;
}
}
ttlvoltage Config::getVoltage() {
#if DEBUG == 2
serialPort0.println("Config::getVoltage()");
#endif
return currentVoltage;
}
2013-04-07 04:49:23 +00:00
timeout Config::getTimeout() {
#if DEBUG == 2
serialPort0.println("Config::getTimeout()");
#endif
2013-04-07 04:49:23 +00:00
return currentTimeout;
}
2013-04-09 21:18:43 +00:00
int Config::getTimeoutMilis() {
#if DEBUG == 2
serialPort0.println("Config::getTimeoutMilis()");
#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;
}
void Config::setMode(serialmode mode) {
#if DEBUG == 2
serialPort0.println("Config::setMode()");
serialPort0.print(" Setting ttl line speed to:");
float baudrate = this->getLineSpeedBaud();
serialPort0.println(baudrate);
#endif
2013-04-09 22:05:22 +00:00
switch (currentMode) {
case 1: // ttl
serialPort3.end();
2013-04-09 22:05:22 +00:00
break;
case 2: // db9_null
serialPort2.end();
2013-04-09 22:05:22 +00:00
break;
case 3: // cisco
serialPort1.end();
2013-04-09 22:05:22 +00:00
break;
}
switch (mode) {
case 0: // ttl
serialPort3.begin(getLineSpeedBaud());
2013-04-09 22:05:22 +00:00
break;
case 1: // db9_null
serialPort2.begin(getLineSpeedBaud());
2013-04-09 22:05:22 +00:00
break;
case 2: // cisco
serialPort1.begin(getLineSpeedBaud());
2013-04-09 22:05:22 +00:00
break;
}
currentMode = mode;
}
void Config::setLineSpeed(linespeed aLineSpeed) {
#if DEBUG == 2
serialPort0.println("Config::setLineSpeed()");
#endif
currentLineSpeed = aLineSpeed;
serialPort0.end();
serialPort0.begin(getLineSpeedBaud());
setMode(currentMode);
}
void Config::setVoltage(ttlvoltage voltage) {
#if DEBUG == 2
serialPort0.println("Config::setVoltage()");
#endif
currentVoltage = voltage;
digitalWrite(voltagePinOnePointEight, LOW);
digitalWrite(voltagePinThreePointThree, LOW);
digitalWrite(voltagePinFivePointZero, LOW);
2013-04-09 21:40:12 +00:00
switch (voltage) {
case 0: // onePointEight
digitalWrite(voltagePinOnePointEight, HIGH);
2013-04-09 21:40:12 +00:00
break;
case 1: // threePointThree
digitalWrite(voltagePinThreePointThree, HIGH);
2013-04-09 21:40:12 +00:00
break;
case 2: // five
digitalWrite(voltagePinFivePointZero, HIGH);
2013-04-09 21:40:12 +00:00
break;
}
}
void Config::setLCDTimeout(timeout aTimeout) {
#if DEBUG == 2
serialPort0.println("Config::setTimeout()");
#endif
2013-04-07 04:49:23 +00:00
currentTimeout = aTimeout;
ui->setLCDTimeout();
2013-04-07 04:49:23 +00:00
}
void Config::setDefaults() {
#if DEBUG == 2
serialPort0.println("Config::setDefaults()");
#endif
setVoltage(onePointEight);
setLineSpeed(oneFifteenTwoK);
2013-04-09 22:05:22 +00:00
setMode(ttl);
setLCDTimeout(thirtyseconds);
2013-04-07 05:32:42 +00:00
}
2013-04-09 22:11:08 +00:00
void Config::processSerialData() {
//if (DEBUG) {
// Serial.println("Config::processSerialData()");
//}
//#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) {
case 0: // ttl
if (serialPort3.available()) {
int inByte = serialPort3.read();
serialPort0.write(inByte);
ui->blinkCancelButton();
dataFile.print(inByte);
2013-04-09 22:11:08 +00:00
}
if (serialPort0.available()) {
int inByte = serialPort0.read();
serialPort3.write(inByte);
ui->blinkOKButton();
dataFile.print(inByte);
2013-04-09 22:11:08 +00:00
}
break;
case 1: // db9_null
if (serialPort2.available()) {
int inByte = serialPort2.read();
serialPort0.write(inByte);
ui->blinkCancelButton();
dataFile.print(inByte);
2013-04-09 22:11:08 +00:00
}
if (serialPort0.available()) {
int inByte = serialPort0.read();
serialPort2.write(inByte);
ui->blinkOKButton();
dataFile.print(inByte);
2013-04-09 22:11:08 +00:00
}
break;
case 2: // cisco
if (serialPort1.available()) {
int inByte = serialPort1.read();
serialPort0.write(inByte);
ui->blinkCancelButton();
dataFile.print(inByte);
2013-04-09 22:11:08 +00:00
}
if (serialPort0.available()) {
int inByte = serialPort0.read();
serialPort1.write(inByte);
ui->blinkOKButton();
dataFile.print(inByte);
2013-04-09 22:11:08 +00:00
}
break;
}
// Flush data written to log file
dataFile.sync();
}