Apple_II_Keyboard/Apple_II_Keyboard.ino

255 lines
7.1 KiB
Arduino
Raw Normal View History

2019-05-10 13:39:26 +00:00
#include "II_Encoder.h"
#include <hidboot.h>
#include <usbhub.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
#include <avr/pgmspace.h>
#include "layoutmemory.h"
#include "backlog.h"
const char string_0[] PROGMEM = " .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------. ";
const char string_1[] PROGMEM = "| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |";
const char string_2[] PROGMEM = "| | __ | || | ______ | || | ______ | || | _____ | || | _________ | | | | _____ | || | _____ | |";
const char string_3[] PROGMEM = "| | / \\ | || | |_ __ \\ | || | |_ __ \\ | || | |_ _| | || | |_ ___ | | | | | |_ _| | || | |_ _| | |";
const char string_4[] PROGMEM = "| | / /\\ \\ | || | | |__) | | || | | |__) | | || | | | | || | | |_ \\_| | | | | | | | || | | | | |";
const char string_5[] PROGMEM = "| | / ____ \\ | || | | ___/ | || | | ___/ | || | | | _ | || | | _| _ | | | | | | | || | | | | |";
const char string_6[] PROGMEM = "| | _/ / \\ \\_ | || | _| |_ | || | _| |_ | || | _| |__/ | | || | _| |___/ | | | | | _| |_ | || | _| |_ | |";
const char string_7[] PROGMEM = "| ||____| |____|| || | |_____| | || | |_____| | || | |________| | || | |_________| | | | | |_____| | || | |_____| | |";
const char string_8[] PROGMEM = "| | | || | | || | | || | | || | | | | | | || | | |";
const char string_9[] PROGMEM = "| '--------------' || '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |";
const char string_A[] PROGMEM = "'----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' ";
const char *const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5, string_6, string_7, string_8, string_9, string_A};
char buffer[145]; // make sure this is large enough for the largest string it must hold
II_Encoder KeyEncoder;
KeyLayout CurrLayout;
Backlog StringBackLog;
class KbdRptParser : public KeyboardReportParser
{ void PrintKey(uint8_t mod, uint8_t key);
protected:
void OnControlKeysChanged(uint8_t before, uint8_t after);
void OnKeyDown (uint8_t mod, uint8_t key);
void OnKeyUp (uint8_t mod, uint8_t key);
void OnKeyPressed(uint8_t key);
public:
bool bDebugPrint;
};
void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
MODIFIERKEYS mod;
*((uint8_t*)&mod) = m;
if (bDebugPrint)
{
Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
Serial.print(" >");
PrintHex<uint8_t>(key, 0x80);
Serial.print("< ");
Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
Serial.print((mod.bmRightShift == 1) ? "S" : " ");
Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
}
};
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
//Serial.print("DN ");
//PrintKey(mod, key);
uint8_t c = OemToAscii(mod, key);
if (bDebugPrint)
{
Serial.print("MOD: ");
Serial.print(mod, HEX);
Serial.print(" KEY: ");
Serial.println(key, HEX);
}
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
if (0x52 == key) //Arrow UP
{
StringBackLog.OneBack();
}
else if (0x51 == key) //Arrow DOWN
{
StringBackLog.OneForeward();
}
else if (c)
{
OnKeyPressed(c);
}
2019-01-18 15:37:17 +00:00
}
2019-05-10 13:39:26 +00:00
void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after)
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
MODIFIERKEYS beforeMod;
*((uint8_t*)&beforeMod) = before;
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
MODIFIERKEYS afterMod;
*((uint8_t*)&afterMod) = after;
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
if (bDebugPrint)
Serial.println("LeftCtrl changed");
}
if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
if (bDebugPrint)
Serial.println("LeftShift changed");
}
if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
if (bDebugPrint)
Serial.println("LeftAlt changed");
}
if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
if (bDebugPrint)
Serial.println("LeftGUI changed");
}
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
if (bDebugPrint)
Serial.println("RightCtrl changed");
}
if (beforeMod.bmRightShift != afterMod.bmRightShift) {
if (bDebugPrint)
Serial.println("RightShift changed");
}
if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
if (bDebugPrint)
Serial.println("RightAlt changed");
}
if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
if (bDebugPrint)
Serial.println("RightGUI changed");
}
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
}
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
//Serial.print("UP ");
//PrintKey(mod, key);
2019-01-18 15:37:17 +00:00
}
2019-05-10 13:39:26 +00:00
void KbdRptParser::OnKeyPressed(uint8_t key)
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
if (bDebugPrint)
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
Serial.print("ASCII: ");
Serial.print((char)key);
Serial.print(" ");
Serial.println((char)key, HEX);
2019-01-18 15:37:17 +00:00
}
2019-05-10 13:39:26 +00:00
else
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
StringBackLog.addchar((char)key);
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
if (0x0D == key)
Serial.println();
else
Serial.print((char)key);
}
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
if (0x11 == key)
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
if (bDebugPrint)
Serial.print("Reset");
KeyEncoder.Reset();
}
else if (0x12 == key)
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
CurrLayout.Toggle();
SetLayout(CurrLayout.Get());
2019-01-18 15:37:17 +00:00
}
2019-05-10 13:39:26 +00:00
else
KeyEncoder.IIputchar((char)key);
};
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
USB Usb;
//USBHub Hub(&Usb);
HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
KbdRptParser Prs;
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
/*! \brief Wrapper for callback use.
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
*/
int IIputsWrapper(const char *string)
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
KeyEncoder.IIputchar(string);
}
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
/*! \brief Print a welcome message
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
Prints some nice ASCII art through the arduino terminal as a welcome.
*/
void print_welcome_msg(void)
{
Serial.println("");
Serial.println("Welcome to:");
Serial.println("");
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
for (int i = 0; i < 11; i++) {
strcpy_P(buffer, (char *)pgm_read_word(&(string_table[i]))); // Necessary casts and dereferencing, just copy.
Serial.println(buffer);
}
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
Serial.println("");
Serial.println("USB-Keyboard interface.");
Serial.println("");
};
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
/*! \brief The Arduino setup functiopn.
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
Called once at startup.
*/
void setup()
2019-01-18 15:37:17 +00:00
{
2019-05-10 13:39:26 +00:00
Prs.bDebugPrint = false;
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
Serial.begin( 115200 );
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
print_welcome_msg();
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
CurrLayout.Init();
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
if (Usb.Init() == -1)
Serial.println("OSC did not start.c");
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
delay( 200 );
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
HidKeyboard.SetReportParser(0, &Prs);
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
KeyEncoder.begin();
//KeyEncoder.IIputs(" ");
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
Prs.SetLayout(CurrLayout.Get());
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
StringBackLog.SetPuts(&IIputsWrapper);
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
}
2019-01-18 15:37:17 +00:00
2019-05-10 13:39:26 +00:00
/*! \brief The Arduino mainloop.
Only the USB Task is polled here.
*/
void loop()
{
Usb.Task();
2019-01-18 15:37:17 +00:00
}
2019-05-10 13:39:26 +00:00