Skip to content

Instantly share code, notes, and snippets.

@ksasao ksasao/rmt.ino
Created Dec 24, 2018

Embed
What would you like to do?
M5Stack Nodeモジュールサンプルコード (赤外線送受信)
// ----------------------------------------------------------
// M5Stack Nodeモジュール向け赤外線送受信 2018/12/24 @ksasao
// ----------------------------------------------------------
// ESP32で赤外線学習リモコンを作る
// https://qiita.com/td2sk/items/4c0ef83bcc7e74e5e8d5#%E9%85%8D%E7%B7%9A
// @td2sk さんのコードをM5Stack動作確認向けに変更
#include <M5Stack.h>
#include "driver/rmt.h"
// 赤外線送信
#define RMT_TX_CHANNEL RMT_CHANNEL_1
#define RMT_TX_GPIO_NUM GPIO_NUM_12
// 赤外線受信
#define RMT_RX_CHANNEL RMT_CHANNEL_0
#define RMT_RX_GPIO_NUM GPIO_NUM_35
#define RMT_CLK_DIV 100
#define RMT_TICK_10_US (2400000000/RMT_CLK_DIV/100000)
#define rmt_item32_TIMEOUT_US 10000
#define MAX_SIGNAL_LEN 1024
size_t received = 0;
rmt_item32_t signals[MAX_SIGNAL_LEN];
void setup() {
M5.begin();
M5.Lcd.setBrightness(30);
M5.Lcd.setTextSize(2);
init_tx();
init_rx();
Serial.println("Initialized...");
M5.Lcd.println("Button A: Receive IR Signal");
M5.Lcd.println("Button B: Send IR Signal");
}
void loop() {
if(M5.BtnA.wasPressed()) {
M5.Lcd.setCursor(0, 0);
M5.Lcd.clear();
rmt_rx_task();
}
if(M5.BtnB.wasPressed()) {
M5.Lcd.setCursor(0, 0);
M5.Lcd.clear();
rmt_tx_task();
}
M5.update();
}
void init_tx() {
rmt_config_t rmt_tx;
rmt_tx.rmt_mode = RMT_MODE_TX;
rmt_tx.channel = RMT_TX_CHANNEL;
rmt_tx.gpio_num = RMT_TX_GPIO_NUM;
rmt_tx.mem_block_num = 4;
rmt_tx.clk_div = RMT_CLK_DIV;
rmt_tx.tx_config.loop_en = false;
rmt_tx.tx_config.carrier_duty_percent = 50;
rmt_tx.tx_config.carrier_freq_hz = 38000;
rmt_tx.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
rmt_tx.tx_config.carrier_en = 1;
rmt_tx.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
rmt_tx.tx_config.idle_output_en = true;
rmt_config(&rmt_tx);
rmt_driver_install(rmt_tx.channel, 0, 0);
}
void init_rx() {
rmt_config_t rmt_rx;
rmt_rx.rmt_mode = RMT_MODE_RX;
rmt_rx.channel = RMT_RX_CHANNEL;
rmt_rx.clk_div = RMT_CLK_DIV;
rmt_rx.gpio_num = RMT_RX_GPIO_NUM;
rmt_rx.mem_block_num = 4;
rmt_rx.rx_config.filter_en = true;
rmt_rx.rx_config.filter_ticks_thresh = 100;
rmt_rx.rx_config.idle_threshold = rmt_item32_TIMEOUT_US / 10 * (RMT_TICK_10_US);
rmt_config(&rmt_rx);
rmt_driver_install(rmt_rx.channel, 1000, 0);
}
void rmt_rx_task() {
RingbufHandle_t rb = NULL;
rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb);
rmt_rx_start(RMT_RX_CHANNEL, 1);
size_t rx_size = 0;
status_println("wait ir signal...");
rmt_item32_t *item = (rmt_item32_t*)xRingbufferReceive(rb, &rx_size, 3000);
rmt_rx_stop(RMT_RX_CHANNEL);
if(!item) {
status_println("no data received...");
return;
}
memcpy(signals, item, sizeof(rmt_item32_t) * rx_size);
for (int i = 0; i < rx_size; ++i) {
signals[i].level0 = ~signals[i].level0;
signals[i].level1 = ~signals[i].level1;
if(signals[i].duration1 == 0){
rx_size = i + 1;
break;
}
}
received = rx_size;
vRingbufferReturnItem(rb, (void*)item);
status_print("received items: ");
status_println(rx_size);
status_println("recv done");
rmt_rx_stop(RMT_RX_CHANNEL);
static char buf[100];
for(int i=0;i<received;i++){
sprintf(buf,"%d %d %d %d\r\n", signals[i].duration0,signals[i].level0,signals[i].duration1,signals[i].level1);
status_print(buf);
}
}
void rmt_tx_task() {
status_println("send...");
rmt_write_items(RMT_TX_CHANNEL, signals, received, 1);
rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY);
status_println("send done");
}
void status_print(char* str){
Serial.print(str);
M5.Lcd.print(str);
}
void status_println(char* str){
Serial.println(str);
M5.Lcd.println(str);
}
void status_print(size_t str){
Serial.print(str);
M5.Lcd.print(str);
}
void status_println(size_t str){
Serial.println(str);
M5.Lcd.println(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.