-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8dirJoystick.h
55 lines (45 loc) · 1.06 KB
/
8dirJoystick.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
dirJoystick - Library for reading joysticks as d-pads.
Created by David Martín Martín, February 10, 2021.
TODO:license
*/
#ifndef _dirJoystick_h
#define _dirJoystick_h
#if ARDUINO >= 100
#include <Arduino.h>
#include "Print.h"
#else
#include "WProgram"
#endif
#include <math.h>
#define DirNoDirection 0x00
#define DirE 0x01
#define DirNE 0x02
#define DirN 0x03
#define DirNW 0x04
#define DirW 0x05
#define DirSW 0x06
#define DirS 0x07
#define DirSE 0x08
#define DirAnalogRes 1024
typedef struct DirPair {
int xAxis;
int yAxis;
} DirPair;
class DirJoystick {
public:
DirJoystick() {}
DirJoystick(int x_axis, int y_axis, short dead_zone_pct = 5);
~DirJoystick() {};
unsigned char readJoystickDirection();
DirPair readJoystick();
private:
void applyDeadZone(int& xAxis, int& yAxis);
void centerValues(int& xAxis, int& yAxis);
unsigned toDegrees(int& xAxis, int& yAxis);
unsigned char _deadZone;
unsigned char xAxisPin;
unsigned char yAxisPin;
unsigned char direction[9] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
};
#endif