#include #include #include #include #include #define WIDTH 128 // OLED display width, in pixels #define HEIGHT 64 // OLED display height, in pixels #define WHITE SSD1306_WHITE // Reset pin not used with the XIAO RP2040, set to -1 Adafruit_SSD1306 display(WIDTH, HEIGHT, &Wire, -1, 1700000UL, 1700000UL); constexpr int CELL_SIZE = 2; constexpr int GRID_W = WIDTH/CELL_SIZE; constexpr int GRID_H = HEIGHT/CELL_SIZE; uint8_t grid[GRID_W * GRID_H]; // index helper function inline int idx(int x, int y) {return y*GRID_W + x; } // set/get helpers inline void setCell(int x, int y, uint8_t alive) { if ((unsigned)x < (unsigned)GRID_W && (unsigned)y < (unsigned)GRID_H) grid[idx(x, y)] = alive ? 1 : 0; } inline uint8_t getCell(int x, int y) { if ((unsigned)x < (unsigned)GRID_W && (unsigned)y < (unsigned)GRID_H) return grid[idx(x, y)]; } // draw a cell inline void drawCellBlock(int cx, int cy, uint8_t alive) { int px = cx*CELL_SIZE; int py = cy*CELL_SIZE; if (alive) { for (int dy = 0; dy < CELL_SIZE; ++dy) for (int dx = 0; dx < CELL_SIZE; ++dx) display.drawPixel(px + dx, py+dy, WHITE); } } void drawGrid() { display.clearDisplay(); for (int y = 0; y < GRID_H; ++y) { for (int x = 0; x < GRID_W; ++x) { if (grid[idx(x, y)]) drawCellBlock(x, y, 1); } } display.display(); } void seedTest() { for (int y = 0; y < GRID_H; ++y) { for (int x = 0; x < GRID_W; ++x) { setCell(x, y, ((x^y)&1)); } } for (int y = 2; y < 6; ++y) for (int x = 2; x < 6; ++x) setCell(x, y, 1); } void setup() { if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { for(;;); } seedTest(); drawGrid(); } void loop() { // nothing yet — next step we'll animate }