"""Airband receiver control program. MicroPython on a Pico 2. Runs at power-up as main.py. Controls: Knob tune. 25 kHz per click, or 1 MHz per click in coarse mode. In channel mode it steps through the known channels instead. On the squelch screen it sets the squelch level. Knob push switch between the tuning screen and the squelch screen Button 1 switch between 25 kHz and 1 MHz steps Button 3 switch between tuning and channel mode Button 2 not used yet Tuning screen: 119.650 MHz *25k frequency, * while a signal has the squelch open, then the step (25k or 1M) or CH in channel mode HND App S/W the channel's name when the frequency is a known one Squelch screen: Squelch 0.66V the level; "off" at zero, which leaves the audio always on Now 0.47V shut the signal strength right now, and whether the audio is on Squelch: the audio comes on when the signal strength (RSSI) rises above the level. It goes off again once the RSSI has stayed below the level, less a small margin, for HANG_MS. MUTE high = audio off. To put this on the Pico, from airband_receiver_kicad/: uvx mpremote cp bridge/firmware/lcd.py bridge/firmware/si5351.py bridge/firmware/channels.py bridge/firmware/main.py : """ from machine import Pin, I2C, ADC import time from lcd import Lcd, has_power from si5351 import Si5351 from channels import CHANNELS IF1_KHZ = 10700 # LO1 = receive frequency - 10.7 MHz LO2_HZ = 10_250_000 XTAL_HZ = 25_000_000 # the Si5351 module's crystal. Adjust this to correct a frequency error BAND = (118000, 136975) # kHz STEP_FINE, STEP_COARSE = 25, 1000 START_KHZ = 119650 KNOB_DIRECTION = 1 # set to -1 if clockwise tunes down STEPS_PER_CLICK = 4 SQUELCH_START_MV = 660 # the bring-up log suggests 0.6-0.7 V: the floor is about 0.46 V SQUELCH_STEP_MV, SQUELCH_MAX_MV = 20, 2000 SQUELCH_MARGIN_MV = 40 # how far below the level the RSSI must fall before the audio goes off HANG_MS = 250 # and for how long NAMES = dict(CHANNELS) STEP = (0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0) enc_a = Pin(1, Pin.IN, Pin.PULL_UP) enc_b = Pin(2, Pin.IN, Pin.PULL_UP) steps = 0 enc_state = (enc_a.value() << 1) | enc_b.value() def turned(_pin): global steps, enc_state new = (enc_a.value() << 1) | enc_b.value() steps += STEP[(enc_state << 2) | new] enc_state = new class Button: """A switch to ground. pressed() is True once per press.""" def __init__(self, gpio): self.pin = Pin(gpio, Pin.IN, Pin.PULL_UP) self.was_down = False self.since = time.ticks_ms() def pressed(self): down = not self.pin.value() now = time.ticks_ms() if down == self.was_down or time.ticks_diff(now, self.since) < 30: return False self.was_down = down self.since = now return down def nearest_channel(khz): return min(range(len(CHANNELS)), key=lambda i: abs(CHANNELS[i][0] - khz)) def main(): Pin(23, Pin.OUT, value=1) # the Pico's own supply in its quieter mode mute = Pin(12, Pin.OUT, value=1) # start with the audio off rssi_pin = ADC(26) # sets GP26 up as an analogue pin led = Pin('LED', Pin.OUT, value=1) knob_push, step_button, mode_button = Button(0), Button(8), Button(10) enc_a.irq(turned, Pin.IRQ_RISING | Pin.IRQ_FALLING) enc_b.irq(turned, Pin.IRQ_RISING | Pin.IRQ_FALLING) lcd = Lcd() synth = Si5351(I2C(1, sda=Pin(14), scl=Pin(15), freq=50000), XTAL_HZ) khz = START_KHZ coarse = False channel_mode = False squelch_screen = False squelch_mv = SQUELCH_START_MV rssi_mv = 0 audio_on = False last_strong = last_rssi_shown = time.ticks_ms() rssi_text = '' channel = nearest_channel(khz) clicks_seen = 0 lcd_ready = synth_ready = False tuned_khz = shown = None last_check = time.ticks_ms() while True: # The knob. clicks = (steps + 2) // STEPS_PER_CLICK moved, clicks_seen = (clicks - clicks_seen) * KNOB_DIRECTION, clicks if moved and squelch_screen: squelch_mv = max(0, min(SQUELCH_MAX_MV, squelch_mv + moved * SQUELCH_STEP_MV)) elif moved and channel_mode: channel = (channel + moved) % len(CHANNELS) khz = CHANNELS[channel][0] elif moved: khz = max(BAND[0], min(BAND[1], khz + moved * (STEP_COARSE if coarse else STEP_FINE))) # The buttons. if knob_push.pressed(): squelch_screen = not squelch_screen if step_button.pressed() and not channel_mode: coarse = not coarse if mode_button.pressed(): squelch_screen = False channel_mode = not channel_mode if channel_mode: channel = nearest_channel(khz) khz = CHANNELS[channel][0] # Power. On USB alone the LCD and the radio are off, and both forget their setup. idle = time.ticks_diff(time.ticks_ms(), last_check) > 500 if idle or not lcd_ready: last_check = time.ticks_ms() if not has_power(lcd): lcd_ready = synth_ready = False elif not lcd_ready: time.sleep_ms(300) lcd.start() lcd_ready, shown = True, None if lcd_ready and not synth_ready and synth.present(): tuned_khz = None # The radio. I2C is only used when the frequency changes. if lcd_ready and tuned_khz != khz: try: lo1_hz = (khz - IF1_KHZ) * 1000 if synth_ready: synth.tune(lo1_hz) else: synth.start(lo1_hz, LO2_HZ) synth_ready = True except OSError: synth_ready = False tuned_khz = khz led.value(synth_ready) # Squelch. One reading per pass, about 100 a second: the RSSI pin is a weak source. reading = rssi_pin.read_u16() * 3300 // 65535 rssi_mv = (rssi_mv + reading) // 2 ticks = time.ticks_ms() if squelch_mv == 0 or rssi_mv >= squelch_mv: audio_on, last_strong = True, ticks elif rssi_mv >= squelch_mv - SQUELCH_MARGIN_MV: last_strong = ticks if audio_on else last_strong elif time.ticks_diff(ticks, last_strong) > HANG_MS: audio_on = False mute.value(not audio_on) # The screen. if lcd_ready and squelch_screen: if time.ticks_diff(ticks, last_rssi_shown) > 250: # a steady number, four times a second last_rssi_shown = ticks rssi_text = 'Now {}.{:02d}V'.format(rssi_mv // 1000, rssi_mv % 1000 // 10) level = '{}.{:02d}V'.format(squelch_mv // 1000, squelch_mv % 1000 // 10) if squelch_mv else 'off' now = ('Squelch ' + level, '{} {}'.format(rssi_text, 'open' if audio_on else 'shut')) elif lcd_ready: tag = ' CH' if channel_mode else (' 1M' if coarse else '25k') name = NAMES.get(khz, '') if synth_ready else 'No radio on I2C' signal = '*' if audio_on and squelch_mv else ' ' now = ('{}.{:03d} MHz {}{}'.format(khz // 1000, khz % 1000, signal, tag), name) if lcd_ready: if now != shown: lcd.write(0, now[0]) lcd.write(1, now[1]) shown = now time.sleep_ms(10) main()