AC-SOCKET.ino 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/products
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/products
  8. *
  9. * Describe: AC Socket.
  10. * Date: 2021/9/1
  11. *******************************************************************************
  12. Click button B to turn on the power. Click button A to turn off the power.
  13. 单击按钮 B 打开电源。 单击按钮 A 关闭电源。
  14. */
  15. #include <Arduino.h>
  16. #include <M5Stack.h>
  17. #include "modbus.h"
  18. #include "protocol.h"
  19. volatile uint32_t tim = 0;
  20. uint32_t time_now = 0;
  21. uint8_t ucTestFlag = 0;
  22. bool ubCoilState = false;
  23. void setup() {
  24. M5.begin(true, false, true, false);
  25. M5.Power.begin();
  26. mb_init(0xac, 9600, &tim);
  27. Serial1.begin(9600, SERIAL_8N1, 16, 17);
  28. time_now = millis();
  29. // put your setup code here, to run once:
  30. }
  31. void loop() {
  32. M5.update();
  33. if (millis() - time_now > 60000UL) {
  34. time_now = millis();
  35. if (ucTestFlag) {
  36. if (ubCoilState) {
  37. char data_str[] = {0xAA, 5, 0x00, 0x00, 0x00, 0x00};
  38. mb_send_frame((uint8_t *)data_str, 6);
  39. } else {
  40. char data_str[] = {0xAA, 5, 0x00, 0x00, 0xff, 0x00};
  41. mb_send_frame((uint8_t *)data_str, 6);
  42. }
  43. ubCoilState = 1 - ubCoilState;
  44. }
  45. }
  46. if (M5.BtnA.wasPressed()) {
  47. char data_str[] = {0xAA, 5, 0x00, 0x00, 0x00, 0x00};
  48. mb_send_frame((uint8_t *)data_str, 6);
  49. }
  50. if (M5.BtnB.wasPressed()) {
  51. char data_str[] = {0xAA, 5, 0x00, 0x00, 0xff, 0x00};
  52. mb_send_frame((uint8_t *)data_str, 6);
  53. }
  54. if (M5.BtnC.wasPressed()) {
  55. ucTestFlag = 1 - ucTestFlag;
  56. }
  57. vTaskDelay(pdMS_TO_TICKS(10));
  58. // while(Serial1.available()) {
  59. // protocol_rec_put(Serial1.read());
  60. // micros();
  61. // }
  62. // put your main code here, to run repeatedly:
  63. }
  64. void mb_send_one_byte(uint8_t data) { Serial1.write(data); }
  65. void protocol_callback(CmdData cmd) { Serial.printf("got ... \r\n"); }