diff --git a/pycardium/modules/spo2_algo.c b/pycardium/modules/spo2_algo.c index 04a6f2c3276dfcd4327740854ef8722762809595..27daa2e30ce9f787d835e6fd3fa2c95c4a76c6d9 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 c9f687ac4069db2800364d35b8c130e67259572e..57bf8ed80b94bd86646bdc7e29ca3c17a9ef8d32 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; \