Skip to content

Commit

Permalink
Port to gpiod/gpiodevice.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Jun 13, 2024
1 parent 6576183 commit 507a965
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
34 changes: 25 additions & 9 deletions examples/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import math
import pathlib
import time
import select

import RPi.GPIO as GPIO
import ST7789

import gpiod
import gpiodevice
from gpiod.line import Bias, Edge
import st7789
import yaml
from fonts.ttf import ManropeBold as UserFont
from PIL import Image, ImageDraw, ImageFont
Expand Down Expand Up @@ -94,7 +98,7 @@ def heading(self, data, units):
else:
data = "{:0.0f}".format(data)

tw, th = self._draw.textsize(data, self.font_large)
_, _, tw, th = self._draw.textbbox((0, 0), data, self.font_large)

self._draw.text(
(0, 32),
Expand Down Expand Up @@ -326,7 +330,7 @@ def render(self):
y = oy - math.cos(p) * radius

name = "".join([word[0] for word in name.split(" ")])
tw, th = self._draw.textsize(name, font=self.font_small)
_, _, tw, th = self._draw.textbbox((0, 0), name, font=self.font_small)
x -= tw / 2
y -= th / 2
self._draw.text((x, y), name, font=self.font_small, fill=COLOR_GREY)
Expand Down Expand Up @@ -507,12 +511,21 @@ def __init__(self, views):
self._current_view = 0
self._current_subview = 0

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setmode(GPIO.BCM)
#GPIO.setwarnings(False)
#GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#for pin in BUTTONS:
# GPIO.add_event_detect(pin, GPIO.FALLING, self.handle_button, bouncetime=200)

config = {}
for pin in BUTTONS:
GPIO.add_event_detect(pin, GPIO.FALLING, self.handle_button, bouncetime=200)
config[pin] = gpiod.LineSettings(edge_detection=Edge.FALLING, bias=Bias.PULL_UP)

chip = gpiodevice.find_chip_by_platform()
self._buttons = chip.request_lines(consumer="LTR559", config=config)
self._poll = select.poll()
self._poll.register(self._buttons.fd, select.POLLIN)

def handle_button(self, pin):
index = BUTTONS.index(pin)
Expand Down Expand Up @@ -562,6 +575,9 @@ def view(self):
return self.get_current_view()

def update(self):
if self._poll.poll(10):
for event in self._buttons.read_edge_events():
self.handle_button(event.line_offset)
self.view.update()

def render(self):
Expand Down Expand Up @@ -695,7 +711,7 @@ def update(self, interval=5.0):


def main():
display = ST7789.ST7789(
display = st7789.ST7789(
rotation=90,
port=0,
cs=1,
Expand Down
48 changes: 40 additions & 8 deletions weatherhat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import math
import threading
import time
import select

import ioexpander as io
import RPi.GPIO as GPIO
import gpiod
import gpiodevice
from gpiod.line import Bias, Edge
from bme280 import BME280
from ltr559 import LTR559
from smbus2 import SMBus
Expand All @@ -12,7 +15,6 @@

__version__ = '0.0.2'


# Wind Vane
PIN_WV = 8 # P0.3 ANE6

Expand Down Expand Up @@ -46,16 +48,25 @@
class WeatherHAT:
def __init__(self):
self.updated_wind_rain = False
self._interrupt_pin = 4
self._lock = threading.Lock()
self._i2c_dev = SMBus(1)

self._bme280 = BME280(i2c_dev=self._i2c_dev)
self._ltr559 = LTR559(i2c_dev=self._i2c_dev)

self._ioe = io.IOE(i2c_addr=0x12, interrupt_pin=4)
self._ioe = io.IOE(i2c_addr=0x12)

self._chip = gpiodevice.find_chip_by_platform()

# Fudge to enable pull-up on interrupt pin
self._ioe._gpio.setup(self._ioe._interrupt_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
self._int = self._chip.request_lines(
consumer="weatherhat",
config={
self._interrupt_pin: gpiod.LineSettings(
edge_detection=Edge.FALLING, bias=Bias.PULL_UP
)
}
)

# Input voltage of IO Expander, this is 3.3 on Breakout Garden
self._ioe.set_adc_vref(3.3)
Expand All @@ -77,8 +88,6 @@ def __init__(self):
self._ioe.set_mode(PIN_R5, io.IN_PU)
self._ioe.output(PIN_R3, 0)
self._ioe.set_pin_interrupt(PIN_R4, True)
self._ioe.on_interrupt(self.handle_ioe_interrupt)
self._ioe.clear_interrupt()

# Data API... kinda
self.temperature_offset = -7.5
Expand All @@ -101,6 +110,16 @@ def __init__(self):

self.reset_counts()

self._poll_thread = threading.Thread(target=self._t_poll_ioexpander)
self._poll_thread.start()

self._ioe.enable_interrupt_out()
self._ioe.clear_interrupt()

def __del__(self):
self._polling = False
self._poll_thread.join()

def reset_counts(self):
self._lock.acquire(blocking=True)
self._ioe.clear_switch_counter(PIN_ANE2)
Expand Down Expand Up @@ -134,8 +153,21 @@ def hpa_to_inches(self, hpa):
def degrees_to_cardinal(self, degrees):
value, cardinal = min(wind_degrees_to_cardinal.items(), key=lambda item: abs(item[0] - degrees))
return cardinal

def _t_poll_ioexpander(self):
self._polling = True
poll = select.poll()
poll.register(self._int.fd, select.POLLIN)
while self._polling:
if not poll.poll(10):
continue
for event in self._int.read_edge_events():
if event.line_offset == self._interrupt_pin:
self.handle_ioe_interrupt()
time.sleep(1.0 / 100)

def update(self, interval=60.0):

# Time elapsed since last update
delta = float(time.time() - self._t_start)

Expand Down Expand Up @@ -181,7 +213,7 @@ def update(self, interval=60.0):

self.rain = rain_hz * RAIN_MM_PER_TICK

def handle_ioe_interrupt(self, pin):
def handle_ioe_interrupt(self):
self._lock.acquire(blocking=True)
self._ioe.clear_interrupt()

Expand Down

0 comments on commit 507a965

Please sign in to comment.