1
0

ir_transmit.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "freertos/queue.h"
  4. #include "esp_log.h"
  5. #include "driver/rmt.h"
  6. #include "toshiba_ir.h"
  7. #include "config.h"
  8. // RMT values
  9. #define RMT_TX_CHANNEL RMT_CHANNEL_0
  10. #define RMT_TX_GPIO GPIO_NUM_26
  11. // channel clock period = 1 uS
  12. #define RMT_CLK_DIV 80
  13. // Variable that holds the IR protocol transmission. 1 start bit, 72 data bits and one stop bit
  14. rmt_item32_t toshiba_rmt[1 + 72 + 1 + 1];
  15. QueueHandle_t toshibaTxQueue = NULL;
  16. void toshibaTxTask(void *pvParameter)
  17. {
  18. ESP_LOGI("TX", "toshibaTxTask() starting.");
  19. uint8_t data[8];
  20. while( true ) {
  21. if( xQueueReceive( toshibaTxQueue, data, 100 ) == pdTRUE ) {
  22. ESP_LOGI("TOSHIBA","Received a TX from MQTT");
  23. uint8_t irPair = 1;
  24. for(uint8_t b=0;b<kToshibaNumberOfBits;b++) {
  25. uint8_t byteNo = b / 8;
  26. uint8_t shiftBits = b % 8;
  27. uint8_t bit_data = (1u << (7-shiftBits)) & data[byteNo];
  28. uint32_t space = bit_data ? kToshibaAcOneSpace : kToshibaAcZeroSpace;
  29. toshiba_rmt[irPair].val = (kToshibaAcBitMark << 0) | (1 << 15) | (space << 16); // Header of IR Transmit
  30. irPair++;
  31. }
  32. ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, &(toshiba_rmt[0]), 1+72+1, true));
  33. }
  34. }
  35. }
  36. void initIrTransmit() {
  37. rmt_config_t rmt_tx;
  38. rmt_tx.rmt_mode = RMT_MODE_TX;
  39. rmt_tx.channel = RMT_TX_CHANNEL;
  40. rmt_tx.gpio_num = GPIO_IR_TX_DATA;
  41. rmt_tx.mem_block_num = 1;
  42. rmt_tx.clk_div = RMT_CLK_DIV;
  43. rmt_tx.tx_config.loop_en = false;
  44. rmt_tx.tx_config.carrier_duty_percent = 30;
  45. rmt_tx.tx_config.carrier_freq_hz = 38000;
  46. rmt_tx.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
  47. rmt_tx.tx_config.carrier_en = true;
  48. rmt_tx.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
  49. rmt_tx.tx_config.idle_output_en = true;
  50. rmt_tx.flags = 0;
  51. ESP_ERROR_CHECK( rmt_config(&rmt_tx) );
  52. ESP_ERROR_CHECK( rmt_driver_install(rmt_tx.channel, 0, 0) );
  53. // Init the ir data field
  54. toshiba_rmt[0].val = (kToshibaAcHdrMark << 0) | (1 << 15) | (kToshibaAcHdrSpace << 16); // Header of IR Transmit
  55. toshiba_rmt[73].val = (kToshibaAcBitMark << 0) | (1 << 15) | (kToshibaAcZeroSpace << 16); // Header of IR Transmit
  56. toshiba_rmt[74].val = (1 << 16); // End marker
  57. // 0x47503FC
  58. // 0x1FE823A
  59. for(int i=1;i<73;i++) {
  60. toshiba_rmt[i].val = (kToshibaAcBitMark << 0) | (1 << 15) | (kToshibaAcZeroSpace << 16); // Header of IR Transmit
  61. }
  62. toshibaTxQueue = xQueueCreate( 5, kToshibaNumberOfBytes );
  63. xTaskCreatePinnedToCore(toshibaTxTask, "toshibaTxTask", 1024*10, NULL, 2, NULL,0);
  64. ESP_LOGI("IR_TX","Init done.");
  65. }