#include "LCD_driver.h" #include #include extern "C" { #include "FreeRTOS.h" #include "task.h" } // 共通変数の定義 static float current_hue = 0.0f; void update_hue_on_tap() { current_hue += 30.0f; if (current_hue >= 360.0f) current_hue -= 360.0f; } void hsb_to_rgb(float h, float s, float b, uint16_t &rgb) { float r, g, bl; float c = b * s; float x = c * (1 - std::abs(std::fmod(h / 60.0, 2) - 1)); float m = b - c; if (h < 60) { r = c; g = x; bl = 0; } else if (h < 120) { r = x; g = c; bl = 0; } else if (h < 180) { r = 0; g = c; bl = x; } else if (h < 240) { r = 0; g = x; bl = c; } else if (h < 300) { r = x; g = 0; bl = c; } else { r = c; g = 0; bl = x; } uint8_t r5 = (uint8_t)((r + m) * 31); uint8_t g6 = (uint8_t)((g + m) * 63); uint8_t b5 = (uint8_t)((bl + m) * 31); rgb = (r5 << 11) | (g6 << 5) | b5; } void LCD_Driver::init() { spi_init(SPI_PORT, 40000 * 1000); gpio_set_function(PIN_SCK, GPIO_FUNC_SPI); gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI); gpio_set_function(PIN_MISO, GPIO_FUNC_SPI); gpio_init(PIN_CS); gpio_set_dir(PIN_CS, GPIO_OUT); gpio_put(PIN_CS, 1); gpio_init(PIN_DC); gpio_set_dir(PIN_DC, GPIO_OUT); gpio_put(PIN_DC, 1); gpio_init(PIN_RST); gpio_set_dir(PIN_RST, GPIO_OUT); gpio_put(PIN_RST, 1); gpio_init(PIN_T_CS); gpio_set_dir(PIN_T_CS, GPIO_OUT); gpio_put(PIN_T_CS, 1); writeCommand(0x01); sleep_ms(150); writeCommand(0x11); sleep_ms(150); writeCommand(0x3A); writeData(0x55); writeCommand(0x36); writeData(0x08); writeCommand(0x29); } void LCD_Driver::writeCommand(uint8_t cmd) { gpio_put(PIN_DC, 0); gpio_put(PIN_CS, 0); spi_write_blocking(SPI_PORT, &cmd, 1); gpio_put(PIN_CS, 1); } void LCD_Driver::writeData(uint8_t data) { gpio_put(PIN_DC, 1); gpio_put(PIN_CS, 0); spi_write_blocking(SPI_PORT, &data, 1); gpio_put(PIN_CS, 1); } void LCD_Driver::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { writeCommand(0x2A); writeData(x0 >> 8); writeData(x0 & 0xFF); writeData(x1 >> 8); writeData(x1 & 0xFF); writeCommand(0x2B); writeData(y0 >> 8); writeData(y0 & 0xFF); writeData(y1 >> 8); writeData(y1 & 0xFF); writeCommand(0x2C); } void LCD_Driver::drawHSBBackground() { setAddrWindow(0, 0, 239, 319); uint16_t color; hsb_to_rgb(current_hue, 1.0f, 1.0f, color); uint8_t high = color >> 8, low = color & 0xFF; gpio_put(PIN_DC, 1); gpio_put(PIN_CS, 0); for (int i = 0; i < 240 * 320; i++) { spi_write_blocking(SPI_PORT, &high, 1); spi_write_blocking(SPI_PORT, &low, 1); if (i % 1000 == 0) vTaskDelay(0); // 他のタスクに譲る } gpio_put(PIN_CS, 1); } bool is_touched() { gpio_set_dir(PIN_T_IRQ, GPIO_IN); gpio_set_function(PIN_T_IRQ, GPIO_FUNC_SIO); if (gpio_get(PIN_T_IRQ) == 0) { gpio_put(PIN_T_CS, 0); uint8_t cmd = 0x90; spi_write_blocking(SPI_PORT, &cmd, 1); uint8_t dummy; spi_read_blocking(SPI_PORT, 0, &dummy, 1); gpio_put(PIN_T_CS, 1); return true; } return false; }