conway

Example of the component georgik/sdl v3.3.0~7
# Conway's Game of Life - SDL3 Example

**🧬 Classic cellular automaton** with basic age-based coloring.

Standard Conway's Game of Life implementation that adapts to different ESP32 display sizes:

- **Grid Scaling** - Calculates appropriate cell size for each display
- **Age Coloring** - Living cells get brighter as they survive longer
- **Pattern Reset** - Starts with new random patterns periodically
- **Cross-Platform** - Same code works on various ESP32 boards

![Conway's Game of Life on ESP32](preview.gif)

## ✨ Features

### šŸŽÆ Universal Resolution Adaptation
- **M5 Atom S3**: 42Ɨ42 grid with 3Ɨ3 pixel cells (128Ɨ128 display)
- **M5Stack CoreS3**: 64Ɨ48 grid with 5Ɨ5 pixel cells (320Ɨ240 display)
- **ESP32-S3-BOX-3**: 64Ɨ48 grid with 5Ɨ5 pixel cells (320Ɨ240 display)
- **ESP32-P4**: Up to 106Ɨ80 grid with 12Ɨ12 pixel cells (1280Ɨ800 display)

### šŸŽØ Visual Features
- **Age-based coloring**: Older cells become brighter and more cyan
- **Generation counter**: Visual dots indicating current generation (mod 16)
- **Grid border**: Subtle border around the game area
- **Smooth animation**: 10 FPS for optimal viewing experience

### 🧬 Game Rules
Classic Conway's Game of Life rules with enhancements:
- **Birth**: Dead cell with exactly 3 neighbors becomes alive
- **Survival**: Live cell with 2 or 3 neighbors survives (and ages)
- **Death**: Live cell with <2 or >3 neighbors dies
- **Aging**: Living cells accumulate age for color visualization

## šŸš€ Quick Start

### Create Project from Example
```bash
# Create Conway project from component registry
idf.py create-project-from-example "georgik/sdl:conway"
cd conway

# Configure board and build
idf.py menuconfig  # Select your board in "ESP-BSP SDL Configuration"
idf.py build flash monitor
```

### Board Selection Using SDKCONFIG_DEFAULTS Parameter

```bash
# M5 Atom S3 (128Ɨ128, No PSRAM) - Default
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.m5_atom_s3" build flash monitor

# ESP32-S3-BOX-3 (320Ɨ240, OCTAL PSRAM)
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.esp-box-3" build flash monitor

# M5Stack CoreS3 (320Ɨ240, QUAD PSRAM) 
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.m5stack_core_s3" build flash monitor

# M5Stack Tab5 (1280Ɨ720, 32MB PSRAM, ESP32-P4)
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.m5stack_tab5" build flash monitor

# ESP32-P4 RISC-V (up to 1280Ɨ800, 32MB PSRAM)
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.esp32_p4_function_ev_board" build flash monitor

# Universal: ANY ESP32 DevKit + configurable display
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.esp_bsp_generic" menuconfig  # Configure your hardware
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.esp_bsp_generic" build flash monitor

# DevKit testing (virtual display, no physical display needed)
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.esp_bsp_devkit" build flash monitor
```

### First Time Setup (Default Board)

```bash
# Uses M5 Atom S3 by default
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.m5_atom_s3" build flash monitor
```

## šŸ“± Display Adaptation Examples

| Board | Resolution | Grid | Cell Size | Colors |
|-------|------------|------|-----------|---------|
| M5 Atom S3 | 128Ɨ128 | 42Ɨ42 | 3Ɨ3 px | Age-based gradient |
| M5Stack CoreS3 | 320Ɨ240 | 64Ɨ48 | 5Ɨ5 px | Full color range |
| ESP32-S3-BOX-3 | 320Ɨ240 | 64Ɨ48 | 5Ɨ5 px | Full color range |
| ESP32-P4 Function EV | 1280Ɨ800 | 106Ɨ80 | 12Ɨ12 px | Rich detail |

## šŸŽ›ļø Configuration

The example automatically calculates optimal grid dimensions based on display resolution:

```c
// Grid size limits
#define MIN_GRID_WIDTH 32    // Minimum cells horizontally
#define MIN_GRID_HEIGHT 24   // Minimum cells vertically  
#define MAX_GRID_WIDTH 128   // Maximum cells horizontally
#define MAX_GRID_HEIGHT 96   // Maximum cells vertically
#define CELL_MIN_SIZE 3      // Minimum cell size in pixels
#define CELL_MAX_SIZE 12     // Maximum cell size in pixels
```

## šŸ’¾ Memory Usage

- **Stack**: 40KB thread stack (larger than snow example for game logic)
- **Heap**: Dynamic allocation for grid arrays (~6-24KB depending on resolution)
- **PSRAM**: Utilized on supported boards for optimal performance

## šŸ”¬ Technical Details

### Algorithm Implementation
- **Double Buffering**: Uses current/next grid buffers for smooth updates
- **Toroidal Topology**: Grid wraps around edges (no boundaries)
- **Age Tracking**: Each cell stores age value (0-255) for visual effects
- **Memory Efficient**: Dynamic grid allocation based on display size

### Color Mapping
```c
// Age-based color gradient
uint8_t r = (255 * age) / max_age;  // Red component increases with age
uint8_t g = (255 * age) / max_age;  // Green component increases with age
uint8_t b = 255;                    // Blue component stays high
```

### Pattern Generation
- **Random Initialization**: ~33% initial alive probability
- **Glider Injection**: Classic glider pattern added at center
- **Auto-Reset**: New random pattern every 500 generations

## šŸŽ“ Educational Value

Perfect for demonstrating:
- **Cellular Automata**: Classic Conway's Game of Life rules
- **Emergence**: Complex patterns from simple rules
- **Universal Computation**: Turing-complete system
- **Graphics Programming**: Real-time rendering and color gradients
- **Embedded Systems**: Efficient algorithms for resource-constrained devices

## šŸš€ Extensions

Easy to extend with:
- **Touch Interaction**: Add/remove cells by touching screen
- **Pattern Library**: Load famous patterns (gliders, oscillators, etc.)
- **Speed Control**: Variable animation speed
- **Different Rules**: B3/S23 variations
- **Sound**: Audio feedback for birth/death events

## šŸ“Š Performance

- **Frame Rate**: 10 FPS (100ms per generation)
- **CPU Usage**: Moderate (O(n²) per generation)
- **Memory**: Scales with display resolution
- **Power**: Efficient for continuous operation

## šŸ”§ Troubleshooting

### Grid Not Visible
- Check display initialization in serial output
- Verify board selection matches your hardware
- Ensure PSRAM is configured correctly for large grids

### Performance Issues
- Large displays may need slower update rates
- Consider reducing MAX_GRID_WIDTH/HEIGHT for very high resolutions
- Monitor heap usage with `esp_get_free_heap_size()`

---

Experience the beauty of emergence and complexity arising from simple rules, perfectly adapted to your ESP32 development board! šŸŽ®āœØ

To create a project from this example, run:

idf.py create-project-from-example "georgik/sdl=3.3.0~7:conway"

or download archive (~16.27 KB)