#define TFT_DC 17
#define TFT_RST 20
#define SCR_WD 240
#define SCR_HT 240 // 320 - to allow access to full 240x320 frame buffer
import AceTime
import FixMath
import random
import st7789
from machine import Pin, SPI
// Configuración de la pantalla
spi = SPI(1, baudrate=40000000, polarity=1, phase=1)
tft = st7789.ST7789(
spi,
240,
240,
reset=Pin(20, Pin.OUT),
dc=Pin(17, Pin.OUT),
cs=Pin(15, Pin.OUT),
rotation=2
)
// Colores
BLACK = st7789.color565(0, 0, 0)
WHITE = st7789.color565(255, 255, 255)
YELLOW = st7789.color565(255, 255, 0)
SKY_BLUE = st7789.color565(3, 155, 255)
BROWN = st7789.color565(81, 64, 0)
// Constantes
HOR = 240
XC = 120
YC = 120
DEG2RAD = math.pi / 180
last_roll = 0
last_pitch = 0
// Dibuja el horizonte
def draw_horizon(roll, pitch):
global last_roll, last_pitch
sx = math.cos(roll * DEG2RAD)
sy = math.sin(roll * DEG2RAD)
x0 = int(sx * HOR)
y0 = int(sy * HOR)
// Línea central
tft.line(XC - x0, YC - y0 - pitch, XC + x0, YC + y0 - pitch, WHITE)
last_roll = roll
last_pitch = pitch
// Actualiza el horizonte
def update_horizon(roll, pitch):
global last_roll, last_pitch
while last_pitch != pitch or last_roll != roll:
delta_pitch = 1 if last_pitch < pitch else -1 if last_pitch > pitch else 0
delta_roll = 1 if last_roll < roll else -1 if last_roll > roll else 0
draw_horizon(last_roll + delta_roll, last_pitch + delta_pitch)
// Prueba de roll
def test_roll():
for _ in range(360):
roll = int(180 * math.sin(time.ticks_ms() * DEG2RAD / 10))
update_horizon(roll, 0)
time.sleep_ms(10)
// Prueba de pitch
def test_pitch():
for p in range(-80, 80):
update_horizon(0, p)
time.sleep_ms(10)
for p in range(80, -1, -1):
update_horizon(0, p)
time.sleep_ms(10)
// Inicio
def main():
tft.fill(BLACK)
tft.fill_rect(0, 0, 240, 120, SKY_BLUE)
tft.fill_rect(0, 120, 240, 120, BROWN)
draw_horizon(0, 0)
time.sleep(2)
test_roll()
test_pitch()
while True:
roll = random.randint(-180, 180)
pitch = random.randint(-80, 80)
update_horizon(roll, pitch)
time.sleep_ms(100)
main()