From b030c45705685063aa6df3c8558076a57e1faae5 Mon Sep 17 00:00:00 2001 From: achol Date: Fri, 7 Dec 2018 00:27:49 +0100 Subject: [PATCH] Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes --- docs/feature_joystick.md | 96 ++++++++++++++++---- drivers/avr/analog.c | 24 +++++ quantum/joystick.c | 6 +- quantum/joystick.h | 9 +- quantum/process_keycode/process_joystick.c | 101 ++++++++++++++++++--- quantum/process_keycode/process_joystick.h | 3 +- tmk_core/common/report.h | 3 +- tmk_core/protocol/vusb/vusb.c | 19 +++- 8 files changed, 223 insertions(+), 38 deletions(-) diff --git a/docs/feature_joystick.md b/docs/feature_joystick.md index 43a4f22835..1d1d08ed04 100644 --- a/docs/feature_joystick.md +++ b/docs/feature_joystick.md @@ -8,6 +8,21 @@ This is enabled by adding the following to `rules.mk` JOYSTICK_ENABLE = yes ``` +The joystick feature provides two services : + * reading an analog input device + * sending gamepad HID reports + +Both services can be used without the other, depending on wether you just want to read a device but not send gamepad reports (for volume control for instance) +or send gamepad reports based on values computed by the keyboard. + +### Analog circuit + +An analog device such as a potentiometer found on a gamepad's analog axes is based on a [voltage divider](https://en.wikipedia.org/wiki/Voltage_divider). +It is composed of three connectors linked to the ground, the power input and power output (usually the middle one). The power output holds the voltage that varies based on the position of the cursor, +which value will be read using your MCU's [ADC](https://en.wikipedia.org/wiki/Analog-to-digital_converter). +Depending on what pins are already used by your keyboard's matrix, the rest of the circuit can get a little bit more complicated, +feeding the power input and ground connection through pins and using diodes to avoid bad interactions with the matrix scanning procedures. + ### Configuring the joystick The default joystick has 2 axes and and 8 buttons. This can be changed from the config.h file : @@ -19,54 +34,101 @@ The default joystick has 2 axes and and 8 buttons. This can be changed from the #define JOYSTICK_AXES_COUNT 3 ``` -When defining axes for your joystick, you have to provide a definition array for it. You can do this from your keymap.c file. -A joystick will either be read from an input pin that allows the use of an ADC, or can be virtual, so that its value is provided by your code. -You have to define an array of type ''joystick_config_t'' and of proper size, in the following way : +When defining axes for your joystick, you have to provide a definition array. You can do this from your keymap.c file. +A joystick will either be read from an input pin that allows the use of the ADC, or can be virtual, so that its value is provided by your code. +You have to define an array of type ''joystick_config_t'' and of proper size. + +There are three ways for your circuit to work with the ADC, that relies on the use of 1, 2 or 3 pins of the MCU: + * 1 pin : your analog device is directly connected to your device Ground and Vcc. The only pin used is the ADC pin of your choice. + * 2 pins : your analog device is powered through a pin that allows toggling it on or off. The other pin is used to read the input value through the ADC + * 3 pins : both the power input and ground are connected to pins that must be set to a proper state before reading and restored afterwards. + +The configuration of each axis is performed using one of four macros: + * JOYSTICK_AXIS_VIRTUAL : no ADC reading must be performed, that value will be provided by keyboard/keymap-level code + * JOYSTICK_AXIS_IN (INPUT_PIN, LOW, REST, HIGH) : a voltage will be read on the provided pin, which must be an ADC-capable pin. + * JOYSTICK_AXIS_IN_OUT (INPUT_PIN, OUTPUT_PIN, LOW, REST, HIGH) : the provided OUTPUT_PIN will be set high before INPUT_PIN is read. + * JOYSTICK_AXIS_IN_OUT_GROUND(INPUT_PIN, OUTPUT_PIN, GROUND_PIN, LOW, REST, HIGH) : the OUTPUT_PIN will be set high and GROUND_PIN will be set low before reading from INPUT_PIN + +In any case where an ADC reading takes place (when INPUT_PIN is provided), additional LOW, REST and HIGH parameters are used. +They implement the calibration of the analog device by defining the range of read values that will be mapped to the lowest, resting position and highest possible value for the axis (-127 to 127). +In practice, you have to provide the lowest/highest raw adc reading, and the raw reading at resting position, when no deflection is applied. You can provide inverted LOW and HIGH to invert the axis. + +For instance, an axes configuration can be defined in the following way : ``` //joystick config joystick_config_t joystick_axes[JOYSTICK_AXES_COUNT] = { - [0] = {A1, D7, 65280, 65472}, - [1] = {JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, 65280, 65472} + [0] = JOYSTICK_AXIS_IN_OUT_GROUND(A4, B0, A7, 900, 575, 285) +, [1] = JOYSTICK_AXIS_VIRTUAL }; ``` -In this example, the first axis will be read from the D7 pin while A1 is set high, using an analogRead, whereas the second axis will not be read. -If you connected the your analog component to Vcc instead of an output pin, you can set the output setting to JS_VIRTUAL_AXIS. +When the ADC reads 900 or higher, the returned axis value will be -127, whereas it will be 127 when the ADC reads 285 or lower. Zero is returned when 575 is read. + +In this example, the first axis will be read from the A4 pin while B0 is set high and A7 is set low, using an analogRead, whereas the second axis will not be read. + In order to give a value to the second axis, you can do so in any customizable entry point of quantum : as an action, in process_record_user or in matrix_scan_user, or even in joystick_task(void) which is called even when no key has been pressed. -You assign a value by writing to joystick_status.axes[axis_index] a signed 8bit value (-127 to 127). Then it is necessary to assign the flag JS_UPDATED to joystick_status.status in order for the change to be taken into account. +You assign a value by writing to joystick_status.axes[axis_index] a signed 8bit value (ranging from -127 to 127). Then it is necessary to assign the flag JS_UPDATED to joystick_status.status in order for an updated HID report to be sent. The following example writes two axes based on keypad presses, with KP_5 as a precision modifier : ``` #ifdef JOYSTICK_ENABLE -static bool precision_on; +static uint8_t precision_val = 70; +static uint8_t axesFlags = 0; +enum Axes{ + Precision = 1, + Axis1High = 2, + Axis1Low = 4, + Axis2High = 8, + Axis2Low = 16 +}; #endif bool process_record_user(uint16_t keycode, keyrecord_t *record) { switch(keycode){ #ifdef JOYSTICK_ENABLE // virtual joystick + #if JOYSTICK_AXES_COUNT>1 case KC_P8: - joystick_status.axes[1] -= (record->event.pressed ? 1 : -1) * (precision_on ?70 : 127); + if (record->event.pressed){ + axesFlags |= Axis2Low; + } else { + axesFlags &= ~Axis2Low; + } joystick_status.status |= JS_UPDATED; break; case KC_P2: - joystick_status.axes[1] += (record->event.pressed ? 1 : -1) * (precision_on ?70 : 127); + if (record->event.pressed){ + axesFlags |= Axis2High; + } else { + axesFlags &= ~Axis2High; + } joystick_status.status |= JS_UPDATED; break; + #endif case KC_P4: - joystick_status.axes[0] -= (record->event.pressed ? 1 : -1) * (precision_on ?70 : 127); + if (record->event.pressed){ + axesFlags |= Axis1Low; + } else { + axesFlags &= ~Axis1Low; + } joystick_status.status |= JS_UPDATED; break; case KC_P6: - joystick_status.axes[0] += (record->event.pressed ? 1 : -1) * (precision_on ?70 : 127); + if (record->event.pressed){ + axesFlags |= Axis1High; + } else { + axesFlags &= ~Axis1High; + } joystick_status.status |= JS_UPDATED; break; case KC_P5: - precision_on = record->event.pressed; - joystick_status.axes[0] *= record->event.pressed ? 70/127.f : 127/70.f; - joystick_status.axes[1] *= record->event.pressed ? 70/127.f : 127/70.f; + if (record->event.pressed){ + axesFlags |= Precision; + } else { + axesFlags &= ~Precision; + } joystick_status.status |= JS_UPDATED; break; #endif @@ -81,5 +143,3 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { Joystick buttons are normal quantum keycode, defined as JS_BUTTON0 to JS_BUTTON_MAX, which depends on the number of buttons you have configured. To trigger a joystick button, just add the corresponding keycode to your keymap. - - diff --git a/drivers/avr/analog.c b/drivers/avr/analog.c index abe478b712..e79e49b04a 100644 --- a/drivers/avr/analog.c +++ b/drivers/avr/analog.c @@ -46,6 +46,7 @@ int16_t analogRead(uint8_t pin) { #endif } +<<<<<<< HEAD int16_t analogReadPin(pin_t pin) { return adc_read(pinToMux(pin)); } uint8_t pinToMux(pin_t pin) { @@ -94,6 +95,29 @@ uint8_t pinToMux(pin_t pin) { case C5: return _BV(MUX2) | _BV(MUX0); // ADC5 // ADC7:6 not present in DIP package and not shared by GPIO pins default: return _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // 0V +======= +// Mux input +int16_t adc_read(uint8_t mux) +{ +#if defined(__AVR_AT90USB162__) + return 0; +#else + uint16_t res = 0; + + ADCSRA = (1<>>>>>> 5939a4430... Add save and restore of each pin used in reading joystick (AVR). #endif // clang-format on } diff --git a/quantum/joystick.c b/quantum/joystick.c index e6906913db..ebad4ec3df 100644 --- a/quantum/joystick.c +++ b/quantum/joystick.c @@ -2,7 +2,11 @@ joystick_t joystick_status = { .buttons = {0}, - .axes = {0}, + .axes = { +#if JOYSTICK_AXES_COUNT>0 + 0 +#endif + }, .status = 0 }; diff --git a/quantum/joystick.h b/quantum/joystick.h index 6ca9ab8878..a1d2c7c831 100644 --- a/quantum/joystick.h +++ b/quantum/joystick.h @@ -16,12 +16,19 @@ // #define JS_VIRTUAL_AXIS 0xFF +#define JOYSTICK_AXIS_VIRTUAL {JS_VIRTUAL_AXIS,JS_VIRTUAL_AXIS,JS_VIRTUAL_AXIS,0 ,1023} +#define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) {JS_VIRTUAL_AXIS,INPUT_PIN ,JS_VIRTUAL_AXIS,LOW,REST,HIGH} +#define JOYSTICK_AXIS_IN_OUT(INPUT_PIN, OUTPUT_PIN, LOW, REST, HIGH) {OUTPUT_PIN ,INPUT_PIN ,JS_VIRTUAL_AXIS,LOW,REST,HIGH} +#define JOYSTICK_AXIS_IN_OUT_GROUND(INPUT_PIN, OUTPUT_PIN, GROUND_PIN, LOW, REST, HIGH) {OUTPUT_PIN ,INPUT_PIN ,GROUND_PIN ,LOW,REST,HIGH} + typedef struct { uint8_t output_pin; uint8_t input_pin; + uint8_t ground_pin; //the AVR ADC offers 10 bit precision, with significant bits on the higher part uint16_t min_digit; + uint16_t mid_digit; uint16_t max_digit; } joystick_config_t; @@ -36,7 +43,7 @@ typedef struct { uint8_t buttons[JOYSTICK_BUTTON_COUNT/8+1]; - int8_t axes[JOYSTICK_AXES_COUNT]; + int16_t axes[JOYSTICK_AXES_COUNT]; uint8_t status:2; } joystick_t; diff --git a/quantum/process_keycode/process_joystick.c b/quantum/process_keycode/process_joystick.c index f90fd23937..11cfd5e1d7 100644 --- a/quantum/process_keycode/process_joystick.c +++ b/quantum/process_keycode/process_joystick.c @@ -24,12 +24,14 @@ bool process_joystick(uint16_t keycode, keyrecord_t *record){ __attribute__ ((weak)) void joystick_task(void){ - if (process_joystick_analog() && (joystick_status.status & JS_UPDATED)){ + if (process_joystick_analogread() && (joystick_status.status & JS_UPDATED)){ send_joystick_packet(&joystick_status); joystick_status.status &= ~JS_UPDATED; } } + + bool process_joystick_buttons(uint16_t keycode, keyrecord_t *record){ if (keycode < JS_BUTTON0 || keycode > JS_BUTTON_MAX){ @@ -47,33 +49,110 @@ bool process_joystick_buttons(uint16_t keycode, keyrecord_t *record){ return true; } +uint8_t savePinState(uint8_t pin){ +#ifdef __AVR__ + uint8_t pinNumber = pin & 0xF; + return ((PIN_ADDRESS(pin, 2) >> pinNumber) & 0x1) << 1 + | ((PIN_ADDRESS(pin, 1) >> pinNumber) & 0x1) ; +#else + return 0; +#endif +} + +void restorePinState(uint8_t pin, uint8_t restoreState){ +#ifdef __AVR__ + uint8_t pinNumber = pin & 0xF; + PIN_ADDRESS(pin, 2) = (PIN_ADDRESS(pin, 2) & ~_BV(pinNumber)) | (((restoreState >> 1) & 0x1) << pinNumber); + PIN_ADDRESS(pin, 1) = (PIN_ADDRESS(pin, 1) & ~_BV(pinNumber)) | ((restoreState & 0x1) << pinNumber); +#else + return; +#endif +} + __attribute__ ((weak)) -bool process_joystick_analog(){ +bool process_joystick_analogread(){ + return process_joystick_analogread_quantum(); +} + +bool process_joystick_analogread_quantum(){ + #if JOYSTICK_AXES_COUNT > 0 for (int axis_index=0 ; axis_index 0){ + //the value is in the higher range + range = joystick_axes[axis_index].max_digit; + ranged_val = 127*fminf(1.f, (axis_val - (float)(ref)) /(range - (float)ref)); + } + + if (ranged_val!=joystick_status.axes[axis_index]){ + joystick_status.axes[axis_index] = ranged_val; joystick_status.status |= JS_UPDATED; } - writePinLow(joystick_axes[axis_index].output_pin); + //restore output, ground and input status + if (joystick_axes[axis_index].output_pin!=JS_VIRTUAL_AXIS) { + restorePinState(joystick_axes[axis_index].output_pin, outputSavedState); + } + if (joystick_axes[axis_index].ground_pin!=JS_VIRTUAL_AXIS) { + restorePinState(joystick_axes[axis_index].ground_pin, groundSavedState); + } + + restorePinState(joystick_axes[axis_index].input_pin, inputSavedState); + } + #endif return true; } diff --git a/quantum/process_keycode/process_joystick.h b/quantum/process_keycode/process_joystick.h index f19a9221c2..28562d2595 100644 --- a/quantum/process_keycode/process_joystick.h +++ b/quantum/process_keycode/process_joystick.h @@ -8,6 +8,7 @@ bool process_joystick(uint16_t keycode, keyrecord_t *record); void joystick_task(void); -bool process_joystick_analog(void); +bool process_joystick_analogread(void); +bool process_joystick_analogread_quantum(void); #endif //PROCESS_JOYSTICK_H \ No newline at end of file diff --git a/tmk_core/common/report.h b/tmk_core/common/report.h index 61ef6ea66c..82f150f26a 100644 --- a/tmk_core/common/report.h +++ b/tmk_core/common/report.h @@ -29,7 +29,8 @@ enum hid_report_ids { REPORT_ID_MOUSE, REPORT_ID_SYSTEM, REPORT_ID_CONSUMER, - REPORT_ID_NKRO + REPORT_ID_NKRO, + REPORT_ID_JOYSTICK }; /* Mouse buttons */ diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c index 61d1ba53e7..571ca33890 100644 --- a/tmk_core/protocol/vusb/vusb.c +++ b/tmk_core/protocol/vusb/vusb.c @@ -157,8 +157,7 @@ typedef struct { void send_joystick_packet(joystick_t* status) { vusb_joystick_report_t r = { - .report_id = 0x4, - + .report_id = REPORT_ID_JOYSTICK, #if JOYSTICK_AXES_COUNT>0 .axes = { status->axes[0] @@ -401,9 +400,10 @@ const PROGMEM uchar mouse_extra_hid_report[] = { 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x04, // USAGE (Joystick) 0xa1, 0x01, // COLLECTION (Application) - 0x85, 0x4, // REPORT_ID (4) + 0x85, REPORT_ID_JOYSTICK, // REPORT_ID (6) 0x09, 0x01, // USAGE (Pointer) 0xa1, 0x00, // COLLECTION (Physical) +#if JOYSTICK_AXES_COUNT > 0 #if JOYSTICK_AXES_COUNT >= 1 0x09, 0x30, // USAGE (X) #endif @@ -427,7 +427,8 @@ const PROGMEM uchar mouse_extra_hid_report[] = { 0x75, 0x08, // REPORT_SIZE (8) 0x95, JOYSTICK_AXES_COUNT, // REPORT_COUNT (JOYSTICK_AXES_COUNT) 0x81, 0x02, // INPUT (Data,Var,Abs) - 0xc0, // END_COLLECTION +#endif +#if JOYSTICK_BUTTON_COUNT> 0 0x05, 0x09, // USAGE_PAGE (Button) 0x19, 0x01, // USAGE_MINIMUM (Button 1) 0x29, JOYSTICK_BUTTON_COUNT, // USAGE_MAXIMUM @@ -436,8 +437,16 @@ const PROGMEM uchar mouse_extra_hid_report[] = { 0x75, 0x01, // REPORT_SIZE (1) 0x95, JOYSTICK_BUTTON_COUNT, // REPORT_COUNT 0x81, 0x02, // INPUT (Data,Var,Abs) + //fill up report to get it byte-aligned +#if (JOYSTICK_BUTTON_COUNT % 8) != 0 + 0x75, 0x01, // REPORT_SIZE (1) + 0x95, 8 - (JOYSTICK_BUTTON_COUNT % 8), // REPORT_COUNT + 0x81, 0x01, // INPUT (Data,Var,Abs) + #endif +#endif + 0xc0, // END_COLLECTION 0xc0 // END_COLLECTION -#endif //GAMEPAD_ENABLE +#endif //JOYSTICK_ENABLE }; #endif