deckle studioIm Editor öffnen

Jump & Run: STM32F411 + Farb-TFT

Plattformspiel in C auf einem Cortex-M4: Pfeiltasten und Leertaste.

  • STM32F411
  • ST7735 TFT
  • CH9350L
Im Simulator öffnenOhne Konto: Simulieren drücken, und es läuft.
Das Display, während Jump & Run: STM32F411 + Farb-TFT läuft
Auf dem simulierten Display

So funktioniert es

Ein seitlich scrollendes Plattformspiel in Bare-Metal-C auf einem STM32F411 (Cortex-M4) mit dem 1,8"-Farb-TFT ST7735S im Querformat. Pfeiltasten laufen, Leertaste springt (oder SW1–SW3 anklicken). Münzen sammeln, über Lücken springen, die Fahne erreichen. Das Display scrollt in Hardware (SCRLAR/VSCSAD): Pro Bild gehen nur die neuen Spalten und die Spielfigur über SPI, dazwischen schläft der Kern. Der Quelltext steckt in U1: im Code-Editor ändern (Level, Tempo, Farben) und neu starten.

Verdrahtung

Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.

NetzPins
GNDC1 2C2 2C3 2C5 2C6 2C7 2C8 2DS1 GNDJ1 GNDKB1 GNDR3 2SW1 2SW2 2SW3 2U1 GNDU2 GNDU3 GNDU3 RSTV1 −
3V3C2 1C3 1DS1 VCCDS1 LEDU1 VDDU2 OUT
V5C1 1C6 1C8 1J1 VBUSKB1 VBUSU2 INU2 ENU3 VIN5V1 +
VCAP1C5 1U1 VCAP1
BOOT0R3 1U1 BOOT0
BTN_JUMPSW3 1U1 PC2
BTN_LEFTSW1 1U1 PC0
BTN_RIGHTSW2 1U1 PC1
KBD_3V3C7 1U3 VDD33
KBD_RXU1 PA3U3 TXD
MOSIDS1 SDA/MOSIU1 PA7
SCKDS1 SCKU1 PA5
TFT_CSDS1 CSU1 PA4
TFT_DCDS1 DCU1 PA6
TFT_RESDS1 RESU1 PA1
UART_TXU1 PA2U3 RXD
USB_DMJ1 D−KB1 D−U3 DM (port 1 D−)
USB_DPJ1 D+KB1 D+U3 DP (port 1 D+)

Bauteile

Firmware

sketch.ino · 558 Zeilen · die ersten 120 hier, alles im Editor

// Jump & run on an STM32F411 with a 1.8" ST7735S TFT (landscape, 160 x 128, RGB565).
//
// Wiring: SPI1 SCK PA5, MOSI PA7 · TFT CS PA4, DC PA6, RES PA1 · buttons to GND with pull-ups:
// left PC0, right PC1, jump PC2 · USB keyboard on a CH9350L host module: its TXD to PA3
// (USART2 RX), 115200 8N1. In the simulator your keyboard is that USB keyboard: arrows or A / D
// run, space, W or arrow up jump.
//
// Speed: the display scrolls in hardware (ST7735S SCRLAR/VSCSAD). Per frame only the columns
// that come into view and the places where a sprite was or is now are sent over SPI, a few
// hundred bytes instead of 40 kB for a full frame. Between frames the core sleeps (WFI).
#include "clab.h"

#define TFT_CS  PA(4)
#define TFT_DC  PA(6)
#define TFT_RES PA(1)
#define BTN_L   PC(0)
#define BTN_R   PC(1)
#define BTN_J   PC(2)

#define W 144           // playfield width; the 16 px strip on the left is the fixed status bar
#define BAR 16
#define H 128
#define TS 8            // tile size
#define MAP_W 240       // level width in tiles (1920 px)
#define MAP_H 16
#define MAXE 18         // enemies in the level

// ---- SPI1 and the display ---------------------------------------------------------------------
static void spi_init(void) {
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
  // PA5 SCK, PA7 MOSI: alternate function 5
  GPIOA->MODER = (GPIOA->MODER & ~((3u << 10) | (3u << 14))) | (2u << 10) | (2u << 14);
  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~((15u << 20) | (15u << 28))) | (5u << 20) | (5u << 28);
  GPIOA->OSPEEDR |= (3u << 10) | (3u << 14);
  SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI;   // mode 0, f_PCLK / 2
  SPI1->CR1 |= SPI_CR1_SPE;
}
static inline void spi_byte(uint8_t b) {
  while (!(SPI1->SR & SPI_SR_TXE)) {}
  *(volatile uint8_t *)&SPI1->DR = b;
}
static inline void spi_done(void) { while (SPI1->SR & SPI_SR_BSY) {} }
static void cmd(uint8_t c) { spi_done(); pin_write(TFT_DC, false); spi_byte(c); spi_done(); pin_write(TFT_DC, true); }
static inline void data(uint8_t d) { spi_byte(d); }
static void cmd16x2(uint8_t c, uint16_t a, uint16_t b) { cmd(c); data(a >> 8); data(a); data(b >> 8); data(b); }

