mirror of
https://github.com/micropython/micropython.git
synced 2025-12-16 09:50:15 +01:00
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
This commit adds `Sensor.attr_set()` and `Sensor.attr_get_*()` methods that are necessary to set various sensor attributes if they haven't been set statically in the device tree. This is needed, for example, because the LSM6DS3TR-C sensor on the XIAO BLE NRF52840 SENSE board will not work with `zsensor` because it doesn't have any default configuration for sampling frequency. Various `SENSOR_ATTR_*` constants from `zephyr/incude/zephyr/drivers/sensor.h` have been added as `ATTR_*` constants in the `zsensor` module. Signed-off-by: Ned Konz <ned@metamagix.tech>
248 lines
8.4 KiB
C
248 lines
8.4 KiB
C
/*
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2018 Linaro Limited
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/drivers/sensor.h>
|
|
#include "zephyr_device.h"
|
|
|
|
#if MICROPY_PY_ZSENSOR
|
|
|
|
typedef struct _mp_obj_sensor_t {
|
|
mp_obj_base_t base;
|
|
const struct device *dev;
|
|
} mp_obj_sensor_t;
|
|
|
|
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) {
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
|
mp_obj_sensor_t *o = mp_obj_malloc(mp_obj_sensor_t, type);
|
|
o->dev = zephyr_device_find(args[0]);
|
|
return MP_OBJ_FROM_PTR(o);
|
|
}
|
|
|
|
static mp_obj_t sensor_measure(mp_obj_t self_in) {
|
|
mp_obj_sensor_t *self = MP_OBJ_TO_PTR(self_in);
|
|
int st = sensor_sample_fetch(self->dev);
|
|
if (st != 0) {
|
|
mp_raise_OSError(-st);
|
|
}
|
|
return mp_const_none;
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_1(sensor_measure_obj, sensor_measure);
|
|
|
|
static void sensor_get_internal(mp_obj_t self_in, mp_obj_t channel_in, struct sensor_value *res) {
|
|
mp_obj_sensor_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
int st = sensor_channel_get(self->dev, mp_obj_get_int(channel_in), res);
|
|
if (st != 0) {
|
|
mp_raise_OSError(-st);
|
|
}
|
|
}
|
|
|
|
static mp_obj_t sensor_get_float(mp_obj_t self_in, mp_obj_t channel_in) {
|
|
struct sensor_value val;
|
|
sensor_get_internal(self_in, channel_in, &val);
|
|
return mp_obj_new_float(val.val1 + (mp_float_t)val.val2 / 1000000);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_2(sensor_get_float_obj, sensor_get_float);
|
|
|
|
static mp_obj_t sensor_get_micros(mp_obj_t self_in, mp_obj_t channel_in) {
|
|
struct sensor_value val;
|
|
sensor_get_internal(self_in, channel_in, &val);
|
|
return MP_OBJ_NEW_SMALL_INT(val.val1 * 1000000 + val.val2);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_2(sensor_get_micros_obj, sensor_get_micros);
|
|
|
|
static mp_obj_t sensor_get_millis(mp_obj_t self_in, mp_obj_t channel_in) {
|
|
struct sensor_value val;
|
|
sensor_get_internal(self_in, channel_in, &val);
|
|
return MP_OBJ_NEW_SMALL_INT(val.val1 * 1000 + val.val2 / 1000);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_2(sensor_get_millis_obj, sensor_get_millis);
|
|
|
|
static mp_obj_t sensor_get_int(mp_obj_t self_in, mp_obj_t channel_in) {
|
|
struct sensor_value val;
|
|
sensor_get_internal(self_in, channel_in, &val);
|
|
return MP_OBJ_NEW_SMALL_INT(val.val1);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_2(sensor_get_int_obj, sensor_get_int);
|
|
|
|
static void sensor_attr_get_internal(mp_obj_t self_in, mp_obj_t channel_in, mp_obj_t attr_in, struct sensor_value *res) {
|
|
mp_obj_sensor_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
int st = sensor_attr_get(self->dev, mp_obj_get_int(channel_in), mp_obj_get_int(attr_in), res);
|
|
if (st != 0) {
|
|
mp_raise_OSError(-st);
|
|
}
|
|
}
|
|
|
|
static mp_obj_t sensor_attr_get_float(mp_obj_t self_in, mp_obj_t channel_in, mp_obj_t attr_in) {
|
|
struct sensor_value val;
|
|
sensor_attr_get_internal(self_in, channel_in, attr_in, &val);
|
|
return mp_obj_new_float(val.val1 + (mp_float_t)val.val2 / 1000000);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_3(sensor_attr_get_float_obj, sensor_attr_get_float);
|
|
|
|
static mp_obj_t sensor_attr_get_micros(mp_obj_t self_in, mp_obj_t channel_in, mp_obj_t attr_in) {
|
|
struct sensor_value val;
|
|
sensor_attr_get_internal(self_in, channel_in, attr_in, &val);
|
|
return MP_OBJ_NEW_SMALL_INT(val.val1 * 1000000 + val.val2);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_3(sensor_attr_get_micros_obj, sensor_attr_get_micros);
|
|
|
|
static mp_obj_t sensor_attr_get_millis(mp_obj_t self_in, mp_obj_t channel_in, mp_obj_t attr_in) {
|
|
struct sensor_value val;
|
|
sensor_attr_get_internal(self_in, channel_in, attr_in, &val);
|
|
return MP_OBJ_NEW_SMALL_INT(val.val1 * 1000 + val.val2 / 1000);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_3(sensor_attr_get_millis_obj, sensor_attr_get_millis);
|
|
|
|
static mp_obj_t sensor_attr_get_int(mp_obj_t self_in, mp_obj_t channel_in, mp_obj_t attr_in) {
|
|
struct sensor_value val;
|
|
sensor_attr_get_internal(self_in, channel_in, attr_in, &val);
|
|
return MP_OBJ_NEW_SMALL_INT(val.val1);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_3(sensor_attr_get_int_obj, sensor_attr_get_int);
|
|
|
|
static mp_obj_t mp_sensor_attr_set(size_t n_args, const mp_obj_t *args) {
|
|
mp_obj_sensor_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
mp_obj_t channel_in = args[1];
|
|
mp_obj_t attr_in = args[2];
|
|
struct sensor_value val;
|
|
if (n_args == 4) {
|
|
// One float argument
|
|
float v = mp_obj_get_float(args[3]);
|
|
val.val1 = (int32_t)v;
|
|
val.val2 = (int32_t)((v - val.val1) * 1000000);
|
|
} else {
|
|
// Two integer arguments
|
|
val.val1 = mp_obj_get_int(args[3]);
|
|
val.val2 = mp_obj_get_int(args[4]);
|
|
}
|
|
int st = sensor_attr_set(self->dev, mp_obj_get_int(channel_in), mp_obj_get_int(attr_in), &val);
|
|
if (st != 0) {
|
|
mp_raise_OSError(-st);
|
|
}
|
|
return mp_const_none;
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(sensor_attr_set_obj, 4, 5, mp_sensor_attr_set);
|
|
|
|
static const mp_rom_map_elem_t sensor_locals_dict_table[] = {
|
|
{ MP_ROM_QSTR(MP_QSTR_measure), MP_ROM_PTR(&sensor_measure_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_get_float), MP_ROM_PTR(&sensor_get_float_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_get_micros), MP_ROM_PTR(&sensor_get_micros_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_get_millis), MP_ROM_PTR(&sensor_get_millis_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_get_int), MP_ROM_PTR(&sensor_get_int_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_attr_get_float), MP_ROM_PTR(&sensor_attr_get_float_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_attr_get_micros), MP_ROM_PTR(&sensor_attr_get_micros_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_attr_get_millis), MP_ROM_PTR(&sensor_attr_get_millis_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_attr_get_int), MP_ROM_PTR(&sensor_attr_get_int_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_attr_set), MP_ROM_PTR(&sensor_attr_set_obj) },
|
|
};
|
|
|
|
static MP_DEFINE_CONST_DICT(sensor_locals_dict, sensor_locals_dict_table);
|
|
|
|
static MP_DEFINE_CONST_OBJ_TYPE(
|
|
sensor_type,
|
|
MP_QSTR_Sensor,
|
|
MP_TYPE_FLAG_NONE,
|
|
make_new, sensor_make_new,
|
|
locals_dict, &sensor_locals_dict
|
|
);
|
|
|
|
static const mp_rom_map_elem_t mp_module_zsensor_globals_table[] = {
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zsensor) },
|
|
{ MP_ROM_QSTR(MP_QSTR_Sensor), MP_ROM_PTR(&sensor_type) },
|
|
|
|
#define C(name) { MP_ROM_QSTR(MP_QSTR_##name), MP_ROM_INT(SENSOR_CHAN_##name) }
|
|
C(ACCEL_X),
|
|
C(ACCEL_Y),
|
|
C(ACCEL_Z),
|
|
C(ACCEL_XYZ),
|
|
C(GYRO_X),
|
|
C(GYRO_Y),
|
|
C(GYRO_Z),
|
|
C(GYRO_XYZ),
|
|
C(MAGN_X),
|
|
C(MAGN_Y),
|
|
C(MAGN_Z),
|
|
C(MAGN_XYZ),
|
|
C(DIE_TEMP),
|
|
C(AMBIENT_TEMP),
|
|
C(PRESS),
|
|
C(PROX),
|
|
C(HUMIDITY),
|
|
C(LIGHT),
|
|
C(IR),
|
|
C(RED),
|
|
C(GREEN),
|
|
C(BLUE),
|
|
C(ALTITUDE),
|
|
C(PM_1_0),
|
|
C(PM_2_5),
|
|
C(PM_10),
|
|
C(DISTANCE),
|
|
C(CO2),
|
|
C(VOC),
|
|
C(GAS_RES),
|
|
C(VOLTAGE),
|
|
#undef C
|
|
#define C(name) { MP_ROM_QSTR(MP_QSTR_ATTR_##name), MP_ROM_INT(SENSOR_ATTR_##name) }
|
|
C(SAMPLING_FREQUENCY),
|
|
C(LOWER_THRESH),
|
|
C(UPPER_THRESH),
|
|
C(SLOPE_TH),
|
|
C(SLOPE_DUR),
|
|
C(HYSTERESIS),
|
|
C(OVERSAMPLING),
|
|
C(FULL_SCALE),
|
|
C(OFFSET),
|
|
C(CALIB_TARGET),
|
|
C(CONFIGURATION),
|
|
C(CALIBRATION),
|
|
C(FEATURE_MASK),
|
|
C(ALERT),
|
|
C(FF_DUR),
|
|
C(BATCH_DURATION),
|
|
C(GAIN),
|
|
C(RESOLUTION),
|
|
#undef C
|
|
};
|
|
|
|
static MP_DEFINE_CONST_DICT(mp_module_zsensor_globals, mp_module_zsensor_globals_table);
|
|
|
|
const mp_obj_module_t mp_module_zsensor = {
|
|
.base = { &mp_type_module },
|
|
.globals = (mp_obj_dict_t *)&mp_module_zsensor_globals,
|
|
};
|
|
|
|
MP_REGISTER_MODULE(MP_QSTR_zsensor, mp_module_zsensor);
|
|
|
|
#endif // MICROPY_PY_HASHLIB
|