receiver.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "freertos/queue.h"
  9. #include "driver/gpio.h"
  10. #include "esp_sleep.h"
  11. #include "esp_log.h"
  12. #include "driver/uart.h"
  13. #include "driver/rtc_io.h"
  14. #include "esp_intr_alloc.h"
  15. #include "esp_attr.h"
  16. #include "driver/timer.h"
  17. #include <stddef.h>
  18. #include "esp_intr_alloc.h"
  19. #include "receiver.h"
  20. #include "rxTimer.h"
  21. #include "toshiba_ir.h"
  22. #include "wifi.h"
  23. #include "mqtt.h"
  24. #ifdef IR_RECEIVER
  25. uint32_t dataArr[1000];
  26. uint32_t dataArrIdx = 0;
  27. uint8_t bitReverse(uint8_t b) {
  28. b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
  29. b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
  30. b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
  31. return b;
  32. }
  33. void receiverTask(void *pvParameter)
  34. {
  35. ESP_LOGI("RX", "Receiver task starting.");
  36. uint32_t width;
  37. while (1) {
  38. // Here we receive every individual pulse that has been detected by the isr-pulse-rx-function: timer_tg0_isr
  39. while( xQueueReceive( rxQueue, &width, 100 ) == pdTRUE ) {
  40. //ESP_LOGI("D", "%u", width); // Debug to show every received pulse as a separate log line
  41. // Receive the Toshiba IR
  42. // Send the pulse to the receiver that tries to interpret the pulse
  43. union ToshibaProtocolU* data = (union ToshibaProtocolU*)nextPulseToshiba_ir(width);
  44. if( data != NULL ) {
  45. char mqtt_s[100];
  46. sprintf(mqtt_s,"A %02X%02X%02X%02X%02X%02X%02X%02X",data->raw[0],data->raw[1],data->raw[2],data->raw[3],data->raw[4],data->raw[5],data->raw[6],data->raw[7]);
  47. sprintf(mqtt_s,"B %02X%02X%02X%02X%02X%02X%02X%02X",data->raw[8],data->raw[9],data->raw[10],data->raw[11],data->raw[12],data->raw[13],data->raw[14],data->raw[15]);
  48. sprintf(mqtt_s,"C %02X%02X%02X",data->raw[16],data->raw[17],data->raw[18]);
  49. //ESP_LOGI("TOSHIBA","MQTT: %s", mqtt_s);
  50. #ifdef MQTT_ENABLED
  51. sendMQTTMessage("hall/ac/ir_cmd_rx", mqtt_s);
  52. #endif
  53. ESP_LOGI("TOSHIBA", "Data A: %02X%02X%02X%02X%02X%02X%02X%02X",data->raw[0],data->raw[1],data->raw[2],data->raw[3],data->raw[4],data->raw[5],data->raw[6],data->raw[7]);
  54. ESP_LOGI("TOSHIBA", "Data B: %02X%02X%02X%02X%02X%02X%02X%02X",data->raw[8],data->raw[9],data->raw[10],data->raw[11],data->raw[12],data->raw[13],data->raw[14],data->raw[15]);
  55. ESP_LOGI("TOSHIBA", "Data C: %02X%02X%02X",data->raw[16],data->raw[17],data->raw[18]);
  56. const uint8_t tempBits = data->raw[6] & 0x1F;
  57. float setTemp = 16.0f + (tempBits>>1);
  58. if( tempBits & 0x01 ) setTemp += 0.5f;
  59. //ESP_LOGI("TOSHIBA", "Temp: %.1f", setTemp );
  60. const uint8_t fan = data->raw[8]>>4;
  61. const uint8_t power = data->raw[5]&0x01;
  62. ESP_LOGI("TOSHIBA","Power:%u Temp:%.1f Fan:%u",power,setTemp,fan);
  63. #ifdef WIFI_ENABLED
  64. //sendUDPMessage(dataStr, true);
  65. #endif
  66. }
  67. }
  68. }
  69. vTaskDelete(NULL);
  70. }
  71. void initReceiver() {
  72. gpio_pad_select_gpio(GPIO_IR_RX_DATA);
  73. gpio_set_direction(GPIO_IR_RX_DATA, GPIO_MODE_INPUT);
  74. gpio_pad_select_gpio(GPIO_IR_TX_DATA);
  75. gpio_set_direction(GPIO_IR_TX_DATA, GPIO_MODE_OUTPUT);
  76. gpio_set_level(GPIO_IR_TX_DATA, 0);
  77. Toshiba_ir_ResetDecoder();
  78. ESP_LOGI("RX", "Receiver has been initialized.");
  79. xTaskCreatePinnedToCore(&receiverTask, "receiverTask", 8192, NULL, tskIDLE_PRIORITY + 5, NULL, 0);
  80. }
  81. #endif