feat(agb02/gpio): auto-repeat on joystick

This commit is contained in:
catvayor 2024-10-14 17:44:22 +02:00
parent 6733ec2b77
commit 071cf7741f
Signed by: lbailly
GPG key ID: CE3E645251AC63F3

View file

@ -10,10 +10,11 @@ using namespace std::literals::chrono_literals;
constexpr std::chrono::microseconds debounce = 40ms; constexpr std::chrono::microseconds debounce = 40ms;
constexpr std::chrono::microseconds poll_period = 5ms; 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 server_ratelimit = 50ms;
constexpr std::chrono::microseconds retry_timeout = 500ms; constexpr std::chrono::microseconds retry_timeout = 500ms;
constexpr std::pair<double, double> joystick_movement(.3, .3); constexpr double joystick_movement = 0.2;
const gpiod::line::offsets drive_down = { 21, 13, 6 }; 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 /// /// internal state and buffers ///
std::chrono::time_point now = std::chrono::system_clock::now();
gpiod::line::values joystick_read(4); gpiod::line::values joystick_read(4);
gpiod::line::values joystick_last_read(4);
line_reader.get_values(joystick, joystick_read);
std::vector<std::chrono::time_point<std::chrono::system_clock>> rising_point = { now, now, now, now };
std::pair<double, double> spot_pos(0.0, 0.0); //TODO: init from server std::pair<double, double> 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_pos = 0; //TODO: init from server
uint8_t decoder_realpos = read_decoder_realpos(line_reader); 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 black_pressed = false;
bool has_changed = true; 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; std::string postData;
for(;;){ for(;;){
std::this_thread::sleep_for(poll_period); std::this_thread::sleep_for(poll_period);
now = std::chrono::system_clock::now();
/// joystick /// /// joystick ///
std::swap(joystick_read, joystick_last_read);
line_reader.get_values(joystick, joystick_read); line_reader.get_values(joystick, joystick_read);
if(bool(joystick_read[0])) spot_pos.first += joystick_movement.first; spot_pos.first += joystick_move(0);
if(bool(joystick_read[1])) spot_pos.second += joystick_movement.second; spot_pos.second += joystick_move(1);
if(bool(joystick_read[2])) spot_pos.first -= joystick_movement.first; spot_pos.first -= joystick_move(2);
if(bool(joystick_read[3])) spot_pos.second -= joystick_movement.second; spot_pos.second -= joystick_move(3);
if (bool(joystick_read[0]) || bool(joystick_read[1]) if (bool(joystick_read[0]) || bool(joystick_read[1])
|| bool(joystick_read[2]) || bool(joystick_read[3])){ || 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; has_changed = true;
/// server notification /// server notification
std::chrono::time_point now = std::chrono::system_clock::now();
if(has_changed && (now - last_send > server_ratelimit)){ if(has_changed && (now - last_send > server_ratelimit)){
postData.clear(); postData.clear();
std::format_to(std::back_inserter(postData), "{{" std::format_to(std::back_inserter(postData), "{{"