1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.9 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <PS2KeyAdvanced.h>
#include "enigma_types.h"
#define DATAPIN 2
#define IRQPIN 3
PS2KeyAdvanced keyboard;
// Сконвертировать HEX значение клавиши
// в её алфавитный индекс;
//
// возвращает индекс буквы
size_t toKeyIndex(const String& value)
{
size_t index = -1;
for (size_t i = 0; i < ALPHABET_SIZE; ++i)
{
if (key_values[i] == value)
{
index = i;
break;
}
}
return index;
}
// Взять текущий сдвиг в алфавите.
// Считается суммарно по всем дискам в положении
// на момент вызова;
//
// возвращает текущий индекс с применением сдвига
size_t shift(size_t index)
{
for (size_t i = 0; i < WHEELS_AMOUNT; ++i)
{
index += key_shifts[i];
}
return index % ALPHABET_SIZE;
}
// Зашифровать символ из алфавита
// по алгоритму энигмы;
//
// возвращает зашифрованный символ для вывода
// на экран
size_t encode(size_t index)
{
// тут должно быть короче конвертирование по плагборду
const size_t new_index = shift(index);
// и вот тут тоже должноб ыть конвертирование по плагборду, ща
}
/////////////////////////////////////////////////////
void setup()
{
keyboard.begin(DATAPIN, IRQPIN);
Serial.begin(115200);
}
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;
const size_t encoded_index = encode(index);
}