Skip to content

Commit 8c946df

Browse files
author
bitluni
committed
Examples added
1 parent dceeee7 commit 8c946df

File tree

6 files changed

+710
-0
lines changed

6 files changed

+710
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//example for parallel LEDs not using the graphics interface
2+
//using the library
3+
#include <ESP32Lib.h>
4+
#include "camera.h"
5+
#include "tools.h"
6+
7+
ParallelLED gfx(4); //four color components for RGBW
8+
9+
volatile bool frameAvailable = false;
10+
#include "effects.h"
11+
12+
void setup()
13+
{
14+
Serial.begin(115200);
15+
cameraInit();
16+
setCameraParams();
17+
gfx.setGamma(3.2f, 2.0f, 4.0f, 3.2f); //gamma adjustment
18+
const int pins[] = {16, 12, 13, 15, 14, 2, -1, -1};
19+
gfx.init(pins, 8 * 40); //8 x 40 LEDs per channel (8 channels total, here only 6 used)
20+
}
21+
22+
void loop()
23+
{
24+
camera_fb_t * fb = esp_camera_fb_get();
25+
if (fb)
26+
{
27+
downSample(fb->buf); //fb->width, fb->height, fb->format, fb->buf, fb->len
28+
frameAvailable = true;
29+
esp_camera_fb_return(fb);
30+
};
31+
if(frameAvailable)
32+
{
33+
processImage();
34+
frameAvailable = false;
35+
}
36+
}