static void tft_init(void) {
  pin_output(TFT_CS); pin_output(TFT_DC); pin_output(TFT_RES);
  pin_write(TFT_CS, true);
  pin_write(TFT_RES, false); delay_ms(1); pin_write(TFT_RES, true); delay_ms(130);
  pin_write(TFT_CS, false);
  cmd(0x01); delay_ms(130);                 // SWRESET
  cmd(0x11); delay_ms(130);                 // SLPOUT
  cmd(0x3A); data(0x05);                    // COLMOD 16 bit
  cmd(0x36); data(0xA8);                    // MADCTL MY | MV | BGR: landscape, BGR colour filter panel
  // SCRLAR: lines 0..143 scroll (the playfield), lines 144..159 stay: in landscape the status bar
  cmd16x2(0x33, 0, W); data(0); data(BAR);
  cmd(0x29);                                // DISPON
}
/** hardware scroll so that world column `cam` is at the left edge */
static void scroll_to(int cam) {
  uint16_t ssa = (uint16_t)((W - (cam % W)) % W);
  cmd(0x37); data(ssa >> 8); data(ssa);
}

// ---- level -----------------------------------------------------------------------------------
enum { SKY, GROUND, GRASS, BRICK, COIN, FLAG, POLE, QBOX, USED, WATER, BUSH, FLOWER, CASTLE };
static uint8_t map[MAP_W][MAP_H];
static uint8_t gtop[MAP_W];   // ground row of each column (MAP_H over water)
static uint32_t seed;
static int rnd(int n) { seed = seed * 1103515245u + 12345u; return (int)((seed >> 16) % (uint32_t)n); }
/** small hash for decoration (clouds, pebbles, grass) that never changes */
static uint32_t hash(uint32_t x) { x ^= x >> 16; x *= 0x7feb352du; x ^= x >> 15; x *= 0x846ca68bu; return x ^ (x >> 16); }

// enemies: slimes crawl (stomp them), bats flutter (stomp them), hedgehogs have spikes (jump over)
enum { E_SLIME, E_BAT, E_SPIKY };
typedef struct { int x, y, vx, alive, ox, oy, type, bx, by, ph, oph; } Enemy;   // x, y in pixels
static Enemy en[MAXE];
static int nen;
static int castle_x, castle_y;   // castle at the end (pixels)

static void make_level(void) {
  seed = 12345; nen = 0;
  int ground = 13;
  for (int x = 0; x < MAP_W; x++) {
    if (x > 12 && x < MAP_W - 24 && rnd(100) < 6) {   // a gap with water at the bottom
      int len = 2 + rnd(2);
      for (int k = 0; k < len && x < MAP_W; k++, x++) { for (int y = 0; y < MAP_H; y++) map[x][y] = y >= 14 ? WATER : SKY; gtop[x] = MAP_H; }
      x--; continue;
    }
    if (x > 12 && x < MAP_W - 24 && rnd(100) < 10) { ground += rnd(3) - 1; if (ground < 9) ground = 9; if (ground > 13) ground = 13; }
    gtop[x] = (uint8_t)ground;
    for (int y = 0; y < MAP_H; y++) map[x][y] = y < ground ? SKY : y == ground ? GRASS : GROUND;
    if (x > 3 && x < MAP_W - 20 && rnd(100) < 16) map[x][ground - 1] = rnd(3) ? BUSH : FLOWER;
  }
  // brick platforms 4-5 tiles above the ground in front (one jump), ?-boxes in them, coins on top;
  // now and then a second one higher up
  for (int x = 18; x < MAP_W - 26; x += 8 + rnd(7)) {
    int g = gtop[x] < MAP_H ? gtop[x] : 13, y = g - 4 - rnd(2), len = 3 + rnd(4);
    for (int k = 0; k < len; k++) {
      map[x + k][y] = rnd(5) == 0 ? QBOX : BRICK;
      if (map[x + k][y] == BRICK && rnd(3)) map[x + k][y - 1] = COIN;
    }
    if (rnd(3) == 0 && y >= 6) {
      int x2 = x + len - 1;
      for (int k = 0; k < 3; k++) { map[x2 + k][y - 4] = k == 1 ? QBOX : BRICK; map[x2 + k][y - 5] = COIN; }
    }
  }
  // the goal: a pole with a pennant, then the castle
  int g = gtop[MAP_W - 14];
  for (int y = 3; y < g; y++) map[MAP_W - 14][y] = POLE;
  map[MAP_W - 14][3] = FLAG;
  castle_x = (MAP_W - 9) * TS; castle_y = (g - 5) * TS;
  for (int x = MAP_W - 9; x < MAP_W - 4; x++) for (int y = g - 5; y < g; y++) map[x][y] = CASTLE;
  // enemies, away from the start
  for (int x = 26; x < MAP_W - 26 && nen < MAXE; x += 10 + rnd(8)) {
    int kind = rnd(10);
    Enemy *e = &en[nen];
    if (kind < 3) {                                    // bat: flies a wave above the ground
Im Simulator öffnen

Weitere Beispiele: Mikrocontroller