73 lines
1.8 KiB
C
73 lines
1.8 KiB
C
|
|
#ifndef _JOYSTICK_H_
|
||
|
|
#define _JOYSTICK_H_
|
||
|
|
|
||
|
|
#include <Arduino.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
|
||
|
|
#define BUTTON_STABLETIME 100
|
||
|
|
#define JOYSTICK_STABLETIME 3000
|
||
|
|
|
||
|
|
// Button states
|
||
|
|
#define BUTTON_STATE_BOUNCING (1 << 0)
|
||
|
|
#define BUTTON_STATE_DOWN (1 << 1)
|
||
|
|
#define BUTTON_STATE_UP (1 << 2)
|
||
|
|
#define BUTTON_STATE_HELD (1 << 3)
|
||
|
|
|
||
|
|
// Joystick states
|
||
|
|
#define JOYSTICK_STATE_CALIBRATING (1 << 0)
|
||
|
|
#define JOYSTICK_STATE_CALIBRATED (1 << 1)
|
||
|
|
#define JOYSTICK_STATE_READING (1 << 2)
|
||
|
|
#define JOYSTICK_STATE_READY (1 << 3)
|
||
|
|
|
||
|
|
typedef struct Calibration {
|
||
|
|
// When did calibration start
|
||
|
|
uint32_t start_time;
|
||
|
|
// Low calibration range
|
||
|
|
uint32_t low;
|
||
|
|
// High calibration range
|
||
|
|
uint32_t high;
|
||
|
|
// How long we should sample values on this joystick
|
||
|
|
uint32_t stable_time;
|
||
|
|
} Calibration;
|
||
|
|
|
||
|
|
typedef struct Button {
|
||
|
|
// What pin is this button on
|
||
|
|
uint8_t pin;
|
||
|
|
// What is the state of this button (bitmask of BUTTON_STATE_*)
|
||
|
|
uint8_t state;
|
||
|
|
// Is this button pressed when it is LOW or HIGH?
|
||
|
|
uint32_t pressedvalue;
|
||
|
|
// How long do we wait when debouncing this button
|
||
|
|
uint32_t debouncetime;
|
||
|
|
// When did we start debouncing
|
||
|
|
uint32_t debounce_start_time;
|
||
|
|
} Button;
|
||
|
|
|
||
|
|
typedef struct JoystickAxis {
|
||
|
|
// What ADC pin is this joystick axis connected to
|
||
|
|
uint8_t pin;
|
||
|
|
// Calibration data
|
||
|
|
Calibration calibration;
|
||
|
|
// Position as of the last read time.
|
||
|
|
uint32_t position;
|
||
|
|
} JoystickAxis;
|
||
|
|
|
||
|
|
typedef struct Joystick {
|
||
|
|
// X axis data
|
||
|
|
JoystickAxis x;
|
||
|
|
// Y axis data
|
||
|
|
JoystickAxis y;
|
||
|
|
// This is the Z axis switch on the joystick control
|
||
|
|
Button button;
|
||
|
|
// What is the state of this joystick (bitmask of JOYSTICK_STATE_*)
|
||
|
|
uint8_t state;
|
||
|
|
} Joystick;
|
||
|
|
|
||
|
|
int initJoystick(Joystick *js, uint8_t pin_x, uint8_t pin_y, uint8_t pin_z);
|
||
|
|
int initButton(Button *button);
|
||
|
|
int calibrateJoystick(Joystick *js);
|
||
|
|
int readButton(Button *button);
|
||
|
|
int readJoystick(Joystick *js);
|
||
|
|
|
||
|
|
#endif // _JOYSTICK_H_
|