Skip to content

Commit 12b89ef

Browse files
committed
MLX90640: MicroPython module.
1 parent 8b53ab2 commit 12b89ef

File tree

7 files changed

+145
-1
lines changed

7 files changed

+145
-1
lines changed

drivers/mlx90640/mlx90640.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdint.h>
12
#include "src/headers/MLX90640_API.h"
23
#include "common/pimoroni_i2c.hpp"
34

@@ -8,13 +9,16 @@ void MLX90640_I2CConfigure(pimoroni::I2C *i2c_instance);
89
namespace pimoroni {
910
class MLX90640 {
1011
public:
12+
static const int WIDTH = 32;
13+
static const int HEIGHT = 24;
14+
1115
enum MLX90640_Error {
1216
OK = 0,
1317
INVALID_BAUDRATE = 1,
1418
INVALID_FPS = 2,
1519
};
1620

17-
float mlx90640To[768] = {0.0f};
21+
float mlx90640To[WIDTH * HEIGHT] = {0.0f};
1822
float emissivity = 1.0f;
1923
float reflected_temperature = 8.0f;
2024

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
add_library(usermod_mlx90640 INTERFACE)
2+
3+
target_sources(usermod_mlx90640 INTERFACE
4+
${CMAKE_CURRENT_LIST_DIR}/mlx90640.c
5+
${CMAKE_CURRENT_LIST_DIR}/mlx90640.cpp
6+
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/mlx90640/mlx90640.cpp
7+
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/mlx90640/src/functions/MLX90640_API.cpp
8+
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/mlx90640/MLX90640_RP2040_I2C_Driver.cpp
9+
)
10+
11+
target_include_directories(usermod_mlx90640 INTERFACE
12+
${CMAKE_CURRENT_LIST_DIR}
13+
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/mlx90640/
14+
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/mlx90640/src/headers/
15+
)
16+
17+
target_compile_definitions(usermod_mlx90640 INTERFACE
18+
MODULE_MLX90640_ENABLED=1
19+
)
20+
21+
target_link_libraries(usermod INTERFACE usermod_mlx90640)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "mlx90640.h"
2+
3+
4+
MP_DEFINE_CONST_FUN_OBJ_2(MLX90640_setup_obj, MLX90640_setup);
5+
MP_DEFINE_CONST_FUN_OBJ_1(MLX90640_get_frame_obj, MLX90640_get_frame);
6+
7+
STATIC const mp_rom_map_elem_t MLX90640_locals_dict_table[] = {
8+
{ MP_ROM_QSTR(MP_QSTR_setup), MP_ROM_PTR(&MLX90640_setup_obj) },
9+
{ MP_ROM_QSTR(MP_QSTR_get_frame), MP_ROM_PTR(&MLX90640_get_frame_obj) },
10+
};
11+
STATIC MP_DEFINE_CONST_DICT(MLX90640_locals_dict, MLX90640_locals_dict_table);
12+
13+
#ifdef MP_DEFINE_CONST_OBJ_TYPE
14+
MP_DEFINE_CONST_OBJ_TYPE(
15+
MLX90640_MLX90640_type,
16+
MP_QSTR_MLX90640,
17+
MP_TYPE_FLAG_NONE,
18+
make_new, MLX90640_make_new,
19+
locals_dict, (mp_obj_dict_t*)&MLX90640_locals_dict
20+
);
21+
#else
22+
const mp_obj_type_t MLX90640_MLX90640_type = {
23+
{ &mp_type_type },
24+
.name = MP_QSTR_MLX90640,
25+
.make_new = MLX90640_make_new,
26+
.locals_dict = (mp_obj_dict_t*)&MLX90640_locals_dict,
27+
};
28+
#endif
29+
30+
STATIC const mp_map_elem_t MLX90640_globals_table[] = {
31+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_msa301) },
32+
{ MP_OBJ_NEW_QSTR(MP_QSTR_MLX90640), (mp_obj_t)&MLX90640_type },
33+
};
34+
STATIC MP_DEFINE_CONST_DICT(mp_module_MLX90640_globals, MLX90640_globals_table);
35+
36+
const mp_obj_module_t MLX90640_user_cmodule = {
37+
.base = { &mp_type_module },
38+
.globals = (mp_obj_dict_t*)&mp_module_MLX90640_globals,
39+
};
40+
41+
#if MICROPY_VERSION <= 70144
42+
MP_REGISTER_MODULE(MP_QSTR_MLX90640, MLX90640_user_cmodule, MODULE_MLX90640_ENABLED);
43+
#else
44+
MP_REGISTER_MODULE(MP_QSTR_MLX90640, MLX90640_user_cmodule);
45+
#endif
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "drivers/mlx90640/mlx90640.hpp"
2+
3+
#include "micropython/modules/util.hpp"
4+
5+
using namespace pimoroni;
6+
7+
extern "C" {
8+
#include "mlx90640.h"
9+
#include "pimoroni_i2c.h"
10+
11+
typedef struct _ModMLX90640_obj_t {
12+
mp_obj_base_t base;
13+
MLX90640 *breakout;
14+
_PimoroniI2C_obj_t *i2c;
15+
int address;
16+
} ModMLX90640_obj_t;
17+
18+
mp_obj_t MLX90640_setup(mp_obj_t self_in, mp_obj_t fps) {
19+
_ModMLX90640_obj_t *self = MP_OBJ_TO_PTR2(self_in, _ModMLX90640_obj_t);
20+
21+
self->breakout->setup(mp_obj_get_int(fps));
22+
23+
return mp_const_none;
24+
}
25+
26+
mp_obj_t MLX90640_get_frame(mp_obj_t self_in) {
27+
_ModMLX90640_obj_t *self = MP_OBJ_TO_PTR2(self_in, _ModMLX90640_obj_t);
28+
29+
self->breakout->get_frame();
30+
31+
mp_obj_list_t *list = MP_OBJ_TO_PTR2(mp_obj_new_list(MLX90640::WIDTH * MLX90640::HEIGHT, NULL), mp_obj_list_t);
32+
33+
for(auto y = 0u; y < MLX90640::HEIGHT; y++) {
34+
for(auto x = 0u; x < MLX90640::WIDTH; x++) {
35+
int offset = y * MLX90640::WIDTH + x;
36+
float v = self->breakout->mlx90640To[offset];
37+
list->items[offset] = mp_obj_new_float(v);
38+
}
39+
}
40+
41+
return list;
42+
}
43+
44+
mp_obj_t MLX90640_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
45+
enum { ARG_i2c, ARG_address };
46+
static const mp_arg_t allowed_args[] = {
47+
{ MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} },
48+
{ MP_QSTR_address, MP_ARG_INT, {.u_int = MLX90640_DEFAULT_I2C_ADDRESS}}
49+
};
50+
51+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
52+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
53+
54+
_ModMLX90640_obj_t *self = m_new_obj_with_finaliser(_ModMLX90640_obj_t);
55+
self->base.type = &MLX90640_type;
56+
57+
self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj);
58+
self->address = args[ARG_address].u_int;
59+
60+
self->breakout = m_new_class(MLX90640, (pimoroni::I2C *)(self->i2c->i2c), self->address);
61+
62+
return self;
63+
}
64+
65+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "py/runtime.h"
2+
3+
extern const mp_obj_type_t MLX90640_type;
4+
5+
extern mp_obj_t MLX90640_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
6+
extern mp_obj_t MLX90640_get_frame(mp_obj_t self_in);
7+
extern mp_obj_t MLX90640_setup(mp_obj_t self_in, mp_obj_t fps_in);

micropython/modules/micropython-common.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ include(breakout_bmp280/micropython)
2222
include(breakout_icp10125/micropython)
2323
include(breakout_scd41/micropython)
2424
include(breakout_vl53l5cx/micropython)
25+
include(breakout_mlx90640/micropython)
2526

2627
include(pico_scroll/micropython)
2728
include(pico_rgb_keypad/micropython)

micropython/modules/micropython-picow.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ include(breakout_icp10125/micropython)
4040
include(breakout_scd41/micropython)
4141
include(breakout_vl53l5cx/micropython)
4242
include(pcf85063a/micropython)
43+
include(breakout_mlx90640/micropython)
4344

4445
# Utility
4546
include(adcfft/micropython)

0 commit comments

Comments
 (0)