forked from toniebox-reverse-engineering/hackiebox_cfw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxAccelerometer.cpp
More file actions
114 lines (95 loc) · 3.89 KB
/
BoxAccelerometer.cpp
File metadata and controls
114 lines (95 loc) · 3.89 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "BoxAccelerometer.h"
#include "BoxEvents.h"
void BoxAccelerometer::begin() {
pinMode(62, OUTPUT);
digitalWrite(62, HIGH);
setInterval(250);
Log.info("Init Accelerometer...");
if (!_accel.begin())
Log.error("...not found!");
_accel.setupPL();
_accel.writeRegister(CTRL_REG1, 0x02); //Original 0x02 //F_READ
_accel.writeRegister(XYZ_DATA_CFG, 0x02); //Original 0x02 //FS1
_accel.writeRegister(CTRL_REG2, 0x00); //Original 0x00 //Standby
_accel.writeRegister(F_SETUP, 0x00); //Original 0x00
_accel.writeRegister(TRIG_CFG, 0x08); //Original 0x08 //Trig_PULSE/ZSPEFE/ELE
_accel.writeRegister(PULSE_CFG, 0x54); //Original 0x54 //YSPEFE
_accel.setupTap(0x1B, 0x3F, 0x3F); //Original 0x1B, 0x3F, 0x3F
_accel.writeRegister(PULSE_TMLT, 0x28); //Original 0x28 //TMLT3/TMLT5
_accel.writeRegister(PULSE_LTCY, 0x7F); //Original 0x7F //LTCY6/LTCY5/LTCY4/LTCY3/LTCY2/LTCY1/LTCY0
_accel.writeRegister(HP_FILTER_CUTOFF, 0x10); //Original 0x10 //Pulse_LPF_EN
_accel.writeRegister(CTRL_REG3, 0x12); //Original 0x12 //WAKE_PULSE/IPOL
_accel.writeRegister(CTRL_REG4, 0x40); //Original 0x40 //INT_EN_FIFO
_accel.writeRegister(CTRL_REG5, 0x40); //Original 0x40 //INT_CFG_FIFO INT1
_accel.writeRegister(CTRL_REG1, 0x03); //Original 0x03 //F_READ/ACTIVE
Log.info("...done");
}
void BoxAccelerometer::loop() {
if (_accel.available()) {
//Log.verbose("Accelerometer=(%i, %i, %i), Orient=%i", _accel.getX(), _accel.getY(), _accel.getZ(), _accel.readOrientation());
Orientation orient = (Orientation)_accel.readOrientation();
if (orient == Orientation::EARS_UP2) {
orient = Orientation::EARS_UP;
} else if (orient == Orientation::EARS_DOWN2) {
orient = Orientation::EARS_DOWN;
}
if (_orientation != orient) {
_orientation = orient;
Events.handleAccelerometerOrientationEvent(_orientation);
}
uint8_t tap = _accel.readTap();
if (tap) {
bool AxZ = (tap&0b1000000)>0; //event on axis
bool AxY = (tap&0b0100000)>0;
bool AxX = (tap&0b0010000)>0;
//bool DPE = (tap&0b0001000)>0; //double
bool PolZ = !((tap&0b0000100)>0); //0=positive 1=negative
bool PolY = !((tap&0b0000010)>0);
bool PolX = !((tap&0b0000001)>0);
//X+ = box bottom
//X- = box top
//Y+ = box back left (big ear)
//Y- = box front right (speaker, small ear)
//Z+ = box back right (small ear)
//Z- = box front left (speaker, big ear)
//Something wrong, only blinks red or greenyellow
TapOn tapOn = TapOn::NONE;
if (AxX) {
if (PolX) {
tapOn = TapOn::BOTTOM;
} else {
tapOn = TapOn::TOP;
}
}
if (AxY && AxZ) {
if (PolY && PolZ) {
tapOn = TapOn::BACK;
} else if (!PolY && !PolZ) {
tapOn = TapOn::FRONT;
} else if (PolY && !PolZ) {
tapOn = TapOn::LEFT;
} else if (!PolY && PolZ) {
tapOn = TapOn::RIGHT;
}
} else if (AxY) {
if (PolY) {
tapOn = TapOn::LEFT_BACK;
} else {
tapOn = TapOn::LEFT_FRONT;
}
} else if (AxZ) {
if (PolZ) {
tapOn = TapOn::RIGHT_BACK;
} else {
tapOn = TapOn::RIGHT_FRONT;
}
}
Events.handleAccelerometerTapsEvent(tapOn);
}
}
}
void BoxAccelerometer::reloadConfig() {
}
BoxAccelerometer::Orientation BoxAccelerometer::getOrientation() {
return _orientation;
}