12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/queue.h"
- #include "esp_log.h"
- #include "driver/rmt.h"
- #include "toshiba_ir.h"
- #include "config.h"
- // RMT values
- #define RMT_TX_CHANNEL RMT_CHANNEL_0
- #define RMT_TX_GPIO GPIO_NUM_26
- // channel clock period = 1 uS
- #define RMT_CLK_DIV 80
- // Variable that holds the IR protocol transmission. 1 start bit, 72 data bits and one stop bit
- rmt_item32_t toshiba_rmt[1 + 72 + 1 + 1];
- QueueHandle_t toshibaTxQueue = NULL;
- void toshibaTxTask(void *pvParameter)
- {
- ESP_LOGI("IR_TRANSMIT", "toshibaTxTask() starting.");
- uint8_t data[8];
-
- while( true ) {
- if( xQueueReceive( toshibaTxQueue, data, 100 ) == pdTRUE ) {
- ESP_LOGI("IR_TRANSMIT","Received a TX from MQTT");
- uint8_t irPair = 1;
- for(uint8_t b=0;b<kToshibaNumberOfBits;b++) {
-
- uint8_t byteNo = b / 8;
- uint8_t shiftBits = b % 8;
- uint8_t bit_data = (1u << (7-shiftBits)) & data[byteNo];
- uint32_t space = bit_data ? kToshibaAcOneSpace : kToshibaAcZeroSpace;
- toshiba_rmt[irPair].val = (kToshibaAcBitMark << 0) | (1 << 15) | (space << 16); // Header of IR Transmit
- irPair++;
- }
- ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, &(toshiba_rmt[0]), 1+72+1, true));
- }
- }
- }
- void initIrTransmit() {
- rmt_config_t rmt_tx;
- rmt_tx.rmt_mode = RMT_MODE_TX;
- rmt_tx.channel = RMT_TX_CHANNEL;
- rmt_tx.gpio_num = GPIO_IR_TX_DATA;
- rmt_tx.mem_block_num = 1;
- rmt_tx.clk_div = RMT_CLK_DIV;
- rmt_tx.tx_config.loop_en = false;
- rmt_tx.tx_config.carrier_duty_percent = 30;
- rmt_tx.tx_config.carrier_freq_hz = 38000;
- rmt_tx.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
- rmt_tx.tx_config.carrier_en = true;
- rmt_tx.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
- rmt_tx.tx_config.idle_output_en = true;
- rmt_tx.flags = 0;
-
- ESP_ERROR_CHECK( rmt_config(&rmt_tx) );
- ESP_ERROR_CHECK( rmt_driver_install(rmt_tx.channel, 0, 0) );
- // Init the ir data field
- toshiba_rmt[0].val = (kToshibaAcHdrMark << 0) | (1 << 15) | (kToshibaAcHdrSpace << 16); // Header of IR Transmit
- toshiba_rmt[73].val = (kToshibaAcBitMark << 0) | (1 << 15) | (kToshibaAcZeroSpace << 16); // Header of IR Transmit
- toshiba_rmt[74].val = (1 << 16); // End marker
- // 0x47503FC
- // 0x1FE823A
- for(int i=1;i<73;i++) {
- toshiba_rmt[i].val = (kToshibaAcBitMark << 0) | (1 << 15) | (kToshibaAcZeroSpace << 16); // Header of IR Transmit
- }
- toshibaTxQueue = xQueueCreate( 5, kToshibaNumberOfBytes );
- xTaskCreatePinnedToCore(toshibaTxTask, "toshibaTxTask", 1024*10, NULL, 2, NULL,0);
- ESP_LOGI("IR_TRANSMIT","Init done.");
- }
|