Skip to content

Instantly share code, notes, and snippets.

@aki017
Created September 26, 2017 21:05
Show Gist options
  • Save aki017/61ed299ef051c3da5bd7e1744353c3e0 to your computer and use it in GitHub Desktop.
Save aki017/61ed299ef051c3da5bd7e1744353c3e0 to your computer and use it in GitHub Desktop.
esp32 esp-idf oled ssd1306 lifegame sample
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp32-oled-ssd1306/include/ssd1306.h"
#include "esp32-oled-ssd1306/include/fonts.h"
#include <string.h>
uint16_t get_index(uint8_t x, uint8_t y) {
return x + (y / 8) * 128;
}
void app_main(void) {
printf("Hello world!\n");
uint8_t alpha[1024];
uint8_t beta[1024];
memset(alpha, 0, 1024);
memset(beta, 0, 1024);
uint8_t *front;
uint8_t *back;
for (int i = 0; i < 1024; i++) {
alpha[i] = rand();
}
if (ssd1306_init(0, 4, 5)) {
for (uint32_t generation = 0; true; generation++) {
printf("Gen: %d \r", generation);
fflush(stdout);
front = generation % 2 == 1 ? alpha : beta;
back = generation % 2 == 0 ? alpha : beta;
memset(front, 0, 1024);
for (uint8_t x = 1; x < 127; x++) {
for (uint8_t y = 1; y < 63; y++) {
uint8_t env = 0;
// up
if ((back[get_index(x - 1, y - 1)] & 1 << ((y - 1) % 8)) != 0)
env++;
if ((back[get_index(x + 0, y - 1)] & 1 << ((y - 1) % 8)) != 0)
env++;
if ((back[get_index(x + 1, y - 1)] & 1 << ((y - 1) % 8)) != 0)
env++;
// middle
if ((back[get_index(x - 1, y + 0)] & 1 << ((y + 0) % 8)) != 0)
env++;
if ((back[get_index(x + 1, y + 0)] & 1 << ((y + 0) % 8)) != 0)
env++;
// down
if ((back[get_index(x - 1, y + 1)] & 1 << ((y + 1) % 8)) != 0)
env++;
if ((back[get_index(x + 0, y + 1)] & 1 << ((y + 1) % 8)) != 0)
env++;
if ((back[get_index(x + 1, y + 1)] & 1 << ((y + 1) % 8)) != 0)
env++;
if ((back[get_index(x + 0, y + 0)] & 1 << ((y + 0) % 8)) != 0) {
if (env == 2 || env == 3) {
front[get_index(x, y)] |= (1 << (y % 8));
}
} else {
if (env == 3) {
front[get_index(x, y)] |= (1 << (y % 8));
}
}
}
}
x_update_buffer(0, front, 1024);
ssd1306_refresh(0, false);
vTaskDelay(1000 / 60 / portTICK_PERIOD_MS);
}
} else {
printf("init failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment