@ -15,10 +15,12 @@
*/
# include "joystick.h"
# include "analog.h"
# include "wait.h"
# if defined(JOYSTICK_ANALOG)
# include "analog.h"
# endif
joystick_t joystick_state = {
. buttons = { 0 } ,
. axes =
@ -31,21 +33,47 @@ joystick_t joystick_state = {
} ;
/ / 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 ) {
joystick_read_axes ( ) ;
__attribute__ ( ( weak ) ) void joystick_axis_init ( uint8_t axis ) {
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 ) {
if ( joystick_state . dirty ) {
host_joystick_send ( & joystick_state ) ;
joystick_state . dirty = false ;
}
if ( ! joystick_state . dirty ) return ;
/ / 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 ) {
if ( button > = JOYSTICK_BUTTON_COUNT ) return ;
joystick_state . buttons [ button / 8 ] | = 1 < < ( button % 8 ) ;
joystick_state . dirty = true ;
joystick_flush ( ) ;
@ -53,6 +81,7 @@ void register_joystick_button(uint8_t button) {
void unregister_joystick_button ( uint8_t button ) {
if ( button > = JOYSTICK_BUTTON_COUNT ) return ;
joystick_state . buttons [ button / 8 ] & = ~ ( 1 < < ( button % 8 ) ) ;
joystick_state . dirty = true ;
joystick_flush ( ) ;
@ -61,37 +90,7 @@ void unregister_joystick_button(uint8_t button) {
int16_t joystick_read_axis ( uint8_t axis ) {
if ( axis > = JOYSTICK_AXIS_COUNT ) return 0 ;
/ / disable pull - up resistor
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
int16_t axis_val = joystick_axis_sample ( axis ) ;
/ / test the converted value against the lower range
int32_t ref = joystick_axes [ axis ] . mid_digit ;
@ -111,10 +110,22 @@ int16_t joystick_read_axis(uint8_t axis) {
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 ) {
# if JOYSTICK_AXIS_COUNT > 0
for ( int i = 0 ; i < JOYSTICK_AXIS_COUNT ; + + i ) {
if ( joystick_axes [ i ] . input_pin = = JS_VIRTUAL_AXIS ) {
if ( is_virtual_axis ( i ) ) {
continue ;
}
@ -133,3 +144,11 @@ void joystick_set_axis(uint8_t axis, int16_t value) {
joystick_state . dirty = true ;
}
}
void joystick_init ( void ) {
joystick_init_axes ( ) ;
}
void joystick_task ( void ) {
joystick_read_axes ( ) ;
}