Firmware SDK
twr_lis2dh12.h
1 #ifndef _TWR_LIS2DH12_H
2 #define _TWR_LIS2DH12_H
3 
4 #include <twr_i2c.h>
5 #include <twr_tick.h>
6 #include <twr_scheduler.h>
7 
11 
13 
14 typedef enum
15 {
18 
21 
24 
26 
28 
29 typedef enum
30 {
33 
36 
39 
41 
42 typedef enum
43 {
46 
49 
52 
55 
57 
59 
60 typedef struct
61 {
63  int16_t x_axis;
64 
66  int16_t y_axis;
67 
69  int16_t z_axis;
70 
72 
74 
75 typedef struct
76 {
78  float x_axis;
79 
81  float y_axis;
82 
84  float z_axis;
85 
87 
89 
90 typedef struct
91 {
93  float threshold;
94  uint8_t duration;
95  bool x_low;
96  bool x_high;
97  bool y_low;
98  bool y_high;
99  bool z_low;
100  bool z_high;
101 
103 
105 
106 typedef struct twr_lis2dh12_t twr_lis2dh12_t;
107 
109 
110 typedef enum
111 {
112  TWR_LIS2DH12_STATE_ERROR = -1,
113  TWR_LIS2DH12_STATE_INITIALIZE = 0,
114  TWR_LIS2DH12_STATE_MEASURE = 1,
115  TWR_LIS2DH12_STATE_READ = 2,
116  TWR_LIS2DH12_STATE_UPDATE = 3
117 
118 } twr_lis2dh12_state_t;
119 
120 struct twr_lis2dh12_t
121 {
122  twr_i2c_channel_t _i2c_channel;
123  uint8_t _i2c_address;
124  void (*_event_handler)(twr_lis2dh12_t *, twr_lis2dh12_event_t, void *);
125  void *_event_param;
126  twr_tick_t _update_interval;
127  twr_lis2dh12_state_t _state;
128  bool _accelerometer_valid;
130  bool _alarm_active;
131  bool _irq_flag;
132  twr_scheduler_task_id_t _task_id_interval;
133  twr_scheduler_task_id_t _task_id_measure;
134  bool _measurement_active;
135  twr_lis2dh12_resolution_t _resolution;
136  twr_lis2dh12_scale_t _scale;
137 };
138 
140 
145 
146 bool twr_lis2dh12_init(twr_lis2dh12_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address);
147 
152 
153 void twr_lis2dh12_set_event_handler(twr_lis2dh12_t *self, void (*event_handler)(twr_lis2dh12_t *, twr_lis2dh12_event_t, void *), void *event_param);
154 
158 
160 
165 
167 
173 
175 
181 
183 
189 
191 
197 
199 
205 
207 
209 
210 #endif // _TWR_LIS2DH12_H
twr_i2c_channel_t
I2C channels.
Definition: twr_i2c.h:16
twr_lis2dh12_resolution_t
Resolution and mode.
Definition: twr_lis2dh12.h:30
void twr_lis2dh12_set_event_handler(twr_lis2dh12_t *self, void(*event_handler)(twr_lis2dh12_t *, twr_lis2dh12_event_t, void *), void *event_param)
Set callback function.
Definition: twr_lis2dh12.c:57
bool twr_lis2dh12_measure(twr_lis2dh12_t *self)
Start measurement manually.
Definition: twr_lis2dh12.c:79
bool twr_lis2dh12_set_resolution(twr_lis2dh12_t *self, twr_lis2dh12_resolution_t resolution)
Set resolution.
Definition: twr_lis2dh12.c:400
twr_lis2dh12_event_t
Callback events.
Definition: twr_lis2dh12.h:15
void twr_lis2dh12_set_update_interval(twr_lis2dh12_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_lis2dh12.c:63
twr_lis2dh12_scale_t
Definition: twr_lis2dh12.h:43
bool twr_lis2dh12_init(twr_lis2dh12_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize LIS2DH12.
Definition: twr_lis2dh12.c:33
struct twr_lis2dh12_t twr_lis2dh12_t
LIS2DH12 instance.
Definition: twr_lis2dh12.h:106
bool twr_lis2dh12_get_result_g(twr_lis2dh12_t *self, twr_lis2dh12_result_g_t *result_g)
Get measured acceleration in g.
Definition: twr_lis2dh12.c:100
bool twr_lis2dh12_get_result_raw(twr_lis2dh12_t *self, twr_lis2dh12_result_raw_t *result_raw)
Get measured acceleration as raw value.
Definition: twr_lis2dh12.c:93
bool twr_lis2dh12_set_scale(twr_lis2dh12_t *self, twr_lis2dh12_scale_t scale)
Set scale.
Definition: twr_lis2dh12.c:411
bool twr_lis2dh12_set_alarm(twr_lis2dh12_t *self, twr_lis2dh12_alarm_t *alarm)
Enable or disable accelerometer threshold alarm.
Definition: twr_lis2dh12.c:308
@ TWR_LIS2DH12_RESOLUTION_12BIT
12-bit data output (High-resolution mode)
Definition: twr_lis2dh12.h:35
@ TWR_LIS2DH12_RESOLUTION_10BIT
10-bit data output (Normal mode) (Default)
Definition: twr_lis2dh12.h:32
@ TWR_LIS2DH12_RESOLUTION_8BIT
8-bit data output (Low-power mode)
Definition: twr_lis2dh12.h:38
@ TWR_LIS2DH12_EVENT_ALARM
Alarm event.
Definition: twr_lis2dh12.h:23
@ TWR_LIS2DH12_EVENT_ERROR
Error event.
Definition: twr_lis2dh12.h:17
@ TWR_LIS2DH12_EVENT_UPDATE
Update event.
Definition: twr_lis2dh12.h:20
@ TWR_LIS2DH12_SCALE_4G
±4 g.
Definition: twr_lis2dh12.h:48
@ TWR_LIS2DH12_SCALE_16G
±16 g.
Definition: twr_lis2dh12.h:54
@ TWR_LIS2DH12_SCALE_8G
±8 g.
Definition: twr_lis2dh12.h:51
@ TWR_LIS2DH12_SCALE_2G
±2 g (Default)
Definition: twr_lis2dh12.h:45
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: twr_scheduler.h:22
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16
LIS2DH12 alarm set structure.
Definition: twr_lis2dh12.h:91
float threshold
Alarm threshold in g.
Definition: twr_lis2dh12.h:93
LIS2DH12 result in g.
Definition: twr_lis2dh12.h:76
LIS2DH12 result in raw values.
Definition: twr_lis2dh12.h:61