enigma/code.c.ino

79 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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);
}