diff --git a/pycardium/modules/spo2_algo.c b/pycardium/modules/spo2_algo.c index 04a6f2c3276dfcd4327740854ef8722762809595..4b841a706eca6a41d1f2fbb15e9b678a17726ef3 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" +struct spo2_memory { + uint32_t pun_ir_buffer[5000000]; + uint32_t pun_red_buffer[500]; +}; + static mp_obj_t mp_maxim_rd177(mp_obj_t ir, mp_obj_t red) { - uint32_t pun_ir_buffer[500]; - uint32_t pun_red_buffer[500]; 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: */ + struct spo2_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 ea40190091cd882a810021320f3627d50506910e..7d2fa006e559022e29458fca1bec12300937542c 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; \