Re-factor globals into project header and re-factor UI code out into standard C++ structure

This commit is contained in:
Mike C 2013-03-13 16:29:54 -04:00
parent 4f23967727
commit 1d787fcc52
4 changed files with 236 additions and 222 deletions

View file

@ -6,39 +6,15 @@
license: CC-BY SA 3.0 - Creative commons share-alike 3.0 license: CC-BY SA 3.0 - Creative commons share-alike 3.0
use this code however you'd like, just keep this license and use this code however you'd like, just keep this license and
attribute. attribute.
This sketch currently only shows the four serial modes and
uses S1-S3 to select the current mode.
A trailing * after the mode indicates the current mode
Button Map:
S1: Up
S2: Select/Enter
S3: Down
Serial Modes:
Phone UART
DB9 Normal
DB9 Null Modem
Cisco console
*/ */
// LCD #include <ColorLCDShield.h>
LCDShield lcd; // Line length max is 16
// Buttons
int buttonPins[3] = {3, 4, 5};
// Standard colors // Standard colors
#define BACKGROUND BLACK #define BACKGROUND BLACK
#define TEXT GRAY #define TEXT GRAY
#define HILIGHT GOLD #define HILIGHT GOLD
// Font sizes
#define CHAR_WIDTH 8
#define CHAR_HEIGHT 16
// Serial modes supported // Serial modes supported
// Abused in for loops / lookup tables -- DO NOT CHANGE none or set values // Abused in for loops / lookup tables -- DO NOT CHANGE none or set values
enum serialmode { enum serialmode {
@ -51,12 +27,7 @@ enum serialmode {
}; };
// Map a mode -> text value // Map a mode -> text value
char* modeToText[] = { extern char* modeToText[];
"Phone UART",
"DB9 - Normal",
"DB9 - Null Mdm",
"Cisco console"
};
// Line speeds supported // Line speeds supported
// Abused in for loops / lookup talbes -- DO NOT CHANGE none or set values // Abused in for loops / lookup talbes -- DO NOT CHANGE none or set values
@ -78,36 +49,16 @@ struct linespeedinfo {
}; };
// Known and supported line speeds // Known and supported line speeds
linespeedinfo linespeeds[] = { extern linespeedinfo linespeeds[];
{"2400b", 2400},
{"9600b", 9600}, // LCD
{"19.2k", 19200}, extern LCDShield lcd; // Line length max is 16
{"38.4k", 38400},
{"57.5k", 57600}, // Buttons
{"115k", 115200} extern int buttonPins[];
};
// Mode info needed // Mode info needed
serialmode currentMode = none; extern serialmode currentMode;
serialmode selectedMode = none; extern serialmode selectedMode;
linespeed currentLineSpeed = zero; extern linespeed currentLineSpeed;
// Printing text
void printMode(serialmode aMode);
void printLineSpeed(linespeedinfo linespeed);
// Various sets for mode/selection/linespeed
void setMode(serialmode aMode);
void setSelection(serialmode aMode);
void setLineSpeed(linespeed aLineSpeed);
// Figure out offsets for text printing
int xLoc(float toSkip) { // Physically -- vertical
return (CHAR_HEIGHT * toSkip) + (CHAR_HEIGHT / 2);
}
int yLoc (float toSkip) { // Physical -- horizontal
return (CHAR_WIDTH * toSkip) + (CHAR_WIDTH / 2);
}

View file

