123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #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 logCnt = 0;
-
- 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++;
- if( ++logCnt == 10 ) {
- ESP_LOGI("kWhCounter", "Measure. Pin: %u Tot:%u", state, kWh_cnt);
- logCnt = 0;
- }
- 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
|