/ pi_radio / rfm9x_check.py
rfm9x_check.py
 1  # SPDX-FileCopyrightText: 2018 Brent Rubell for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Wiring Check, Pi Radio w/RFM9x
 7  
 8  Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
 9  Author: Brent Rubell for Adafruit Industries
10  """
11  import time
12  import busio
13  from digitalio import DigitalInOut, Direction, Pull
14  import board
15  # Import the SSD1306 module.
16  import adafruit_ssd1306
17  # Import the RFM9x radio module.
18  import adafruit_rfm9x
19  
20  # Button A
21  btnA = DigitalInOut(board.D5)
22  btnA.direction = Direction.INPUT
23  btnA.pull = Pull.UP
24  
25  # Button B
26  btnB = DigitalInOut(board.D6)
27  btnB.direction = Direction.INPUT
28  btnB.pull = Pull.UP
29  
30  # Button C
31  btnC = DigitalInOut(board.D12)
32  btnC.direction = Direction.INPUT
33  btnC.pull = Pull.UP
34  
35  # Create the I2C interface.
36  i2c = busio.I2C(board.SCL, board.SDA)
37  
38  # 128x32 OLED Display
39  reset_pin = DigitalInOut(board.D4)
40  display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, reset=reset_pin)
41  # Clear the display.
42  display.fill(0)
43  display.show()
44  width = display.width
45  height = display.height
46  
47  # Configure RFM9x LoRa Radio
48  CS = DigitalInOut(board.CE1)
49  RESET = DigitalInOut(board.D25)
50  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
51  
52  while True:
53      # Clear the image
54      display.fill(0)
55  
56      # Attempt to set up the RFM9x Module
57      try:
58          rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, 915.0)
59          display.text('RFM9x: Detected', 0, 0, 1)
60      except RuntimeError as error:
61          # Thrown on version mismatch
62          display.text('RFM9x: ERROR', 0, 0, 1)
63          print('RFM9x Error: ', error)
64  
65      # Check buttons
66      if not btnA.value:
67          # Button A Pressed
68          display.text('Ada', width-85, height-7, 1)
69          display.show()
70          time.sleep(0.1)
71      if not btnB.value:
72          # Button B Pressed
73          display.text('Fruit', width-75, height-7, 1)
74          display.show()
75          time.sleep(0.1)
76      if not btnC.value:
77          # Button C Pressed
78          display.text('Radio', width-65, height-7, 1)
79          display.show()
80          time.sleep(0.1)
81  
82      display.show()
83      time.sleep(0.1)