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
| #include "py/obj.h"
#include "py/runtime.h"
#include "esp_log.h"
#include "driver/temperature_sensor.h"
// 定义一个全局变量来保存句柄
static temperature_sensor_handle_t tsens = NULL;
static const char *TAG = "sensorcoretemper";
// 函数 初始化 并启动
STATIC mp_obj_t sensorcoretemper_init(void) {
ESP_LOGI(TAG, "Install temperature sensor, expected temp ranger range: 10~50 ℃");
temperature_sensor_config_t temp_sensor = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
//ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor));
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &tsens)); // 注意这里的改动
ESP_LOGI(TAG, "Enable temperature sensor");
//ESP_ERROR_CHECK(temperature_sensor_enable());
ESP_ERROR_CHECK(temperature_sensor_enable(tsens)); // 注意这里的改动
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(sensorcoretemper_init_obj, sensorcoretemper_init);
// 函数 获取
STATIC mp_obj_t sensorcoretemper_getT(void) {
ESP_LOGI(TAG, "Read temperature");
float tsens_value;
//ESP_ERROR_CHECK(temperature_sensor_get_celsius(&tsens_value));
ESP_ERROR_CHECK(temperature_sensor_get_celsius(tsens, &tsens_value));
ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);
return mp_obj_new_float(tsens_value);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(sensorcoretemper_getT_obj, sensorcoretemper_getT);
// 函数 停止
STATIC mp_obj_t sensorcoretemper_stop(void) {
//ESP_ERROR_CHECK(temperature_sensor_disable());
ESP_ERROR_CHECK(temperature_sensor_disable(tsens)); // 注意这里的改动
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(sensorcoretemper_stop_obj, sensorcoretemper_stop);
STATIC const mp_rom_map_elem_t sensorcoretemper_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sensorcoretemper) },
{ MP_ROM_QSTR(MP_QSTR_getT), MP_ROM_PTR(&sensorcoretemper_getT_obj) },
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&sensorcoretemper_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&sensorcoretemper_stop_obj) },
};
STATIC MP_DEFINE_CONST_DICT(sensorcoretemper_module_globals, sensorcoretemper_module_globals_table);
const mp_obj_module_t sensorcoretemper_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&sensorcoretemper_module_globals,
};
//MP_REGISTER_MODULE(MP_QSTR_sensorcoretemper, sensorcoretemper_user_cmodule, MODULE_sensorcoretemper_ENABLED);
MP_REGISTER_MODULE(MP_QSTR_sensorcoretemper, sensorcoretemper_user_cmodule);
|