LEGO_PLUS.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Description: Use LEGO PLUS Module to drive the LEGO motor to rotate and monitor the encoder value.
  3. */
  4. #include <M5Stack.h>
  5. #include <Wire.h>
  6. #include "Free_Fonts.h"
  7. #include "utility/CommUtil.h"
  8. #define SLAVE_ADDR 0x56
  9. #define MOTOR_ADDR_BASE 0x00
  10. #define ENCODER_ADDR_BASE 0x08
  11. #define STEP_V 51
  12. #define FRONT 4
  13. #define X_LOCAL 60
  14. #define Y_LOCAL 80
  15. #define XF 30
  16. #define YF 30
  17. int16_t Speed = 0;
  18. CommUtil Util;
  19. /*************************************************
  20. Function:MotorRun
  21. Description: Motor forward and reverse API
  22. Input:
  23. n:Motor 0 to motor 3
  24. Speed:Speed value from -255 to +255,when speed=0,The motor stopped.
  25. Return: Successful return 1
  26. Others:
  27. *************************************************/
  28. int32_t MotorRun(uint8_t n,int16_t Speed){
  29. if(n>3)
  30. return 0;
  31. if(Speed <= -255)
  32. Speed = -255;
  33. if(Speed >=255)
  34. Speed = 255;
  35. Util.writeBytes(SLAVE_ADDR,MOTOR_ADDR_BASE+n*2,(uint8_t *)&Speed,2);
  36. return 1;
  37. }
  38. /*************************************************
  39. Function:ReadEncoder
  40. Description: Motor rotation code value
  41. Input:
  42. n:Motor 0 to motor 3
  43. Return: Successful return 1
  44. Others:
  45. *************************************************/
  46. int32_t ReadEncoder(uint8_t n){
  47. uint8_t dest[4]={0};
  48. if(n>3)
  49. return 0;
  50. Util.readBytes(SLAVE_ADDR,ENCODER_ADDR_BASE+n*4,4,dest);
  51. return *((int32_t*)dest);
  52. }
  53. /*************************************************
  54. Function:header
  55. Description:The UI title
  56. Input:
  57. Return:
  58. Others:
  59. *************************************************/
  60. void header(const char *string, uint16_t color){
  61. M5.Lcd.fillScreen(color);
  62. M5.Lcd.setTextSize(1);
  63. M5.Lcd.setTextColor(TFT_MAGENTA, TFT_BLUE);
  64. M5.Lcd.fillRect(0, 0, 320, 30, TFT_BLUE);
  65. M5.Lcd.setTextDatum(TC_DATUM);
  66. M5.Lcd.drawString(string, 160, 3, 4);
  67. }
  68. /*************************************************
  69. Function:motor_demo
  70. Description:Press the button to control motor rotation,
  71. and coded values are displayed in real time.
  72. Input:
  73. Return:
  74. Others:A -> Speed-- B -> Speed=0 C -> Speed++
  75. *************************************************/
  76. void motor_demo(void){
  77. uint8_t BtnFlag = 0;
  78. M5.update();
  79. if (M5.BtnA.wasReleased()) {
  80. Speed -= STEP_V;
  81. if(Speed <= -255)
  82. Speed = -255;
  83. BtnFlag = 1;
  84. }else if (M5.BtnB.wasReleased()) {
  85. Speed = 0;
  86. BtnFlag = 1;
  87. }
  88. else if (M5.BtnC.wasReleased()) {
  89. Speed += STEP_V;
  90. if(Speed >=255)
  91. Speed = 255;
  92. BtnFlag = 1;
  93. }
  94. if(BtnFlag == 1){
  95. BtnFlag = 0;
  96. for(int i =0;i<4;i++){
  97. MotorRun(i,Speed);
  98. M5.Lcd.setCursor(X_LOCAL, Y_LOCAL + YF*i , FRONT);
  99. M5.Lcd.printf("S%d:%d \n",i,Speed);
  100. }
  101. }
  102. for(int i=0;i<4;i++){
  103. M5.Lcd.setCursor(X_LOCAL + XF*4, Y_LOCAL + YF*i , FRONT);
  104. M5.Lcd.printf("E%d:%d \n",i,ReadEncoder(i));
  105. }
  106. }
  107. void setup() {
  108. M5.begin();
  109. M5.Power.begin();
  110. Wire.begin();
  111. Serial.begin(115200);
  112. M5.Lcd.fillScreen(TFT_BLACK);
  113. header("L E G O +", TFT_BLACK);
  114. M5.Lcd.setTextColor(TFT_WHITE,TFT_BLACK);
  115. for(int i =0;i<4;i++){
  116. M5.Lcd.setCursor(X_LOCAL, Y_LOCAL + YF*i , FRONT);
  117. M5.Lcd.printf("S%d:%d \n",i,Speed);
  118. }
  119. }
  120. void loop() {
  121. motor_demo();
  122. }