Skip to content

Instantly share code, notes, and snippets.

@akx

akx/telsu-c.c Secret

Created November 3, 2024 17:45
Show Gist options
  • Save akx/410ade53e5fd94738366be2be59a9ce9 to your computer and use it in GitHub Desktop.
Save akx/410ade53e5fd94738366be2be59a9ce9 to your computer and use it in GitHub Desktop.
#include "esp_chip_info.h"
#include "led_strip.h"
#include <driver/gpio.h>
#include <driver/spi_common.h>
#include <esp_lcd_io_spi.h>
#include <esp_lcd_panel_dev.h>
#include <esp_lcd_panel_ops.h>
#include <esp_lcd_panel_st7789.h>
#include <esp_log.h>
#include <esp_random.h>
#include <esp_timer.h>
#include <inttypes.h>
#include <stdio.h>
#define LCD_HOST SPI2_HOST
led_strip_handle_t led_strip;
uint16_t rgb888_to_rgb565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
void set_debug_led(uint8_t red, uint8_t green, uint8_t blue) {
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, 0, red, green, blue));
ESP_ERROR_CHECK(led_strip_refresh(led_strip));
}
void cfg_led() {
// WS2812 on GPIO 8
/* LED strip initialization with the GPIO and pixels number*/
led_strip_config_t strip_config = {
.strip_gpio_num =
8, // The GPIO that connected to the LED strip's data line
.max_leds = 1, // The number of LEDs in the strip,
.led_pixel_format =
LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip
.led_model = LED_MODEL_WS2812, // LED strip model
.flags.invert_out = false, // whether to invert the output signal (useful
// when your hardware has a level inverter)
};
led_strip_rmt_config_t rmt_config = {
.clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to
// different power consumption
.resolution_hz = 10 * 1000 * 1000,
.flags.with_dma = false, // whether to enable the DMA feature
};
printf("Configuring LED strip!\n");
ESP_ERROR_CHECK(
led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
set_debug_led(0, 0, 0);
}
void app_main(void) {
esp_log_level_set("*", ESP_LOG_DEBUG);
esp_log_level_set("spi_master", ESP_LOG_INFO);
printf("Hello world!\n");
cfg_led();
printf("Setting backlight!\n");
ESP_ERROR_CHECK(gpio_set_level(23, 1));
spi_bus_config_t buscfg = {
.sclk_io_num = 19,
.mosi_io_num = 18,
.miso_io_num = 15,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 320 * 240 * sizeof(uint16_t),
};
printf("Initializing SPI bus!\n");
ESP_ERROR_CHECK(spi_bus_initialize(
LCD_HOST, &buscfg, SPI_DMA_CH_AUTO)); // Enable the DMA feature
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = 21,
.cs_gpio_num = 20,
.pclk_hz = 80000000,
.lcd_cmd_bits = 8,
.lcd_param_bits = 8,
.spi_mode = 3,
.trans_queue_depth = 10,
};
printf("Initializing SPI panel!\n");
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST,
&io_config, &io_handle));
esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = 22,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
.bits_per_pixel = 16,
.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
};
printf("Initializing ST7789 panel!\n");
ESP_ERROR_CHECK(
esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
printf("Initializing panel!\n");
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
printf("Drawing bitmap!\n");
uint16_t *data = malloc(320 * 240 * sizeof(uint16_t));
uint32_t i = 0;
for (int y = 0; y < 320; y++) {
for (int x = 0; x < 240; x++) {
data[i++] = rgb888_to_rgb565(x ^ y, (x ^ y) >> 1, (x ^ y) >> 2);
}
}
uint32_t frame_times[10];
for (uint32_t frame = 0;; frame++) {
uint32_t start_time = esp_timer_get_time();
for(int x = 0; x < 100; x++) {
uint32_t off1 = esp_random() % (320 * 240 * sizeof(uint16_t));
uint32_t off2 = esp_random() % (320 * 240 * sizeof(uint16_t));
uint16_t tmp = data[off1];
data[off1] = data[off2];
data[off2] = tmp;
}
ESP_ERROR_CHECK(
esp_lcd_panel_draw_bitmap(panel_handle, 0, 0, 240, 320, data));
uint32_t end_time = esp_timer_get_time();
frame_times[frame % 10] = end_time - start_time;
if (frame % 10 == 9) {
uint32_t avg = 0;
for (int i = 0; i < 10; i++) {
avg += frame_times[i];
}
avg /= 10;
uint32_t fps = 1000000 / avg;
printf("Frame: %lu, Avg frame time: %lu usec; FPS: %lu\n",
frame, avg, fps);
}
set_debug_led(frame & 0xFF, ((frame >> 1) & 0xFF) ^ 0x73, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment