esp-who/examples/code_recognition/main/app_peripherals.c

139 lines
4.4 KiB
C

#include "app_peripherals.h"
#include "esp_log.h"
#include "esp_system.h"
static const char *TAG = "app_peripherals";
esp_err_t app_camera_init()
{
ESP_LOGI(TAG, "Camera module is %s", CAMERA_MODULE_NAME);
#if CONFIG_CAMERA_MODULE_ESP_EYE || CONFIG_CAMERA_MODULE_ESP32_CAM_BOARD
/* IO13, IO14 is designed for JTAG by default,
* to use it as generalized input,
* firstly declair it as pullup input */
gpio_config_t conf;
conf.mode = GPIO_MODE_INPUT;
conf.pull_up_en = GPIO_PULLUP_ENABLE;
conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
conf.intr_type = GPIO_INTR_DISABLE;
conf.pin_bit_mask = 1LL << 13;
gpio_config(&conf);
conf.pin_bit_mask = 1LL << 14;
gpio_config(&conf);
#endif
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = CAMERA_PIN_D0;
config.pin_d1 = CAMERA_PIN_D1;
config.pin_d2 = CAMERA_PIN_D2;
config.pin_d3 = CAMERA_PIN_D3;
config.pin_d4 = CAMERA_PIN_D4;
config.pin_d5 = CAMERA_PIN_D5;
config.pin_d6 = CAMERA_PIN_D6;
config.pin_d7 = CAMERA_PIN_D7;
config.pin_xclk = CAMERA_PIN_XCLK;
config.pin_pclk = CAMERA_PIN_PCLK;
config.pin_vsync = CAMERA_PIN_VSYNC;
config.pin_href = CAMERA_PIN_HREF;
config.pin_sscb_sda = CAMERA_PIN_SIOD;
config.pin_sscb_scl = CAMERA_PIN_SIOC;
config.pin_pwdn = CAMERA_PIN_PWDN;
config.pin_reset = CAMERA_PIN_RESET;
config.xclk_freq_hz = XCLK_FREQ_HZ;
config.pixel_format = CAMERA_PIXFORMAT;
config.frame_size = CAMERA_FRAME_SIZE;
config.jpeg_quality = 12;
config.fb_count = CAMERA_FB_COUNT;
config.fb_location = CAMERA_FB_IN_PSRAM;
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
return ESP_FAIL;
}
sensor_t *s = esp_camera_sensor_get();
if (s->id.PID == OV3660_PID || s->id.PID == OV2640_PID)
s->set_vflip(s, 1); //flip it back
else if (s->id.PID == GC0308_PID){
s->set_hmirror(s, 0);
}
else if (s->id.PID == GC032A_PID){
s->set_vflip(s, 1);
// s->set_hmirror(s, 0); //something wrong
}
if (s->id.PID == OV3660_PID)
{
s->set_brightness(s, 2);
s->set_contrast(s, 3);
}
return ESP_OK;
}
#ifdef LCD_CONTROLLER
esp_err_t app_lcd_init(esp_lcd_panel_handle_t *panel_handle){
gpio_config_t bk_gpio_config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << LCD_BCKL
};
ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
gpio_set_level(LCD_BCKL, LCD_BK_LIGHT_OFF_LEVEL);
spi_bus_config_t bus_conf = {
.sclk_io_num = LCD_SCLK,
.mosi_io_num = LCD_MOSI,
.miso_io_num = LCD_MISO,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = LCD_H_RES * LCD_V_RES * sizeof(uint16_t),
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &bus_conf, SPI_DMA_CH_AUTO));
ESP_LOGI(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = LCD_DC,
.cs_gpio_num = LCD_CS,
.pclk_hz = LCD_PIXEL_CLOCK_HZ,
.lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_PARAM_BITS,
.spi_mode = 0,
.trans_queue_depth = 10,
};
// Attach the LCD to the SPI bus
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI2_HOST, &io_config, &io_handle));
// ESP_LOGI(TAG, "Install ST7789 panel driver");
esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = LCD_RST,
.rgb_endian = LCD_RGB_ENDIAN_RGB,
.bits_per_pixel = 16,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_reset(*panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(*panel_handle));
#if CONFIG_CAMERA_MODULE_ESP_S2_KALUGA
esp_lcd_panel_invert_color(*panel_handle, false);
esp_lcd_panel_mirror(*panel_handle, 1, 0); //mirror_x
esp_lcd_panel_swap_xy(*panel_handle, 1);//swap_xy
#else
esp_lcd_panel_invert_color(*panel_handle, true);// Set inversion for esp32s3eye
#endif
// turn on display
esp_lcd_panel_disp_on_off(*panel_handle, true);
gpio_set_level(LCD_BCKL, LCD_BK_LIGHT_ON_LEVEL);
return ESP_OK;
}
#endif