examples/LEDs/LEDWall3Camera/camera.h

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include "esp_camera.h"
2+
3+
//WROVER-KIT PIN Map
4+
#define CAM_PIN_PWDN 32
5+
#define CAM_PIN_RESET -1
6+
#define CAM_PIN_XCLK 0
7+
#define CAM_PIN_SIOD 26
8+
#define CAM_PIN_SIOC 27
9+
10+
#define CAM_PIN_D7 35
11+
#define CAM_PIN_D6 34
12+
#define CAM_PIN_D5 39
13+
#define CAM_PIN_D4 36
14+
#define CAM_PIN_D3 21
15+
#define CAM_PIN_D2 19
16+
#define CAM_PIN_D1 18
17+
#define CAM_PIN_D0 5
18+
#define CAM_PIN_VSYNC 25
19+
#define CAM_PIN_HREF 23
20+
#define CAM_PIN_PCLK 22
21+
22+
static camera_config_t camera_config = {
23+
.pin_pwdn = CAM_PIN_PWDN,
24+
.pin_reset = CAM_PIN_RESET,
25+
.pin_xclk = CAM_PIN_XCLK,
26+
.pin_sscb_sda = CAM_PIN_SIOD,
27+
.pin_sscb_scl = CAM_PIN_SIOC,
28+
29+
.pin_d7 = CAM_PIN_D7,
30+
.pin_d6 = CAM_PIN_D6,
31+
.pin_d5 = CAM_PIN_D5,
32+
.pin_d4 = CAM_PIN_D4,
33+
.pin_d3 = CAM_PIN_D3,
34+
.pin_d2 = CAM_PIN_D2,
35+
.pin_d1 = CAM_PIN_D1,
36+
.pin_d0 = CAM_PIN_D0,
37+
.pin_vsync = CAM_PIN_VSYNC,
38+
.pin_href = CAM_PIN_HREF,
39+
.pin_pclk = CAM_PIN_PCLK,
40+
41+
//XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
42+
.xclk_freq_hz = 20000000,
43+
.ledc_timer = LEDC_TIMER_0,
44+
.ledc_channel = LEDC_CHANNEL_0,
45+
46+
.pixel_format = PIXFORMAT_RGB565,//YUV422,GRAYSCALE,RGB565,JPEG
47+
.frame_size = FRAMESIZE_QQVGA,//QQVGA-QXGA Do not use sizes above QVGA when not JPEG
48+
49+
.jpeg_quality = 12, //0-63 lower number means higher quality
50+
.fb_count = 2 //if more than one, i2s runs in continuous mode. Use only with JPEG
51+
};
52+
53+
bool cameraInit()
54+
{
55+
56+
int tries = 0;
57+
esp_err_t err;
58+
do
59+
{
60+
//power up the camera if PWDN pin is defined
61+
if(CAM_PIN_PWDN != -1){
62+
pinMode(CAM_PIN_PWDN, OUTPUT);
63+
digitalWrite(CAM_PIN_PWDN, HIGH);
64+
delay(500);
65+
digitalWrite(CAM_PIN_PWDN, LOW);
66+
delay(500);
67+
}
68+
69+
//initialize the camera
70+
err = esp_camera_init(&camera_config);
71+
if (err != ESP_OK) {
72+
Serial.println("Camera Init Failed.");
73+
delay(500);
74+
}
75+
tries++;
76+
}
77+
while(err != ESP_OK && tries < 3);
78+
if(err == ESP_OK)
79+
Serial.println("Camera Init Success");
80+
else
81+
{
82+
Serial.println("Restarting...");
83+
ESP.restart();
84+
}
85+
return err;
86+
}
87+
88+
void setCameraParams()
89+
{
90+
sensor_t *sensor = esp_camera_sensor_get();
91+
sensor->set_brightness(sensor, -2);
92+
sensor->set_contrast(sensor, 2);
93+
sensor->set_saturation(sensor, 0);
94+
95+
//0: auto, 1: sun, 2: cloud, 3: indoors
96+
sensor->set_wb_mode(sensor, 0);
97+
98+
//sensor->set_exposure_ctrl(sensor, 1);
99+
//sensor->set_aec_value(sensor, -2);
100+
//sensor->set_ae_level(sensor, 100);
101+
//sensor->set_gain_ctrl(sensor, 100);
102+
103+
104+
/*
105+
* some other prameters.. good luck
106+
int (*set_sharpness) (sensor_t *sensor, int level);
107+
int (*set_denoise) (sensor_t *sensor, int level);
108+
int (*set_gainceiling) (sensor_t *sensor, gainceiling_t gainceiling);
109+
int (*set_quality) (sensor_t *sensor, int quality);
110+
int (*set_colorbar) (sensor_t *sensor, int enable);
111+
int (*set_whitebal) (sensor_t *sensor, int enable);
112+
int (*set_gain_ctrl) (sensor_t *sensor, int enable);
113+
int (*set_exposure_ctrl) (sensor_t *sensor, int enable);
114+
int (*set_hmirror) (sensor_t *sensor, int enable);
115+
int (*set_vflip) (sensor_t *sensor, int enable);
116+
117+
int (*set_aec2) (sensor_t *sensor, int enable);
118+
int (*set_awb_gain) (sensor_t *sensor, int enable);
119+
int (*set_agc_gain) (sensor_t *sensor, int gain);
120+
int (*set_aec_value) (sensor_t *sensor, int gain);
121+
122+
int (*set_special_effect) (sensor_t *sensor, int effect);
123+
int (*set_wb_mode) (sensor_t *sensor, int mode);
124+
int (*set_ae_level) (sensor_t *sensor, int level);
125+
126+
int (*set_dcw) (sensor_t *sensor, int enable);
127+
int (*set_bpc) (sensor_t *sensor, int enable);
128+
int (*set_wpc) (sensor_t *sensor, int enable);
129+
130+
int (*set_raw_gma) (sensor_t *sensor, int enable);
131+
int (*set_lenc) (sensor_t *sensor, int enable); */
132+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
unsigned char newImage[40][48][3];
4+
5+
void downSample(unsigned char *frame)
6+
{
7+
//160x120
8+
for(int y = 0; y < 40; y++)
9+
{
10+
for(int x = 0; x < 48; x++)
11+
{
12+
int r = 0;
13+
int g = 0;
14+
int b = 0;
15+
for(int j = 0; j < 3; j++)
16+
for(int i = 0; i < 3; i++)
17+
{
18+
unsigned char p[3];
19+
getPixel(8 + x * 3 + i, y * 3 + j, frame, p);
20+
r += p[0];
21+
g += p[1];
22+
b += p[2];
23+
}
24+
newImage[y][x][0] = (r * (256 / 9)) >> 8;
25+
newImage[y][x][1] = (g * (256 / 9)) >> 8;
26+
newImage[y][x][2] = (b * (256 / 9)) >> 8;
27+
}
28+
}
29+
}
30+
31+
void showImage()
32+
{
33+
for(int y = 0; y < 40; y++)
34+
for(int x = 0; x < 48; x++)
35+
{
36+
int channel = 0, led = 0;
37+
pixelMap(x, y, channel, led);
38+
gfx.setLED(channel, led, newImage[y][x][1], newImage[y][x][0], newImage[y][x][2], 0);
39+
}
40+
gfx.show();
41+
}
42+
43+
44+
void processImage()
45+
{
46+
showImage();
47+
}

examples/LEDs/LEDWall3Camera/tools.h

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#pragma once
2+
#include <math.h>
3+
long sinTab[256];
4+
5+
void calculateSinTab()
6+
{
7+
for(int i = 0; i < 256; i++)
8+
{
9+
sinTab[i] = int(sin(M_PI / 128.f * i) * 256);
10+
}
11+
}
12+
13+
float smoothstep(float x)
14+
{
15+
x = max(0.f, min(x, 1.f));
16+
return (3 * x * x) - (2 * x * x * x);
17+
}
18+
19+
float rsqrt(float number)
20+
{
21+
long i;
22+
float x2, y;
23+
const float threehalfs = 1.5F;
24+
25+
x2 = number * 0.5F;
26+
y = number;
27+
i = * ( long * ) &y; // evil floating point bit level hacking
28+
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
29+
y = * ( float * ) &i;
30+
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
31+
y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
32+
return y;
33+
}
34+
35+
int rainbow[256][3];
36+
void calcRainbow()
37+
{
38+
const float cb[][3] = {{1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {0, 1, 1}, {0, 0, 1}, {1, 0, 1}};
39+
for (int i = 0; i < 256; i++)
40+
{
41+
//interpolate the colors from the cb array and calculate the R5G5B5 color
42+
float s = 6.f / 256 * i;
43+
int n = int(s);
44+
float f = s - n;
45+
float fi = (1 - f);
46+
const float *cf = cb[n];
47+
const float *cfn = cb[(n + 1) % 6];
48+
int r = int((fi * cf[0] * 255) + (f * cfn[0] * 255));
49+
int g = int((fi * cf[1] * 255) + (f * cfn[1] * 255));
50+
int b = int((fi * cf[2] * 255) + (f * cfn[2] * 255));
51+
rainbow[i][0] = r;
52+
rainbow[i][1] = g;
53+
rainbow[i][2] = b;
54+
}
55+
}
56+
57+
float rdist(int x, int y, float x2, float y2)
58+
{
59+
return rsqrt((x - x2) * (x - x2) + (y - y2) * (y - y2) + 1);
60+
}
61+
62+
float dist(int x, int y, float x2, float y2)
63+
{
64+
return sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
65+
}
66+
67+
void getPixel(int x, int y, unsigned char *image, unsigned char *pixel)
68+
{
69+
int p = image[(y * 160 + x) * 2 + 1] | (image[(y * 160 + x) * 2] << 8);
70+
pixel[0] = (p >> 11) << 3;
71+
pixel[1] = ((p >> 5) & 0b111111) << 2;
72+
pixel[2] = (p & 0b11111) << 3;
73+
}
74+
75+
bool pixelMap(int x, int y, int &channel, int &led)
76+
{
77+
if(x < 0 || x >= 48 || y < 0 || y >= 40)
78+
return false;
79+
channel = x >> 3;
80+
led = (x & 7) + y * 8;
81+
return true;
82+
}

examples/LEDs/NyanCat/NyanCat.ino

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//include libraries
2+
#include <ESP32Lib.h>
3+
4+
//include the sprites converted the SpriteConverter. It's an html found in the utilities folder of the library
5+
#include "nyan.h"
6+
7+
//To know where all the pixels are you need to implement a class with the function map like here:
8+
class LEDWall3: public ParallelLEDGraphics
9+
{
10+
public:
11+
LEDWall3()
12+
:ParallelLEDGraphics(48, 40, 4)
13+
{
14+
}
15+
16+
//this function maps from x,y to channel and led number. channel, and led are return parameters
17+
virtual bool map(int x, int y, int &channel, int &led)
18+
{
19+
//no checks in this example needed
20+
//if(x < 0 || x >= 48 || y < 0 || y >= 40)
21+
//return false;
22+
channel = x >> 3;
23+
led = (x & 7) + y * 8;
24+
return true;
25+
}
26+
};
27+
28+
LEDWall3 gfx;
29+
30+
//initial setup
31+
void setup()
32+
{
33+
const int pins[] = {16, 12, 13, 15, 14, 2, -1, -1};
34+
gfx.init(pins, 8 * 40);
35+
gfx.setGamma(3.2f, 2.0f, 4.0f, 3.2f); //color adjustment for LED displays
36+
}
37+
38+
//just draw each frame
39+
void loop()
40+
{
41+
static int i = 0;
42+
i = (i + 1) % 6; //circulate the 6 sprites
43+
nyan.draw(gfx, i, 24, 20);
44+
gfx.show();
45+
delay(50);
46+
}

0 commit comments

Comments
 (0)