diff --git a/epicardium/modules/light_sensor.c b/epicardium/modules/light_sensor.c index 41b0682a7714a5ff10fa6a1922f24f007d36ae51..4ba82bb203e505bbd1111dcb5257b0e6e97a71b5 100644 --- a/epicardium/modules/light_sensor.c +++ b/epicardium/modules/light_sensor.c @@ -14,48 +14,45 @@ static StaticTimer_t poll_timer_buffer; int epic_light_sensor_init() { - const sys_cfg_adc_t sys_adc_cfg = NULL; /* No system specific configuration needed. */ + const sys_cfg_adc_t sys_adc_cfg = + NULL; /* No system specific configuration needed. */ if (ADC_Init(0x9, &sys_adc_cfg) != E_NO_ERROR) { - return -EINVAL; + return -EINVAL; } GPIO_Config(&gpio_cfg_adc7); - return 0; + return 0; } void readAdcCallback() { - ADC_StartConvert(ADC_CH_7, 0, 0); - ADC_GetData(&last_value); + ADC_StartConvert(ADC_CH_7, 0, 0); + ADC_GetData(&last_value); } int epic_light_sensor_run() { epic_light_sensor_init(); - if (!poll_timer) - { - poll_timer = xTimerCreateStatic( - "light_sensor_adc", - READ_FREQ, - pdTRUE, - NULL, - readAdcCallback, - &poll_timer_buffer - ); - // since &poll_timer_buffer is not NULL, xTimerCreateStatic should allways succeed, so - // we don't need to check for poll_timer being NULL. - - } - if (xTimerIsTimerActive(poll_timer) == pdTRUE) - { - return -EALREADY; - } else { - if (xTimerStart(poll_timer, 0) != pdPASS) - { - return -EBUSY; - } - return 0; - } + if (!poll_timer) { + poll_timer = xTimerCreateStatic( + "light_sensor_adc", + READ_FREQ, + pdTRUE, + NULL, + readAdcCallback, + &poll_timer_buffer + ); + // since &poll_timer_buffer is not NULL, xTimerCreateStatic should allways succeed, so + // we don't need to check for poll_timer being NULL. + } + if (xTimerIsTimerActive(poll_timer) == pdTRUE) { + return -EALREADY; + } else { + if (xTimerStart(poll_timer, 0) != pdPASS) { + return -EBUSY; + } + return 0; + } } int epic_light_sensor_stop() @@ -64,19 +61,17 @@ int epic_light_sensor_stop() return -EINVAL; } - if (xTimerStop(poll_timer, 0) != pdPASS) - { - return -EBUSY; - } - return 0; + if (xTimerStop(poll_timer, 0) != pdPASS) { + return -EBUSY; + } + return 0; } -int epic_light_sensor_get(uint16_t* value) +int epic_light_sensor_get(uint16_t *value) { - if (!poll_timer || !xTimerIsTimerActive(poll_timer)) - { - return -ENODATA; - } + if (!poll_timer || !xTimerIsTimerActive(poll_timer)) { + return -ENODATA; + } *value = last_value; - return 0; + return 0; } diff --git a/pycardium/modules/light_sensor.c b/pycardium/modules/light_sensor.c index 2aac7ebffe5a0dc928e8af9d4ebe2f73da64e675..39f612a91a87946f426b7b1292121b0d0518a9dd 100644 --- a/pycardium/modules/light_sensor.c +++ b/pycardium/modules/light_sensor.c @@ -5,50 +5,55 @@ STATIC mp_obj_t mp_light_sensor_start() { - int status = epic_light_sensor_run(); - if (status == -EBUSY) - { - mp_raise_msg(&mp_type_RuntimeError, "timer could not be scheduled"); - } - return mp_const_none; + int status = epic_light_sensor_run(); + if (status == -EBUSY) { + mp_raise_msg( + &mp_type_RuntimeError, "timer could not be scheduled" + ); + } + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(light_sensor_start_obj, mp_light_sensor_start); STATIC mp_obj_t mp_light_sensor_get_reading() { - uint16_t last; - int status = epic_light_sensor_get(&last); - if (status == -ENODATA) - { - mp_raise_ValueError("sensor not running"); - return mp_const_none; - } - return mp_obj_new_int_from_uint(last); + uint16_t last; + int status = epic_light_sensor_get(&last); + if (status == -ENODATA) { + mp_raise_ValueError("sensor not running"); + return mp_const_none; + } + return mp_obj_new_int_from_uint(last); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(light_sensor_get_obj, mp_light_sensor_get_reading); +STATIC MP_DEFINE_CONST_FUN_OBJ_0( + light_sensor_get_obj, mp_light_sensor_get_reading +); STATIC mp_obj_t mp_light_sensor_stop() { - int status = epic_light_sensor_stop(); - if (status == -EBUSY) - { - mp_raise_msg(&mp_type_RuntimeError, "timer could not be stopped"); - } - return mp_const_none; + int status = epic_light_sensor_stop(); + if (status == -EBUSY) { + mp_raise_msg( + &mp_type_RuntimeError, "timer could not be stopped" + ); + } + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(light_sensor_stop_obj, mp_light_sensor_stop); STATIC const mp_rom_map_elem_t light_sensor_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_light_sensor) }, - { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&light_sensor_start_obj) }, - { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&light_sensor_stop_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_reading), MP_ROM_PTR(&light_sensor_get_obj) } + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_light_sensor) }, + { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&light_sensor_start_obj) }, + { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&light_sensor_stop_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_reading), MP_ROM_PTR(&light_sensor_get_obj) } }; -STATIC MP_DEFINE_CONST_DICT(light_sensor_module_globals, light_sensor_module_globals_table); +STATIC MP_DEFINE_CONST_DICT( + light_sensor_module_globals, light_sensor_module_globals_table +); const mp_obj_module_t light_sensor_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t *)&light_sensor_module_globals, + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&light_sensor_module_globals, }; /* clang-format off */