From 5a7cc7c9c7cc4b2ebac1fcfdc26f4dc88773391b Mon Sep 17 00:00:00 2001 From: schneider <schneider@blinkenlichts.net> Date: Sat, 17 Oct 2020 02:14:17 +0200 Subject: [PATCH] change(spo2): Allocate buffers on heap --- pycardium/modules/spo2_algo.c | 23 +++++++++++++++++------ pycardium/mpconfigport.h | 1 + 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/pycardium/modules/spo2_algo.c b/pycardium/modules/spo2_algo.c index 04a6f2c32..27daa2e30 100644 --- a/pycardium/modules/spo2_algo.c +++ b/pycardium/modules/spo2_algo.c @@ -5,10 +5,13 @@ #include "py/obj.h" #include "py/runtime.h" -static mp_obj_t mp_maxim_rd177(mp_obj_t ir, mp_obj_t red) -{ +struct spo2_memory { uint32_t pun_ir_buffer[500]; uint32_t pun_red_buffer[500]; +}; + +static mp_obj_t mp_maxim_rd177(mp_obj_t ir, mp_obj_t red) +{ int32_t n_ir_buffer_length; int32_t pn_spo2 = 0; @@ -39,16 +42,24 @@ static mp_obj_t mp_maxim_rd177(mp_obj_t ir, mp_obj_t red) ); } + struct spo2_memory *m = + (struct spo2_memory *)MP_STATE_PORT(spo2_memory); + if (!m) { + /* Will raise an exception if out of memory: */ + m = m_malloc(sizeof(struct spo2_memory)); + MP_STATE_PORT(spo2_memory) = m; + } + n_ir_buffer_length = ir_len; for (size_t i = 0; i < ir_len; i++) { - pun_ir_buffer[i] = mp_obj_get_int(ir_elem[i]); - pun_red_buffer[i] = mp_obj_get_int(red_elem[i]); + m->pun_ir_buffer[i] = mp_obj_get_int(ir_elem[i]); + m->pun_red_buffer[i] = mp_obj_get_int(red_elem[i]); } maxim_heart_rate_and_oxygen_saturation( - pun_ir_buffer, + m->pun_ir_buffer, n_ir_buffer_length, - pun_red_buffer, + m->pun_red_buffer, &pn_spo2, &pch_spo2_valid, &pn_heart_rate, diff --git a/pycardium/mpconfigport.h b/pycardium/mpconfigport.h index c9f687ac4..57bf8ed80 100644 --- a/pycardium/mpconfigport.h +++ b/pycardium/mpconfigport.h @@ -114,4 +114,5 @@ typedef long mp_off_t; #define MICROPY_PORT_ROOT_POINTERS \ const char *readline_hist[16]; \ mp_obj_t interrupt_callbacks[EPIC_INT_NUM]; \ + void *spo2_memory; \ -- GitLab