-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacc.cpp
179 lines (154 loc) · 4.85 KB
/
acc.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @file acc.cpp
* @author Bernd Giesecke (bernd@giesecke.tk)
* @brief Lis3DH acc sensor functions
* @version 0.2
* @date 2024-11-18
*
* @copyright Copyright (c) 2024
*
*/
#include "app.h"
// Forward declaration
void acc_int_callback(void);
/** The LIS3DH sensor */
LIS3DH acc_sensor(I2C_MODE, 0x18);
/**
* @brief Initialize LIS3DH 3-axis
*
* @param active true = initialize interrupts, false = put in low power mode
* @return true If sensor was found and is initialized
* @return false If sensor initialization failed
*/
bool init_acc(bool active)
{
// Setup interrupt pin
pinMode(ACC_INT_PIN, INPUT);
Wire.begin();
if (active)
{
acc_sensor.settings.accelSampleRate = 10; // Hz. Can be: 0,1,10,25,50,100,200,400,1600,5000 Hz
acc_sensor.settings.accelRange = 2; // Max G force readable. Can be: 2, 4, 8, 16
acc_sensor.settings.adcEnabled = 0;
acc_sensor.settings.tempEnabled = 0;
acc_sensor.settings.xAccelEnabled = 1;
acc_sensor.settings.yAccelEnabled = 1;
acc_sensor.settings.zAccelEnabled = 1;
}
else
{
acc_sensor.settings.accelSampleRate = 0; // Hz. Can be: 0,1,10,25,50,100,200,400,1600,5000 Hz
acc_sensor.settings.accelRange = 2; // Max G force readable. Can be: 2, 4, 8, 16
acc_sensor.settings.adcEnabled = 0;
acc_sensor.settings.tempEnabled = 0;
acc_sensor.settings.xAccelEnabled = 0;
acc_sensor.settings.yAccelEnabled = 0;
acc_sensor.settings.zAccelEnabled = 0;
}
if (acc_sensor.begin() != 0)
{
MYLOG("ACC", "ACC sensor initialization failed");
return false;
}
uint8_t data_to_write = 0;
if (active)
{
// Enable interrupts
data_to_write |= 0x20; // Z high
data_to_write |= 0x08; // Y high
data_to_write |= 0x02; // X high
acc_sensor.writeRegister(LIS3DH_INT1_CFG, data_to_write); // Enable interrupts on high tresholds for x, y and z
// Set interrupt trigger range
data_to_write = 0;
// data_to_write |= 0x10; // 1/8 range
// acc_sensor.writeRegister(LIS3DH_INT1_THS, data_to_write); // 1/8th range
data_to_write |= 0x08; // 1/16 range
acc_sensor.writeRegister(LIS3DH_INT1_THS, data_to_write); // 1/16th range
// Set interrupt signal length
data_to_write = 0;
data_to_write |= 0x01; // 1 * 1/50 s = 20ms
acc_sensor.writeRegister(LIS3DH_INT1_DURATION, data_to_write);
acc_sensor.readRegister(&data_to_write, LIS3DH_CTRL_REG5);
data_to_write &= 0xF3; // Clear bits of interest
data_to_write |= 0x08; // Latch interrupt (Cleared by reading int1_src)
acc_sensor.writeRegister(LIS3DH_CTRL_REG5, data_to_write); // Set interrupt to latching
// Select interrupt pin 1
data_to_write = 0;
data_to_write |= 0x40; // AOI1 event (Generator 1 interrupt on pin 1)
data_to_write |= 0x20; // AOI2 event ()
acc_sensor.writeRegister(LIS3DH_CTRL_REG3, data_to_write);
// No interrupt on pin 2
acc_sensor.writeRegister(LIS3DH_CTRL_REG6, 0x00);
// Enable high pass filter
acc_sensor.writeRegister(LIS3DH_CTRL_REG2, 0x01);
}
// Set low power mode
data_to_write = 0;
acc_sensor.readRegister(&data_to_write, LIS3DH_CTRL_REG1);
data_to_write |= 0x08;
acc_sensor.writeRegister(LIS3DH_CTRL_REG1, data_to_write);
delay(100);
data_to_write = 0;
acc_sensor.readRegister(&data_to_write, 0x1E);
data_to_write |= 0x90;
acc_sensor.writeRegister(0x1E, data_to_write);
delay(100);
if (active)
{
clear_acc_int();
// Set the interrupt callback function
attachInterrupt(ACC_INT_PIN, acc_int_callback, RISING);
read_acc();
}
else
{
// Power-Down mode
acc_sensor.readRegister(&data_to_write, LIS3DH_CTRL_REG1);
data_to_write &= 0x40;
acc_sensor.writeRegister(LIS3DH_CTRL_REG1, data_to_write);
}
return true;
}
/**
* @brief Read sensor values (unused)
*
*/
void read_acc(void)
{
int16_t acc_x = (int16_t)(acc_sensor.readFloatAccelX() * 1000.0);
int16_t acc_y = (int16_t)(acc_sensor.readFloatAccelY() * 1000.0);
int16_t acc_z = (int16_t)(acc_sensor.readFloatAccelZ() * 1000.0);
MYLOG("ACC", "X %.3f %.3f %d", acc_sensor.readFloatAccelX(), acc_sensor.readFloatAccelX() * 1000.0, acc_x);
MYLOG("ACC", "Y %.3f %.3f %d", acc_sensor.readFloatAccelY(), acc_sensor.readFloatAccelY() * 1000.0, acc_y);
MYLOG("ACC", "Z %.3f %.3f %d", acc_sensor.readFloatAccelZ(), acc_sensor.readFloatAccelZ() * 1000.0, acc_z);
}
/**
* @brief ACC interrupt handler (unused)
*
*/
void acc_int_callback(void)
{
}
/**
* @brief Clear ACC interrupt register to enable next wakeup
*
*/
void clear_acc_int(void)
{
uint8_t data_read;
acc_sensor.readRegister(&data_read, LIS3DH_INT1_SRC);
if (data_read & 0x40)
MYLOG("ACC", "Interrupt Active 0x%X\n", data_read);
if (data_read & 0x20)
MYLOG("ACC", "Z high");
if (data_read & 0x10)
MYLOG("ACC", "Z low");
if (data_read & 0x08)
MYLOG("ACC", "Y high");
if (data_read & 0x04)
MYLOG("ACC", "Y low");
if (data_read & 0x02)
MYLOG("ACC", "X high");
if (data_read & 0x01)
MYLOG("ACC", "X low");
}