Added maze example + fixes.

This commit is contained in:
nitko12 2020-09-21 13:24:50 +02:00
parent e16f608d31
commit a17e1e0c69
6 changed files with 319 additions and 125 deletions

View File

@ -23,8 +23,7 @@
#include "Inkplate.h" //Include Inkplate library to the sketch
#include "SdFat.h" //Include library for SD card
Inkplate display(
INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1 Bit mode (BW)
Inkplate display(INKPLATE_3BIT); // Create an object on Inkplate library and also set library into 1 Bit mode (BW)
SdFile file; // Create SdFile object used for accessing files on SD card
void setup()

View File

@ -0,0 +1,74 @@
#include "Inkplate.h"
#define MAXITERATIONS 150
Inkplate display(INKPLATE_1BIT);
// Takes a long time to render, cca 3 minutes
// Explore different positions to draw
// Some interesting ones can be found here http://www.cuug.ab.ca/dewara/mandelbrot/Mandelbrowser.html
double xFrom = -0.7423, xTo = -0.8463;
double yFrom = 0.1092, yTo = 0.2102;
void setup()
{
Serial.begin(115200);
display.begin();
}
void loop()
{
display.clearDisplay();
for (int j = 0; j < 600; ++j)
{
for (int i = 0; i < 800; ++i)
display.drawPixel(
i, j, colorAt(xFrom + (double)i * (xTo - xFrom) / 800.0, yFrom + (double)j * (yTo - yFrom) / 600.0));
// for whole set:
// display.drawPixel(i, j, colorAt(-2.0 + (3.0 * (double)i / 800.0), -1.0 + 2.0 * (double)j / 600.0));
Serial.println(j);
}
display.display();
delay(5000);
}
struct complex
{
double re;
double im;
};
void addComplex(struct complex *z, struct complex *c)
{
z->re += c->re;
z->im += c->im;
}
void squareComplex(struct complex *z)
{
double re = z->re;
double im = z->im;
z->re = re * re - im * im;
z->im = 2 * re * im;
}
double modulusComplexSqr(struct complex *z)
{
return z->re * z->re + z->im * z->im;
}
uint8_t colorAt(double x, double y)
{
struct complex z = {0.0, 0.0};
struct complex c = {x, y};
int i;
for (i = 0; i < MAXITERATIONS && modulusComplexSqr(&z) <= 4.0; ++i)
{
squareComplex(&z);
addComplex(&z, &c);
}
return i == MAXITERATIONS;
}

View File

@ -0,0 +1,122 @@
#include "Inkplate.h"
Inkplate display(INKPLATE_1BIT);
const int cellSize = 10;
const int w = 790 / cellSize, h = 590 / cellSize;
char maze[w * h];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
void setup()
{
Serial.begin(115200);
display.begin();
// Generate and display the maze
generateMaze(maze, w, h);
showMaze(maze, w, h);
}
void loop()
{
}
// Display the maze
void showMaze(const char *maze, int width, int height)
{
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
if (maze[x + y * width] == 1)
for (int i = 0; i < 4; ++i)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (0 <= xx && xx < w && 0 <= yy && yy < h && maze[yy * width + xx] == 1)
display.drawLine(3 + x * cellSize + cellSize / 2, 3 + y * cellSize + cellSize / 2,
3 + x * cellSize + cellSize / 2 + (dx[i] * cellSize / 2),
3 + y * cellSize + cellSize / 2 + (dy[i] * cellSize / 2), BLACK);
}
display.display();
}
// Carve the maze starting at x, y.
void carveMaze(char *maze, int width, int height, int x, int y)
{
int x1, y1;
int x2, y2;
int dx, dy;
int dir, count;
dir = random(4);
count = 0;
while (count < 4)
{
dx = 0;
dy = 0;
switch (dir)
{
case 0:
dx = 1;
break;
case 1:
dy = 1;
break;
case 2:
dx = -1;
break;
default:
dy = -1;
break;
}
x1 = x + dx;
y1 = y + dy;
x2 = x1 + dx;
y2 = y1 + dy;
if (x2 > 0 && x2 < width && y2 > 0 && y2 < height && maze[y1 * width + x1] == 1 && maze[y2 * width + x2] == 1)
{
maze[y1 * width + x1] = 0;
maze[y2 * width + x2] = 0;
x = x2;
y = y2;
dir = random(4);
count = 0;
}
else
{
dir = (dir + 1) % 4;
count += 1;
}
}
}
// Generate maze in matrix maze with size width, height.
void generateMaze(char *maze, int width, int height)
{
int x, y;
// Initialize the maze.
for (x = 0; x < width * height; x++)
{
maze[x] = 1;
}
maze[1 * width + 1] = 0;
// Seed the random number generator.
srand(time(0));
// Carve the maze.
for (y = 1; y < height; y += 2)
{
for (x = 1; x < width; x += 2)
{
carveMaze(maze, width, height, x, y);
}
}
// Set up the entry and exit.
maze[0 * width + 1] = 0;
maze[(height - 1) * width + (width - 2)] = 0;
}

View File

@ -556,7 +556,8 @@ void Inkplate::einkOn()
setTemperature(Wire.read());
pinModeMCP(7, INPUT_PULLUP);
while(!digitalReadMCP(7));
while (!digitalReadMCP(7))
;
OE_SET;
setPanelState(1);
}

