diff --git a/machines/agb02/agb/agb.cpp b/machines/agb02/agb/agb.cpp index 343be6a..05692b9 100644 --- a/machines/agb02/agb/agb.cpp +++ b/machines/agb02/agb/agb.cpp @@ -10,10 +10,11 @@ using namespace std::literals::chrono_literals; constexpr std::chrono::microseconds debounce = 40ms; constexpr std::chrono::microseconds poll_period = 5ms; +constexpr std::chrono::microseconds autorepeat_delay = 70ms; constexpr std::chrono::microseconds server_ratelimit = 50ms; constexpr std::chrono::microseconds retry_timeout = 500ms; -constexpr std::pair joystick_movement(.3, .3); +constexpr double joystick_movement = 0.2; const gpiod::line::offsets drive_down = { 21, 13, 6 }; @@ -94,8 +95,26 @@ int main(const int argc, char const* const* const argv) { /// internal state and buffers /// + std::chrono::time_point now = std::chrono::system_clock::now(); + gpiod::line::values joystick_read(4); + gpiod::line::values joystick_last_read(4); + line_reader.get_values(joystick, joystick_read); + std::vector> rising_point = { now, now, now, now }; std::pair spot_pos(0.0, 0.0); //TODO: init from server + auto joystick_move = [&](int i) -> double { + if (! bool(joystick_read[i])) + return 0.0; + else if (bool(joystick_last_read[i])){ + if (now - rising_point[i] < autorepeat_delay) + return 0.0; + else + return joystick_movement; + } else { + rising_point[i] = now; + return 1.0; + } + }; uint8_t decoder_pos = 0; //TODO: init from server uint8_t decoder_realpos = read_decoder_realpos(line_reader); @@ -104,19 +123,21 @@ int main(const int argc, char const* const* const argv) { bool black_pressed = false; bool has_changed = true; - std::chrono::time_point last_send = std::chrono::system_clock::now(); + std::chrono::time_point last_send = now; std::string postData; for(;;){ std::this_thread::sleep_for(poll_period); + now = std::chrono::system_clock::now(); /// joystick /// + std::swap(joystick_read, joystick_last_read); line_reader.get_values(joystick, joystick_read); - if(bool(joystick_read[0])) spot_pos.first += joystick_movement.first; - if(bool(joystick_read[1])) spot_pos.second += joystick_movement.second; - if(bool(joystick_read[2])) spot_pos.first -= joystick_movement.first; - if(bool(joystick_read[3])) spot_pos.second -= joystick_movement.second; + spot_pos.first += joystick_move(0); + spot_pos.second += joystick_move(1); + spot_pos.first -= joystick_move(2); + spot_pos.second -= joystick_move(3); if (bool(joystick_read[0]) || bool(joystick_read[1]) || bool(joystick_read[2]) || bool(joystick_read[3])){ @@ -157,7 +178,6 @@ int main(const int argc, char const* const* const argv) { has_changed = true; /// server notification - std::chrono::time_point now = std::chrono::system_clock::now(); if(has_changed && (now - last_send > server_ratelimit)){ postData.clear(); std::format_to(std::back_inserter(postData), "{{"