RF433.ino 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Description: Define `RF433RX` by enabling or commenting the macro, switch
  3. the program function, and burn the transmitting and receiving programs to the
  4. two M5Core masters respectively. Connect RF433R/T to PortB (G26, G36), the
  5. transmitter end presses button A to transmit, and the receiver end can view
  6. the received data through Serial.
  7. */
  8. #include <driver/rmt.h>
  9. #include "M5Stack.h"
  10. #define RF433RX
  11. #define RMT_TX_CHANNEL RMT_CHANNEL_0
  12. #define RMT_RX_CHANNEL RMT_CHANNEL_1
  13. #define RTM_TX_GPIO_NUM 26
  14. #define RTM_RX_GPIO_NUM 36
  15. #define RTM_BLOCK_NUM 1
  16. #define RMT_CLK_DIV 80 /*!< RMT counter clock divider */
  17. #define RMT_1US_TICKS (80000000 / RMT_CLK_DIV / 1000000)
  18. #define RMT_1MS_TICKS (RMT_1US_TICKS * 1000)
  19. rmt_item32_t rmtbuff[2048];
  20. #define T0H 670
  21. #define T1H 320
  22. #define T0L 348
  23. #define T1L 642
  24. #define RMT_CODE_H \
  25. { 670, 1, 320, 0 }
  26. #define RMT_CODE_L \
  27. { 348, 1, 642, 0 }
  28. #define RMT_START_CODE0 \
  29. { 4868, 1, 2469, 0 }
  30. #define RMT_START_CODE1 \
  31. { 1647, 1, 315, 0 }
  32. void initRMT() {
  33. #ifndef RF433RX
  34. rmt_config_t txconfig;
  35. txconfig.rmt_mode = RMT_MODE_TX;
  36. txconfig.channel = RMT_TX_CHANNEL;
  37. txconfig.gpio_num = gpio_num_t(RTM_TX_GPIO_NUM);
  38. txconfig.mem_block_num = RTM_BLOCK_NUM;
  39. txconfig.tx_config.loop_en = false;
  40. txconfig.tx_config.carrier_en = false;
  41. txconfig.tx_config.idle_output_en = true;
  42. txconfig.tx_config.idle_level = rmt_idle_level_t(0);
  43. txconfig.clk_div = RMT_CLK_DIV; //时钟分频
  44. ESP_ERROR_CHECK(rmt_config(&txconfig));
  45. ESP_ERROR_CHECK(rmt_driver_install(txconfig.channel, 0, 0));
  46. #else
  47. rmt_config_t rxconfig;
  48. rxconfig.rmt_mode = RMT_MODE_RX;
  49. rxconfig.channel = RMT_RX_CHANNEL;
  50. rxconfig.gpio_num = gpio_num_t(RTM_RX_GPIO_NUM);
  51. rxconfig.mem_block_num = 6;
  52. rxconfig.clk_div = RMT_CLK_DIV; //时钟分频
  53. rxconfig.rx_config.filter_en = true; //开启滤波器
  54. rxconfig.rx_config.filter_ticks_thresh =
  55. 200 * RMT_1US_TICKS; //滤波信号宽度100*1M = 100us
  56. rxconfig.rx_config.idle_threshold = 3 * RMT_1MS_TICKS;
  57. ESP_ERROR_CHECK(rmt_config(&rxconfig));
  58. ESP_ERROR_CHECK(rmt_driver_install(rxconfig.channel, 2048, 0));
  59. #endif
  60. }
  61. /*
  62. uint8_t databuff1[5] = {0xdd,0x41,0x53,0x80,0x9f};
  63. uint8_t databuff2[5] = {0xdd,0x41,0x5a,0x80,0x96};
  64. uint8_t databuff3[5] = {0xdd,0x41,0x58,0x80,0x95};
  65. */
  66. uint8_t data[6] = {0xAA, 0x55, 0x01, 0x02, 0x03, 0x04};
  67. void send(uint8_t* buff, size_t size) {
  68. rmtbuff[0] = (rmt_item32_t){RMT_START_CODE0};
  69. rmtbuff[1] = (rmt_item32_t){RMT_START_CODE1};
  70. for (int i = 0; i < size; i++) {
  71. uint8_t mark = 0x80;
  72. for (int n = 0; n < 8; n++) {
  73. rmtbuff[2 + i * 8 + n] = ((buff[i] & mark))
  74. ? ((rmt_item32_t){RMT_CODE_H})
  75. : ((rmt_item32_t){RMT_CODE_L});
  76. mark >>= 1;
  77. }
  78. }
  79. for (int i = 0; i < 8; i++) {
  80. ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, rmtbuff, 42, false));
  81. ESP_ERROR_CHECK(rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY));
  82. }
  83. }
  84. void setup() {
  85. M5.begin();
  86. M5.Lcd.setRotation(1);
  87. M5.Lcd.fillRect(0, 0, 320, 240, TFT_BLACK);
  88. M5.Lcd.fillRect(0, 0, 320, 20, M5.Lcd.color565(38, 38, 38));
  89. M5.Lcd.setTextColor(TFT_WHITE);
  90. M5.Lcd.drawString("RF433 factory Test", 20, 2, 2);
  91. pinMode(RTM_TX_GPIO_NUM, OUTPUT);
  92. pinMode(RTM_RX_GPIO_NUM, INPUT);
  93. initRMT();
  94. delay(100);
  95. M5.Lcd.setCursor(5, 100);
  96. M5.Lcd.setTextSize(2);
  97. #ifndef RF433RX
  98. M5.Lcd.print("Click Btn A send data");
  99. #else
  100. M5.Lcd.print("Use Serial to view data");
  101. #endif
  102. // rf.begin<PT2262>(26,27).arg<int>(1000);
  103. }
  104. int parsedData(rmt_item32_t* item, size_t size, uint8_t* dataptr,
  105. size_t maxsize) {
  106. // if((item == nullptr)||(size < 4))return -1;
  107. if (size < 4) return -1;
  108. int cnt = 0;
  109. uint8_t data = 0;
  110. uint8_t bitcnt = 0, hex_cnt = 0;
  111. if (((item[0].level0 == 0)) && (item[0].duration0 > 2300) &&
  112. (item[0].duration0 < 2600)) {
  113. // dataptr = (uint8_t*)malloc(size * sizeof(uint8_t));
  114. rmt_item32_t dataitem;
  115. dataitem.level0 = 1;
  116. dataitem.level1 = 0;
  117. dataitem.duration0 = item[0].duration1;
  118. do {
  119. cnt++;
  120. dataitem.duration1 = item[cnt].duration0;
  121. if (cnt > 1) {
  122. // Serial.printf("%d:%d,%d:%d
  123. // ",dataitem.level0,dataitem.duration0,dataitem.level1,dataitem.duration1);
  124. if (((dataitem.duration0 + dataitem.duration1) < 1100) &&
  125. ((dataitem.duration0 + dataitem.duration1) > 800)) {
  126. data <<= 1;
  127. if (dataitem.duration0 > dataitem.duration1) {
  128. data += 1;
  129. }
  130. bitcnt++;
  131. if (bitcnt >= 8) {
  132. Serial.printf("%02x ", data);
  133. if (hex_cnt >= maxsize) {
  134. return hex_cnt;
  135. }
  136. dataptr[hex_cnt] = data;
  137. data = 0;
  138. hex_cnt++;
  139. bitcnt = 0;
  140. }
  141. } else {
  142. Serial.printf("%d %d:%d,%d:%d ", hex_cnt, dataitem.level0,
  143. dataitem.duration0, dataitem.level1,
  144. dataitem.duration1);
  145. return hex_cnt;
  146. }
  147. }
  148. dataitem.duration0 = item[cnt].duration1;
  149. } while (cnt < size);
  150. Serial.println("END");
  151. }
  152. return hex_cnt;
  153. }
  154. void loop() {
  155. #ifndef RF433RX
  156. if (M5.BtnA.wasPressed()) {
  157. Serial.println("SEND");
  158. send(data, 6);
  159. }
  160. #else
  161. int revicecnt = 0;
  162. RingbufHandle_t rb = nullptr;
  163. rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb);
  164. rmt_rx_start(RMT_RX_CHANNEL, true);
  165. while (rb) {
  166. size_t rx_size = 0;
  167. rmt_item32_t* item =
  168. (rmt_item32_t*)xRingbufferReceive(rb, &rx_size, 500);
  169. if (item != nullptr) {
  170. if (rx_size != 0) {
  171. uint8_t databuff[256];
  172. Serial.printf("\r\nsize:%d\r\n", rx_size);
  173. int size = parsedData(item, rx_size, databuff, 255);
  174. if ((size >= 5) && (databuff[0] == 0xaa) &&
  175. (databuff[1] == 0x55) && (databuff[2] == 0x01) &&
  176. (databuff[3] == 0x02) && (databuff[4] == 0x03)) {
  177. revicecnt++;
  178. Serial.printf("Revice %d\r\n", revicecnt);
  179. if (revicecnt == 4) {
  180. M5.Lcd.fillRect(0, 20, 320, 220, TFT_GREEN);
  181. }
  182. }
  183. }
  184. vRingbufferReturnItem(rb, (void*)item);
  185. } else {
  186. // Serial.printf("mmp????\r\n");
  187. if (revicecnt != 0) {
  188. Serial.printf("Revice %d\r\n", revicecnt);
  189. M5.Lcd.fillRect(0, 20, 320, 220, TFT_RED);
  190. }
  191. revicecnt = 0;
  192. }
  193. }
  194. rmt_rx_stop(RMT_RX_CHANNEL);
  195. #endif
  196. delay(10);
  197. M5.update();
  198. }