Skip to content

Commit 0e52ee9

Browse files
pfalcondpgeorge
authored andcommitted
zephyr/modzsensor: Zephyr sensor subsystem bindings.
1 parent 080b0be commit 0e52ee9

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

ports/zephyr/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ SRC_C = main.c \
4141
modusocket.c \
4242
modutime.c \
4343
modzephyr.c \
44+
modzsensor.c \
4445
modmachine.c \
4546
machine_pin.c \
4647
uart_core.c \

ports/zephyr/modzsensor.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Linaro Limited
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <string.h>
28+
29+
#include "py/runtime.h"
30+
31+
#include <zephyr.h>
32+
#include <sensor.h>
33+
34+
#if MICROPY_PY_ZSENSOR
35+
36+
typedef struct _mp_obj_sensor_t {
37+
mp_obj_base_t base;
38+
struct device *dev;
39+
} mp_obj_sensor_t;
40+
41+
STATIC mp_obj_t sensor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
42+
mp_arg_check_num(n_args, n_kw, 1, 1, false);
43+
mp_obj_sensor_t *o = m_new_obj(mp_obj_sensor_t);
44+
o->base.type = type;
45+
o->dev = device_get_binding(mp_obj_str_get_str(args[0]));
46+
if (o->dev == NULL) {
47+
mp_raise_ValueError("dev not found");
48+
}
49+
return MP_OBJ_FROM_PTR(o);
50+
}
51+
52+
STATIC mp_obj_t sensor_measure(mp_obj_t self_in) {
53+
mp_obj_sensor_t *self = MP_OBJ_TO_PTR(self_in);
54+
int st = sensor_sample_fetch(self->dev);
55+
if (st != 0) {
56+
mp_raise_OSError(-st);
57+
}
58+
return mp_const_none;
59+
}
60+
MP_DEFINE_CONST_FUN_OBJ_1(sensor_measure_obj, sensor_measure);
61+
62+
STATIC void sensor_get_internal(mp_obj_t self_in, mp_obj_t channel_in, struct sensor_value *res) {
63+
mp_obj_sensor_t *self = MP_OBJ_TO_PTR(self_in);
64+
65+
int st = sensor_channel_get(self->dev, mp_obj_get_int(channel_in), res);
66+
if (st != 0) {
67+
mp_raise_OSError(-st);
68+
}
69+
}
70+
71+
STATIC mp_obj_t sensor_get_float(mp_obj_t self_in, mp_obj_t channel_in) {
72+
struct sensor_value val;
73+
sensor_get_internal(self_in, channel_in, &val);
74+
return mp_obj_new_float(val.val1 + (mp_float_t)val.val2 / 1000000);
75+
}
76+
MP_DEFINE_CONST_FUN_OBJ_2(sensor_get_float_obj, sensor_get_float);
77+
78+
STATIC mp_obj_t sensor_get_micros(mp_obj_t self_in, mp_obj_t channel_in) {
79+
struct sensor_value val;
80+
sensor_get_internal(self_in, channel_in, &val);
81+
return MP_OBJ_NEW_SMALL_INT(val.val1 * 1000000 + val.val2);
82+
}
83+
MP_DEFINE_CONST_FUN_OBJ_2(sensor_get_micros_obj, sensor_get_micros);
84+
85+
STATIC mp_obj_t sensor_get_millis(mp_obj_t self_in, mp_obj_t channel_in) {
86+
struct sensor_value val;
87+
sensor_get_internal(self_in, channel_in, &val);
88+
return MP_OBJ_NEW_SMALL_INT(val.val1 * 1000 + val.val2 / 1000);
89+
}
90+
MP_DEFINE_CONST_FUN_OBJ_2(sensor_get_millis_obj, sensor_get_millis);
91+
92+
STATIC mp_obj_t sensor_get_int(mp_obj_t self_in, mp_obj_t channel_in) {
93+
struct sensor_value val;
94+
sensor_get_internal(self_in, channel_in, &val);
95+
return MP_OBJ_NEW_SMALL_INT(val.val1);
96+
}
97+
MP_DEFINE_CONST_FUN_OBJ_2(sensor_get_int_obj, sensor_get_int);
98+
99+
STATIC const mp_rom_map_elem_t sensor_locals_dict_table[] = {
100+
{ MP_ROM_QSTR(MP_QSTR_measure), MP_ROM_PTR(&sensor_measure_obj) },
101+
{ MP_ROM_QSTR(MP_QSTR_get_float), MP_ROM_PTR(&sensor_get_float_obj) },
102+
{ MP_ROM_QSTR(MP_QSTR_get_micros), MP_ROM_PTR(&sensor_get_micros_obj) },
103+
{ MP_ROM_QSTR(MP_QSTR_get_millis), MP_ROM_PTR(&sensor_get_millis_obj) },
104+
{ MP_ROM_QSTR(MP_QSTR_get_int), MP_ROM_PTR(&sensor_get_int_obj) },
105+
};
106+
107+
STATIC MP_DEFINE_CONST_DICT(sensor_locals_dict, sensor_locals_dict_table);
108+
109+
STATIC const mp_obj_type_t sensor_type = {
110+
{ &mp_type_type },
111+
.name = MP_QSTR_Sensor,
112+
.make_new = sensor_make_new,
113+
.locals_dict = (void*)&sensor_locals_dict,
114+
};
115+
116+
STATIC const mp_rom_map_elem_t mp_module_zsensor_globals_table[] = {
117+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zsensor) },
118+
{ MP_ROM_QSTR(MP_QSTR_Sensor), MP_ROM_PTR(&sensor_type) },
119+
120+
#define C(name) { MP_ROM_QSTR(MP_QSTR_ ## name), MP_ROM_INT(SENSOR_CHAN_ ## name) }
121+
C(ACCEL_X),
122+
C(ACCEL_Y),
123+
C(ACCEL_Z),
124+
C(GYRO_X),
125+
C(GYRO_Y),
126+
C(GYRO_Z),
127+
C(MAGN_X),
128+
C(MAGN_Y),
129+
C(MAGN_Z),
130+
C(TEMP),
131+
C(PRESS),
132+
C(PROX),
133+
C(HUMIDITY),
134+
C(LIGHT),
135+
C(ALTITUDE),
136+
#undef C
137+
};
138+
139+
STATIC MP_DEFINE_CONST_DICT(mp_module_zsensor_globals, mp_module_zsensor_globals_table);
140+
141+
const mp_obj_module_t mp_module_zsensor = {
142+
.base = { &mp_type_module },
143+
.globals = (mp_obj_dict_t*)&mp_module_zsensor_globals,
144+
};
145+
146+
#endif //MICROPY_PY_UHASHLIB

