pretty straigtforward port from the AVR version
This commit is contained in:
239
src/main.cpp
239
src/main.cpp
@@ -1,24 +1,247 @@
|
||||
#include <Arduino.h>
|
||||
#include "Keyboard.h"
|
||||
#include "EEPROM.h"
|
||||
|
||||
#define PIN_BUTTON 12
|
||||
#define SERIAL_PORT Serial
|
||||
|
||||
String password="echo \"czimage:CC37*#dfk7,A6b3cqKS\" >cred && chmod 600 cred && s3fs clonezilla /home/partimag -o passwd_file=cred,use_path_request_style,url=https://ccsnstorage.catholiccharities.com:9000";
|
||||
// configuration
|
||||
|
||||
const int slotcount=1; // number of keys to support
|
||||
String passwords[slotcount]; // password storage
|
||||
int pins[slotcount]={12}; // pins to which keys are attached
|
||||
|
||||
// other globals
|
||||
|
||||
String command=""; // serial-port command buffer
|
||||
const String version="0.1"; // firmware version
|
||||
const String hw_version="0.2"; // hardware version
|
||||
|
||||
// prototypes
|
||||
|
||||
void setup();
|
||||
void loop();
|
||||
void SendPassword(int slot);
|
||||
void LoadPasswords();
|
||||
void SavePasswords();
|
||||
void Help();
|
||||
void ShowPassword(int key);
|
||||
void ShowAll();
|
||||
void ShowVersion();
|
||||
String ReadHWVersion();
|
||||
|
||||
// initialization
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(PIN_BUTTON, INPUT_PULLUP);
|
||||
SERIAL_PORT.begin(9600);
|
||||
SERIAL_PORT.write(']');
|
||||
|
||||
// set up input pins
|
||||
|
||||
for (int slot=0; slot<slotcount; slot++)
|
||||
pinMode(pins[slot], INPUT_PULLUP);
|
||||
|
||||
// load passwords
|
||||
|
||||
LoadPasswords();
|
||||
}
|
||||
|
||||
// event loop
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (!digitalRead(PIN_BUTTON))
|
||||
char inchr;
|
||||
bool cmdComplete=false;
|
||||
|
||||
// scan keys
|
||||
|
||||
for (int slot=0; slot<slotcount; slot++)
|
||||
if (!digitalRead(pins[slot]))
|
||||
SendPassword(slot);
|
||||
|
||||
// grab serial-port input
|
||||
|
||||
if (SERIAL_PORT.available()>0)
|
||||
{
|
||||
for (int i=0; i<password.length(); i++)
|
||||
inchr=SERIAL_PORT.read();
|
||||
if (inchr>=32 && inchr<127) // printable?
|
||||
{
|
||||
Keyboard.press(password[i]);
|
||||
Keyboard.releaseAll();
|
||||
command+=inchr;
|
||||
SERIAL_PORT.write(inchr); // echo it back
|
||||
}
|
||||
delay(500);
|
||||
if (inchr==8 || inchr==127) // delete or backspace?
|
||||
if (command.length()>0)
|
||||
{
|
||||
command=command.substring(0,command.length()-1);
|
||||
SERIAL_PORT.write(8); // erase last character
|
||||
SERIAL_PORT.write(32);
|
||||
SERIAL_PORT.write(8);
|
||||
}
|
||||
if (inchr==13) // return?
|
||||
{
|
||||
cmdComplete=true; // end of line
|
||||
SERIAL_PORT.write("\r\n");
|
||||
}
|
||||
}
|
||||
if (cmdComplete) // EOL?
|
||||
{
|
||||
if (command=="")
|
||||
Help();
|
||||
else if (command.substring(0,5)=="show ")
|
||||
{
|
||||
command=command.substring(5);
|
||||
if (command=="all")
|
||||
ShowAll();
|
||||
else
|
||||
ShowPassword(command.toInt());
|
||||
}
|
||||
else if (command.substring(0,4)=="set ")
|
||||
{
|
||||
command=command.substring(4);
|
||||
int slot=0;
|
||||
while (command[0]!=' ')
|
||||
{
|
||||
slot=10*slot+String(command[0]).toInt();
|
||||
command=command.substring(1);
|
||||
}
|
||||
command=command.substring(1);
|
||||
if (slot>=0 && slot<slotcount)
|
||||
{
|
||||
passwords[slot]=command;
|
||||
SavePasswords();
|
||||
ShowPassword(slot);
|
||||
}
|
||||
else
|
||||
{
|
||||
SERIAL_PORT.write(7);
|
||||
SERIAL_PORT.println("?RANGE ERROR");
|
||||
}
|
||||
}
|
||||
else if (command=="version")
|
||||
{
|
||||
ShowVersion();
|
||||
}
|
||||
else
|
||||
{
|
||||
SERIAL_PORT.write(7);
|
||||
SERIAL_PORT.println("?SYNTAX ERROR"); // Apple II-style error messages, because we can :-)
|
||||
}
|
||||
|
||||
command="";
|
||||
SERIAL_PORT.write(']'); // and an Apple II-style prompt, too
|
||||
}
|
||||
}
|
||||
|
||||
void SendPassword(int slot)
|
||||
{
|
||||
for (int i=0; i<passwords[slot].length(); i++)
|
||||
{
|
||||
Keyboard.press(passwords[slot][i]);
|
||||
Keyboard.release(passwords[slot][i]);
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
// read passwords from EEPROM
|
||||
|
||||
void LoadPasswords()
|
||||
{
|
||||
int memptr=16;
|
||||
char c;
|
||||
uint32_t magic;
|
||||
|
||||
EEPROM.begin(4096);
|
||||
|
||||
EEPROM.get(0, magic);
|
||||
if (magic==0xDEADBEEF) // initialized?
|
||||
for (int slot=0; slot<slotcount; slot++)
|
||||
{
|
||||
passwords[slot]="";
|
||||
while ((c=EEPROM.read(memptr++))!=0)
|
||||
passwords[slot]+=c;
|
||||
}
|
||||
else // no...initialize it
|
||||
{
|
||||
for (int i=0; i<4096; i++) // erase all
|
||||
EEPROM.write(i, 0);
|
||||
|
||||
magic=0xDEADBEEF; // write magic value
|
||||
EEPROM.put(0, magic);
|
||||
|
||||
memptr=4; // write hardware version string
|
||||
for (int i=0; i<hw_version.length(); i++)
|
||||
EEPROM.write(memptr++, hw_version[i]);
|
||||
EEPROM.write(memptr, 0);
|
||||
}
|
||||
|
||||
EEPROM.end();
|
||||
}
|
||||
|
||||
// write passwords to EEPROM
|
||||
|
||||
void SavePasswords()
|
||||
{
|
||||
EEPROM.begin(4096);
|
||||
|
||||
int memptr=16;
|
||||
for (int slot=0; slot<slotcount; slot++)
|
||||
{
|
||||
for (int i=0; i<passwords[slot].length(); i++)
|
||||
EEPROM.write(memptr++,passwords[slot][i]);
|
||||
EEPROM.write(memptr++,0);
|
||||
}
|
||||
|
||||
EEPROM.end();
|
||||
}
|
||||
|
||||
// display help on an empty input
|
||||
|
||||
void Help()
|
||||
{
|
||||
SERIAL_PORT.println("Help\r\n====\r\n");
|
||||
SERIAL_PORT.println("set n data set password for key n to data");
|
||||
SERIAL_PORT.println("show n show password for key n");
|
||||
SERIAL_PORT.println("show all show all passwords");
|
||||
SERIAL_PORT.println("version show hardware & firmware information\r\n");
|
||||
SERIAL_PORT.println("(n starts at 0 and goes to one less than the total");
|
||||
SERIAL_PORT.println("number of keys\r\n");
|
||||
}
|
||||
|
||||
void ShowPassword(int key)
|
||||
{
|
||||
if (key>=0 && key<slotcount)
|
||||
SERIAL_PORT.println("pwd "+String(key)+": "+passwords[key]);
|
||||
else
|
||||
{
|
||||
SERIAL_PORT.write(7);
|
||||
SERIAL_PORT.println("?RANGE ERROR");
|
||||
}
|
||||
}
|
||||
|
||||
void ShowAll()
|
||||
{
|
||||
for (int i=0; i<slotcount; i++)
|
||||
ShowPassword(i);
|
||||
}
|
||||
|
||||
void ShowVersion()
|
||||
{
|
||||
SERIAL_PORT.println("Firmware version: "+version);
|
||||
SERIAL_PORT.println("Hardware version: "+ReadHWVersion());
|
||||
SERIAL_PORT.println("Total keys configured: "+String(slotcount));
|
||||
}
|
||||
|
||||
String ReadHWVersion()
|
||||
{
|
||||
int memptr=4;
|
||||
char c;
|
||||
String version="";
|
||||
|
||||
EEPROM.begin(4096);
|
||||
|
||||
while ((c=EEPROM.read(memptr++))!=0)
|
||||
version+=c;
|
||||
return version;
|
||||
|
||||
EEPROM.end();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user