View File

@ -7,9 +7,9 @@
#error "Wrong board selected! Select ESP32 Wrover from board menu!"
#endif
#include "SPI.h"
#include "include/Graphics.h"
#include "include/System.h"
#include "SPI.h"
#include "libs/SdFat/SdFat.h"
#include "include/defines.h"
@ -30,6 +30,7 @@ public:
void einkOn();
void einkOff();
void cleanFast(uint8_t c, uint8_t rep);
bool joinAP(const char *ssid, const char *pass)
{
@ -49,7 +50,6 @@ private:
void display1b();
void display3b();
void cleanFast(uint8_t c, uint8_t rep);
void vscan_start();
void vscan_end();
void hscan_start(uint32_t _d = 0);
@ -60,7 +60,9 @@ private:
uint8_t _beginDone = 0;
const uint8_t waveform3Bit[8][8] = {{0, 0, 0, 0, 1, 1, 1, 0}, {1, 2, 2, 2, 1, 1, 1, 0}, {0, 1, 2, 1, 1, 2, 1, 0}, {0, 2, 1, 2, 1, 2, 1, 0}, {0, 0, 0, 1, 1, 1, 2, 0}, {2, 1, 1, 1, 2, 1, 2, 0}, {1, 1, 1, 2, 1, 2, 2, 0}, {0, 0, 0, 0, 0, 0, 2, 0}};
const uint8_t waveform3Bit[8][8] = {{0, 0, 0, 0, 1, 1, 1, 0}, {1, 2, 2, 2, 1, 1, 1, 0}, {0, 1, 2, 1, 1, 2, 1, 0},
{0, 2, 1, 2, 1, 2, 1, 0}, {0, 0, 0, 1, 1, 1, 2, 0}, {2, 1, 1, 1, 2, 1, 2, 0},
{1, 1, 1, 2, 1, 2, 2, 0}, {0, 0, 0, 0, 0, 0, 2, 0}};
const uint32_t waveform[50] = {
0x00000008, 0x00000008, 0x00200408, 0x80281888, 0x60A81898, 0x60A8A8A8, 0x60A8A8A8, 0x6068A868, 0x6868A868,
0x6868A868, 0x68686868, 0x6A686868, 0x5A686868, 0x5A686868, 0x5A586A68, 0x5A5A6A68, 0x5A5A6A68, 0x55566A68,

View File

@ -101,7 +101,7 @@ bool Image::drawBitmapFromSd(SdFile *p, int x, int y, bool dither, bool invert)
{
int16_t n = ROWSIZE(w, c);
p->read(pixelBuffer, n);
displayBmpLine(x, y + bmpHeader.height - i, &bmpHeader, dither, invert);
displayBmpLine(x, y + bmpHeader.height - i - 1, &bmpHeader, dither, invert);
}
return 1;
}
@ -144,7 +144,7 @@ bool Image::drawBitmapFromBuffer(uint8_t *buf, int x, int y, bool dither, bool i
for (int i = 0; i < bmpHeader.height; ++i)
{
memcpy(pixelBuffer, bufferPtr, ROWSIZE(bmpHeader.width, bmpHeader.color));
displayBmpLine(x, y + bmpHeader.height - i, &bmpHeader, dither, invert);
displayBmpLine(x, y + bmpHeader.height - i - 1, &bmpHeader, dither, invert);
bufferPtr += ROWSIZE(bmpHeader.width, bmpHeader.color);
}
@ -166,8 +166,7 @@ void Image::displayBmpLine(int16_t x, int16_t y, bitmapHeader *bmpHeader, bool d
writePixel(x + j, y, (invert ^ (palette[0] < palette[1])) ^ !!(pixelBuffer[j >> 3] & (1 << (7 - (j & 7)))));
break;
// as for 2 bit, literally cannot find an example online or in PS, so skipped
case 4:
{
case 4: {
uint8_t px = pixelBuffer[j >> 1] & (j & 1 ? 0x0F : 0xF0) >> (j & 1 ? 0 : 4);
uint8_t val;
@ -183,8 +182,7 @@ void Image::displayBmpLine(int16_t x, int16_t y, bitmapHeader *bmpHeader, bool d
writePixel(x + j, y, val);
break;
}
case 8:
{
case 8: {
uint8_t px = pixelBuffer[j];
uint8_t val;
@ -200,8 +198,7 @@ void Image::displayBmpLine(int16_t x, int16_t y, bitmapHeader *bmpHeader, bool d
writePixel(x + j, y, val);
break;
}
case 16:
{
case 16: {
uint16_t px = ((uint16_t)pixelBuffer[(j << 1) | 1] << 8) | pixelBuffer[(j << 1)];
uint8_t r = (px & 0x7C00) >> 7;
@ -222,8 +219,7 @@ void Image::displayBmpLine(int16_t x, int16_t y, bitmapHeader *bmpHeader, bool d
writePixel(x + j, y, val);
break;
}
case 24:
{
case 24: {
uint8_t r = pixelBuffer[j * 3];
uint8_t g = pixelBuffer[j * 3 + 1];
uint8_t b = pixelBuffer[j * 3 + 2];