mirror of
https://github.com/speedypotato/Pico-Game-Controller.git
synced 2026-10-09 15:10:12 +03:00
Merge pull request #22 from speedypotato/bugfix/rollover_cleanup_joy
Removed unnecessary encoder rollover logic
This commit is contained in:
Binary file not shown.
@@ -1,13 +1,12 @@
|
||||
#ifndef CONTROLLER_CONFIG_H
|
||||
#define CONTROLLER_CONFIG_H
|
||||
|
||||
#define SW_GPIO_SIZE 11 // Number of switches
|
||||
#define LED_GPIO_SIZE 10 // Number of switches
|
||||
#define ENC_GPIO_SIZE 2 // Number of encoders
|
||||
#define ENC_PPR 600 // Encoder PPR
|
||||
#define ENC_DEBOUNCE true // Encoder Debouncing
|
||||
#define ENC_PULSE (ENC_PPR * 4) // 4 pulses per PPR
|
||||
#define ENC_ROLLOVER (ENC_PULSE * 2) // Delta Rollover threshold
|
||||
#define SW_GPIO_SIZE 11 // Number of switches
|
||||
#define LED_GPIO_SIZE 10 // Number of switch LEDs
|
||||
#define ENC_GPIO_SIZE 2 // Number of encoders
|
||||
#define ENC_PPR 600 // Encoder PPR
|
||||
#define ENC_DEBOUNCE true // Encoder Debouncing
|
||||
#define ENC_PULSE (ENC_PPR * 4) // 4 pulses per PPR
|
||||
#define REACTIVE_TIMEOUT_MAX 500000 // Cycles before HID falls back to reactive
|
||||
#define WS2812B_LED_SIZE 10 // Number of WS2812B LEDs
|
||||
#define WS2812B_LED_ZONES 2 // Number of WS2812B LED Zones
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "bsp/board.h"
|
||||
#include "controller_config.h"
|
||||
#include "encoders.pio.h"
|
||||
#include "hardware/clocks.h"
|
||||
#include "hardware/dma.h"
|
||||
@@ -19,7 +20,6 @@
|
||||
#include "tusb.h"
|
||||
#include "usb_descriptors.h"
|
||||
#include "ws2812.pio.h"
|
||||
#include "controller_config.h"
|
||||
|
||||
PIO pio, pio_1;
|
||||
uint32_t enc_val[ENC_GPIO_SIZE];
|
||||
@@ -153,35 +153,16 @@ void joy_mode() {
|
||||
|
||||
// find the delta between previous and current enc_val
|
||||
for (int i = 0; i < ENC_GPIO_SIZE; i++) {
|
||||
int delta;
|
||||
int changeType; // -1 for negative 1 for positive
|
||||
if (enc_val[i] > prev_enc_val[i]) { // if the new value is bigger its
|
||||
// a positive change
|
||||
delta = enc_val[i] - prev_enc_val[i];
|
||||
changeType = 1;
|
||||
} else { // otherwise its a negative change
|
||||
delta = prev_enc_val[i] - enc_val[i];
|
||||
changeType = -1;
|
||||
}
|
||||
// Overflow / Underflow
|
||||
if (delta > ENC_ROLLOVER) {
|
||||
// Reverse the change type due to overflow / underflow
|
||||
changeType *= -1;
|
||||
delta = UINT32_MAX - delta + 1; // this should give us how much we
|
||||
// overflowed / underflowed by
|
||||
}
|
||||
|
||||
cur_enc_val[i] =
|
||||
cur_enc_val[i] + ((ENC_REV[i] ? 1 : -1) * delta * changeType);
|
||||
while (cur_enc_val[i] < 0) {
|
||||
cur_enc_val[i] = ENC_PULSE - cur_enc_val[i];
|
||||
}
|
||||
cur_enc_val[i] +=
|
||||
((ENC_REV[i] ? 1 : -1) * (enc_val[i] - prev_enc_val[i]));
|
||||
while (cur_enc_val[i] < 0) cur_enc_val[i] = ENC_PULSE + cur_enc_val[i];
|
||||
cur_enc_val[i] %= ENC_PULSE;
|
||||
|
||||
prev_enc_val[i] = enc_val[i];
|
||||
}
|
||||
|
||||
report.joy0 = ((double)cur_enc_val[0] / ENC_PULSE) * 256;
|
||||
report.joy1 = ((double)cur_enc_val[1] / ENC_PULSE) * 256;
|
||||
report.joy0 = ((double)cur_enc_val[0] / ENC_PULSE) * (UINT8_MAX + 1);
|
||||
report.joy1 = ((double)cur_enc_val[1] / ENC_PULSE) * (UINT8_MAX + 1);
|
||||
|
||||
tud_hid_n_report(0x00, REPORT_ID_JOYSTICK, &report, sizeof(report));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user