From 5de88cc12636d73dc7306f5f2aa06bc5a60e15eb Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Sat, 26 Jun 2021 07:46:25 +0100 Subject: [PATCH] Fix bug in vtest.py. Add pyboard setup example. --- gui/demos/vtest.py | 47 +++++++++-------- hardware_setup.py | 86 +++++++++++++++----------------- setup_examples/st7735r144_pyb.py | 60 ++++++++++++++++++++++ 3 files changed, 126 insertions(+), 67 deletions(-) create mode 100644 setup_examples/st7735r144_pyb.py diff --git a/gui/demos/vtest.py b/gui/demos/vtest.py index 7294389..fd3601d 100644 --- a/gui/demos/vtest.py +++ b/gui/demos/vtest.py @@ -35,28 +35,32 @@ class BackScreen(Screen): Label(wri, 2, 2, 'Ensure back refreshes properly') CloseButton(wri) -# Create a random vector. Interpolate between current vector and the new one. -# Change pointer color dependent on magnitude. + +# Update a pointer with a vector. Change pointer color dependent on magnitude. +def vset(ptr, v): + mag = abs(v) + if mag < 0.3: + ptr.value(v, BLUE) + elif mag < 0.7: + ptr.value(v, GREEN) + else: + ptr.value(v, RED) + +def get_vec(): # Return a random vector + mag = urandom.getrandbits(16) / 2**16 # 0..1 (well, almost 1) + phi = pi * urandom.getrandbits(16) / 2**15 # 0..2*pi + return rect(mag, phi) + async def ptr_test(dial): - ptr = Pointer(dial) - v = 0j + ptr0 = Pointer(dial) + ptr1 = Pointer(dial) steps = 20 # No. of interpolation steps - # BUG getting a weird visual flicker on occasion, with yellow - # being briefly displayed. Where is that coming from? - # Does not seem to be affected by max value. TODO - grv = lambda : urandom.getrandbits(16) / 2**15 - 1 # Random: range -1.0 to +0.999 while True: - v1 = grv() + 1j * grv() # Random vector - dv = (v1 - v) / steps # Interpolation vector + dv0 = (get_vec() - ptr0.value()) / steps # Interpolation vectors + dv1 = (get_vec() - ptr1.value()) / steps for _ in range(steps): - v += dv - mag = abs(v) - if mag < 0.3: - ptr.value(v, BLUE) - elif mag < 0.7: - ptr.value(v, GREEN) - else: - ptr.value(v, RED) + vset(ptr0, ptr0.value() + dv0) + vset(ptr1, ptr1.value() + dv1) await asyncio.sleep_ms(200) # Analog clock demo. @@ -77,6 +81,7 @@ async def aclock(dial, lbldate, lbltim): while True: t = time.localtime() + # Maths rotates vectors counterclockwise, hence - signs. hrs.value(hstart * uv(-t[3] * pi/6 - t[4] * pi / 360), CYAN) mins.value(mstart * uv(-t[4] * pi/30), CYAN) secs.value(sstart * uv(-t[5] * pi/30), RED) @@ -99,10 +104,10 @@ class VScreen(Screen): # Set up random vector display with two pointers dial = Dial(wri, 2, 2, height = 100, ticks = 12, fgcolor = YELLOW, style=Dial.COMPASS) self.reg_task(ptr_test(dial)) - self.reg_task(ptr_test(dial)) # Set up clock display: instantiate labels - lbldate = Label(wri, 110, 2, 200, **labels) - lbltim = Label(wri, 150, 2, 80, **labels) + gap = 4 + lbldate = Label(wri, dial.mrow + gap, 2, 200, **labels) + lbltim = Label(wri, lbldate.mrow + gap, 2, 80, **labels) dial = Dial(wri, 2, 120, height = 100, ticks = 12, fgcolor = GREEN, pip = GREEN) self.reg_task(aclock(dial, lbldate, lbltim)) diff --git a/hardware_setup.py b/hardware_setup.py index 5e4bd58..df02c1b 100644 --- a/hardware_setup.py +++ b/hardware_setup.py @@ -1,60 +1,54 @@ -# st7735r144_setup.py For my PCB with 1.44 inch 128*128 TFT Customise for your hardware config +# ili9341_pico.py Customise for your hardware config # Released under the MIT License (MIT). See LICENSE. -# Copyright (c) 2020 Peter Hinch +# Copyright (c) 2021 Peter Hinch # As written, supports: -# Adfruit 1.44 inch Color TFT LCD display with MicroSD Card Breakout: -# https://www.adafruit.com/product/2088 +# ili9341 240x320 displays on Pi Pico +# Edit the driver import for other displays. -# WIRING (Adafruit pin nos and names). -# Pyb SSD -# Gnd Gnd -# 3V3 Vcc -# Y11 RESET -# Y12 D/C -# W32 TFT_CS -# Y8 MOSI -# Y6 SCK -# Vin LITE (10) Backlight +# Demo of initialisation procedure designed to minimise risk of memory fail +# when instantiating the frame buffer. The aim is to do this as early as +# possible before importing other modules. -# Switch wiring -# X1 Next -# X2 Sel -# X3 Prev -# X4 Increase -# X5 Decrease +# WIRING +# Pico Display +# GPIO Pin +# 3v3 36 Vin +# IO6 9 CLK Hardware SPI0 +# IO7 10 DATA (AKA SI MOSI) +# IO8 11 DC +# IO9 12 Rst +# Gnd 13 Gnd +# IO10 14 CS -import machine +# Pushbuttons are wired between the pin and Gnd +# Pico pin Meaning +# 16 Operate current control +# 17 Decrease value of current control +# 18 Select previous control +# 19 Select next control +# 20 Increase value of current control + +from machine import Pin, SPI, freq import gc -from machine import Pin, SPI -import gc -import time - -from drivers.st7735r.st7735r144_4bit import ST7735R as SSD -pp = Pin('EN_3V3') -pp(1) -time.sleep(1) - -pdc = Pin('Y12', Pin.OUT_PP, value=0) -pcs = Pin('W32', Pin.OUT_PP, value=1) -prst = Pin('Y11', Pin.OUT_PP, value=1) -spi = SPI(2, baudrate=6_000_000) +from drivers.ili93xx.ili9341 import ILI9341 as SSD +freq(250_000_000) # RP2 overclock +# Create and export an SSD instance +pdc = Pin(8, Pin.OUT, value=0) # Arbitrary pins +prst = Pin(9, Pin.OUT, value=1) +pcs = Pin(10, Pin.OUT, value=1) +spi = SPI(0, baudrate=30_000_000) gc.collect() # Precaution before instantiating framebuf -ssd = SSD(spi, pcs, pdc, prst) # Create a display instance -from gui.core.ugui import Display +ssd = SSD(spi, pcs, pdc, prst, usd=True) +from gui.core.ugui import Display # Create and export a Display instance # Define control buttons -nxt = Pin(Pin.board.X5, Pin.IN, Pin.PULL_UP) # Move to next control -sel = Pin(Pin.board.X1, Pin.IN, Pin.PULL_UP) # Operate current control -prev = Pin(Pin.board.X4, Pin.IN, Pin.PULL_UP) # Move to previous control -increase = Pin(Pin.board.X2, Pin.IN, Pin.PULL_UP) # Increase control's value -decrease = Pin(Pin.board.X3, Pin.IN, Pin.PULL_UP) # Decrease control's value +nxt = Pin(19, Pin.IN, Pin.PULL_UP) # Move to next control +sel = Pin(16, Pin.IN, Pin.PULL_UP) # Operate current control +prev = Pin(18, Pin.IN, Pin.PULL_UP) # Move to previous control +increase = Pin(20, Pin.IN, Pin.PULL_UP) # Increase control's value +decrease = Pin(17, Pin.IN, Pin.PULL_UP) # Decrease control's value display = Display(ssd, nxt, sel, prev, increase, decrease) - - - - - diff --git a/setup_examples/st7735r144_pyb.py b/setup_examples/st7735r144_pyb.py new file mode 100644 index 0000000..5e4bd58 --- /dev/null +++ b/setup_examples/st7735r144_pyb.py @@ -0,0 +1,60 @@ +# st7735r144_setup.py For my PCB with 1.44 inch 128*128 TFT Customise for your hardware config + +# Released under the MIT License (MIT). See LICENSE. +# Copyright (c) 2020 Peter Hinch + +# As written, supports: +# Adfruit 1.44 inch Color TFT LCD display with MicroSD Card Breakout: +# https://www.adafruit.com/product/2088 + +# WIRING (Adafruit pin nos and names). +# Pyb SSD +# Gnd Gnd +# 3V3 Vcc +# Y11 RESET +# Y12 D/C +# W32 TFT_CS +# Y8 MOSI +# Y6 SCK +# Vin LITE (10) Backlight + +# Switch wiring +# X1 Next +# X2 Sel +# X3 Prev +# X4 Increase +# X5 Decrease + +import machine +import gc + +from machine import Pin, SPI +import gc +import time + +from drivers.st7735r.st7735r144_4bit import ST7735R as SSD +pp = Pin('EN_3V3') +pp(1) +time.sleep(1) + +pdc = Pin('Y12', Pin.OUT_PP, value=0) +pcs = Pin('W32', Pin.OUT_PP, value=1) +prst = Pin('Y11', Pin.OUT_PP, value=1) +spi = SPI(2, baudrate=6_000_000) +gc.collect() # Precaution before instantiating framebuf +ssd = SSD(spi, pcs, pdc, prst) # Create a display instance +from gui.core.ugui import Display + +# Create and export a Display instance +# Define control buttons +nxt = Pin(Pin.board.X5, Pin.IN, Pin.PULL_UP) # Move to next control +sel = Pin(Pin.board.X1, Pin.IN, Pin.PULL_UP) # Operate current control +prev = Pin(Pin.board.X4, Pin.IN, Pin.PULL_UP) # Move to previous control +increase = Pin(Pin.board.X2, Pin.IN, Pin.PULL_UP) # Increase control's value +decrease = Pin(Pin.board.X3, Pin.IN, Pin.PULL_UP) # Decrease control's value +display = Display(ssd, nxt, sel, prev, increase, decrease) + + + + +