enigma/code.c.ino

105 lines
1.8 KiB
Arduino
Raw Normal View History

2022-03-21 12:53:49 -04:00
#include <LiquidCrystal_I2C.h>
2022-03-17 16:25:21 -04:00
#include <PS2KeyAdvanced.h>
#include <Wire.h>
2022-03-21 12:53:49 -04:00
2022-03-17 16:25:21 -04:00
#include "enigma_types.h"
#include "HCuOLED.h"
#include "SPI.h"
2022-03-21 12:53:49 -04:00
2022-03-17 16:25:21 -04:00
#define DATAPIN 4
#define IRQPIN 3
2022-03-21 12:53:49 -04:00
#define CS_DI 10
#define DC_DI 9
#define RST_DI 8
2022-03-17 16:25:21 -04:00
PS2KeyAdvanced keyboard;
2022-03-21 12:53:49 -04:00
HCuOLED HCuOLED(SH1106, CS_DI, DC_DI, RST_DI);
2022-03-17 16:25:21 -04:00
LiquidCrystal_I2C lcd(0x27, 16, 2);
2022-03-21 12:53:49 -04:00
String lcd_output;
2022-03-17 16:25:21 -04:00
size_t toKeyIndex(const String& input_hex)
{
size_t index = -1;
for (size_t i = 0; i < ALPHABET_SIZE; ++i)
{
if (key_values[i].hex == input_hex)
{
index = i;
break;
}
}
return index;
}
2022-03-21 12:53:49 -04:00
size_t encode(size_t index)
2022-03-17 16:25:21 -04:00
{
for (size_t i = 0; i < WHEELS_AMOUNT; ++i)
{
index += key_shifts[i];
}
return index % ALPHABET_SIZE;
}
void rotate()
{
2022-03-21 12:53:49 -04:00
for (size_t i = 0; i < WHEELS_AMOUNT; ++i)
2022-03-17 16:25:21 -04:00
{
2022-03-21 12:53:49 -04:00
++key_shifts[i];
if (key_shifts[i] != ALPHABET_SIZE)
key_shifts[i] = 0;
else
break;
2022-03-17 16:25:21 -04:00
}
}
void setup()
{
HCuOLED.Reset();
keyboard.begin(DATAPIN, IRQPIN);
Serial.begin(115200);
2022-03-21 12:53:49 -04:00
lcd.begin();
2022-03-17 16:25:21 -04:00
lcd.backlight();
}
void loop()
{
if (!keyboard.available())
return;
const uint16_t key = keyboard.read();
if (key <= 0)
return;
const String value = String(key, HEX);
const size_t index = toKeyIndex(value);
if (index == -1)
return;
rotate();
2022-03-21 12:53:49 -04:00
const String encoded_letter = key_values[encode(index)].view;
2022-03-17 16:25:21 -04:00
size_t y = 0;
for (size_t i = 0; i < WHEELS_AMOUNT; ++i)
{
HCuOLED.Cursor(4,y);
HCuOLED.SetFont(MedProp_11pt);
HCuOLED.Print(key_shifts[i]);
y += 20;
}
if (lcd_output.length() == 16)
{
lcd.setCursor(0, 0);
lcd_output = "";
}
lcd_output = (lcd_output + encoded_letter);
lcd.clear();
lcd.print(lcd_output);
}