Skip to content

Commit

Permalink
Merge pull request #10 from veroxzik/fix/encoder
Browse files Browse the repository at this point in the history
Encoder fix + PID fix
  • Loading branch information
Gladuin authored Dec 25, 2022
2 parents 289a480 + 70a729f commit 84cba20
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config-tool/config-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def get_filtered_devices():
filter = hid.HidDeviceFilter(vendor_id = 0x0001, product_id = 0x0001)
# might be in konami spoof mode
if len(filter.get_devices()) == 0:
filter = hid.HidDeviceFilter(vendor_id = 0x1ccf, product_id = 0x8086)
filter = hid.HidDeviceFilter(vendor_id = 0x1ccf, product_id = 0x8048)
return filter.get_devices()

def send(data):
Expand Down
2 changes: 1 addition & 1 deletion iidx-controller/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
static const uint8_t encoder_pin1 = ENCODER_PIN_B;
#else
#define VID 0x1ccf
#define PID 0x8086
#define PID 0x8048
#define MF_NAME L"Konami Amusement"
#define PROD_NAME L"beatmania IIDX controller premium model"
static const uint8_t encoder_pin0 = ENCODER_PIN_B;
Expand Down
3 changes: 2 additions & 1 deletion iidx-controller/src/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
} configuration_struct;

// Macros
#define ADJUSTED_PPR ((int)((float)ENCODER_PPR * ((float)255 / (float)config->increments_per_full_turn)))
#define PPR_SCALE (255.0f / (float)ENCODER_PPR)
#define INCREMENT_SCALE ((float)config->increments_per_full_turn / (float)ENCODER_PPR)
#define NUM_BUTTONS (sizeof(button_pins) / sizeof(uint8_t))
#define NUM_LEDS (sizeof(led_pins) / sizeof(uint8_t))

Expand Down
4 changes: 2 additions & 2 deletions iidx-controller/src/HID/Descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
uint16_t size = NO_DESCRIPTOR;

// adjust logical maximum of encoder
joystick_report[41] = ADJUSTED_PPR & 0xFF;
joystick_report[42] = ADJUSTED_PPR >> 8;
joystick_report[41] = 0xFF;
joystick_report[42] = 0;

// adjust min max of mouse report
mouse_report[17] = (-ENCODER_PPR / 2) & 0xFF;
Expand Down
10 changes: 7 additions & 3 deletions iidx-controller/src/HID/IIDXHID.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,15 @@ void create_joystick_report(input_data_struct_joystick* input_data) {
}

if (config->tt_mode == 0) {
input_data->turntable_position = get_encoder_state();
if (config->increments_per_full_turn == 255) {
input_data->turntable_position = get_encoder_state();
} else {
input_data->turntable_position = get_encoder_virtual_state();
}
} else if ((config->tt_mode == 2) || (config->tt_mode == 3)) {
switch (get_digital_encoder_state()) {
case 1:
if (config->tt_mode == 2) input_data->turntable_position = ADJUSTED_PPR;
if (config->tt_mode == 2) input_data->turntable_position = 0xFF;
if (config->tt_mode == 3) input_data->button_status = input_data->button_status | 0b0000100000000000;
break;

Expand All @@ -215,7 +219,7 @@ void create_joystick_report(input_data_struct_joystick* input_data) {
break;

default:
if (config->tt_mode == 2) input_data->turntable_position = ADJUSTED_PPR / 2;
if (config->tt_mode == 2) input_data->turntable_position = 0xFF / 2;
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions iidx-controller/src/IO/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
#define INTERRUPT_PERIOD 12
#endif

#define LIMIT(min,val,max) if (val >= max) { val -= max; } else if (val < min) { val += max; }

volatile byte encoder_state_volatile;
volatile int16_t encoder_value_volatile = 0;
volatile int16_t encoder_value_volatile = 0; // Count from 0 to (ENCODER_PPR - 1)
volatile int16_t encoder_virtual_volatile = 0; // Count from 0 to 255
volatile int16_t encoder_virtual_raw_volatile = 0;
volatile int16_t encoder_virtual_old_volatile = 0;

int digital_rotation = 0;

Expand Down Expand Up @@ -81,13 +85,23 @@ uint8_t get_digital_encoder_state() {
}

uint16_t get_encoder_state() {
if (encoder_value_volatile >= ADJUSTED_PPR) {
encoder_value_volatile = 0;
} else if (encoder_value_volatile < 0) {
encoder_value_volatile = ADJUSTED_PPR - 1;
}
LIMIT(0, encoder_value_volatile, ENCODER_PPR)

return (uint16_t)((float)encoder_value_volatile * PPR_SCALE);
}

return encoder_value_volatile;
uint16_t get_encoder_virtual_state() {
encoder_virtual_raw_volatile = (int16_t)((float)encoder_value_volatile * INCREMENT_SCALE);
if (encoder_virtual_raw_volatile < encoder_virtual_old_volatile) {
encoder_virtual_volatile--;
} else if (encoder_virtual_raw_volatile > encoder_virtual_old_volatile) {
encoder_virtual_volatile++;
}
LIMIT(0, encoder_value_volatile, ENCODER_PPR)
encoder_virtual_old_volatile = encoder_virtual_raw_volatile;
LIMIT(0, encoder_virtual_raw_volatile, 255)
LIMIT(0, encoder_virtual_volatile, 255)
return encoder_virtual_volatile;
}

void compute_encoder() {
Expand Down
1 change: 1 addition & 0 deletions iidx-controller/src/IO/Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
EXTERNC void initialise_encoder();
EXTERNC uint8_t get_digital_encoder_state();
EXTERNC uint16_t get_encoder_state();
EXTERNC uint16_t get_encoder_virtual_state();

#undef EXTERNC

Expand Down

0 comments on commit 84cba20

Please sign in to comment.