1
0
Fork 0

Update 'code.c.ino'

master
NaiJi ✨ 2 years ago
parent dee279a789
commit a1933c04df

@ -1,67 +1,118 @@
#include <PS2KeyAdvanced.h> #include <PS2KeyAdvanced.h>
#include "enigma_types.h" #include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DATAPIN 2 #include "enigma_types.h"
#define IRQPIN 3 #include "HCuOLED.h"
#include "SPI.h"
PS2KeyAdvanced keyboard; #define DATAPIN 4
#define IRQPIN 3
// Сконвертировать HEX значение клавиши #define CS_DI 10
// в её алфавитный индекс; #define DC_DI 9
// #define RST_DI 8
// возвращает индекс буквы PS2KeyAdvanced keyboard;
size_t toKeyIndex(const String& input_hex) HCuOLED HCuOLED(SH1106, CS_DI, DC_DI, RST_DI); // For SH1106 displays (HCMODU0058 & HCMODU0059)
{ // Set the LCD address to 0x27 for a 16 chars and 2 line display
size_t index = -1; LiquidCrystal_I2C lcd(0x27, 16, 2);
for (size_t i = 0; i < ALPHABET_SIZE; ++i)
{ size_t toKeyIndex(const String& input_hex)
if (key_values[i].hex == input_hex) {
{ size_t index = -1;
index = i; for (size_t i = 0; i < ALPHABET_SIZE; ++i)
break; {
} if (key_values[i].hex == input_hex)
} {
index = i;
return index; break;
} }
}
// Взять текущий сдвиг в алфавите.
// Считается суммарно по всем дискам в положении return index;
// на момент вызова; }
//
// возвращает текущий индекс с применением сдвига size_t shift(size_t index)
size_t shift(size_t index) {
{ for (size_t i = 0; i < WHEELS_AMOUNT; ++i)
for (size_t i = 0; i < WHEELS_AMOUNT; ++i) {
{ index += key_shifts[i];
index += key_shifts[i]; }
}
return index % ALPHABET_SIZE;
return index % ALPHABET_SIZE; }
}
void rotate()
///////////////////////////////////////////////////// {
++key_shifts[0];
void setup() if (key_shifts[0] == 26)
{ {
keyboard.begin(DATAPIN, IRQPIN); key_shifts[0] = 1;
Serial.begin(115200); ++key_shifts[1];
} if (key_shifts[1] == 26)
{
void loop() key_shifts[1] = 1;
{ ++key_shifts[2];
if (!keyboard.available()) if (key_shifts[2] == 26)
return; {
key_shifts[2] = 1;
const uint16_t key = keyboard.read(); }
if (key <= 0) }
return; }
const String value = String(key, HEX); Serial.print(key_shifts[0]);
const size_t index = toKeyIndex(value); Serial.print(" ");
if (index == -1) Serial.print(key_shifts[1]);
return; Serial.print(" ");
Serial.print(key_shifts[2]);
const String encoded_letter = key_values[shift(index)].view; Serial.println(" ");
// отправить encoded_letter на экран }
}
String lcd_output;
void setup()
{
HCuOLED.Reset();
keyboard.begin(DATAPIN, IRQPIN);
Serial.begin(115200);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
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();
const String encoded_letter = key_values[shift(index)].view;
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);
}

Loading…
Cancel
Save