-
Notifications
You must be signed in to change notification settings - Fork 6
/
magnetometer.cpp
261 lines (211 loc) · 7.38 KB
/
magnetometer.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright 2020-2022 AVSystem <avsystem@avsystem.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Generated by anjay_codegen.py on 2020-05-20 12:42:19
*
* LwM2M Object: Magnetometer
* ID: 3314, URN: urn:oma:lwm2m:ext:3314, Optional, Multiple
*
* This IPSO object can be used to represent a 1-3 axis magnetometer with
* optional compass direction.
*/
#if (SENSORS_IKS01A2 == 1)
#include <algorithm>
#include <assert.h>
#include <stdbool.h>
#include <anjay/anjay.h>
#include <avsystem/commons/avs_defs.h>
#include <avsystem/commons/avs_list_cxx.hpp>
#include <avsystem/commons/avs_log.h>
#include <mbed.h>
#include <XNucleoIKS01A2.h>
#include "magnetometer.h"
#define MAGNETOMETER_OBJ_LOG(...) avs_log(magnetometer_obj, __VA_ARGS__)
#define SENSOR_ID LSM303AGR_MAG_WHO_AM_I
using namespace std;
namespace {
/**
* Sensor Units: R, Single, Optional
* type: string, range: N/A, unit: N/A
* Measurement Units Definition.
*/
constexpr anjay_rid_t RID_SENSOR_UNITS = 5701;
/**
* X Value: R, Single, Mandatory
* type: float, range: N/A, unit: N/A
* The measured value along the X axis.
*/
constexpr anjay_rid_t RID_X_VALUE = 5702;
/**
* Y Value: R, Single, Optional
* type: float, range: N/A, unit: N/A
* The measured value along the Y axis.
*/
constexpr anjay_rid_t RID_Y_VALUE = 5703;
/**
* Z Value: R, Single, Optional
* type: float, range: N/A, unit: N/A
* The measured value along the Z axis.
*/
constexpr anjay_rid_t RID_Z_VALUE = 5704;
constexpr anjay_oid_t MAGNETOMETER_OID = 3314;
// Convert from mG to T
constexpr double VALUE_SCALE = 1e-7;
struct MagnetometerObject {
const anjay_dm_object_def_t *const def;
LSM303AGRMagSensor *sensor;
int32_t x_value;
int32_t y_value;
int32_t z_value;
MagnetometerObject();
~MagnetometerObject();
MagnetometerObject(const MagnetometerObject &) = delete;
MagnetometerObject &operator=(const MagnetometerObject &) = delete;
};
inline MagnetometerObject *
get_obj(const anjay_dm_object_def_t *const *obj_ptr) {
assert(obj_ptr);
static const MagnetometerObject *const FAKE_OBJECT_PTR = nullptr;
static const auto DEF_PTR_OFFSET =
intptr_t(reinterpret_cast<const char *>(&FAKE_OBJECT_PTR[1].def)
- reinterpret_cast<const char *>(&FAKE_OBJECT_PTR[1]));
return reinterpret_cast<MagnetometerObject *>(intptr_t(obj_ptr)
- DEF_PTR_OFFSET);
}
int list_resources(anjay_t *,
const anjay_dm_object_def_t *const *,
anjay_iid_t,
anjay_dm_resource_list_ctx_t *ctx) {
anjay_dm_emit_res(ctx, RID_SENSOR_UNITS, ANJAY_DM_RES_R,
ANJAY_DM_RES_PRESENT);
anjay_dm_emit_res(ctx, RID_X_VALUE, ANJAY_DM_RES_R, ANJAY_DM_RES_PRESENT);
anjay_dm_emit_res(ctx, RID_Y_VALUE, ANJAY_DM_RES_R, ANJAY_DM_RES_PRESENT);
anjay_dm_emit_res(ctx, RID_Z_VALUE, ANJAY_DM_RES_R, ANJAY_DM_RES_PRESENT);
return 0;
}
int resource_read(anjay_t *,
const anjay_dm_object_def_t *const *obj_ptr,
anjay_iid_t iid,
anjay_rid_t rid,
anjay_riid_t riid,
anjay_output_ctx_t *ctx) {
MagnetometerObject *obj = get_obj(obj_ptr);
assert(obj);
assert(iid == 0);
switch (rid) {
case RID_SENSOR_UNITS:
assert(riid == ANJAY_ID_INVALID);
return anjay_ret_string(ctx, "T");
case RID_X_VALUE:
assert(riid == ANJAY_ID_INVALID);
return anjay_ret_float(ctx, obj->x_value * VALUE_SCALE);
case RID_Y_VALUE:
assert(riid == ANJAY_ID_INVALID);
return anjay_ret_float(ctx, obj->y_value * VALUE_SCALE);
case RID_Z_VALUE:
assert(riid == ANJAY_ID_INVALID);
return anjay_ret_float(ctx, obj->z_value * VALUE_SCALE);
default:
return ANJAY_ERR_METHOD_NOT_ALLOWED;
}
}
struct ObjDef : public anjay_dm_object_def_t {
ObjDef() : anjay_dm_object_def_t() {
oid = MAGNETOMETER_OID;
handlers.list_instances = anjay_dm_list_instances_SINGLE;
handlers.list_resources = list_resources;
handlers.resource_read = resource_read;
handlers.transaction_begin = anjay_dm_transaction_NOOP;
handlers.transaction_validate = anjay_dm_transaction_NOOP;
handlers.transaction_commit = anjay_dm_transaction_NOOP;
handlers.transaction_rollback = anjay_dm_transaction_NOOP;
}
} const OBJ_DEF;
MagnetometerObject::MagnetometerObject() : def(&OBJ_DEF) {}
MagnetometerObject::~MagnetometerObject() {}
const anjay_dm_object_def_t **magnetometer_object_create(void) {
LSM303AGRMagSensor *sensor =
XNucleoIKS01A2::instance(D14, D15)->magnetometer;
uint8_t id = 0;
int32_t sensor_value[3];
if (sensor->read_id(&id) || id != SENSOR_ID || sensor->enable()
|| sensor->get_m_axes(sensor_value)) {
MAGNETOMETER_OBJ_LOG(WARNING, "Failed to initialize magnetometer");
return NULL;
}
MagnetometerObject *obj = new (nothrow) MagnetometerObject;
if (!obj) {
return NULL;
}
obj->sensor = sensor;
obj->x_value = sensor_value[0];
obj->y_value = sensor_value[1];
obj->z_value = sensor_value[2];
return const_cast<const anjay_dm_object_def_t **>(&obj->def);
}
void magnetometer_object_release(const anjay_dm_object_def_t **def) {
if (def) {
delete get_obj(def);
}
}
const anjay_dm_object_def_t **OBJ_DEF_PTR;
} // namespace
int magnetometer_object_install(anjay_t *anjay) {
if (OBJ_DEF_PTR) {
MAGNETOMETER_OBJ_LOG(ERROR,
"Magnetometer Object has been already installed");
return -1;
}
OBJ_DEF_PTR = magnetometer_object_create();
if (!OBJ_DEF_PTR) {
return 0;
}
return anjay_register_object(anjay, OBJ_DEF_PTR);
}
void magnetometer_object_uninstall(anjay_t *anjay) {
if (OBJ_DEF_PTR) {
if (anjay_unregister_object(anjay, OBJ_DEF_PTR)) {
MAGNETOMETER_OBJ_LOG(
ERROR, "Error during unregistering Magnetometer Object");
}
magnetometer_object_release(OBJ_DEF_PTR);
OBJ_DEF_PTR = nullptr;
}
}
void magnetometer_object_update(anjay_t *anjay) {
if (!OBJ_DEF_PTR) {
return;
}
MagnetometerObject *obj = get_obj(OBJ_DEF_PTR);
int32_t value[3];
if (obj->sensor->get_m_axes(value)) {
MAGNETOMETER_OBJ_LOG(ERROR, "Failed to get magnetometer values");
return;
}
if (value[0] != obj->x_value) {
obj->x_value = value[0];
(void) anjay_notify_changed(anjay, MAGNETOMETER_OID, 0, RID_X_VALUE);
}
if (value[1] != obj->y_value) {
obj->y_value = value[1];
(void) anjay_notify_changed(anjay, MAGNETOMETER_OID, 0, RID_Y_VALUE);
}
if (value[2] != obj->z_value) {
obj->z_value = value[2];
(void) anjay_notify_changed(anjay, MAGNETOMETER_OID, 0, RID_Z_VALUE);
}
}
#endif // SENSORS_IKS01A2