diff --git a/Core/Inc/pse_stepper_planer.h b/Core/Inc/pse_stepper_planer.h index 28c479c..ff022c9 100644 --- a/Core/Inc/pse_stepper_planer.h +++ b/Core/Inc/pse_stepper_planer.h @@ -22,6 +22,8 @@ void pse_stepper_planer_tick(pse_unit* units, uint8_t units_num); void pse_sp_start_all(pse_unit* units, int unit_num); void pse_sp_stop_all(pse_unit* units, int unit_num); +void pse_sp_set_timer(TIM_HandleTypeDef* tim); + void pse_stepper_planer_compute_sps(pse_unit* unit); #endif /* INC_PSE_STEPPER_PLANER_H_ */ diff --git a/Core/Src/home_screen.c b/Core/Src/home_screen.c index 6428e80..c3d565e 100644 --- a/Core/Src/home_screen.c +++ b/Core/Src/home_screen.c @@ -47,11 +47,16 @@ static void unit_widget_enabled_handler(lv_event_t * e){ lv_obj_t* button = lv_event_get_current_target(e); lv_obj_t* label = lv_obj_get_child(button, 0); lv_state_t state = lv_obj_get_state(button); + + + pse_unit* unit = lv_event_get_user_data(e); if(state & LV_STATE_CHECKED){ - lv_label_set_text(label, LV_SYMBOL_CLOSE); + lv_label_set_text(label, LV_SYMBOL_OK); + unit->enabled = 0; } else{ - lv_label_set_text(label, LV_SYMBOL_OK); + lv_label_set_text(label, LV_SYMBOL_CLOSE); + unit->enabled = 1; } } } @@ -96,11 +101,11 @@ static lv_obj_t* PSE_unit_widget(lv_obj_t* parent, pse_unit* pse_unit){ lv_obj_t* enabled = lv_btn_create(cont); lv_obj_t* enabled_label = lv_label_create(enabled); lv_obj_set_width(enabled, lv_pct(100)); - lv_obj_add_flag(enabled, LV_OBJ_FLAG_CHECKABLE); - lv_obj_add_event_cb(enabled, unit_widget_enabled_handler, LV_EVENT_ALL, NULL); + lv_obj_add_flag(enabled, LV_OBJ_FLAG_CHECKABLE); + lv_obj_add_event_cb(enabled, unit_widget_enabled_handler, LV_EVENT_ALL, pse_unit); lv_obj_set_flex_grow(enabled, 1); lv_obj_center(enabled_label); - lv_label_set_text(enabled_label, LV_SYMBOL_OK); + lv_label_set_text(enabled_label, LV_SYMBOL_CLOSE); if(!pse_unit->enabled){ lv_obj_add_state(enabled, LV_STATE_CHECKED); lv_event_send(enabled, LV_EVENT_VALUE_CHANGED, NULL); diff --git a/Core/Src/main.c b/Core/Src/main.c index 4bbc6a7..c36cfd2 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -256,7 +256,7 @@ int main(void) ADS7843_Init(&Touch_Def); load_units(pse_units, pse_syringes, pse_stepper_confs, PSE_UNITS_NUM); - HAL_TIM_Base_Start_IT(&htim4); + pse_sp_set_timer(&htim4); Home_Screen_Gen(pse_units, PSE_UNITS_NUM); /* USER CODE END 2 */ diff --git a/Core/Src/pse_stepper_planer.c b/Core/Src/pse_stepper_planer.c index a60b828..e9afc41 100644 --- a/Core/Src/pse_stepper_planer.c +++ b/Core/Src/pse_stepper_planer.c @@ -11,6 +11,9 @@ #include "pse_stepper_planer.h" +static int units_running = 0; // counter of currently running units, to disable interrupts when unnecessary +static TIM_HandleTypeDef* htim; // main interrupt timer handle + void pse_stepper_planer_tick(pse_unit* units, uint8_t units_num){ for(int i = 0; i < units_num; i++){ pse_stepper_conf* c = units[i].stepper_conf; @@ -30,9 +33,20 @@ void pse_stepper_planer_tick(pse_unit* units, uint8_t units_num){ } void pse_sp_start_axis(pse_stepper_conf* conf){ + if(HAL_GPIO_ReadPin(conf->EN_GPIO_Port, conf->EN_GPIO_Pin) == GPIO_PIN_SET){ + if(units_running == 0) + HAL_TIM_Base_Start_IT(htim); + units_running++; + } + HAL_GPIO_WritePin(conf->EN_GPIO_Port, conf->EN_GPIO_Pin, GPIO_PIN_RESET); } void pse_sp_stop_axis(pse_stepper_conf* conf){ + if(HAL_GPIO_ReadPin(conf->EN_GPIO_Port, conf->EN_GPIO_Pin) == GPIO_PIN_RESET){ + units_running--; + if(units_running == 0) + HAL_TIM_Base_Stop_IT(htim); + } HAL_GPIO_WritePin(conf->EN_GPIO_Port, conf->EN_GPIO_Pin, GPIO_PIN_SET); } @@ -51,14 +65,24 @@ void pse_stepper_planer_compute_sps(pse_unit* unit){ void pse_sp_start_all(pse_unit* units, int units_num){ + HAL_TIM_Base_Start_IT(htim); + units_running = units_num; + for(int i = 0; i < units_num; i++){ if(units[i].enabled) pse_sp_start_axis(units[i].stepper_conf); } } void pse_sp_stop_all(pse_unit* units, int units_num){ + HAL_TIM_Base_Stop_IT(htim); + units_running = 0; + for(int i = 0; i < units_num; i++){ if(units[i].enabled) pse_sp_stop_axis(units[i].stepper_conf); } } + +void pse_sp_set_timer(TIM_HandleTypeDef* tim){ + htim = tim; +}