LASER.ino 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Description: LASER Unit wireless UART application: burn the program to two M5Cores
  3.    And connect LASER.TX and LASER.RX to PORTC port respectively.
  4. Point LASER.TX to LASER.RX and press the button on the panel to send characters to the receiver of LASER.RX.
  5. */
  6. #include <M5Stack.h>
  7. char ch;
  8. // serial 2 write and read
  9. //#define RX
  10. void setup() {
  11. M5.begin();
  12. M5.Power.begin();
  13. Serial.begin(115200);
  14. // Serial2.begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert)
  15. Serial2.begin(9600, SERIAL_8N1, 16, 17);
  16. pinMode(5, OUTPUT);
  17. digitalWrite(5, 1);
  18. M5.Lcd.setTextSize(4);
  19. M5.Lcd.setTextColor(GREEN);
  20. M5.Lcd.setCursor(60, 50);
  21. #ifdef RX
  22. M5.Lcd.print("LASER RX");
  23. #elif defined TX
  24. M5.Lcd.print("LASER TX");
  25. #else
  26. M5.Lcd.setCursor(30, 50);
  27. M5.Lcd.print("LASER TX/RX");
  28. M5.Lcd.setCursor(50, 200);
  29. M5.Lcd.print('A');
  30. M5.Lcd.setCursor(150, 200);
  31. M5.Lcd.print('B');
  32. M5.Lcd.setCursor(240, 200);
  33. M5.Lcd.print('C');
  34. #endif
  35. M5.Lcd.setCursor(0, 100);
  36. }
  37. void loop() {
  38. #ifdef RX
  39. M5.update();
  40. if(Serial2.available()) {
  41. char ch = Serial2.read();
  42. M5.Lcd.print(ch);
  43. }
  44. if (M5.BtnA.wasReleased()) {
  45. M5.Lcd.clear();
  46. M5.Lcd.setCursor(0, 0);
  47. }
  48. #elif defined TX
  49. Serial2.write('A');
  50. delay(50);
  51. #else
  52. if (M5.BtnA.wasReleased()) {
  53. ch = 'A';
  54. Serial2.write(ch);
  55. } else if (M5.BtnB.wasReleased()) {
  56. ch = 'B';
  57. Serial2.write(ch);
  58. } else if (M5.BtnC.wasReleased()) {
  59. ch = 'C';
  60. Serial2.write(ch);
  61. }
  62. M5.update();
  63. if(Serial2.available()) {
  64. char ch = Serial2.read();
  65. M5.Lcd.print(ch);
  66. }
  67. #endif
  68. }