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.

69 lines
1.6 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;
}
/////////////////////////////////////////////////////
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 String encoded_value = key_values[shift(index)];
// encoded_value вот тут надо как-то превратить в букву
// и вывести на экран, хз как пока, надо курить
}