From d5a8c0053937b459c479b49e595aeb2031d88a03 Mon Sep 17 00:00:00 2001 From: catvayor Date: Sat, 12 Oct 2024 16:00:59 +0200 Subject: [PATCH] feat(agb02): fix decoder readings --- machines/agb02/agb/agb.cpp | 30 +++--- machines/agb02/agb/decoder_table.inl | 132 +++++++++++++++++++++++++++ meta.nix | 1 + 3 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 machines/agb02/agb/decoder_table.inl diff --git a/machines/agb02/agb/agb.cpp b/machines/agb02/agb/agb.cpp index fa92675..6b1bc3b 100644 --- a/machines/agb02/agb/agb.cpp +++ b/machines/agb02/agb/agb.cpp @@ -6,13 +6,13 @@ using namespace std::literals::chrono_literals; constexpr std::chrono::microseconds debounce = 40ms; -constexpr std::chrono::microseconds poll_period = 50ms; +constexpr std::chrono::microseconds poll_period = 5ms; constexpr std::pair joystick_movement(1.0, 1.0); const gpiod::line::offsets drive_down = { 21, 13, 6 }; -const gpiod::line::offsets decoder = { 0, 0, 0, 0, 0, 0, 0, 0 }; // lsbf +const gpiod::line::offsets decoder = { 2, 3, 4, 14, 15, 18, 23, 24 }; // lsbf const gpiod::line::offsets joystick = { 0, 0, 0, 0 }; // x+, y+, x-, y- const gpiod::line::offset black_button = 0; const gpiod::line::offset white_button = 0; @@ -24,21 +24,15 @@ const gpiod::line_settings input_settings = .set_active_low(true) .set_debounce_period(debounce); -// thanks to https://en.wikipedia.org/wiki/Gray_code#Converting_to_and_from_Gray_code -inline uint8_t gray_to_binary(uint8_t num) -{ - num ^= num >> 4; - num ^= num >> 2; - num ^= num >> 1; - return num; -} +constexpr std::array decoder_table = + #include "decoder_table.inl" uint8_t read_decoder_realpos(gpiod::line_request& line_reader){ static gpiod::line::values decoder_read(8); line_reader.get_values(decoder, decoder_read); uint8_t graycode = 0; for(uint8_t i = 0; i < 8; ++i) graycode |= uint8_t(decoder_read[i]) << i; - return gray_to_binary(graycode); + return decoder_table[graycode]; }; inline void clamp_decoder(uint8_t& decoder, int move){ @@ -63,7 +57,10 @@ int main(const int argc, char const* const* const argv) { ) .add_line_settings({ black_button, white_button }, input_settings) .add_line_settings(joystick, input_settings) - .add_line_settings(decoder, input_settings) + .add_line_settings(decoder, + gpiod::line_settings(input_settings) + .set_active_low(false) + .set_debounce_period(0ms)) .do_request(); // let the settings apply @@ -121,16 +118,17 @@ int main(const int argc, char const* const* const argv) { // CW if(seen_travel < 50 && new_realpos < decoder_realpos) - clamp_decoder(decoder_pos, seen_travel); + clamp_decoder(decoder_pos, -seen_travel); if(seen_travel >= 50 && new_realpos > decoder_realpos) - clamp_decoder(decoder_pos, 256 - seen_travel); + clamp_decoder(decoder_pos, seen_travel - 128); // CCW if(seen_travel < 50 && new_realpos > decoder_realpos) - clamp_decoder(decoder_pos, -seen_travel); + clamp_decoder(decoder_pos, seen_travel); if(seen_travel >= 50 && new_realpos < decoder_realpos) - clamp_decoder(decoder_pos, seen_travel - 256); + clamp_decoder(decoder_pos, 128 - seen_travel); + decoder_realpos = new_realpos; if(seen_travel) ; //TODO: send to server } diff --git a/machines/agb02/agb/decoder_table.inl b/machines/agb02/agb/decoder_table.inl new file mode 100644 index 0000000..445b2e9 --- /dev/null +++ b/machines/agb02/agb/decoder_table.inl @@ -0,0 +1,132 @@ +[]() { + std::array table; + table[127] = 0; + table[63] = 1; + table[62] = 2; + table[58] = 3; + table[56] = 4; + table[184] = 5; + table[152] = 6; + table[24] = 7; + table[8] = 8; + table[72] = 9; + table[73] = 10; + table[77] = 11; + table[79] = 12; + table[15] = 13; + table[47] = 14; + table[175] = 15; + table[191] = 16; + table[159] = 17; + table[31] = 18; + table[29] = 19; + table[28] = 20; + table[92] = 21; + table[76] = 22; + table[12] = 23; + table[4] = 24; + table[36] = 25; + table[164] = 26; + table[166] = 27; + table[167] = 28; + table[135] = 29; + table[151] = 30; + table[215] = 31; + table[223] = 32; + table[207] = 33; + table[143] = 34; + table[142] = 35; + table[14] = 36; + table[46] = 37; + table[38] = 38; + table[6] = 39; + table[2] = 40; + table[18] = 41; + table[82] = 42; + table[83] = 43; + table[211] = 44; + table[195] = 45; + table[203] = 46; + table[235] = 47; + table[239] = 48; + table[231] = 49; + table[199] = 50; + table[71] = 51; + table[7] = 52; + table[23] = 53; + table[19] = 54; + table[3] = 55; + table[1] = 56; + table[9] = 57; + table[41] = 58; + table[169] = 59; + table[233] = 60; + table[225] = 61; + table[229] = 62; + table[245] = 63; + table[247] = 64; + table[243] = 65; + table[227] = 66; + table[163] = 67; + table[131] = 68; + table[139] = 69; + table[137] = 70; + table[129] = 71; + table[128] = 72; + table[132] = 73; + table[148] = 74; + table[212] = 75; + table[244] = 76; + table[240] = 77; + table[242] = 78; + table[250] = 79; + table[251] = 80; + table[249] = 81; + table[241] = 82; + table[209] = 83; + table[193] = 84; + table[197] = 85; + table[196] = 86; + table[192] = 87; + table[64] = 88; + table[66] = 89; + table[74] = 90; + table[106] = 91; + table[122] = 92; + table[120] = 93; + table[121] = 94; + table[125] = 95; + table[253] = 96; + table[252] = 97; + table[248] = 98; + table[232] = 99; + table[224] = 100; + table[226] = 101; + table[98] = 102; + table[96] = 103; + table[32] = 104; + table[33] = 105; + table[37] = 106; + table[53] = 107; + table[61] = 108; + table[60] = 109; + table[188] = 110; + table[190] = 111; + table[254] = 112; + table[126] = 113; + table[124] = 114; + table[116] = 115; + table[112] = 116; + table[113] = 117; + table[49] = 118; + table[48] = 119; + table[16] = 120; + table[144] = 121; + table[146] = 122; + table[154] = 123; + table[158] = 124; + table[30] = 125; + table[94] = 126; + table[95] = 127; + return table; +} (); diff --git a/meta.nix b/meta.nix index cb4fd43..281ff89 100644 --- a/meta.nix +++ b/meta.nix @@ -44,6 +44,7 @@ let targetHost = null; }; arch = "aarch64-linux"; + imports = [ agenix ]; }; }; };