|
| 1 | +// "Minotaur Maze" plaything for Adafruit Hallowing. Uses ray casting, |
| 2 | +// DMA and related shenanigans to smoothly move about a 3D maze. |
| 3 | +// Tilt Hallowing to turn right/left and move forward/back. |
| 4 | + |
| 5 | +// Ray casting code adapted from tutorial by Lode Vandevenne: |
| 6 | +// https://lodev.org/cgtutor/raycasting.html |
| 7 | + |
| 8 | +#include <Adafruit_LIS3DH.h> // Accelerometer library |
| 9 | +#include <Adafruit_GFX.h> // Core graphics library |
| 10 | +#include <Adafruit_ST7735.h> // Display-specific graphics library |
| 11 | +#include <Adafruit_ZeroDMA.h> // Direct memory access library |
| 12 | + |
| 13 | +#define TFT_RST 37 // TFT reset pin |
| 14 | +#define TFT_DC 38 // TFT display/command mode pin |
| 15 | +#define TFT_CS 39 // TFT chip select pin |
| 16 | +#define TFT_BACKLIGHT 7 // TFT backlight LED pin |
| 17 | + |
| 18 | +// Declarations for some Hallowing hardware -- display, accelerometer, SPI |
| 19 | +Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_RST); |
| 20 | +Adafruit_LIS3DH accel; |
| 21 | +SPISettings settings(12000000, MSBFIRST, SPI_MODE0); |
| 22 | + |
| 23 | +// Declarations related to DMA (direct memory access), which lets us walk |
| 24 | +// and chew gum at the same time. This is VERY specific to SAMD chips and |
| 25 | +// means this is not trivially ported to other devices. |
| 26 | +Adafruit_ZeroDMA dma; |
| 27 | +DmacDescriptor *dptr; // Initial allocated DMA descriptor |
| 28 | +DmacDescriptor desc[2][3] __attribute__((aligned(16))); |
| 29 | +uint8_t dList = 0; // Active DMA descriptor list index (0-1) |
| 30 | + |
| 31 | +// DMA transfer-in-progress indicator and callback |
| 32 | +static volatile bool dma_busy = false; |
| 33 | +static void dma_callback(Adafruit_ZeroDMA *dma) { |
| 34 | + dma_busy = false; |
| 35 | +} |
| 36 | + |
| 37 | +// This is the maze map. It's fixed at 32 bits wide, can be any height but |
| 38 | +// is 32 in this example. '1' bits indicate solid walls, '0' indicate empty |
| 39 | +// space that can be navigated. Perimeter wall bits MUST be set! Keep the |
| 40 | +// center area empty since the player is initially placed there. |
| 41 | +uint32_t worldMap[] = { |
| 42 | + 0b11111111111111111111111111111111, |
| 43 | + 0b10000000000000100000000001000001, |
| 44 | + 0b10000000000000101111011111011101, |
| 45 | + 0b10000000000000001000001000000101, |
| 46 | + 0b10000000000000111011101010111101, |
| 47 | + 0b10000010100000100010000010000101, |
| 48 | + 0b10000010100000111111111010101101, |
| 49 | + 0b10000011100000100000000000100001, |
| 50 | + 0b10000000000000111011101110111101, |
| 51 | + 0b10000000000000100010000010001001, |
| 52 | + 0b10000000000000111111111111101111, |
| 53 | + 0b10000000000000000000000000000001, |
| 54 | + 0b11111011111011100111111011111111, |
| 55 | + 0b10000000001010000001000000000001, |
| 56 | + 0b10100000101010000001001001001001, |
| 57 | + 0b10101010101000000000000000000001, |
| 58 | + 0b10101010101000000000000000000001, |
| 59 | + 0b10100000101010000001001001001001, |
| 60 | + 0b10000000001010000001000000000001, |
| 61 | + 0b11111011111011100111111011111111, |
| 62 | + 0b10000000000000000000000000000001, |
| 63 | + 0b10000010100000000111000010101001, |
| 64 | + 0b10001000001000000111000001010101, |
| 65 | + 0b10000000000000000111000000000001, |
| 66 | + 0b10010000000100000000000011111101, |
| 67 | + 0b10000001000000000000000010000101, |
| 68 | + 0b10010000000100000011111010100101, |
| 69 | + 0b10000000000000000010001010000001, |
| 70 | + 0b10001000001000000010101010000101, |
| 71 | + 0b10000010100000000010101011111101, |
| 72 | + 0b10000000000000000000100000000001, |
| 73 | + 0b11111111111111111111111111111111, |
| 74 | +}; |
| 75 | +#define MAPHEIGHT (sizeof worldMap / sizeof worldMap[0]) |
| 76 | + |
| 77 | +// This macro tests whether bit at (X,Y) in the map is set. |
| 78 | +#define isBitSet(X,Y) (worldMap[MAPHEIGHT-1-(Y)] & (0x80000000>>(X))) |
| 79 | +// (X,Y) are in Cartesian coordinates with (0,0) at bottom-left (hence the |
| 80 | +// MAPHEIGHT-1-Y inversion above) -- all the navigation and ray-casting math |
| 81 | +// is done in Cartesian space, consistent with the trigonometric functions, |
| 82 | +// whereas bitmap is represented top-to-bottom. |
| 83 | + |
| 84 | +// DMA shenanigans are used for the solid color fills (sky, walls and |
| 85 | +// floor). Typically one would use the DMA "source address increment" to |
| 86 | +// copy graphics data from RAM or flash to SPI (to the screen). But a trick |
| 87 | +// we can use for certain fills requires only a single byte of storage for |
| 88 | +// each color. DMA source increment is turned OFF -- the same byte is issued |
| 89 | +// over and over to fill a given span. Downside is a limited palette |
| 90 | +// consisting of 256 colors with the high and low bytes of a 16-bit pixel |
| 91 | +// value being the same. With the TFT's 5-6-5 bit color packing, the |
| 92 | +// resulting selections are a bit weird (there's no 100% pure red, green or |
| 93 | +// blue, only combinations) but usable. e.g. an 8-bit value 0x82 expands to |
| 94 | +// a 16-bit pixel value of 0x8282 = 0b10000 010100 00010 = 16/31 (~52%) red, |
| 95 | +// 20/63 (~32%) green, 2/31 (6%) blue. |
| 96 | +const uint8_t colorSky = 0x3E, // Color of sky |
| 97 | + colorGround = 0x82, // Color of ground |
| 98 | + colorNorth = 0x04, // Color of north-facing walls |
| 99 | + colorSouth = 0x05, // Color of south-facing walls |
| 100 | + colorEast = 0x06, // Color of east-facing walls |
| 101 | + colorWest = 0x07; // Color of west-facing walls |
| 102 | + |
| 103 | +#define FOV (90.0 * (M_PI / 180.0)) // Field of view |
| 104 | + |
| 105 | +float posX = 16.0, // Observer position, |
| 106 | + posY = MAPHEIGHT / 2.0, // begin at center of map |
| 107 | + heading = 0.0; // Initial heading = east |
| 108 | + |
| 109 | +uint32_t startTime, frames = 0; // For frames-per-second calculation |
| 110 | + |
| 111 | +// SETUP -- RUNS ONCE AT PROGRAM START ------------------------------------- |
| 112 | + |
| 113 | +void setup(void) { |
| 114 | + Serial.begin(115200); |
| 115 | + |
| 116 | + // Initialize accelerometer, set to 2G range |
| 117 | + if(accel.begin(0x18) || accel.begin(0x19)) { |
| 118 | + accel.setRange(LIS3DH_RANGE_2_G); |
| 119 | + } |
| 120 | + |
| 121 | + // Initialize and clear screen |
| 122 | + tft.initR(INITR_144GREENTAB); |
| 123 | + tft.setRotation(1); |
| 124 | + tft.fillScreen(0); |
| 125 | + |
| 126 | + // More shenanigans: the display mapping is reconfigured so pixels are |
| 127 | + // issued in COLUMN-MAJOR sequence (i.e. vertical lines), left-to-right, |
| 128 | + // with pixel (0,0) at top left. The ray casting algorithm determines the |
| 129 | + // wall height at each column...drawing is then just a matter of blasting |
| 130 | + // a column's worth of pixels. |
| 131 | + digitalWrite(TFT_CS, LOW); |
| 132 | + digitalWrite(TFT_DC, LOW); |
| 133 | +#ifdef ST77XX_MADCTL |
| 134 | + SPI.transfer(ST77XX_MADCTL); // Current TFT lib |
| 135 | +#else |
| 136 | + SPI.transfer(ST7735_MADCTL); // Older TFT lib |
| 137 | +#endif |
| 138 | + digitalWrite(TFT_DC, HIGH); |
| 139 | + SPI.transfer(0x28); |
| 140 | + digitalWrite(TFT_CS, HIGH); |
| 141 | + |
| 142 | + pinMode(TFT_BACKLIGHT, OUTPUT); |
| 143 | + digitalWrite(TFT_BACKLIGHT, HIGH); // Main screen turn on |
| 144 | + |
| 145 | + // Set up SPI DMA. While the Hallowing has a known SPI peripheral and this |
| 146 | + // could be much simpler, the extra code here will help if adapting this |
| 147 | + // sketch to other SAMD boards (Feather M0, M4, etc.) |
| 148 | + int dmac_id; |
| 149 | + volatile uint32_t *data_reg; |
| 150 | + dma.allocate(); |
| 151 | + if(&PERIPH_SPI == &sercom0) { |
| 152 | + dma.setTrigger(SERCOM0_DMAC_ID_TX); |
| 153 | + data_reg = &SERCOM0->SPI.DATA.reg; |
| 154 | +#if defined SERCOM1 |
| 155 | + } else if(&PERIPH_SPI == &sercom1) { |
| 156 | + dma.setTrigger(SERCOM1_DMAC_ID_TX); |
| 157 | + data_reg = &SERCOM1->SPI.DATA.reg; |
| 158 | +#endif |
| 159 | +#if defined SERCOM2 |
| 160 | + } else if(&PERIPH_SPI == &sercom2) { |
| 161 | + dma.setTrigger(SERCOM2_DMAC_ID_TX); |
| 162 | + data_reg = &SERCOM2->SPI.DATA.reg; |
| 163 | +#endif |
| 164 | +#if defined SERCOM3 |
| 165 | + } else if(&PERIPH_SPI == &sercom3) { |
| 166 | + dma.setTrigger(SERCOM3_DMAC_ID_TX); |
| 167 | + data_reg = &SERCOM3->SPI.DATA.reg; |
| 168 | +#endif |
| 169 | +#if defined SERCOM4 |
| 170 | + } else if(&PERIPH_SPI == &sercom4) { |
| 171 | + dma.setTrigger(SERCOM4_DMAC_ID_TX); |
| 172 | + data_reg = &SERCOM4->SPI.DATA.reg; |
| 173 | +#endif |
| 174 | +#if defined SERCOM5 |
| 175 | + } else if(&PERIPH_SPI == &sercom5) { |
| 176 | + dma.setTrigger(SERCOM5_DMAC_ID_TX); |
| 177 | + data_reg = &SERCOM5->SPI.DATA.reg; |
| 178 | +#endif |
| 179 | + } |
| 180 | + dma.setAction(DMA_TRIGGER_ACTON_BEAT); |
| 181 | + dma.setCallback(dma_callback); |
| 182 | + |
| 183 | + // Initialize DMA descriptor lists. There are TWO lists, used for |
| 184 | + // alternating even/odd scanlines (columns in this case)...one list is |
| 185 | + // calculated and filled while the other is being transferred out SPI. |
| 186 | + // Each list contains three elements (though not all three are used every |
| 187 | + // time), corresponding to the sky, wall and ground pixels for a column. |
| 188 | + for(uint8_t s=0; s<2; s++) { // Even/odd scanlines |
| 189 | + for(uint8_t d=0; d<3; d++) { // 3 descriptors per line |
| 190 | + // No need to set SRCADDR, BTCNT or DESCADDR -- done later |
| 191 | + desc[s][d].BTCTRL.bit.VALID = true; |
| 192 | + desc[s][d].BTCTRL.bit.EVOSEL = 0x3; |
| 193 | + desc[s][d].BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_NOACT; |
| 194 | + desc[s][d].BTCTRL.bit.BEATSIZE = DMA_BEAT_SIZE_BYTE; |
| 195 | + desc[s][d].BTCTRL.bit.SRCINC = 0; |
| 196 | + desc[s][d].BTCTRL.bit.DSTINC = 0; |
| 197 | + desc[s][d].BTCTRL.bit.STEPSEL = DMA_STEPSEL_SRC; |
| 198 | + desc[s][d].BTCTRL.bit.STEPSIZE = DMA_ADDRESS_INCREMENT_STEP_SIZE_1; |
| 199 | + desc[s][d].DSTADDR.reg = (uint32_t)data_reg; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + // The DMA library MUST allocate at least one valid descriptor, so that's |
| 204 | + // done here. It's not used in the conventional sense though, just before |
| 205 | + // a transfer we copy the first scanline descriptor to this spot. |
| 206 | + dptr = dma.addDescriptor(NULL, NULL, 42, DMA_BEAT_SIZE_BYTE, false, false); |
| 207 | + |
| 208 | + startTime = millis(); // Starting time for frame-per-second calculation |
| 209 | +} |
| 210 | + |
| 211 | +// LOOP -- REPEATS INDEFINITELY -------------------------------------------- |
| 212 | + |
| 213 | +void loop() { |
| 214 | + |
| 215 | + // Update heading and position from accelerometer... |
| 216 | + uint8_t mapX = (uint8_t)posX, // Current square of map |
| 217 | + mapY = (uint8_t)posY; // (before changing pos.) |
| 218 | + accel.read(); // Read accelerometer |
| 219 | + heading += (float)accel.y / -20000.0; // Update direction |
| 220 | + float v = (abs(accel.x) < abs(accel.z)) ? // If board held flat(ish) |
| 221 | + (float)accel.x / 20000.0 : // Use accel X for velocity |
| 222 | + (float)accel.z / -20000.0; // else accel Z is velocity |
| 223 | + if(v > 0.19) v = 0.19; // Keep speed under 0.2 |
| 224 | + else if(v < -0.19) v = -0.19; |
| 225 | + float vx = cos(heading) * v, // Direction vector X, Y |
| 226 | + vy = sin(heading) * v, |
| 227 | + newX = posX + vx, // New position |
| 228 | + newY = posY + vy; |
| 229 | + |
| 230 | + // Prevent going through solid walls (or getting too close to them) |
| 231 | + if(vx > 0) { |
| 232 | + if(isBitSet((int)(newX + 0.2), (int)newY)) newX = mapX + 0.8; |
| 233 | + } else { |
| 234 | + if(isBitSet((int)(newX - 0.2), (int)newY)) newX = mapX + 0.2; |
| 235 | + } |
| 236 | + if(vy > 0) { |
| 237 | + if(isBitSet((int)newX, (int)(newY + 0.2))) newY = mapY + 0.8; |
| 238 | + } else { |
| 239 | + if(isBitSet((int)newX, (int)(newY - 0.2))) newY = mapY + 0.2; |
| 240 | + } |
| 241 | + |
| 242 | + posX = newX; |
| 243 | + posY = newY; |
| 244 | + |
| 245 | + SPI.beginTransaction(settings); // SPI init |
| 246 | + digitalWrite(TFT_CS, LOW); // Chip select |
| 247 | + tft.setAddrWindow(0, 0, 128, 128); // Set address window to full screen |
| 248 | + digitalWrite(TFT_CS, LOW); // Re-select after addr function |
| 249 | + digitalWrite(TFT_DC, HIGH); // Data mode... |
| 250 | + |
| 251 | + // Ray casting code is much abbreviated here. |
| 252 | + // See Lode Vandevenne's original tutorial for an in-depth explanation: |
| 253 | + // https://lodev.org/cgtutor/raycasting.html |
| 254 | + |
| 255 | + int8_t stepX, stepY; // X/Y direction steps (+1 or -1) |
| 256 | + uint8_t skyPixels, floorPixels, // # of pixels in sky, floor |
| 257 | + side, // North/south or east/west wall hit? |
| 258 | + i; // Index in DMA descriptor list |
| 259 | + uint16_t wallPixels; // # of wall pixels |
| 260 | + float frac, rayDirX, rayDirY, |
| 261 | + sideDistX, sideDistY, // Ray length, current to next X/Y side |
| 262 | + deltaDistX, deltaDistY, // X-to-X, Y-to-Y ray lengths |
| 263 | + perpWallDist, // Distance to wall |
| 264 | + x1 = cos(heading + FOV / 2.0), // Image plane left edge |
| 265 | + y1 = sin(heading + FOV / 2.0), |
| 266 | + x2 = cos(heading - FOV / 2.0), // Image plane right edge |
| 267 | + y2 = sin(heading - FOV / 2.0), |
| 268 | + dx = x2 - x1, dy = y2 - y1; |
| 269 | + |
| 270 | + for(uint8_t col = 0; col < 128; col++) { // For each column... |
| 271 | + frac = ((float)col + 0.5) / 128.0; // 0 to 1 left to right |
| 272 | + rayDirX = x1 + dx * frac; |
| 273 | + rayDirY = y1 + dy * frac; |
| 274 | + mapX = (uint8_t)posX; |
| 275 | + mapY = (uint8_t)posY; |
| 276 | + deltaDistX = (rayDirX != 0.0) ? fabs(1 / rayDirX) : 0.0; |
| 277 | + deltaDistY = (rayDirY != 0.0) ? fabs(1 / rayDirY) : 0.0; |
| 278 | + |
| 279 | + // Calculate X/Y steps and initial sideDist |
| 280 | + if(rayDirX < 0) { |
| 281 | + stepX = -1; |
| 282 | + sideDistX = (posX - mapX) * deltaDistX; |
| 283 | + } else { |
| 284 | + stepX = 1; |
| 285 | + sideDistX = (mapX + 1.0 - posX) * deltaDistX; |
| 286 | + } if (rayDirY < 0) { |
| 287 | + stepY = -1; |
| 288 | + sideDistY = (posY - mapY) * deltaDistY; |
| 289 | + } else { |
| 290 | + stepY = 1; |
| 291 | + sideDistY = (mapY + 1.0 - posY) * deltaDistY; |
| 292 | + } |
| 293 | + |
| 294 | + do { // Bresenham DDA line algorithm...walk map squares... |
| 295 | + if(sideDistX < sideDistY) { |
| 296 | + sideDistX += deltaDistX; |
| 297 | + mapX += stepX; |
| 298 | + side = 0; // East/west |
| 299 | + } else { |
| 300 | + sideDistY += deltaDistY; |
| 301 | + mapY += stepY; |
| 302 | + side = 1; // North/south |
| 303 | + } |
| 304 | + } while(!isBitSet(mapX, mapY)); // Continue until wall hit |
| 305 | + |
| 306 | + // Calc distance projected on camera direction |
| 307 | + perpWallDist = side ? ((mapY - posY + (1 - stepY) / 2) / rayDirY) : |
| 308 | + ((mapX - posX + (1 - stepX) / 2) / rayDirX); |
| 309 | + |
| 310 | + wallPixels = (int)(128.0 / perpWallDist); // Colum height in pixels |
| 311 | + if(wallPixels >= 128) { // >= screen height? |
| 312 | + wallPixels = 128; // Clip to screen height |
| 313 | + skyPixels = floorPixels = 0; // No sky or ground |
| 314 | + } else { |
| 315 | + skyPixels = (128 - wallPixels) / 2; // 1/2 of non-wall is sky |
| 316 | + floorPixels = 128 - wallPixels - skyPixels; // Any remainder is floor |
| 317 | + } |
| 318 | + |
| 319 | + // Build DMA descriptor list with up to 3 elements... |
| 320 | + i = 0; |
| 321 | + if(skyPixels) { // Any sky pixels in this column? |
| 322 | + desc[dList][i].SRCADDR.reg = (uint32_t)&colorSky; |
| 323 | + desc[dList][i].BTCNT.reg = skyPixels * 2; |
| 324 | + desc[dList][i].DESCADDR.reg = (uint32_t)&desc[dList][i + 1]; |
| 325 | + i++; |
| 326 | + } |
| 327 | + if(wallPixels) { // Any wall pixels? |
| 328 | + // North/south or east/west facing? |
| 329 | + desc[dList][i].SRCADDR.reg = (uint32_t)(side ? |
| 330 | + ((stepY > 0) ? &colorSouth : &colorNorth) : |
| 331 | + ((stepX > 0) ? &colorWest : &colorEast )); |
| 332 | + desc[dList][i].BTCNT.reg = wallPixels * 2; |
| 333 | + desc[dList][i].DESCADDR.reg = (uint32_t)&desc[dList][i + 1]; |
| 334 | + i++; |
| 335 | + } |
| 336 | + if(floorPixels) { // Any floor pixels? |
| 337 | + desc[dList][i].SRCADDR.reg = (uint32_t)&colorGround; |
| 338 | + desc[dList][i].BTCNT.reg = floorPixels * 2; |
| 339 | + desc[dList][i].DESCADDR.reg = (uint32_t)&desc[dList][i + 1]; |
| 340 | + i++; |
| 341 | + } |
| 342 | + desc[dList][i - 1].DESCADDR.reg = 0; // End descriptor list |
| 343 | + |
| 344 | + while(dma_busy); // Wait for prior DMA transfer to finish |
| 345 | + // Copy scanline's first descriptor to the DMA lib's descriptor table |
| 346 | + memcpy(dptr, &desc[dList][0], sizeof(DmacDescriptor)); |
| 347 | + dma_busy = true; // Mark as busy (DMA callback clears this) |
| 348 | + dma.startJob(); // Start new DMA transfer |
| 349 | + dList = 1 - dList; // Swap active DMA descriptor list index |
| 350 | + } |
| 351 | + while(dma_busy); // Wait for last DMA transfer to complete |
| 352 | + digitalWrite(TFT_CS, HIGH); // Deselect |
| 353 | + SPI.endTransaction(); // SPI done |
| 354 | + |
| 355 | + if(!(++frames & 255)) { // Every 256th frame, show frame rate |
| 356 | + uint32_t elapsed = (millis() - startTime) / 1000; |
| 357 | + if(elapsed) Serial.println(frames / elapsed); |
| 358 | + } |
| 359 | +} |
0 commit comments