Browse Source

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
pull/4226/head
achol 5 years ago
committed by a-chol
parent
commit
b030c45705
8 changed files with 223 additions and 38 deletions
  1. +78
    -18
      docs/feature_joystick.md
  2. +24
    -0
      drivers/avr/analog.c
  3. +5
    -1
      quantum/joystick.c
  4. +8
    -1
      quantum/joystick.h
  5. +90
    -11
      quantum/process_keycode/process_joystick.c
  6. +2
    -1
      quantum/process_keycode/process_joystick.h
  7. +2
    -1
      tmk_core/common/report.h
  8. +14
    -5
      tmk_core/protocol/vusb/vusb.c

+ 78
- 18
docs/feature_joystick.md View File

@ -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.

+ 24
- 0
drivers/avr/analog.c View File

@ -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<<ADEN) | ADC_PRESCALER; // enable ADC
#ifndef __AVR_ATmega32A__
ADCSRB = (1<<ADHSM) | (mux & 0x20); // high speed mode
#endif
ADMUX = aref | (mux & 0x1F); // configure mux input
ADCSRA |= (1<<ADSC); // start the conversion
while (ADCSRA & (1<<ADSC)) ; // wait for result
res = ADCL; // must read LSB first
res |= (ADCH << 8) | res;
ADCSRA &= ~(1<<ADEN);//turn off the ADC
return res;
>>>>>>> 5939a4430... Add save and restore of each pin used in reading joystick (AVR).
#endif
// clang-format on
}


+ 5
- 1
quantum/joystick.c View File

@ -2,7 +2,11 @@
joystick_t joystick_status = {
.buttons = {0},
.axes = {0},
.axes = {
#if JOYSTICK_AXES_COUNT>0
0
#endif
},
.status = 0
};


+ 8
- 1
quantum/joystick.h View File

@ -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;


+ 90
- 11
quantum/process_keycode/process_joystick.c View File

@ -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<JOYSTICK_AXES_COUNT ; ++axis_index){
if (joystick_axes[axis_index].output_pin==JS_VIRTUAL_AXIS || joystick_axes[axis_index].input_pin==JS_VIRTUAL_AXIS){
if (joystick_axes[axis_index].input_pin==JS_VIRTUAL_AXIS){
continue;
}
setPinOutput(joystick_axes[axis_index].output_pin);
writePinHigh(joystick_axes[axis_index].output_pin);
//save previous input pin status as well
uint8_t inputSavedState = savePinState(joystick_axes[axis_index].input_pin);
//disable pull-up resistance
setPinInput(joystick_axes[axis_index].input_pin);
//disable pull-up resistor
writePinLow(joystick_axes[axis_index].input_pin);
//if pin was a pull-up input, we need to uncharge it by turning it low
// before making it a low input
setPinOutput(joystick_axes[axis_index].input_pin);
wait_us(10);
//save and apply output pin status
uint8_t outputSavedState = 0;
if (joystick_axes[axis_index].output_pin!=JS_VIRTUAL_AXIS) {
//save previous output pin status
outputSavedState = savePinState(joystick_axes[axis_index].output_pin);
setPinOutput(joystick_axes[axis_index].output_pin);
writePinHigh(joystick_axes[axis_index].output_pin);
}
uint8_t groundSavedState = 0;
if (joystick_axes[axis_index].ground_pin!=JS_VIRTUAL_AXIS) {
//save previous output pin status
groundSavedState = savePinState(joystick_axes[axis_index].ground_pin);
setPinOutput(joystick_axes[axis_index].ground_pin);
writePinLow(joystick_axes[axis_index].ground_pin);
}
wait_us(10);
setPinInput(joystick_axes[axis_index].input_pin);
wait_us(10);
#ifdef __AVR__
int16_t axis_val = analogReadPin(joystick_axes[axis_index].input_pin);
#else
int16_t axis_val = 0;
//default to resting position
int16_t axis_val = joystick_axes[axis_index].mid_digit;
#endif
if (axis_val!=joystick_status.axes[axis_index]){
joystick_status.axes[axis_index] = axis_val;
//test the converted value against the lower range
uint16_t ref = joystick_axes[axis_index].mid_digit;
uint16_t range = joystick_axes[axis_index].min_digit;
int16_t ranged_val = -127*fminf(1.f, (axis_val - (float)(ref)) /(range - (float)ref));
if (ranged_val > 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;
}

+ 2
- 1
quantum/process_keycode/process_joystick.h View File

@ -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

+ 2
- 1
tmk_core/common/report.h View File

@ -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 */


+ 14
- 5
tmk_core/protocol/vusb/vusb.c View File

@ -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


Loading…
Cancel
Save