#include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" #include "driver/ledc.h" #include "driver/pcnt.h" #include "esp_attr.h" #include "esp_log.h" #include "kWhCounter.h" #ifdef ENABLE_KWH_COUNTER uint32_t kWh_cnt = 0; void counterControlTask(void *pvParameters) { ESP_LOGI("kWhCounter", "counterControlTask starting. Core:%d",xPortGetCoreID()); uint32_t state = 0; TickType_t vLastWakeTime = xTaskGetTickCount(); const TickType_t xFreq = 100 / portTICK_PERIOD_MS; while( 1 ) { for(uint8_t i=10; i>0; i-- ) { vTaskDelayUntil( &vLastWakeTime, xFreq ); if( gpio_get_level(KWH_COUNTER_INPUT_IO) == 1 ) state++; vTaskDelay(100 / portTICK_PERIOD_MS); } if( state > 0 ) kWh_cnt++; ESP_LOGI("kWhCounter", "Measure. Pin: %u Tot:%u", state, kWh_cnt); state = 0; } } void kWhCounter_init() { ESP_LOGI("kWhCounter", "kWhCounter_init()"); gpio_set_direction(KWH_COUNTER_INPUT_IO, GPIO_MODE_INPUT); xTaskCreate(counterControlTask, "counterControlTask", 1024*10, NULL, 2, NULL); } #endif