Skip to content

Instantly share code, notes, and snippets.

@rishubil
Last active November 9, 2021 08:02
Show Gist options
  • Save rishubil/504cf4871b804dbe182e0229ae509096 to your computer and use it in GitHub Desktop.
Save rishubil/504cf4871b804dbe182e0229ae509096 to your computer and use it in GitHub Desktop.
/*
ESP32-CAM to HyperDisplay_KWH018ST01
ESP32-CAM -> Display
--------------------
2 -> PWM
12 -> D/C
13 -> MOSI
14 -> SCLK
15 -> CS
*/
#include <SPI.h>
#include "HyperDisplay_KWH018ST01_4WSPI.h"
#include "esp_camera.h"
#define CAMERA_MODEL_AI_THINKER
// #define SERIAL_PORT Serial
// camera pins
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
// display pins
#define PWM_PIN 2
#define CS_PIN 15
#define DC_PIN 12
// led pin
#define LED_PIN 33
// dummy pin
#define DUMMY_PIN 16
// cam options
#define CAM_FRAMESIZE FRAMESIZE_QQVGA
#define CAM_SPEED 20000000
#define CAM_FB_COUNT 1
// display options
#define X_SIZE 120 // QQVGA
#define Y_SIZE 160 // QQVGA
#define NUM_PIX ((X_SIZE) * (Y_SIZE))
#define VH true
#define SPI_SPEED 20000000 // Requests host uC to use the fastest possible SPI speed up to 32 MHz
// PWM options
#define PWM_CHANNEL 7
#define PWM_FREQUENCY 110000
#define PWM_RESOLUTION 7
KWH018ST01_4WSPI TFT_DISPLAY;
SPIClass ESP_SPI(HSPI);
ILI9163C_color_18_t *screenMem;
camera_fb_t *fb;
void setup()
{
// SERIAL_PORT.begin(9600);
initCamera();
initDisplay();
initBacklight();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop()
{
bool isLoaded = loadCamImage();
if (isLoaded)
{
drawScreen();
}
}
void initBacklight()
{
int pwmMax = pow(2, PWM_RESOLUTION) - 1;
int brightness = round(pwmMax * 0.5);
ledcSetup(PWM_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
ledcAttachPin(PWM_PIN, PWM_CHANNEL);
ledcWrite(PWM_CHANNEL, brightness);
}
void initDisplay()
{
TFT_DISPLAY.begin(
DC_PIN,
CS_PIN,
DUMMY_PIN, // we'll control PWM_PIN manually
ESP_SPI,
SPI_SPEED
);
TFT_DISPLAY.clearDisplay();
}
bool loadCamImage()
{
if (fb)
{
esp_camera_fb_return(fb);
}
fb = esp_camera_fb_get();
if (!fb)
{
return false;
}
screenMem = (ILI9163C_color_18_t *)fb->buf;
return true;
}
void drawScreen()
{
TFT_DISPLAY.fillFromArray(0, 0, X_SIZE - 1, Y_SIZE - 1, (color_t)screenMem, NUM_PIX, VH);
}
void initCamera()
{
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = CAM_SPEED;
config.pixel_format = PIXFORMAT_RGB888;
config.frame_size = CAM_FRAMESIZE;
config.fb_count = CAM_FB_COUNT;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK)
{
// SERIAL_PORT.printf("Camera init failed with error 0x%x", err);
return;
}
sensor_t *s = esp_camera_sensor_get();
s->set_hmirror(s, 1);
// s->set_vflip(s, 1);
s->set_brightness(s, 0); // -2 to 2
s->set_contrast(s, 0); // -2 to 2
s->set_saturation(s, 1); // -2 to 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment