|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 Tim C for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025 Anne Barela for Adafruit Industries |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | +# |
| 6 | +import gc |
| 7 | +import supervisor |
| 8 | +import displayio |
| 9 | +from lvfontio import OnDiskFont |
| 10 | +from adafruit_fruitjam.peripherals import request_display_config |
| 11 | +from adafruit_color_terminal import ColorTerminal |
| 12 | + |
| 13 | +# Routine goto - position output to row, column |
| 14 | +def goto(row, column): |
| 15 | + terminal.write(f"\x1B[{int(row)};{int(column)}H") |
| 16 | + |
| 17 | +# Routine cls - clear screen |
| 18 | +def cls(): |
| 19 | + terminal.write("\x1B[2J") |
| 20 | + goto(1, 1) |
| 21 | + |
| 22 | +def draw_box(width, height, x, y, color_box): |
| 23 | + top_left = '\xDA' |
| 24 | + horizontal = '\xC4' |
| 25 | + top_right = '\xBF' |
| 26 | + vertical = '\xB3' |
| 27 | + bottom_left = '\xC0' |
| 28 | + bottom_right = '\xD9' |
| 29 | + |
| 30 | + if width < 3 or height < 3: |
| 31 | + return |
| 32 | + |
| 33 | + # Top line |
| 34 | + out_char(top_left + horizontal*(width - 2) + top_right, x, y, color_box) |
| 35 | + |
| 36 | + # Middle lines |
| 37 | + for i in range(1, height - 1): |
| 38 | + out_char(vertical + ' '*(width - 2) + vertical, x+i, y, color_box) |
| 39 | + |
| 40 | + # Bottom line |
| 41 | + out_char(bottom_left + horizontal*(width - 2) + bottom_right, x+height-1, |
| 42 | + y, color_box) |
| 43 | + |
| 44 | +def out_char(value, row, column, color): |
| 45 | + goto(row, column) |
| 46 | + terminal.write(f"\033[3{color}m"+value) |
| 47 | + |
| 48 | +def set_color(palette_index): |
| 49 | + """Set FOREGROUND color only""" |
| 50 | + if not (0 <= palette_index <= 7): |
| 51 | + raise ValueError("Palette index must be between 0 and 7") |
| 52 | + terminal.write(f"\033[3{palette_index}m") |
| 53 | + |
| 54 | +def set_bgcolor(palette_index): |
| 55 | + """Set BACKGROUND color only""" |
| 56 | + if not (0 <= palette_index <= 7): |
| 57 | + raise ValueError("Palette index must be between 0 and 7") |
| 58 | + terminal.write(f"\033[4{palette_index}m") |
| 59 | + |
| 60 | +def reset_ansi(): |
| 61 | + """Reset to default colors (usually white on black)""" |
| 62 | + terminal.write("\033[0m") |
| 63 | + |
| 64 | +def set_colors(fg_color=None, bg_color=None): |
| 65 | + """Set both foreground and background colors""" |
| 66 | + if fg_color is not None: |
| 67 | + set_color(fg_color) |
| 68 | + if bg_color is not None: |
| 69 | + set_bgcolor(bg_color) |
| 70 | + |
| 71 | +# Use the easy library call to set the resolution |
| 72 | +request_display_config(640, 480) |
| 73 | + |
| 74 | +print(f"\nFree before display init: {gc.mem_free()}") |
| 75 | + |
| 76 | +# Initialize display |
| 77 | +main_group = displayio.Group() |
| 78 | +display = supervisor.runtime.display |
| 79 | +display.root_group = main_group |
| 80 | + |
| 81 | +print(f"free: {gc.mem_free()}") |
| 82 | + |
| 83 | +colors = 8 |
| 84 | + |
| 85 | +red = 0xff0000 |
| 86 | +yellow = 0xcccc00 |
| 87 | +orange = 0xff5500 |
| 88 | +blue = 0x0000ff |
| 89 | +pink = 0xff00ff |
| 90 | +purple = 0x5500ff |
| 91 | +white = 0xffffff |
| 92 | +green = 0x00ff00 |
| 93 | +aqua = 0x125690 |
| 94 | +black = 0x000000 |
| 95 | + |
| 96 | +terminal_palette = displayio.Palette(colors) |
| 97 | +terminal_palette[0] = black |
| 98 | +terminal_palette[1] = red |
| 99 | +terminal_palette[2] = green |
| 100 | +terminal_palette[3] = yellow |
| 101 | +terminal_palette[4] = blue |
| 102 | +terminal_palette[5] = purple |
| 103 | +terminal_palette[6] = aqua |
| 104 | +terminal_palette[7] = white |
| 105 | + |
| 106 | +font = OnDiskFont("fonts/cp437_16h.bin") # LVGL .bin font |
| 107 | + |
| 108 | +char_size = font.get_bounding_box() |
| 109 | +print(f"Character size: {char_size}") |
| 110 | +print(f"font.bitmap info: {font.bitmap.width}, {font.bitmap.height}") |
| 111 | + |
| 112 | +screen_size = (display.width // char_size[0], display.height // char_size[1]) |
| 113 | +print(f"Screen size: {screen_size}") |
| 114 | +print(f"Screen depth: {display.framebuffer.color_depth} bits") |
| 115 | + |
| 116 | +terminal = ColorTerminal(font, screen_size[0], screen_size[1]) |
| 117 | +main_group.append(terminal.tilegrid) |
| 118 | +print("Created Color Terminal") |
| 119 | + |
| 120 | +# Clear screen and set initial colors properly |
| 121 | +cls() |
| 122 | + |
| 123 | +# Test individual colored text - each should stay its own color |
| 124 | +out_char("Red (1,1)", 1, 1, 1) # Red text, then reset |
| 125 | +out_char("Green (10,1)", 10, 1, 2) # Green text, then reset |
| 126 | +out_char("Blue (1,30)", 1, 30, 4) # Blue text, then reset |
| 127 | +out_char("Yellow (10,30)", 10, 30, 3) # Yellow text, then reset |
| 128 | +out_char("Magenta (15,1)", 15, 1, 5) # Magenta text, then reset |
| 129 | +out_char("Cyan (15,30)", 15, 30, 6) # Cyan text, then reset |
| 130 | + |
| 131 | +# Draw box in a specific color (should be yellow) |
| 132 | +draw_box(10, 8, 6, 17, 3) |
| 133 | + |
| 134 | +# Write some normal text (should be in default color) |
| 135 | +goto(20, 5) |
| 136 | +terminal.write("This should be default color") |
| 137 | + |
| 138 | +# Write more colored text using out_char |
| 139 | +out_char("Isolated Red", 22, 5, 1) |
| 140 | +out_char("Isolated Green", 23, 5, 2) |
| 141 | +out_char("Isolated Blue", 24, 5, 4) |
| 142 | + |
| 143 | +# Final text should be in default color |
| 144 | +goto(26, 5) |
| 145 | +terminal.write("This should also be default color") |
| 146 | + |
| 147 | +print("Done - all colors should be visible simultaneously") |
| 148 | +while True: |
| 149 | + pass |
0 commit comments