-
Notifications
You must be signed in to change notification settings - Fork 10
+ TFT attempts to bug-fix #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| #define MADCTL_ROW_COLUMN_EXCHANGE (1<<5) | ||
| #define MADCTL_COLUMN_ADDRESS_ORDER_SWAP (1<<6) | ||
|
|
||
| uint8_t INVERSION = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using all-caps for a variable name like |
||
|
|
||
| #define CHECK_BIT(var, pos) (((var)>>(pos)) & 1) | ||
|
|
||
|
|
@@ -58,36 +59,6 @@ static int graphics_buffer_shift_y = 0; | |
|
|
||
| enum graphics_mode_t graphics_mode = GRAPHICSMODE_DEFAULT; | ||
|
|
||
| static const uint8_t init_seq[] = { | ||
| 1, 20, 0x01, // Software reset | ||
| 1, 10, 0x11, // Exit sleep mode | ||
| 2, 2, 0x3a, 0x55, // Set colour mode to 16 bit | ||
| #ifdef ILI9341 | ||
| // ILI9341 | ||
| #ifdef INVERSION | ||
| // ILI9341: переворот и инверсия цветов | ||
| 2, 0, 0x36, MADCTL_MY | MADCTL_MX | MADCTL_ROW_COLUMN_EXCHANGE | MADCTL_BGR_PIXEL_ORDER, // flip + BGR | ||
| #else | ||
| 2, 0, 0x36, MADCTL_ROW_COLUMN_EXCHANGE | MADCTL_BGR_PIXEL_ORDER, // Set MADCTL | ||
| #endif | ||
| #else | ||
| // ST7789 | ||
| 2, 0, 0x36, MADCTL_COLUMN_ADDRESS_ORDER_SWAP | MADCTL_ROW_COLUMN_EXCHANGE, // Set MADCTL | ||
| #endif | ||
| 5, 0, 0x2a, 0x00, 0x00, SCREEN_WIDTH >> 8, SCREEN_WIDTH & 0xff, // CASET: column addresses | ||
| 5, 0, 0x2b, 0x00, 0x00, SCREEN_HEIGHT >> 8, SCREEN_HEIGHT & 0xff, // RASET: row addresses | ||
| #ifdef INVERSION | ||
| 1, 2, 0x21, // Inversion ON | ||
| #else | ||
| 1, 2, 0x20, // Inversion OFF | ||
| #endif | ||
| 1, 2, 0x13, // Normal display on, then 10 ms delay | ||
| 1, 2, 0x29, // Main screen turn on, then wait 500 ms | ||
| 0 // Terminate list | ||
| }; | ||
| // Format: cmd length (including cmd byte), post delay in units of 5 ms, then cmd payload | ||
| // Note the delays have been shortened a little | ||
|
|
||
| static inline void lcd_set_dc_cs(const bool dc, const bool cs) { | ||
| sleep_us(5); | ||
| gpio_put_masked((1u << TFT_DC_PIN) | (1u << TFT_CS_PIN), !!dc << TFT_DC_PIN | !!cs << TFT_CS_PIN); | ||
|
|
@@ -182,10 +153,39 @@ void graphics_init() { | |
|
|
||
| gpio_put(TFT_CS_PIN, 1); | ||
| gpio_put(TFT_RST_PIN, 1); | ||
|
|
||
| const uint8_t init_seq[] = { | ||
| 1, 20, 0x01, // Software reset | ||
| 1, 10, 0x11, // Exit sleep mode | ||
| 2, 2, 0x3a, 0x55, // Set colour mode to 16 bit | ||
| 1, 2, 0x20 + INVERSION, | ||
| #ifdef ILI9341 | ||
| // ILI9341 | ||
| #ifdef INVERSION | ||
| // ILI9341: переворот и инверсия цветов | ||
| 2, 0, 0x36, MADCTL_MY | MADCTL_MX | MADCTL_ROW_COLUMN_EXCHANGE | MADCTL_BGR_PIXEL_ORDER, // flip + BGR | ||
| #else | ||
| 2, 0, 0x36, MADCTL_ROW_COLUMN_EXCHANGE | MADCTL_BGR_PIXEL_ORDER, // Set MADCTL | ||
| #endif | ||
| #else | ||
|
Comment on lines
+162
to
+170
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The refactoring from a compile-time To fix this, you'll need to dynamically construct the |
||
| // ST7789 | ||
| // 2, 0, 0x36, MADCTL_COLUMN_ADDRESS_ORDER_SWAP | MADCTL_ROW_COLUMN_EXCHANGE, // Set MADCTL | ||
| 2, 0, 0x36, MADCTL_COLUMN_ADDRESS_ORDER_SWAP | 32, // Set MADCTL | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the magic number 2, 0, 0x36, MADCTL_COLUMN_ADDRESS_ORDER_SWAP | MADCTL_ROW_COLUMN_EXCHANGE, // Set MADCTL |
||
| #endif | ||
| 5, 0, 0x2a, 0x00, 0x00, SCREEN_WIDTH >> 8, SCREEN_WIDTH & 0xff, // CASET: column addresses | ||
| 5, 0, 0x2b, 0x00, 0x00, SCREEN_HEIGHT >> 8, SCREEN_HEIGHT & 0xff, // RASET: row addresses | ||
| 1, 2, 0x13, // Normal display on, then 10 ms delay | ||
| 1, 2, 0x29, // Main screen turn on, then wait 500 ms | ||
| 0 // Terminate list | ||
| }; | ||
| // Format: cmd length (including cmd byte), post delay in units of 5 ms, then cmd payload | ||
| // Note the delays have been shortened a little | ||
|
|
||
|
|
||
| lcd_init(init_seq); | ||
| gpio_put(TFT_LED_PIN, 1); | ||
|
|
||
| for (int i = 0; i < sizeof palette; i++) { | ||
| for (int i = 0; i < sizeof(palette) / sizeof(palette[0]); ++i) { | ||
| graphics_set_palette(i, 0x0000); | ||
| } | ||
| clrScr(0); | ||
|
|
@@ -236,6 +236,26 @@ void st7789_dma_pixels(const uint16_t* pixels, const uint num_pixels) { | |
| dma_channel_hw_addr(st7789_chan)->ctrl_trig = ctrl | DMA_CH0_CTRL_TRIG_INCR_READ_BITS; | ||
| } | ||
|
|
||
| static const uint16_t textmode_palette[16] = { | ||
| //R, G, B | ||
| RGB888(0x00,0x00, 0x00), //black | ||
| RGB888(0x00,0x00, 0xC4), //blue | ||
| RGB888(0x00,0xC4, 0x00), //green | ||
| RGB888(0x00,0xC4, 0xC4), //cyan | ||
| RGB888(0xC4,0x00, 0x00), //red | ||
| RGB888(0xC4,0x00, 0xC4), //magenta | ||
| RGB888(0xC4,0x7E, 0x00), //brown | ||
| RGB888(0xC4,0xC4, 0xC4), //light gray | ||
| RGB888(0x4E,0x4E, 0x4E), //dark gray | ||
| RGB888(0x4E,0x4E, 0xDC), //light blue | ||
| RGB888(0x4E,0xDC, 0x4E), //light green | ||
| RGB888(0x4E,0xF3, 0xF3), //light cyan | ||
| RGB888(0xDC,0x4E, 0x4E), //light red | ||
| RGB888(0xF3,0x4E, 0xF3), //light magenta | ||
| RGB888(0xF3,0xF3, 0x4E), //yellow | ||
| RGB888(0xFF,0xFF, 0xFF), //white | ||
| }; | ||
|
|
||
| void __inline __scratch_y("refresh_lcd") refresh_lcd() { | ||
| switch (graphics_mode) { | ||
| case TEXTMODE_DEFAULT: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,7 @@ SETTINGS settings = { | |
| .player_2_input = GAMEPAD1, | ||
| .nes_palette = 0, | ||
| .swap_ab = false, | ||
| .inversion = false | ||
| }; | ||
|
|
||
| static FATFS fs, fs1; | ||
|
|
@@ -1010,6 +1011,10 @@ int InfoNES_Menu() { | |
| return 1; | ||
| } | ||
|
|
||
| #ifdef TFT | ||
| extern "C" uint8_t INVERSION; | ||
| #endif | ||
|
|
||
| void load_config() { | ||
| char pathname[256]; | ||
| sprintf(pathname, "%s\\pico-nes.cfg", HOME_DIR); | ||
|
|
@@ -1021,6 +1026,9 @@ void load_config() { | |
| UINT br; | ||
| f_read(&fd, &settings, sizeof(settings), &br); | ||
| f_close(&fd); | ||
| #ifdef TFT | ||
| INVERSION = settings.inversion; | ||
| #endif | ||
| } | ||
|
|
||
| void save_config() { | ||
|
|
@@ -1099,6 +1107,10 @@ const MenuItem menu_items[] = { | |
| { "Flash frame: %s", ARRAY, &settings.flash_frame, nullptr,1, { "NO ", "YES" } }, | ||
| { "VGA Mode: %s", ARRAY, &settings.palette, nullptr,1, { "RGB333", "RGB222" } }, | ||
| #endif | ||
| #if TFT | ||
| { "" }, | ||
| { "Investion %s", ARRAY, &settings.inversion, nullptr, 1, { "NO ", "YES" } }, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| #endif | ||
| #if SOFTTV | ||
| { "" }, | ||
| { "TV system %s", ARRAY, &tv_out_mode.tv_system, nullptr, 1, { "PAL ", "NTSC" } }, | ||
|
|
@@ -1225,6 +1237,21 @@ int menu() { | |
| } | ||
| draw_text(result, x, y, color, bg_color); | ||
| } | ||
| if (gamepad1_bits.b || (gamepad1_bits.select && !gamepad1_bits.start) || | ||
| keyboard_bits.b || (keyboard_bits.select && !keyboard_bits.start) | ||
| ) { | ||
| exit = true; | ||
| } | ||
| #ifdef TFT | ||
| if (INVERSION != settings.inversion) { | ||
| save_config(); | ||
| f_unmount(""); | ||
| watchdog_enable(10, true); | ||
| while(true) { | ||
| tight_loop_contents(); | ||
| } | ||
| } | ||
| #endif | ||
| sleep_ms(100); | ||
| } | ||
| graphics_set_flashmode(settings.flash_line, settings.flash_frame); | ||
|
|
@@ -1447,8 +1474,10 @@ int main() { | |
| gpio_put(PICO_DEFAULT_LED_PIN, false); | ||
| } | ||
|
|
||
| #ifndef TFT | ||
| uint8_t link = testPins(VGA_BASE_PIN, VGA_BASE_PIN + 1); | ||
| SELECT_VGA = (link == 0) || (link == 0x1F); | ||
| #endif | ||
|
|
||
| // board_init(); | ||
| tuh_init(BOARD_TUH_RHPORT); | ||
|
|
@@ -1460,14 +1489,14 @@ int main() { | |
| #if USE_NESPAD | ||
| nespad_begin(clock_get_hz(clk_sys) / 1000, NES_GPIO_CLK, NES_GPIO_DATA, NES_GPIO_LAT); | ||
| #endif | ||
| sem_init(&vga_start_semaphore, 0, 1); | ||
| multicore_launch_core1(render_core); | ||
| sem_release(&vga_start_semaphore); | ||
|
|
||
| f_mount(&fs, "", 1); | ||
| f_mkdir(HOME_DIR); | ||
| load_config(); | ||
|
|
||
| sem_init(&vga_start_semaphore, 0, 1); | ||
| multicore_launch_core1(render_core); | ||
| sem_release(&vga_start_semaphore); | ||
|
|
||
| #ifndef BUILD_IN_GAMES | ||
| if (is_start_locked() || !parseROM(reinterpret_cast<const uint8_t *>(rom))) { | ||
| InfoNES_Menu(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While adding
#ifndefprevents a compilation error, it introduces a more subtle issue. TheRGB888macro is also defined indrivers/st7789/st7789.hto perform a 24-bit to 16-bit color conversion. WhenTFTis enabled,graphics.hincludesst7789.h, and due to this#ifndef, the 16-bit conversion macro fromst7789.his used everywhereRGB888is called. This is problematic in files likesrc/main.cpp, whereNesPalette888is initialized withRGB888and expects 24-bit color values, not 16-bit. This can lead to incorrect colors.A better approach is to have uniquely named macros for different functionalities. I recommend the following changes:
drivers/st7789/st7789.h, renameRGB888to something likeRGB888_TO_565.drivers/st7789/st7789.c, update thetextmode_paletteinitialization to use the new macro name.drivers/graphics/graphics.h), you can then safely defineRGB888for 24-bit color without the#ifndefguard.This will make the code clearer and prevent subtle color bugs.