Skip to content

Commit

Permalink
partially implement veroxzik's #6 pr changes
Browse files Browse the repository at this point in the history
left out the encoder logic part because it does not work with ADJUSTED_PPR
  • Loading branch information
Gladuin committed Aug 13, 2022
1 parent c4475ef commit 191d30e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
2 changes: 2 additions & 0 deletions iidx-controller/src/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

#ifdef __cplusplus
#define EXTERNC extern "C"
Expand Down
12 changes: 6 additions & 6 deletions iidx-controller/src/HID/Descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "Descriptors.h"


#define BUTTON_PADDING (8 - ((sizeof(button_pins) + 2) % 8))
#define LED_PADDING (8 - (sizeof(led_pins) % 8))
#define BUTTON_PADDING (8 - ((NUM_BUTTONS + 2) % 8))
#define LED_PADDING (8 - (NUM_LEDS % 8))

#if (defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS)))
#define HAS_MULTIPLE_DESCRIPTOR_ADDRESS_SPACES
Expand Down Expand Up @@ -59,11 +59,11 @@ USB_Descriptor_HIDReport_Datatype_t joystick_report[] = {
// Buttons
HID_RI_USAGE_PAGE(8, 9),
HID_RI_USAGE_MINIMUM(8, 1),
HID_RI_USAGE_MAXIMUM(8, (sizeof(button_pins) + 2)),
HID_RI_USAGE_MAXIMUM(8, (NUM_BUTTONS + 2)),
HID_RI_LOGICAL_MINIMUM(8, 0),
HID_RI_LOGICAL_MAXIMUM(8, 1),
HID_RI_REPORT_SIZE(8, 1),
HID_RI_REPORT_COUNT(8, (sizeof(button_pins) + 2)),
HID_RI_REPORT_COUNT(8, (NUM_BUTTONS + 2)),
HID_RI_UNIT_EXPONENT(8, 0),
HID_RI_UNIT(8, 0),
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
Expand Down Expand Up @@ -92,7 +92,7 @@ USB_Descriptor_HIDReport_Datatype_t joystick_report[] = {
HID_RI_LOGICAL_MINIMUM(8, 0),
HID_RI_LOGICAL_MAXIMUM(8, 1),
HID_RI_REPORT_SIZE(8, 1),
HID_RI_REPORT_COUNT(8, sizeof(led_pins)),
HID_RI_REPORT_COUNT(8, NUM_LEDS),
HID_RI_COLLECTION(8, 2),
HID_RI_USAGE(8, 11),
HID_RI_USAGE(8, 10),
Expand Down Expand Up @@ -145,7 +145,7 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM keyboard_report[] = {
HID_RI_USAGE_PAGE(8, 7),
HID_RI_USAGE_MINIMUM(8, 0),
HID_RI_USAGE_MAXIMUM(8, 101),
HID_RI_REPORT_COUNT(8, sizeof(button_pins) + 2), // amount of keys
HID_RI_REPORT_COUNT(8, NUM_BUTTONS + 2), // amount of keys
HID_RI_REPORT_SIZE(8, 8),
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),

Expand Down
8 changes: 4 additions & 4 deletions iidx-controller/src/HID/IIDXHID.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef struct {
typedef struct {
uint8_t modifier;
uint8_t reserved;
uint8_t keycode[sizeof(button_pins) + 2];
uint8_t keycode[NUM_BUTTONS + 2];
} input_data_struct_keyboard;

typedef struct {
Expand Down Expand Up @@ -226,19 +226,19 @@ void create_keyboard_report(input_data_struct_keyboard* input_data) {
if (config->controller_mode == 1) {
uint16_t button_status = get_button_state();

for (int i = 0; i < sizeof(button_pins); i++) {
for (int i = 0; i < NUM_BUTTONS; i++) {
if (button_status & ((uint16_t)1 << i)) input_data->keycode[i] = 0x04 + i;
}
}

if (config->tt_mode == 4) {
switch (get_digital_encoder_state()) {
case 1:
input_data->keycode[sizeof(button_pins)] = (0x03 + sizeof(button_pins)) + 1;
input_data->keycode[NUM_BUTTONS] = (0x03 + NUM_BUTTONS) + 1;
break;

case 2:
input_data->keycode[sizeof(button_pins) + 1] = (0x03 + sizeof(button_pins)) + 2;
input_data->keycode[NUM_BUTTONS + 1] = (0x03 + NUM_BUTTONS) + 2;
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions iidx-controller/src/IO/Buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
#include "../Configuration.h"


Bounce buttons[sizeof(button_pins)];
Bounce buttons[NUM_BUTTONS];
uint16_t button_status;

static configuration_struct *config;


void set_debounce_interval() {
for (int i = 0; i < sizeof(button_pins); i++) {
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i].interval(config->debounce_time);
}
}

void initialise_buttons() {
get_configuration(&config);

for (int i = 0; i < sizeof(button_pins); i++) {
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i] = Bounce();
buttons[i].attach(button_pins[i], INPUT_PULLUP);
}
Expand All @@ -31,7 +31,7 @@ void initialise_buttons() {
}

uint16_t get_button_state() {
for (int i = 0; i < sizeof(button_pins); i++) {
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i].update();

switch (buttons[i].read()) {
Expand Down
17 changes: 12 additions & 5 deletions iidx-controller/src/IO/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

#define AMOUNT 300

// Set to 133 for 0.5ms (2kHz) on a 16MHz processor: (16000000L / 64 / 2000 -> 133)
// Set to 50 for 0.2ms (5kHz) on a 16MHz processor: (16000000L / 64 / 5000 -> 50)
// Set to 25 for 0.1ms (10kHz) on a 16MHz processor: (16000000L / 64 / 10000 -> 25)
// Set to 12 for 0.05ms (20kHz) on a 16MHz processor: (16000000L / 64 / 20833 -> 12)
#if ENCODER_PPR < 360
#define INTERRUPT_PERIOD 25
#else // High PPR encoders have the possibility to switch faster than the timer
#define INTERRUPT_PERIOD 12
#endif


volatile byte encoder_state_volatile;
volatile int16_t encoder_value_volatile = 0;
Expand All @@ -27,11 +37,8 @@ void setup_timer_interrupt() {
// Set compare match register (write to the high bit first)
OCR3AH = 0;

// Set compare match register for particular frequency increments (cpu_clock_frequency / prescaler / desired_interrupt_frequency)
// OCR3AL = 133; // = (16000000) / 64 / 2000 -> 133 This is clock_frequency / prescaler / desired_frequency ( 2 KHz, 0.5ms)
// OCR3AL = 50; // = (16000000) / 64 / 5000 -> 50 This is clock_frequency / prescaler / desired_frequency ( 5 KHz, 0.2ms)
// OCR3AL = 25; // = (16000000) / 64 / 10000 -> 25 This is clock_frequency / prescaler / desired_frequency (10 kHz, 0.1ms)
OCR3AL = 25;
// Set compare match register for particular frequency increments
OCR3AL = INTERRUPT_PERIOD;

// Enable timer compare interrupt
TIMSK3 = (1 << OCIE3A);
Expand Down
8 changes: 4 additions & 4 deletions iidx-controller/src/IO/LEDs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
#include "LEDs.h"

void initialise_leds() {
for (int i = 0; i < sizeof(led_pins); i++) {
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(led_pins[i], OUTPUT);
digitalWrite(led_pins[i], HIGH);
}

delay(200);

for (int i = 0; i < sizeof(led_pins); i++) {
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(led_pins[i], LOW);
}
}

void write_leds(uint16_t led_status, bool flipped) {
if (flipped) {
led_status = led_status << (16 - sizeof(led_pins));
led_status = led_status << (16 - NUM_LEDS);

// thanks http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits
// flip first half of led_status
Expand All @@ -36,7 +36,7 @@ void write_leds(uint16_t led_status, bool flipped) {
led_status = (b << 8) | a;
}

for (int i = 0; i < sizeof(led_pins); i++) {
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(led_pins[i], ((led_status >> i) & 1));
}
}

0 comments on commit 191d30e

Please sign in to comment.