@ -0,0 +1,160 @@
/*
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.
*/
#include <Arduino.h>
#include "Project.h"
#include "UI.h"
// Figure out offsets for text printing
int xLoc(float toSkip) { // Physically -- vertical
return (CHAR_HEIGHT * toSkip) + (CHAR_HEIGHT / 2);
}
int yLoc (float toSkip) { // Physical -- horizontal
return (CHAR_WIDTH * toSkip) + (CHAR_WIDTH / 2);
}
void setLineSpeed(linespeed aLineSpeed) {
if (aLineSpeed >= maxlinespeed) {
currentLineSpeed = (linespeed)0;
}
else {
currentLineSpeed = aLineSpeed;
}
bool sel = selectedMode == modelinespeed ? true : false;
printLineSpeed(currentLineSpeed, sel);
}
void setMode(serialmode aMode) {
if (aMode != modelinespeed) {
serialmode previousMode = currentMode;
currentMode = aMode;
lcd.setStr(" ", xLoc(previousMode), yLoc(0), TEXT, BACKGROUND);
lcd.setStr("*", xLoc(currentMode), yLoc(0), TEXT, BACKGROUND);
}
else {
setLineSpeed((linespeed)(currentLineSpeed + 1));
}
}
void setSelection(serialmode aMode) {
serialmode previousSelection = selectedMode;
selectedMode = aMode;
int yLocOne = yLoc(1);
int xSelected = 0;
if (selectedMode != modelinespeed) {
xSelected = xLoc(selectedMode) + CHAR_HEIGHT;
}
int xPrevious = 0;
if (previousSelection != modelinespeed) {
xPrevious = xLoc(previousSelection) + CHAR_HEIGHT;
}
int previousLength = 0;
if (previousSelection != modelinespeed) {
previousLength = strlen(modeToText[previousSelection]) * CHAR_WIDTH;
}
int selectedLength = 0;
if (selectedMode != modelinespeed) {
selectedLength = strlen(modeToText[selectedMode]) * CHAR_WIDTH;
}
if (selectedMode != modelinespeed) {
lcd.setLine(xSelected, yLocOne, xSelected, yLocOne + selectedLength, HILIGHT);
}
else {
printLineSpeed(currentLineSpeed, true);
}
if (previousSelection != modelinespeed) {
lcd.setLine(xPrevious, yLocOne, xPrevious, yLocOne + previousLength, BACKGROUND);
}
else {
printLineSpeed(currentLineSpeed, false);
}
}
void printTitles() {
lcd.setStr(" RX Ln Spd Tx ", xLoc(5), 0, TEXT, BACKGROUND);
}
void printMode(serialmode aMode) {
lcd.setStr(modeToText[aMode], xLoc(aMode), yLoc(1), TEXT, BACKGROUND);
}
void printLineSpeed(linespeed aLineSpeed, bool selected) {
int xPosText = xLoc(6);
int yPosText = yLoc(5);
int xPosLine = xLoc(7);
int yPosLine = yLoc(5);
char* toPrint = linespeeds[aLineSpeed].description;
int length = strlen(toPrint);
lcd.setStr(" ", xPosText, yPosText, BACKGROUND, BACKGROUND);
lcd.setLine(xPosLine, yPosLine, xPosLine, yPosLine + 7 * CHAR_WIDTH, BACKGROUND);
lcd.setStr(toPrint, xPosText, yPosText, TEXT, BACKGROUND);
if (selected) {
lcd.setLine(xPosLine, yPosLine, xPosLine, yPosLine + length * CHAR_WIDTH, HILIGHT);
}
}
void printRx(bool show) {
int vertXPosStart = xLoc(6.25);
int vertYPosStart = yLoc(1.5);
int vertXPosEnd = xLoc(7.25);
int vertYPosEnd = yLoc(1.5);
int lftXPosStart = vertXPosEnd;
int lftYPosStart = vertYPosStart;
int lftXPosEnd = vertXPosStart + (CHAR_HEIGHT / 2);
int lftYPosEnd = vertYPosStart - CHAR_WIDTH;
int rtXPosStart = vertXPosEnd;
int rtYPosStart = vertYPosStart;
int rtXPosEnd = vertXPosStart + (CHAR_HEIGHT / 2);
int rtYPosEnd = vertYPosStart + CHAR_WIDTH;
int color = show ? EMERALD : BACKGROUND;
lcd.setLine(vertXPosStart, vertYPosStart, vertXPosEnd, vertYPosEnd, color);
lcd.setLine(lftXPosStart, lftYPosStart, lftXPosEnd, lftYPosEnd, color);
lcd.setLine(rtXPosStart, rtYPosStart, rtXPosEnd, rtYPosEnd, color);
}
void printTx(bool show) {
int vertXPosStart = xLoc(6.25);
int vertYPosStart = yLoc(13.5);
int vertXPosEnd = xLoc(7.25);
int vertYPosEnd = yLoc(13.5);
int lftXPosStart = vertXPosStart;
int lftYPosStart = vertYPosStart;
int lftXPosEnd = vertXPosStart + (CHAR_HEIGHT / 2);
int lftYPosEnd = vertYPosStart - CHAR_WIDTH;
int rtXPosStart = vertXPosStart;
int rtYPosStart = vertYPosStart;
int rtXPosEnd = vertXPosStart + (CHAR_HEIGHT / 2);
int rtYPosEnd = vertYPosStart + CHAR_WIDTH;
int color = show ? SKYBLUE : BACKGROUND;
lcd.setLine(vertXPosStart, vertYPosStart, vertXPosEnd, vertYPosEnd, color);
lcd.setLine(lftXPosStart, lftYPosStart, lftXPosEnd, lftYPosEnd, color);
lcd.setLine(rtXPosStart, rtYPosStart, rtXPosEnd, rtYPosEnd, color);
}

