diff --git a/main/pumps.c b/main/pumps.c index 414fa62..bd5356d 100644 --- a/main/pumps.c +++ b/main/pumps.c @@ -108,7 +108,7 @@ uint8_t pump_disable(int8_t i) { } void pump_timer_done(TimerHandle_t timer) { - uint8_t idx = (pcTimerGetName(timer) - 0x48); + uint8_t idx = (uint8_t)(pcTimerGetName(timer) - 0x48); ESP_LOGD(TAG, "pump done: %d", idx); xQueueSend(pump_step_queue, &idx, portMAX_DELAY); @@ -147,44 +147,48 @@ void pumps_run() { void pump_step_task() { uint8_t idx; - BaseType_t awoke = pdFALSE; + TickType_t awoke = pdFALSE; ESP_LOGI(TAG, "Starting pump stepping task"); - while(xQueueReceive(pump_step_queue, &idx, &awoke)) { - ESP_LOGI(TAG, "step %d complete for %d", pump_states[idx], idx); + while(true) { + while(xQueueReceive(pump_step_queue, &idx, awoke)) { + ESP_LOGI(TAG, "step %d complete for %d", pump_states[idx], idx); - if(pump_states[idx] == PUMP_STATE_RAMPING_UP) { - uint16_t pump_time = pumps_duration[idx] * 250 - (RAMP_UP_TIME + RAMP_DOWN_TIME) / 2; + if(pump_states[idx] == PUMP_STATE_RAMPING_UP) { + uint16_t pump_time = pumps_duration[idx] * 250 - (RAMP_UP_TIME + RAMP_DOWN_TIME) / 2; - if(RAMP_UP_TIME + RAMP_DOWN_TIME / 2 > pumps_duration[idx] * 250) { - ESP_LOGW(TAG, "Pump %d total ramp time great that duration, running for 1 Tick", idx); + if(RAMP_UP_TIME + RAMP_DOWN_TIME / 2 > pumps_duration[idx] * 250) { + ESP_LOGW(TAG, "Pump %d total ramp time great that duration, running for 1 Tick", idx); - pump_time = 10; + pump_time = 10; + } + + ESP_LOGI(TAG, "Running pump %d for %dms", idx, pump_time); + + xTimerChangePeriod(pump_timers[idx], pump_time / portTICK_PERIOD_MS, portMAX_DELAY); + xTimerStart(pump_timers[idx], portMAX_DELAY); + + pump_states[idx] = PUMP_STATE_RUNNING; + + continue; + } else if(pump_states[idx] == PUMP_STATE_RUNNING) { + ledc_set_fade_with_time(ledc_motor_channels[idx].speed_mode, ledc_motor_channels[idx].channel, 0, RAMP_UP_TIME); + ledc_fade_start(ledc_motor_channels[idx].speed_mode, ledc_motor_channels[idx].channel, LEDC_FADE_NO_WAIT); + + pump_states[idx] = PUMP_STATE_RAMPING_DOWN; + continue; + } else if(pump_states[idx] == PUMP_STATE_RAMPING_DOWN) { + ESP_LOGI(TAG, "Pump cycles complete: %d", idx); + pump_states[idx] = PUMP_STATE_IDLE; + + running--; + } else { + ESP_LOGE(TAG, "Pump step fired with unknown state: %d", pump_states[idx]); } - - ESP_LOGI(TAG, "Running pump %d for %dms", idx, pump_time); - - xTimerChangePeriod(pump_timers[idx], pump_time / portTICK_PERIOD_MS, portMAX_DELAY); - xTimerStart(pump_timers[idx], portMAX_DELAY); - - pump_states[idx] = PUMP_STATE_RUNNING; - - continue; - } else if(pump_states[idx] == PUMP_STATE_RUNNING) { - ledc_set_fade_with_time(ledc_motor_channels[idx].speed_mode, ledc_motor_channels[idx].channel, 0, RAMP_UP_TIME); - ledc_fade_start(ledc_motor_channels[idx].speed_mode, ledc_motor_channels[idx].channel, LEDC_FADE_NO_WAIT); - - pump_states[idx] = PUMP_STATE_RAMPING_DOWN; - continue; - } else if(pump_states[idx] == PUMP_STATE_RAMPING_DOWN) { - ESP_LOGI(TAG, "Pump cycles complete: %d", idx); - pump_states[idx] = PUMP_STATE_IDLE; - - running--; - } else { - ESP_LOGE(TAG, "Pump step fired with unknown state: %d", pump_states[idx]); } + + vTaskDelay(portTICK_PERIOD_MS / 10); } }