Compare commits
1 Commits
master
...
joystick_f
Author | SHA1 | Date | |
---|---|---|---|
|
1814a34de4 |
@ -830,15 +830,12 @@ ifeq ($(strip $(JOYSTICK_ENABLE)), yes)
|
|||||||
$(call CATASTROPHIC_ERROR,Invalid JOYSTICK_DRIVER,JOYSTICK_DRIVER="$(JOYSTICK_DRIVER)" is not a valid joystick driver)
|
$(call CATASTROPHIC_ERROR,Invalid JOYSTICK_DRIVER,JOYSTICK_DRIVER="$(JOYSTICK_DRIVER)" is not a valid joystick driver)
|
||||||
endif
|
endif
|
||||||
OPT_DEFS += -DJOYSTICK_ENABLE
|
OPT_DEFS += -DJOYSTICK_ENABLE
|
||||||
|
OPT_DEFS += -DJOYSTICK_$(strip $(shell echo $(JOYSTICK_DRIVER) | tr '[:lower:]' '[:upper:]'))
|
||||||
SRC += $(QUANTUM_DIR)/process_keycode/process_joystick.c
|
SRC += $(QUANTUM_DIR)/process_keycode/process_joystick.c
|
||||||
SRC += $(QUANTUM_DIR)/joystick.c
|
SRC += $(QUANTUM_DIR)/joystick.c
|
||||||
|
|
||||||
ifeq ($(strip $(JOYSTICK_DRIVER)), analog)
|
ifeq ($(strip $(JOYSTICK_DRIVER)), analog)
|
||||||
ANALOG_DRIVER_REQUIRED = yes
|
ANALOG_DRIVER_REQUIRED = yes
|
||||||
OPT_DEFS += -DANALOG_JOYSTICK_ENABLE
|
|
||||||
endif
|
|
||||||
ifeq ($(strip $(JOYSTICK_DRIVER)), digital)
|
|
||||||
OPT_DEFS += -DDIGITAL_JOYSTICK_ENABLE
|
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
@ -50,10 +50,6 @@ Axes can be configured using one of the following macros:
|
|||||||
|
|
||||||
* `JOYSTICK_AXIS_IN(input_pin, low, rest, high)`
|
* `JOYSTICK_AXIS_IN(input_pin, low, rest, high)`
|
||||||
The ADC samples the provided pin. `low`, `high` and `rest` correspond to the minimum, maximum, and resting (or centered) analog values of the axis, respectively.
|
The ADC samples the provided pin. `low`, `high` and `rest` correspond to the minimum, maximum, and resting (or centered) analog values of the axis, respectively.
|
||||||
* `JOYSTICK_AXIS_IN_OUT(input_pin, output_pin, low, rest, high)`
|
|
||||||
Same as `JOYSTICK_AXIS_IN()`, but the provided `output_pin` will be pulled high before `input_pin` is read.
|
|
||||||
* `JOYSTICK_AXIS_IN_OUT_GROUND(input_pin, output_pin, ground_pin, low, rest, high)`
|
|
||||||
Same as `JOYSTICK_AXIS_IN_OUT()`, but the provided `ground_pin` will be pulled low before reading from `input_pin`.
|
|
||||||
* `JOYSTICK_AXIS_VIRTUAL`
|
* `JOYSTICK_AXIS_VIRTUAL`
|
||||||
No ADC reading is performed. The value should be provided by user code.
|
No ADC reading is performed. The value should be provided by user code.
|
||||||
|
|
||||||
@ -160,12 +156,8 @@ Describes a single axis.
|
|||||||
|
|
||||||
#### Members :id=api-joystick-config-t-members
|
#### Members :id=api-joystick-config-t-members
|
||||||
|
|
||||||
- `pin_t output_pin`
|
|
||||||
A pin to set as output high when reading the analog value, or `JS_VIRTUAL_AXIS`.
|
|
||||||
- `pin_t input_pin`
|
- `pin_t input_pin`
|
||||||
The pin to read the analog value from, or `JS_VIRTUAL_AXIS`.
|
The pin to read the analog value from, or `JS_VIRTUAL_AXIS`.
|
||||||
- `pin_t ground_pin`
|
|
||||||
A pin to set as output low when reading the analog value, or `JS_VIRTUAL_AXIS`.
|
|
||||||
- `uint16_t min_digit`
|
- `uint16_t min_digit`
|
||||||
The minimum analog value.
|
The minimum analog value.
|
||||||
- `uint16_t mid_digit`
|
- `uint16_t mid_digit`
|
||||||
|
@ -15,10 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "joystick.h"
|
#include "joystick.h"
|
||||||
|
|
||||||
#include "analog.h"
|
|
||||||
#include "wait.h"
|
#include "wait.h"
|
||||||
|
|
||||||
|
#if defined(JOYSTICK_ANALOG)
|
||||||
|
# include "analog.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
joystick_t joystick_state = {
|
joystick_t joystick_state = {
|
||||||
.buttons = {0},
|
.buttons = {0},
|
||||||
.axes =
|
.axes =
|
||||||
@ -31,21 +33,47 @@ joystick_t joystick_state = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// array defining the reading of analog values for each axis
|
// array defining the reading of analog values for each axis
|
||||||
__attribute__((weak)) joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT] = {};
|
__attribute__((weak)) joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT] = {
|
||||||
|
#if JOYSTICK_AXIS_COUNT > 0
|
||||||
|
[0 ...(JOYSTICK_AXIS_COUNT - 1)] = JOYSTICK_AXIS_VIRTUAL
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
__attribute__((weak)) void joystick_task(void) {
|
__attribute__((weak)) void joystick_axis_init(uint8_t axis) {
|
||||||
joystick_read_axes();
|
if (axis >= JOYSTICK_AXIS_COUNT) return;
|
||||||
|
|
||||||
|
#if defined(JOYSTICK_ANALOG)
|
||||||
|
setPinInput(joystick_axes[axis].input_pin);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) uint16_t joystick_axis_sample(uint8_t axis) {
|
||||||
|
if (axis >= JOYSTICK_AXIS_COUNT) return 0;
|
||||||
|
|
||||||
|
#if defined(JOYSTICK_ANALOG)
|
||||||
|
return analogReadPin(joystick_axes[axis].input_pin);
|
||||||
|
#else
|
||||||
|
// default to resting position
|
||||||
|
return joystick_axes[axis].mid_digit;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool is_virtual_axis(uint8_t axis) {
|
||||||
|
return joystick_axes[axis].input_pin == NO_PIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
void joystick_flush(void) {
|
void joystick_flush(void) {
|
||||||
if (joystick_state.dirty) {
|
if (!joystick_state.dirty) return;
|
||||||
host_joystick_send(&joystick_state);
|
|
||||||
joystick_state.dirty = false;
|
// TODO: host.h?
|
||||||
}
|
void host_joystick_send(joystick_t * joystick);
|
||||||
|
host_joystick_send(&joystick_state);
|
||||||
|
joystick_state.dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void register_joystick_button(uint8_t button) {
|
void register_joystick_button(uint8_t button) {
|
||||||
if (button >= JOYSTICK_BUTTON_COUNT) return;
|
if (button >= JOYSTICK_BUTTON_COUNT) return;
|
||||||
|
|
||||||
joystick_state.buttons[button / 8] |= 1 << (button % 8);
|
joystick_state.buttons[button / 8] |= 1 << (button % 8);
|
||||||
joystick_state.dirty = true;
|
joystick_state.dirty = true;
|
||||||
joystick_flush();
|
joystick_flush();
|
||||||
@ -53,6 +81,7 @@ void register_joystick_button(uint8_t button) {
|
|||||||
|
|
||||||
void unregister_joystick_button(uint8_t button) {
|
void unregister_joystick_button(uint8_t button) {
|
||||||
if (button >= JOYSTICK_BUTTON_COUNT) return;
|
if (button >= JOYSTICK_BUTTON_COUNT) return;
|
||||||
|
|
||||||
joystick_state.buttons[button / 8] &= ~(1 << (button % 8));
|
joystick_state.buttons[button / 8] &= ~(1 << (button % 8));
|
||||||
joystick_state.dirty = true;
|
joystick_state.dirty = true;
|
||||||
joystick_flush();
|
joystick_flush();
|
||||||
@ -61,37 +90,7 @@ void unregister_joystick_button(uint8_t button) {
|
|||||||
int16_t joystick_read_axis(uint8_t axis) {
|
int16_t joystick_read_axis(uint8_t axis) {
|
||||||
if (axis >= JOYSTICK_AXIS_COUNT) return 0;
|
if (axis >= JOYSTICK_AXIS_COUNT) return 0;
|
||||||
|
|
||||||
// disable pull-up resistor
|
int16_t axis_val = joystick_axis_sample(axis);
|
||||||
writePinLow(joystick_axes[axis].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].input_pin);
|
|
||||||
|
|
||||||
wait_us(10);
|
|
||||||
|
|
||||||
if (joystick_axes[axis].output_pin != JS_VIRTUAL_AXIS) {
|
|
||||||
setPinOutput(joystick_axes[axis].output_pin);
|
|
||||||
writePinHigh(joystick_axes[axis].output_pin);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (joystick_axes[axis].ground_pin != JS_VIRTUAL_AXIS) {
|
|
||||||
setPinOutput(joystick_axes[axis].ground_pin);
|
|
||||||
writePinLow(joystick_axes[axis].ground_pin);
|
|
||||||
}
|
|
||||||
|
|
||||||
wait_us(10);
|
|
||||||
|
|
||||||
setPinInput(joystick_axes[axis].input_pin);
|
|
||||||
|
|
||||||
wait_us(10);
|
|
||||||
|
|
||||||
#if defined(ANALOG_JOYSTICK_ENABLE) && (defined(__AVR__) || defined(PROTOCOL_CHIBIOS))
|
|
||||||
int16_t axis_val = analogReadPin(joystick_axes[axis].input_pin);
|
|
||||||
#else
|
|
||||||
// default to resting position
|
|
||||||
int16_t axis_val = joystick_axes[axis].mid_digit;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// test the converted value against the lower range
|
// test the converted value against the lower range
|
||||||
int32_t ref = joystick_axes[axis].mid_digit;
|
int32_t ref = joystick_axes[axis].mid_digit;
|
||||||
@ -111,10 +110,22 @@ int16_t joystick_read_axis(uint8_t axis) {
|
|||||||
return ranged_val;
|
return ranged_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void joystick_init_axes(void) {
|
||||||
|
#if JOYSTICK_AXIS_COUNT > 0
|
||||||
|
for (int i = 0; i < JOYSTICK_AXIS_COUNT; ++i) {
|
||||||
|
if (is_virtual_axis(i)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
joystick_axis_init(i);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void joystick_read_axes(void) {
|
void joystick_read_axes(void) {
|
||||||
#if JOYSTICK_AXIS_COUNT > 0
|
#if JOYSTICK_AXIS_COUNT > 0
|
||||||
for (int i = 0; i < JOYSTICK_AXIS_COUNT; ++i) {
|
for (int i = 0; i < JOYSTICK_AXIS_COUNT; ++i) {
|
||||||
if (joystick_axes[i].input_pin == JS_VIRTUAL_AXIS) {
|
if (is_virtual_axis(i)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,3 +144,11 @@ void joystick_set_axis(uint8_t axis, int16_t value) {
|
|||||||
joystick_state.dirty = true;
|
joystick_state.dirty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void joystick_init(void) {
|
||||||
|
joystick_init_axes();
|
||||||
|
}
|
||||||
|
|
||||||
|
void joystick_task(void) {
|
||||||
|
joystick_read_axes();
|
||||||
|
}
|
||||||
|
@ -52,24 +52,15 @@
|
|||||||
|
|
||||||
#define JOYSTICK_MAX_VALUE ((1L << (JOYSTICK_AXIS_RESOLUTION - 1)) - 1)
|
#define JOYSTICK_MAX_VALUE ((1L << (JOYSTICK_AXIS_RESOLUTION - 1)) - 1)
|
||||||
|
|
||||||
// configure on input_pin of the joystick_axes array entry to JS_VIRTUAL_AXIS
|
// configure on input_pin of the joystick_axes array entry to NO_PIN
|
||||||
// to prevent it from being read from the ADC. This allows outputing forged axis value.
|
// to prevent it from being read from the ADC. This allows outputting forged axis value.
|
||||||
//
|
|
||||||
#define JS_VIRTUAL_AXIS 0xFF
|
|
||||||
|
|
||||||
#define JOYSTICK_AXIS_VIRTUAL \
|
#define JOYSTICK_AXIS_VIRTUAL \
|
||||||
{ JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, 0, 1023 }
|
{ NO_PIN, 0, JOYSTICK_MAX_VALUE / 2, JOYSTICK_MAX_VALUE }
|
||||||
#define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) \
|
#define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) \
|
||||||
{ JS_VIRTUAL_AXIS, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH }
|
{ INPUT_PIN, 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 {
|
typedef struct {
|
||||||
pin_t output_pin;
|
|
||||||
pin_t input_pin;
|
pin_t input_pin;
|
||||||
pin_t ground_pin;
|
|
||||||
|
|
||||||
// the AVR ADC offers 10 bit precision, with significant bits on the higher part
|
// the AVR ADC offers 10 bit precision, with significant bits on the higher part
|
||||||
uint16_t min_digit;
|
uint16_t min_digit;
|
||||||
@ -87,6 +78,14 @@ typedef struct {
|
|||||||
|
|
||||||
extern joystick_t joystick_state;
|
extern joystick_t joystick_state;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Handle the initialization of the subsystem.
|
||||||
|
*/
|
||||||
|
void joystick_init(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Handle various subsystem background tasks.
|
||||||
|
*/
|
||||||
void joystick_task(void);
|
void joystick_task(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -117,6 +116,9 @@ void unregister_joystick_button(uint8_t button);
|
|||||||
*/
|
*/
|
||||||
int16_t joystick_read_axis(uint8_t axis);
|
int16_t joystick_read_axis(uint8_t axis);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Sample and process the all axis.
|
||||||
|
*/
|
||||||
void joystick_read_axes(void);
|
void joystick_read_axes(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -127,6 +129,4 @@ void joystick_read_axes(void);
|
|||||||
*/
|
*/
|
||||||
void joystick_set_axis(uint8_t axis, int16_t value);
|
void joystick_set_axis(uint8_t axis, int16_t value);
|
||||||
|
|
||||||
void host_joystick_send(joystick_t *joystick);
|
|
||||||
|
|
||||||
/** \} */
|
/** \} */
|
||||||
|
@ -440,6 +440,9 @@ void keyboard_init(void) {
|
|||||||
#ifdef DIP_SWITCH_ENABLE
|
#ifdef DIP_SWITCH_ENABLE
|
||||||
dip_switch_init();
|
dip_switch_init();
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef JOYSTICK_ENABLE
|
||||||
|
joystick_init();
|
||||||
|
#endif
|
||||||
#ifdef SLEEP_LED_ENABLE
|
#ifdef SLEEP_LED_ENABLE
|
||||||
sleep_led_init();
|
sleep_led_init();
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user