Added initial sketch

This commit is contained in:
Mike C 2013-03-10 14:26:41 -04:00
parent f63acd1fd6
commit 60463b589f
2 changed files with 155 additions and 0 deletions

View file

@ -0,0 +1,57 @@
// LCD
LCDShield lcd; // Line length max is 16
// Buttons
int buttonPins[3] = {3, 4, 5};
// Standard colors
#define BACKGROUND BLACK
#define TEXT GRAY
#define HILIGHT GOLD
// Font sizes
#define CHAR_WIDTH 8
#define CHAR_HEIGHT 16
// Serial modes supported
// Abused in for loops / lookup tables -- DO NOT CHANGE none or set values
enum serialmode {
phone,
db9_norm,
db9_null,
cisco,
none=-1
};
// Map a mode -> text value
char* modeToText[] = {
"Phone UART",
"DB9 - Normal",
"DB9 - Null Mdm",
"Cisco console"
};
// Mode info needed
serialmode currentMode = none;
serialmode selectedMode = none;
// Printing text
void printMode(serialmode aMode);
void setMode(serialmode aMode);
void setSelection(serialmode aMode);
// Defaults
void setDefaults() {
setMode(phone);
setSelection(phone);
}
// Figure out offsets
int xLoc(int toSkip) { // Physically -- vertical
return (CHAR_HEIGHT * toSkip) + (CHAR_HEIGHT / 2);
}
int yLoc (int toSkip) { // Physical -- horizontal
return (CHAR_WIDTH * toSkip) + (CHAR_WIDTH / 2);
}

View file

@ -0,0 +1,98 @@
/*
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.
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: Down
S3: Select/Enter
Serial Modes:
Phone UART
DB9 Normal
DB9 Null Modem
Cisco console
*/
#include <ColorLCDShield.h>
#include "Project.h"
void setup() {
/* Set up the button pins as inputs, set pull-up resistor */
for (int i=0; i<3; i++) {
pinMode(buttonPins[i], INPUT);
digitalWrite(buttonPins[i], HIGH);
}
/* Initialize the LCD, set the contrast, clear the screen */
lcd.init(PHILIPS);
lcd.contrast(-63);
lcd.clear(BACKGROUND);
// Print the modes
// Uses enum trickery -- don't assign values to serialmode enum values
for (int i=0; i<=cisco; i++) {
printMode((serialmode)i);
}
// Setup defaults
setDefaults();
}
void loop() {
if (!digitalRead(buttonPins[0])) {
serialmode newMode = (serialmode)(selectedMode + 1);
if (newMode <= cisco) {
setSelection(newMode);
}
// Wait for release before going on
while(!digitalRead(buttonPins[0]));
}
if (!digitalRead(buttonPins[1])) {
serialmode newMode = (serialmode)(selectedMode - 1);
if (newMode >= 0) {
setSelection(newMode);
}
// Wait for release before going on
while(!digitalRead(buttonPins[1]));
}
if (!digitalRead(buttonPins[2])) {
setMode(selectedMode);
// Wait for release before going on
while(!digitalRead(buttonPins[2]));
}
}
void setMode(serialmode aMode) {
serialmode previousMode = currentMode;
currentMode = aMode;
lcd.setStr(" ", xLoc(previousMode), yLoc(0), TEXT, BACKGROUND); // Clear old *
lcd.setStr("*", xLoc(currentMode), yLoc(0), TEXT, BACKGROUND); // Add new *
}
void setSelection(serialmode aMode) {
serialmode previousSelection = selectedMode;
selectedMode = aMode;
int previousLength = strlen(modeToText[previousSelection]) * CHAR_WIDTH;
int selectedLength = strlen(modeToText[selectedMode]) * CHAR_WIDTH;
lcd.setLine(xLoc(selectedMode) + CHAR_HEIGHT, yLoc(1), xLoc(selectedMode) + CHAR_HEIGHT, yLoc(1) + selectedLength, HILIGHT);
lcd.setLine(xLoc(previousSelection) + CHAR_HEIGHT, yLoc(1), xLoc(previousSelection) + CHAR_HEIGHT, yLoc(1) + previousLength, BACKGROUND);
}
void printMode(serialmode aMode) {
lcd.setStr(modeToText[aMode], xLoc(aMode), yLoc(1), TEXT, BACKGROUND);
}