kWhCounter.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "freertos/queue.h"
  4. #include "driver/ledc.h"
  5. #include "driver/pcnt.h"
  6. #include "esp_attr.h"
  7. #include "esp_log.h"
  8. #include "kWhCounter.h"
  9. #ifdef ENABLE_KWH_COUNTER
  10. uint32_t kWh_cnt = 0;
  11. void counterControlTask(void *pvParameters) {
  12. ESP_LOGI("kWhCounter", "counterControlTask starting. Core:%d",xPortGetCoreID());
  13. uint32_t state = 0;
  14. TickType_t vLastWakeTime = xTaskGetTickCount();
  15. const TickType_t xFreq = 100 / portTICK_PERIOD_MS;
  16. while( 1 ) {
  17. for(uint8_t i=10; i>0; i-- ) {
  18. vTaskDelayUntil( &vLastWakeTime, xFreq );
  19. if( gpio_get_level(KWH_COUNTER_INPUT_IO) == 1 ) state++;
  20. vTaskDelay(100 / portTICK_PERIOD_MS);
  21. }
  22. if( state > 0 ) kWh_cnt++;
  23. ESP_LOGI("kWhCounter", "Measure. Pin: %u Tot:%u", state, kWh_cnt);
  24. state = 0;
  25. }
  26. }
  27. void kWhCounter_init()
  28. {
  29. ESP_LOGI("kWhCounter", "kWhCounter_init()");
  30. gpio_set_direction(KWH_COUNTER_INPUT_IO, GPIO_MODE_INPUT);
  31. xTaskCreate(counterControlTask, "counterControlTask", 1024*10, NULL, 2, NULL);
  32. }
  33. #endif