Fix encoding

This commit is contained in:
NaiJi ✨ 2022-03-22 14:58:12 +03:00
parent 1c4c94774d
commit ab989d189a
2 changed files with 186 additions and 15 deletions

View File

@ -18,9 +18,9 @@ HCuOLED HCuOLED(SH1106, CS_DI, DC_DI, RST_DI);
LiquidCrystal_I2C lcd(0x27, 16, 2); LiquidCrystal_I2C lcd(0x27, 16, 2);
String lcd_output; String lcd_output;
size_t toKeyIndex(const String& input_hex) uint16_t toKeyIndex(const String& input_hex)
{ {
size_t index = -1; uint16_t index = -1;
for (size_t i = 0; i < ALPHABET_SIZE; ++i) for (size_t i = 0; i < ALPHABET_SIZE; ++i)
{ {
if (key_values[i].hex == input_hex) if (key_values[i].hex == input_hex)
@ -33,14 +33,67 @@ size_t toKeyIndex(const String& input_hex)
return index; return index;
} }
size_t forward(size_t index, size_t current_wheel)
{
size_t input = (index + key_shifts[current_wheel]) % ALPHABET_SIZE;
size_t output = 0;
for (size_t i = 0; i < ALPHABET_SIZE; ++i)
{
if (mutations[current_wheel][i].from == input)
{
output = mutations[current_wheel][i].to;
break;
}
}
return output;
}
size_t backward(size_t index, size_t current_wheel)
{
int output = 0;
for (size_t i = 0; i < ALPHABET_SIZE; ++i)
{
if (input == mutations[current_wheel][i].to)
{
output = (mutations[current_wheel][i].from - key_shifts[current_wheel]);
while (output < 0)
{
output += ALPHABET_SIZE;
}
output = output % ALPHABET_SIZE;
}
}
return output;
}
size_t encode(size_t index) size_t encode(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 = forward(index, i);
} }
return index % ALPHABET_SIZE; size_t overturning_input = index % ALPHABET_SIZE;
for (size_t i = 0; i < ALPHABET_SIZE; ++i)
{
if (mutations[3][i].from == overturning_input)
{
index = mutations[3][i].to;
break;
}
}
for (size_t i = (WHEELS_AMOUNT - 1); i >= 0; --i)
{
index = backward(index, i);
}
return index;
} }
void rotate() void rotate()
@ -75,17 +128,17 @@ void loop()
return; return;
const String value = String(key, HEX); const String value = String(key, HEX);
const size_t index = toKeyIndex(value); const uint16_t index = toKeyIndex(value);
if (index == -1) if (index == -1)
return; return;
rotate(); size_t encoded_index = encode(index);
const String encoded_letter = key_values[encoded_index].view;
const String encoded_letter = key_values[encode(index)].view;
size_t y = 0; size_t y = 0;
for (size_t i = 0; i < WHEELS_AMOUNT; ++i) for (size_t i = 0; i < WHEELS_AMOUNT; ++i)
{ {
HCuOLED.Cursor(4,y); HCuOLED.Cursor(4, y);
HCuOLED.SetFont(MedProp_11pt); HCuOLED.SetFont(MedProp_11pt);
HCuOLED.Print(key_shifts[i]); HCuOLED.Print(key_shifts[i]);
y += 20; y += 20;
@ -101,4 +154,5 @@ void loop()
lcd.clear(); lcd.clear();
lcd.print(lcd_output); lcd.print(lcd_output);
rotate();
} }

View File

@ -38,16 +38,133 @@ const Key key_values[ALPHABET_SIZE] =
{"4d", "m"} // Code4D - m {"4d", "m"} // Code4D - m
}; };
// Сдвиг в алфавите для каждого из "дисков". // Сдвиг в алфавите для каждого из "дисков"
// Обязан быть в отрезке [1; 25], так как полный круг // относительно их нормального состояния
// возвращает значение обратно в 0.
// Таким образом, при конфигурации { 1, 2, 5 } и
// нажатой ' d ' сначала произойдёт сдвиг на 1, ' d '
// станет ' f ', потом на 2, ' f ' станет ' h ',
// в конце ещё сдвиг на 5 и ' h ' станет ' x '.
size_t key_shifts[WHEELS_AMOUNT] = size_t key_shifts[WHEELS_AMOUNT] =
{ {
3, 3,
1, 1,
2 2
};
struct Mutation
{
size_t from = 0;
size_t to = 0;
};
const Mutation mutations[WHEELS_AMOUNT + 1][ALPHABET_SIZE] =
{
{
{0, 15 },
{1, 4 },
{2, 25 },
{3, 20 },
{4, 14 },
{5, 7 },
{6, 23 },
{7, 18 },
{8, 2 },
{9, 21 },
{10, 5 },
{11, 12 },
{12, 19 },
{13, 1 },
{14, 6 },
{15, 11 },
{16, 17 },
{17, 8 },
{18, 13 },
{19, 16 },
{20, 9 },
{21, 22 },
{22, 0 },
{23, 24 },
{24, 3 },
{25, 10 }
},
{
{0, 25 },
{1, 14 },
{2, 20 },
{3, 4 },
{4, 18 },
{5, 24 },
{6, 3 },
{7, 10 },
{8, 5 },
{9, 22 },
{10, 15 },
{11, 2 },
{12, 8 },
{13, 16 },
{14, 23 },
{15, 7 },
{16, 12 },
{17, 21 },
{18, 1 },
{19, 11 },
{20, 6 },
{21, 13 },
{22, 9 },
{23, 17 },
{24, 0 },
{25, 19 }
},
{
{0, 4 },
{1, 7 },
{2, 17 },
{3, 21 },
{4, 23 },
{5, 6 },
{6, 0 },
{7, 14 },
{8, 1 },
{9, 16 },
{10, 20 },
{11, 18 },
{12, 8 },
{13, 12 },
{14, 25 },
{15, 5 },
{16, 11 },
{17, 24 },
{18, 13 },
{19, 22 },
{20, 10 },
{21, 19 },
{22, 15 },
{23, 3 },
{24, 9 },
{25, 2 }
},
{
{0, 21},
{1, 10},
{2, 22},
{3, 17},
{4, 6},
{5, 8},
{6, 4},
{7, 19},
{8, 5},
{9, 25},
{10, 1},
{11, 20},
{12, 18},
{13, 15},
{14, 16},
{15, 13},
{16, 14},
{17, 3},
{18, 12},
{19, 7},
{20, 11},
{21, 0},
{22, 2},
{23, 24},
{24, 23},
{25, 9}
}
}; };