[{"data":1,"prerenderedAt":203},["ShallowReactive",2],{"example-page-retro-console":3},{"id":4,"info":5,"parts":6,"wired":23,"refs":24,"nets":25,"moreNets":196,"code":197},"retro-console","An ESP32 is a small game console: FabGL draws the games at 320 × 200 pixels in 64 colours into two frame buffers; I2S1 sends the finished picture by DMA through the resistor DAC (two bits per colour) to the VGA monitor, 75 times a second. Six buttons are the controller: the arrow keys, Space (A) and Enter (START) on your keyboard. Three games of our own: SNAKE (an arrow key starts it; steer the snake to the apples, it grows and gets faster), BRICKS (keep the ball in play with the paddle and break the wall; A launches the ball), METEORS (fly the ship left and right and shoot the falling rocks with A). START goes back to the menu, which keeps the best score of each game. The sound: FabGL's SoundGenerator mixes square, triangle and noise voices and sends them by I2S0 DMA to the DAC on GPIO25, then a volume potentiometer, a PAM8403 amplifier and the speaker: turn on the sound with the speaker button. The sketch (retro_console) is in the code view: change a game or write your own.",[7,10,12,15,17,19,21],[8,9],"button",6,[11,9],"resistor",[13,14],"esp32_devkitc",1,[16,14],"pam8403",[18,14],"pot",[20,14],"speaker",[22,14],"vga_monitor",false,{"A":8,"A1":13,"A2":16,"DOWN":8,"J1":22,"LEFT":8,"RIGHT":8,"RV1":18,"START":8,"UP":8,"R6":11,"R5":11,"R4":11,"R3":11,"R2":11,"R1":11,"LS1":20},[26,52,59,68,74,81,88,94,100,106,112,118,124,130,136,143,150,156,162,169,175,181,189],{"name":27,"pins":28},"0",[29,32,35,37,39,41,43,45,48,50],[30,31],"A","2",[33,34],"A1","GND",[36,34],"A2",[38,31],"DOWN",[40,34],"J1",[42,31],"LEFT",[44,31],"RIGHT",[46,47],"RV1","1",[49,31],"START",[51,31],"UP",{"name":53,"pins":54},"V5",[55,57],[33,56],"5V",[36,58],"+5V",{"name":60,"pins":61},"VOL",[62,64,66],[36,63],"L in",[36,65],"R in",[46,67],"W",{"name":69,"pins":70},"VSYNC",[71,73],[33,72],"GPIO15",[40,69],{"name":75,"pins":76},"B0",[77,79],[33,78],"GPIO4",[80,47],"R6",{"name":82,"pins":83},"B1",[84,86],[33,85],"GPIO5",[87,47],"R5",{"name":89,"pins":90},"BLUE",[91,92,93],[40,89],[87,31],[80,31],{"name":95,"pins":96},"BTN_A",[97,98],[30,47],[33,99],"GPIO13",{"name":101,"pins":102},"BTN_DOWN",[103,105],[33,104],"GPIO33",[38,47],{"name":107,"pins":108},"BTN_LEFT",[109,111],[33,110],"GPIO26",[42,47],{"name":113,"pins":114},"BTN_RIGHT",[115,117],[33,116],"GPIO27",[44,47],{"name":119,"pins":120},"BTN_START",[121,123],[33,122],"GPIO14",[49,47],{"name":125,"pins":126},"BTN_UP",[127,129],[33,128],"GPIO32",[51,47],{"name":131,"pins":132},"DAC",[133,135],[33,134],"GPIO25",[46,31],{"name":137,"pins":138},"G0",[139,141],[33,140],"GPIO18",[142,47],"R4",{"name":144,"pins":145},"G1",[146,148],[33,147],"GPIO19",[149,47],"R3",{"name":151,"pins":152},"GREEN",[153,154,155],[40,151],[149,31],[142,31],{"name":157,"pins":158},"HSYNC",[159,161],[33,160],"GPIO23",[40,157],{"name":163,"pins":164},"R0",[165,167],[33,166],"GPIO21",[168,47],"R2",{"name":170,"pins":171},"R1",[172,174],[33,173],"GPIO22",[170,47],{"name":176,"pins":177},"RED",[178,179,180],[40,176],[170,31],[168,31],{"name":182,"pins":183},"SPK_N",[184,186],[36,185],"L−",[187,188],"LS1","−",{"name":190,"pins":191},"SPK_P",[192,194],[36,193],"L+",[187,195],"+",0,{"file":198,"lang":199,"text":200,"lines":201,"more":202},"sketch.ino","cpp","\u002F\u002F ESP32 retro console: three games on a VGA monitor (FabGL, 320x200, 64 colours, double buffered),\n\u002F\u002F six buttons (arrows on GPIO32 \u002F 33 \u002F 26 \u002F 27, A on GPIO13, START on GPIO14, to GND with the internal\n\u002F\u002F pull-ups) and sound (FabGL's SoundGenerator: I2S0 DMA to the DAC on GPIO25, then a volume\n\u002F\u002F potentiometer, a PAM8403 amplifier and a speaker). Games: SNAKE (eat, grow, do not bite yourself),\n\u002F\u002F BRICKS (break the wall with the ball) and METEORS (a ship against falling rocks).\n#include \"fabgl.h\"\n\nfabgl::VGAController VGA;\nfabgl::Canvas *cv;\nfabgl::SoundGenerator sound(8000);\n\nconst int W = 320, H = 200;\n\n\u002F\u002F ---- buttons ----------------------------------------------------------------------------------\nenum { B_UP, B_DOWN, B_LEFT, B_RIGHT, B_A, B_START };\nconst int BTN[6] = { 32, 33, 26, 27, 13, 14 };\nuint8_t held = 0, pressed = 0, seen = 0, latched = 0;\nuint8_t sample() { uint8_t now = 0; for (int i = 0; i \u003C 6; i++) if (digitalRead(BTN[i]) == LOW) now |= 1 \u003C\u003C i; return now; }\n\u002F\u002F between two frames the buttons are sampled every millisecond: a short tap is not lost\nvoid latchButtons() { uint8_t now = sample(); latched |= now & ~seen; seen = now; }\nvoid readButtons() {\n  latchButtons();\n  uint8_t now = sample();\n  pressed = latched;\n  latched = 0;\n  held = now | pressed;\n}\nbool isDown(int b) { return (held >> b) & 1; }\nbool hit(int b) { return (pressed >> b) & 1; }\n\n\u002F\u002F ---- sound ------------------------------------------------------------------------------------\nvoid beep(int hz, int ms, int vol = 90) { sound.playSound(SquareWaveformGenerator(), hz, ms, vol); }\nvoid tone2(int hz, int ms, int vol = 110) { sound.playSound(TriangleWaveformGenerator(), hz, ms, vol); }\nvoid crash(int ms) { sound.playSound(NoiseWaveformGenerator(), 1, ms, 90); }\n\u002F\u002F a short melody, one note after the other (frequency, milliseconds; 0 ends it)\nconst int *tune = nullptr;\nuint32_t tuneAt = 0;\nvoid play(const int *t) { tune = t; tuneAt = 0; }\nvoid tickTune() {\n  if (!tune || millis() \u003C tuneAt) return;\n  if (!tune[0]) { tune = nullptr; return; }\n  if (tune[0] > 1) beep(tune[0], tune[1] - 10);\n  tuneAt = millis() + tune[1];\n  tune += 2;\n}\nconst int TUNE_START[] = { 523, 90, 659, 90, 784, 90, 1047, 180, 0 };\nconst int TUNE_OVER[] = { 392, 160, 330, 160, 262, 160, 196, 360, 0 };\nconst int TUNE_LEVEL[] = { 784, 80, 988, 80, 1175, 80, 1568, 200, 0 };\n\n\u002F\u002F ---- drawing helpers ----------------------------------------------------------------------------\nRGB888 rgb(int r, int g, int b) { return RGB888(r, g, b); }\nvoid text(int x, int y, const char *s, RGB888 c) { cv->setPenColor(c); cv->drawText(x, y, s); }\nvoid textC(int y, const char *s, RGB888 c) { text((W - (int)strlen(s) * 8) \u002F 2, y, s, c); }\nvoid box(int x, int y, int w, int h, RGB888 c) { cv->setBrushColor(c); cv->fillRectangle(x, y, x + w - 1, y + h - 1); }\nvoid hud(const char *name, int score, int extra, const char *extraName) {\n  box(0, 0, W, 12, rgb(0, 0, 0));\n  char s[48];\n  text(4, 2, name, rgb(255, 255, 0));\n  snprintf(s, sizeof s, \"SCORE %d\", score); text(120, 2, s, rgb(255, 255, 255));\n  if (extraName) { snprintf(s, sizeof s, \"%s %d\", extraName, extra); text(232, 2, s, rgb(0, 255, 255)); }\n}\n\nint hiScore[3];\nenum Mode { MENU, SNAKE, BRICKS, METEORS };\nMode mode = MENU;\nvoid toMenu();\n\n\u002F\u002F =================================================================================================\n\u002F\u002F SNAKE: steer with the arrows, eat the apples, the snake grows and gets faster\n\u002F\u002F =================================================================================================\nnamespace snake {\n  const int CS = 10, GW = 32, GH = 18, OY = 16;\n  int8_t sx[GW * GH], sy[GW * GH];\n  int len, dir, nextDir, fx, fy, score;\n  uint32_t stepMs, lastStep;\n  bool over, waiting;\n  void place() {\n    for (;;) {\n      fx = random(GW); fy = random(GH);\n      bool onSnake = false;\n      for (int i = 0; i \u003C len; i++) if (sx[i] == fx && sy[i] == fy) onSnake = true;\n      if (!onSnake) return;\n    }\n  }\n  void start() {\n    len = 4;\n    for (int i = 0; i \u003C len; i++) { sx[i] = 10 - i; sy[i] = 9; }\n    dir = nextDir = 0; score = 0; stepMs = 150; lastStep = millis(); over = false; waiting = true;\n    place();\n  }\n  void update() {\n    if (over) { if (hit(B_A) || hit(B_START)) toMenu(); return; }\n    if (hit(B_UP) && dir != 1) nextDir = 3;\n    if (hit(B_DOWN) && dir != 3) nextDir = 1;\n    if (hit(B_LEFT) && dir != 0) nextDir = 2;\n    if (hit(B_RIGHT) && dir != 2) nextDir = 0;\n    if (hit(B_START)) { toMenu(); return; }\n    if (waiting) { if (pressed & 0x0F) waiting = false; else return; }\n    if (millis() - lastStep \u003C stepMs) return;\n    lastStep = millis();\n    dir = nextDir;\n    int nx = sx[0] + (dir == 0) - (dir == 2), ny = sy[0] + (dir == 1) - (dir == 3);\n    bool dead = nx \u003C 0 || ny \u003C 0 || nx >= GW || ny >= GH;\n    for (int i = 0; i \u003C len - 1 && !dead; i++) if (sx[i] == nx && sy[i] == ny) dead = true;\n    if (dead) { over = true; crash(250); play(TUNE_OVER); if (score > hiScore[0]) hiScore[0] = score; return; }\n    bool eat = nx == fx && ny == fy;\n    if (eat) { len++; score += 10; if (stepMs > 60) stepMs -= 7; tone2(880, 50); tone2(1320, 50); }\n    for (int i = len - 1; i > 0; i--) { sx[i] = sx[i - 1]; sy[i] = sy[i - 1]; }\n    sx[0] = nx; sy[0] = ny;\n    if (eat) place();\n  }\n  void draw() {\n    box(0, 0, W, H, rgb(0, 0, 64));\n    box(0, OY - 2, W, 2, rgb(0, 128, 255));\n    \u002F\u002F the apple\n    cv->setBrushColor(rgb(255, 0, 0)); cv->fillEllipse(fx * CS + 5, OY + fy * CS + 5, 9, 9);\n    box(fx * CS + 5, OY + fy * CS, 2, 3, rgb(0, 255, 0));\n    \u002F\u002F the snake, the head brighter\n    for (int i = len - 1; i >= 0; i--) box(sx[i] * CS + 1, OY + sy[i] * CS + 1, CS - 2, CS - 2, i ? rgb(0, 192, 64) : rgb(128, 255, 128));\n    hud(\"SNAKE\", score, len, \"LENGTH\");",343,true,1796304409713]