M5BIT.ino 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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) M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, LIGHTGREY);
  40. else M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, BLUE);
  41. if (M5.BtnC.wasPressed()) {
  42. if (world[i] > 0) M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, WHITE);
  43. else M5.Lcd.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, BLACK);
  44. ++i;
  45. if (i >= UNIT_SIZE) i=0;
  46. }
  47. if (M5.BtnA.wasPressed()) {
  48. if (world[i] > 0) M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, WHITE);
  49. else M5.Lcd.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, BLACK);
  50. --i;
  51. if (i < 0 ) i=UNIT_SIZE -1;
  52. }
  53. if (M5.BtnB.wasPressed()) {
  54. if (world[i] > 0) world[i]=0;
  55. else world[i]=1;
  56. Serial2.print(world[i]);
  57. Serial2.print(GETX(i));
  58. Serial2.println(GETY(i));
  59. }
  60. }