2022-09-27 10:37:13 +02:00
|
|
|
/* Copyright 2022
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-07-25 14:01:15 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
#include <stdbool.h>
|
2020-11-28 21:02:18 +01:00
|
|
|
#include <stdint.h>
|
2022-11-26 17:14:45 +01:00
|
|
|
|
2022-03-26 19:38:09 +01:00
|
|
|
#include "gpio.h"
|
2020-11-28 21:02:18 +01:00
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
/**
|
2023-03-22 04:32:49 +01:00
|
|
|
* \file
|
2022-11-26 17:14:45 +01:00
|
|
|
*
|
2023-03-22 04:32:49 +01:00
|
|
|
* \defgroup joystick HID Joystick
|
2022-11-26 17:14:45 +01:00
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
2020-07-25 14:01:15 +02:00
|
|
|
#ifndef JOYSTICK_BUTTON_COUNT
|
|
|
|
# define JOYSTICK_BUTTON_COUNT 8
|
2022-04-26 00:54:34 +02:00
|
|
|
#elif JOYSTICK_BUTTON_COUNT > 32
|
|
|
|
# error Joystick feature only supports up to 32 buttons
|
2020-07-25 14:01:15 +02:00
|
|
|
#endif
|
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
#ifndef JOYSTICK_AXIS_COUNT
|
|
|
|
# define JOYSTICK_AXIS_COUNT 2
|
|
|
|
#elif JOYSTICK_AXIS_COUNT > 6
|
2022-04-26 00:54:34 +02:00
|
|
|
# error Joystick feature only supports up to 6 axes
|
|
|
|
#endif
|
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
#if JOYSTICK_AXIS_COUNT == 0 && JOYSTICK_BUTTON_COUNT == 0
|
2022-04-26 00:54:34 +02:00
|
|
|
# error Joystick feature requires at least one axis or button
|
2020-07-25 14:01:15 +02:00
|
|
|
#endif
|
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
#ifndef JOYSTICK_AXIS_RESOLUTION
|
|
|
|
# define JOYSTICK_AXIS_RESOLUTION 8
|
|
|
|
#elif JOYSTICK_AXIS_RESOLUTION < 8 || JOYSTICK_AXIS_RESOLUTION > 16
|
|
|
|
# error JOYSTICK_AXIS_RESOLUTION must be between 8 and 16
|
2020-11-28 21:02:18 +01:00
|
|
|
#endif
|
2020-07-25 14:01:15 +02:00
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
#define JOYSTICK_MAX_VALUE ((1L << (JOYSTICK_AXIS_RESOLUTION - 1)) - 1)
|
2020-07-25 14:01:15 +02:00
|
|
|
|
2024-02-16 15:14:46 +01:00
|
|
|
// configure on input_pin of the joystick_axes array entry to NO_PIN
|
|
|
|
// to prevent it from being read from the ADC. This allows outputting forged axis value.
|
2020-07-25 14:01:15 +02:00
|
|
|
#define JOYSTICK_AXIS_VIRTUAL \
|
2024-02-16 15:14:46 +01:00
|
|
|
{ NO_PIN, 0, JOYSTICK_MAX_VALUE / 2, JOYSTICK_MAX_VALUE }
|
2020-07-25 14:01:15 +02:00
|
|
|
#define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) \
|
2024-02-16 15:14:46 +01:00
|
|
|
{ INPUT_PIN, LOW, REST, HIGH }
|
2020-07-25 14:01:15 +02:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
pin_t input_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;
|
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
extern joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT];
|
2020-07-25 14:01:15 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2021-12-24 23:31:15 +01:00
|
|
|
uint8_t buttons[(JOYSTICK_BUTTON_COUNT - 1) / 8 + 1];
|
2022-11-26 17:14:45 +01:00
|
|
|
int16_t axes[JOYSTICK_AXIS_COUNT];
|
|
|
|
bool dirty;
|
2020-07-25 14:01:15 +02:00
|
|
|
} joystick_t;
|
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
extern joystick_t joystick_state;
|
2020-07-25 14:01:15 +02:00
|
|
|
|
2024-02-16 15:14:46 +01:00
|
|
|
/**
|
|
|
|
* \brief Handle the initialization of the subsystem.
|
|
|
|
*/
|
|
|
|
void joystick_init(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Handle various subsystem background tasks.
|
|
|
|
*/
|
2022-09-27 10:37:13 +02:00
|
|
|
void joystick_task(void);
|
2022-11-26 17:14:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Send the joystick report to the host, if it has been marked as dirty.
|
|
|
|
*/
|
2022-03-26 19:38:09 +01:00
|
|
|
void joystick_flush(void);
|
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
/**
|
|
|
|
* \brief Set the state of a button, and flush the report.
|
|
|
|
*
|
|
|
|
* \param button The index of the button to press, from 0 to 31.
|
|
|
|
*/
|
2022-03-26 19:38:09 +01:00
|
|
|
void register_joystick_button(uint8_t button);
|
2022-11-26 17:14:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Reset the state of a button, and flush the report.
|
|
|
|
*
|
|
|
|
* \param button The index of the button to release, from 0 to 31.
|
|
|
|
*/
|
2022-03-26 19:38:09 +01:00
|
|
|
void unregister_joystick_button(uint8_t button);
|
2022-09-27 10:37:13 +02:00
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
/**
|
|
|
|
* \brief Sample and process the analog value of the given axis.
|
|
|
|
*
|
|
|
|
* \param axis The axis to read.
|
|
|
|
*
|
|
|
|
* \return A signed 16-bit integer, where 0 is the resting or mid point.
|
|
|
|
*/
|
2022-09-27 10:37:13 +02:00
|
|
|
int16_t joystick_read_axis(uint8_t axis);
|
2022-11-26 17:14:45 +01:00
|
|
|
|
2024-02-16 15:14:46 +01:00
|
|
|
/**
|
|
|
|
* \brief Sample and process the all axis.
|
|
|
|
*/
|
2022-11-26 17:14:45 +01:00
|
|
|
void joystick_read_axes(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Set the value of the given axis.
|
|
|
|
*
|
|
|
|
* \param axis The axis to set the value of.
|
|
|
|
* \param value The value to set.
|
|
|
|
*/
|
|
|
|
void joystick_set_axis(uint8_t axis, int16_t value);
|
2022-09-27 10:37:13 +02:00
|
|
|
|
2022-11-26 17:14:45 +01:00
|
|
|
/** \} */
|