1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #include "tft_display_driver.h" #include "hal_spi.h" #include "hal_gpio.h" #include "stdio.h" #include "string.h"
#define TFT_SPI_DEVICE SPI_DEVICE_1 #define TFT_CS_PORT GPIO_PORT_B #define TFT_CS_PIN 0 #define TFT_RST_PORT GPIO_PORT_B #define TFT_RST_PIN 1 #define TFT_DC_PORT GPIO_PORT_B #define TFT_DC_PIN 2
#define TFT_BLACK 0x0000 #define TFT_WHITE 0xFFFF #define TFT_RED 0xF800 #define TFT_GREEN 0x07E0 #define TFT_BLUE 0x001F #define TFT_YELLOW 0xFFE0
#define FONT_WIDTH_SMALL 6 #define FONT_HEIGHT_SMALL 8
const uint8_t font_data_small[256][8] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00}, };
bool tft_driver_init(void) { printf("TFT Driver: Initializing\n");
hal_gpio_init(TFT_CS_PORT, TFT_CS_PIN, GPIO_DIRECTION_OUTPUT); hal_gpio_init(TFT_RST_PORT, TFT_RST_PIN, GPIO_DIRECTION_OUTPUT); hal_gpio_init(TFT_DC_PORT, TFT_DC_PIN, GPIO_DIRECTION_OUTPUT);
spi_config_t spi_config = { .device = TFT_SPI_DEVICE, .clock_speed = 10000000, .mode = 0, .bit_order = 0 }; if (!hal_spi_init(&spi_config)) { printf("TFT Driver: SPI initialization failed\n"); return false; }
hal_gpio_set_level(TFT_RST_PORT, TFT_RST_PIN, GPIO_LEVEL_LOW); for(volatile int i=0; i<100000; i++); hal_gpio_set_level(TFT_RST_PORT, TFT_RST_PIN, GPIO_LEVEL_HIGH); for(volatile int i=0; i<100000; i++);
printf("TFT Driver: Sending initialization commands\n"); uint8_t init_cmds[] = { 0x01, 0x02, 0x03 }; hal_gpio_set_level(TFT_CS_PORT, TFT_CS_PIN, GPIO_LEVEL_LOW); hal_gpio_set_level(TFT_DC_PORT, TFT_DC_PIN, GPIO_LEVEL_LOW); hal_spi_transfer(TFT_SPI_DEVICE, init_cmds, NULL, sizeof(init_cmds)); hal_gpio_set_level(TFT_CS_PORT, TFT_CS_PIN, GPIO_LEVEL_HIGH);
tft_driver.clear_screen(COLOR_BLACK);
return true; }
bool tft_driver_set_orientation(display_orientation_t orientation) { printf("TFT Driver: Setting orientation %d\n", orientation); return true; }
bool tft_driver_clear_screen(tft_color_t color) { printf("TFT Driver: Clearing screen with color %d\n", color); uint16_t color_code = 0; switch (color) { case COLOR_BLACK: color_code = TFT_BLACK; break; case COLOR_WHITE: color_code = TFT_WHITE; break; case COLOR_RED: color_code = TFT_RED; break; case COLOR_GREEN: color_code = TFT_GREEN; break; case COLOR_BLUE: color_code = TFT_BLUE; break; case COLOR_YELLOW:color_code = TFT_YELLOW;break; default: color_code = TFT_BLACK; break; }
uint8_t data[2]; data[0] = (color_code >> 8) & 0xFF; data[1] = color_code & 0xFF;
hal_gpio_set_level(TFT_CS_PORT, TFT_CS_PIN, GPIO_LEVEL_LOW); hal_gpio_set_level(TFT_DC_PORT, TFT_DC_PIN, GPIO_LEVEL_HIGH);
for (int y = 0; y < 320; y++) { for (int x = 0; x < 240; x++) { hal_spi_transfer(TFT_SPI_DEVICE, data, NULL, 2); } } hal_gpio_set_level(TFT_CS_PORT, TFT_CS_PIN, GPIO_LEVEL_HIGH); return true; }
bool tft_driver_draw_pixel(uint16_t x, uint16_t y, tft_color_t color) { return true; }
bool tft_driver_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, tft_color_t color) { return true; }
bool tft_driver_draw_rectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, tft_color_t color, bool fill) { return true; }
bool tft_driver_draw_circle(uint16_t x_center, uint16_t y_center, uint16_t radius, tft_color_t color, bool fill) { return true; }
bool tft_driver_draw_char(uint16_t x, uint16_t y, char ch, tft_color_t color, tft_font_t font) { if (font != FONT_SMALL) return false;
if (ch < 32 || ch > 126) ch = '?';
uint8_t char_index = ch - 32; const uint8_t *font_data = font_data_small[char_index];
for (int row = 0; row < FONT_HEIGHT_SMALL; row++) { for (int col = 0; col < FONT_WIDTH_SMALL; col++) { if ((font_data[row] >> col) & 0x01) { tft_driver.draw_pixel(x + col, y + row, color); } } } return true; }
bool tft_driver_draw_string(uint16_t x, uint16_t y, const char *str, tft_color_t color, tft_font_t font) { uint16_t current_x = x; uint16_t char_width = (font == FONT_SMALL) ? FONT_WIDTH_SMALL : 0; if (char_width == 0) return false;
for (int i = 0; str[i] != '\0'; i++) { tft_driver.draw_char(current_x, y, str[i], color, font); current_x += char_width; if (current_x > 240) break; } return true; }
bool tft_driver_draw_bitmap(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t *bitmap) { return true; }
bool tft_driver_set_backlight(uint8_t brightness) { printf("TFT Driver: Setting backlight brightness to %d%%\n", brightness); return true; }
tft_display_driver_t tft_driver = { .init = tft_driver_init, .set_orientation = tft_driver_set_orientation, .clear_screen = tft_driver_clear_screen, .draw_pixel = tft_driver_draw_pixel, .draw_line = tft_driver_draw_line, .draw_rectangle = tft_driver_draw_rectangle, .draw_circle = tft_driver_draw_circle, .draw_char = tft_driver_draw_char, .draw_string = tft_driver_draw_string, .draw_bitmap = tft_driver_draw_bitmap, .set_backlight = tft_driver_set_backlight };
|