View file

@ -0,0 +1,27 @@
/*
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.
*/
// Font sizes
#define CHAR_WIDTH 8
#define CHAR_HEIGHT 16
void setLineSpeed(linespeed aLineSpeed);
void setMode(serialmode aMode);
void setSelection(serialmode aMode);
void printTitles();
void printMode(serialmode aMode);
void printLineSpeed(linespeed aLineSpeed, bool selected);
void printRx(bool show);
void printTx(bool show);
int xLoc(float toSkip);
int yLoc (float toSkip);

View file

@ -6,27 +6,48 @@
license: CC-BY SA 3.0 - Creative commons share-alike 3.0 license: CC-BY SA 3.0 - Creative commons share-alike 3.0
use this code however you'd like, just keep this license and use this code however you'd like, just keep this license and
attribute. attribute.
This sketch currently only shows the four serial modes and
uses S1-S3 to select the current mode.
A trailing * after the mode indicates the current mode
Button Map:
S1: Up
S2: Select/Enter
S3: Down
Serial Modes:
Phone UART
DB9 Normal
DB9 Null Modem
Cisco console
*/ */
#include <ColorLCDShield.h> #include <ColorLCDShield.h>
#include "Project.h" #include "Project.h"
#include "UI.h"
// Map a mode -> text value
char* modeToText[4] = {
"Phone UART",
"DB9 - Normal",
"DB9 - Null Mdm",
"Cisco console"
};
// Known and supported line speeds
linespeedinfo linespeeds[6] = {
{"2400b", 2400},
{"9600b", 9600},
{"19.2k", 19200},
{"38.4k", 38400},
{"57.5k", 57600},
{"115k", 115200}
};
// LCD
LCDShield lcd; // Line length max is 16
// Buttons
int buttonPins[3] = {3, 4, 5};
// Mode info needed
serialmode currentMode = none;
serialmode selectedMode = none;
linespeed currentLineSpeed = zero;
// Defaults
void setDefaults() {
setMode(phone);
setSelection(phone);
setLineSpeed(oneNineteenTwoK);
}
void setup() { void setup() {
/* Set up the button pins as inputs, set pull-up resistor */ /* Set up the button pins as inputs, set pull-up resistor */
@ -81,148 +102,3 @@ void loop() {
while(!digitalRead(buttonPins[0])); while(!digitalRead(buttonPins[0]));
} }
} }
// Defaults
void setDefaults() {
setMode(phone);
setSelection(phone);
setLineSpeed(oneNineteenTwoK);
}
void setLineSpeed(linespeed aLineSpeed) {
if (aLineSpeed >= maxlinespeed) {
currentLineSpeed = (linespeed)0;
}
else {
currentLineSpeed = aLineSpeed;
}
bool sel = selectedMode == modelinespeed ? true : false;
printLineSpeed(currentLineSpeed, sel);
}
void setMode(serialmode aMode) {
if (aMode != modelinespeed) {
serialmode previousMode = currentMode;
currentMode = aMode;
lcd.setStr(" ", xLoc(previousMode), yLoc(0), TEXT, BACKGROUND);
lcd.setStr("*", xLoc(currentMode), yLoc(0), TEXT, BACKGROUND);
}
else {
setLineSpeed((linespeed)(currentLineSpeed + 1));
}
}
void setSelection(serialmode aMode) {
serialmode previousSelection = selectedMode;
selectedMode = aMode;
int yLocOne = yLoc(1);
int xSelected = 0;
if (selectedMode != modelinespeed) {
xSelected = xLoc(selectedMode) + CHAR_HEIGHT;
}
int xPrevious = 0;
if (previousSelection != modelinespeed) {
xPrevious = xLoc(previousSelection) + CHAR_HEIGHT;
}
int previousLength = 0;
if (previousSelection != modelinespeed) {
previousLength = strlen(modeToText[previousSelection]) * CHAR_WIDTH;
}
int selectedLength = 0;
if (selectedMode != modelinespeed) {
selectedLength = strlen(modeToText[selectedMode]) * CHAR_WIDTH;
}
if (selectedMode != modelinespeed) {
lcd.setLine(xSelected, yLocOne, xSelected, yLocOne + selectedLength, HILIGHT);
}
else {
printLineSpeed(currentLineSpeed, true);
}
if (previousSelection != modelinespeed) {
lcd.setLine(xPrevious, yLocOne, xPrevious, yLocOne + previousLength, BACKGROUND);
}
else {
printLineSpeed(currentLineSpeed, false);
}
}
void printTitles() {
lcd.setStr(" RX Ln Spd Tx ", xLoc(5), 0, TEXT, BACKGROUND);
}
void printMode(serialmode aMode) {
lcd.setStr(modeToText[aMode], xLoc(aMode), yLoc(1), TEXT, BACKGROUND);
}
void printLineSpeed(linespeed aLineSpeed, bool selected) {
int xPosText = xLoc(6);
int yPosText = yLoc(5);
int xPosLine = xLoc(7);
int yPosLine = yLoc(5);
char* toPrint = linespeeds[aLineSpeed].description;
int length = strlen(toPrint);
lcd.setStr(" ", xPosText, yPosText, BACKGROUND, BACKGROUND);
lcd.setLine(xPosLine, yPosLine, xPosLine, yPosLine + 7 * CHAR_WIDTH, BACKGROUND);
lcd.setStr(toPrint, xPosText, yPosText, TEXT, BACKGROUND);
if (selected) {
lcd.setLine(xPosLine, yPosLine, xPosLine, yPosLine + length * CHAR_WIDTH, HILIGHT);
}
}
void printRx(bool show) {
int vertXPosStart = xLoc(6.25);
int vertYPosStart = yLoc(1.5);
int vertXPosEnd = xLoc(7.25);
int vertYPosEnd = yLoc(1.5);
int lftXPosStart = vertXPosEnd;
int lftYPosStart = vertYPosStart;
int lftXPosEnd = vertXPosStart + (CHAR_HEIGHT / 2);
int lftYPosEnd = vertYPosStart - CHAR_WIDTH;
int rtXPosStart = vertXPosEnd;
int rtYPosStart = vertYPosStart;
int rtXPosEnd = vertXPosStart + (CHAR_HEIGHT / 2);
int rtYPosEnd = vertYPosStart + CHAR_WIDTH;
int color = show ? EMERALD : BACKGROUND;
lcd.setLine(vertXPosStart, vertYPosStart, vertXPosEnd, vertYPosEnd, color);
lcd.setLine(lftXPosStart, lftYPosStart, lftXPosEnd, lftYPosEnd, color);
lcd.setLine(rtXPosStart, rtYPosStart, rtXPosEnd, rtYPosEnd, color);
}
void printTx(bool show) {
int vertXPosStart = xLoc(6.25);
int vertYPosStart = yLoc(13.5);
int vertXPosEnd = xLoc(7.25);
int vertYPosEnd = yLoc(13.5);
int lftXPosStart = vertXPosStart;
int lftYPosStart = vertYPosStart;
int lftXPosEnd = vertXPosStart + (CHAR_HEIGHT / 2);
int lftYPosEnd = vertYPosStart - CHAR_WIDTH;
int rtXPosStart = vertXPosStart;
int rtYPosStart = vertYPosStart;
int rtXPosEnd = vertXPosStart + (CHAR_HEIGHT / 2);
int rtYPosEnd = vertYPosStart + CHAR_WIDTH;
int color = show ? SKYBLUE : BACKGROUND;
lcd.setLine(vertXPosStart, vertYPosStart, vertXPosEnd, vertYPosEnd, color);
lcd.setLine(lftXPosStart, lftYPosStart, lftXPosEnd, lftYPosEnd, color);
lcd.setLine(rtXPosStart, rtYPosStart, rtXPosEnd, rtYPosEnd, color);
}