"""LCD routine for the airband bridge (rev B). MicroPython on a Pico 2. This file has to be on the Pico for the tests to find it: uvx mpremote cp bridge/firmware/lcd.py :lcd.py Wiring, from bridge/README.md: RS = GP3 and D4-D7 = GP4-GP7 go straight to the LCD. E = GP11 goes through the level shifter. RW is tied low, so the LCD is write-only. Every command is followed by a fixed wait. RS and D4-D7 are only ever pulled low or released, never driven high. The LCD has its own pull-ups to 5 V on those lines, and the Pico's pull resistors are off. """ from machine import Pin import time RS, D4, D5, D6, D7, E = 3, 4, 5, 6, 7, 11 NAMES = ('RS', 'D4', 'D5', 'D6', 'D7') BLOCK = '\xff' # a solid cell in the LCD's built-in font class OpenDrain: """A pin that is either pulled low or let go. It never drives high.""" def __init__(self, gpio): self.pin = Pin(gpio, Pin.IN, pull=None) def set(self, high): if high: self.pin.init(Pin.IN, pull=None) # let go: the LCD's pull-up takes it to 5 V else: self.pin.init(Pin.OUT, pull=None, value=0) # the value is set before the pin starts driving def read(self): return self.pin.value() class Lcd: def __init__(self): self.rs = OpenDrain(RS) self.data = [OpenDrain(g) for g in (D4, D5, D6, D7)] self.e = Pin(E, Pin.OUT, value=0) def _nibble(self, n): for bit, line in enumerate(self.data): line.set((n >> bit) & 1) time.sleep_us(20) # a released line takes 1-2 us to rise self.e.value(1) time.sleep_us(5) self.e.value(0) # the LCD reads the lines on this falling edge time.sleep_us(100) # an ordinary command takes 37 us def _byte(self, value, is_text): self.rs.set(is_text) self._nibble(value >> 4) self._nibble(value & 0x0F) def command(self, value): self._byte(value, False) if value in (0x01, 0x02): time.sleep_ms(3) # clear and home are slow def start(self): time.sleep_ms(50) self.rs.set(False) for wait_ms in (5, 1, 1): # three times: gets the LCD to a known state whatever it was doing self._nibble(0x3) time.sleep_ms(wait_ms) self._nibble(0x2) # 4-bit mode self.command(0x28) # 2 rows, 5x8 dots self.command(0x08) # display off self.command(0x01) # clear self.command(0x06) # cursor moves right after each character self.command(0x0C) # display on, no cursor def write(self, row, text): self.command(0x80 | (0x40 * row)) for ch in '{:<16}'.format(text)[:16]: self._byte(ord(ch), True) def has_power(lcd): """True if the LCD has 5 V. Lets RS and the data lines go and looks for the LCD's pull-ups.""" lines = [lcd.rs] + lcd.data for line in lines: line.set(True) time.sleep_us(50) return any(line.read() for line in lines) def wait_for_power(lcd, seconds=60): """Wait until the LCD has 5 V. Returns False if it never arrives. The LCD's pull-ups only work when it has power, so a released line that reads high means the LCD is powered. An LCD that gets its power after start() has run misses the setup and shows garbage.""" lines = [lcd.rs] + lcd.data for line in lines: line.set(True) time.sleep_ms(2) waited = False deadline = time.ticks_add(time.ticks_ms(), seconds * 1000) while not any(line.read() for line in lines): if not waited: print('the LCD has no 5 V. Switch on the 12 V supply. Waiting up to {} s...'.format(seconds)) waited = True if time.ticks_diff(deadline, time.ticks_ms()) < 0: return False time.sleep_ms(100) if waited: print('the LCD has power now') time.sleep_ms(500) # let the supply settle return True