ports/zephyr/mpconfigport.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#define MICROPY_PY_UTIME (1)
7373
#define MICROPY_PY_UTIME_MP_HAL (1)
7474
#define MICROPY_PY_ZEPHYR (1)
75+
#define MICROPY_PY_ZSENSOR (1)
7576
#define MICROPY_PY_SYS_MODULES (0)
7677
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG)
7778
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
@@ -111,6 +112,7 @@ extern const struct _mp_obj_module_t mp_module_machine;
111112
extern const struct _mp_obj_module_t mp_module_time;
112113
extern const struct _mp_obj_module_t mp_module_usocket;
113114
extern const struct _mp_obj_module_t mp_module_zephyr;
115+
extern const struct _mp_obj_module_t mp_module_zsensor;
114116

115117
#if MICROPY_PY_USOCKET
116118
#define MICROPY_PY_USOCKET_DEF { MP_ROM_QSTR(MP_QSTR_usocket), MP_ROM_PTR(&mp_module_usocket) },
@@ -132,11 +134,18 @@ extern const struct _mp_obj_module_t mp_module_zephyr;
132134
#define MICROPY_PY_ZEPHYR_DEF
133135
#endif
134136

137+
#if MICROPY_PY_ZSENSOR
138+
#define MICROPY_PY_ZSENSOR_DEF { MP_ROM_QSTR(MP_QSTR_zsensor), MP_ROM_PTR(&mp_module_zsensor) },
139+
#else
140+
#define MICROPY_PY_ZSENSOR_DEF
141+
#endif
142+
135143
#define MICROPY_PORT_BUILTIN_MODULES \
136144
{ MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) }, \
137145
MICROPY_PY_USOCKET_DEF \
138146
MICROPY_PY_UTIME_DEF \
139147
MICROPY_PY_ZEPHYR_DEF \
148+
MICROPY_PY_ZSENSOR_DEF \
140149

141150
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
142151
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_module_time) }, \

ports/zephyr/prj_base.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ CONFIG_NEWLIB_LIBC=y
1414
CONFIG_FLOAT=y
1515
CONFIG_MAIN_STACK_SIZE=4736
1616

17+
# Enable sensor subsystem (doesn't add code if not used).
18+
# Specific sensors should be enabled per-board.
19+
CONFIG_SENSOR=y
20+
1721
# Networking config
1822
CONFIG_NETWORKING=y
1923
CONFIG_NET_IPV4=y

0 commit comments

Comments
 (0)