M5BIT.ino 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Description: Use UART communication to control the LED matrix on microbit.
  3. */
  4. #include <M5Stack.h>
  5. #include <M5StackUpdater.h>
  6. #define WIDTH 320
  7. #define HEIGHT 240
  8. #define BLOCK_SIZE 40
  9. #define UNIT_WIDTH 5
  10. #define UNIT_HEIGHT 5
  11. #define UNIT_SIZE 25
  12. #define GETX(i) ((i) % (5))
  13. #define GETY(i) ((i) / (5))
  14. int world[UNIT_SIZE];
  15. int i;
  16. void setup() {
  17. M5.begin();
  18. M5.Power.begin();
  19. Wire.begin();
  20. if (digitalRead(BUTTON_A_PIN) == 0) {
  21. Serial.println("Will load menu binary");
  22. updateFromFS(SD);
  23. ESP.restart();
  24. }
  25. Serial2.begin(115200, SERIAL_8N1, 16, 17);
  26. M5.Lcd.fillScreen(BLACK);
  27. M5.Lcd.setTextSize(2);
  28. M5.Lcd.setCursor(35, 220);
  29. M5.Lcd.println(" < * >");
  30. for (i = 0; i < UNIT_SIZE; i++) {
  31. world[i] = 0;
  32. }
  33. i = UNIT_SIZE / 2;
  34. }
  35. void loop() {
  36. M5.update();
  37. int x = GETX(i) + 1;
  38. int y = GETY(i);
  39. if (world[i] > 0)
  40. M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2,
  41. BLOCK_SIZE - 2, LIGHTGREY);
  42. else
  43. M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2,
  44. BLOCK_SIZE - 2, BLUE);
  45. if (M5.BtnC.wasPressed()) {
  46. if (world[i] > 0)
  47. M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1,
  48. BLOCK_SIZE - 2, BLOCK_SIZE - 2, WHITE);
  49. else
  50. M5.Lcd.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE,
  51. BLOCK_SIZE, BLACK);
  52. ++i;
  53. if (i >= UNIT_SIZE) i = 0;
  54. }
  55. if (M5.BtnA.wasPressed()) {
  56. if (world[i] > 0)
  57. M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1,
  58. BLOCK_SIZE - 2, BLOCK_SIZE - 2, WHITE);
  59. else
  60. M5.Lcd.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE,
  61. BLOCK_SIZE, BLACK);
  62. --i;
  63. if (i < 0) i = UNIT_SIZE - 1;
  64. }
  65. if (M5.BtnB.wasPressed()) {
  66. if (world[i] > 0)
  67. world[i] = 0;
  68. else
  69. world[i] = 1;
  70. Serial2.print(world[i]);
  71. Serial2.print(GETX(i));
  72. Serial2.println(GETY(i));
  73. }
  74. }