Skip to content

Commit

Permalink
Merge pull request #29 from pimoroni/repackage
Browse files Browse the repository at this point in the history
Repackage to pyproject/toml, port to gpiod/gpiodevice
  • Loading branch information
Gadgetoid authored Jun 21, 2024
2 parents 30ff0b2 + 9278003 commit e8184bd
Show file tree
Hide file tree
Showing 20 changed files with 460 additions and 225 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:

steps:
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}

Expand All @@ -35,7 +35,7 @@ jobs:
make build
- name: Upload Packages
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ env.RELEASE_FILE }}
path: dist/
path: dist/
11 changes: 7 additions & 4 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ jobs:
test:
name: linting & spelling
runs-on: ubuntu-latest

env:
TERM: xterm-256color

steps:
- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Python '3,11'
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: '3.11'

Expand All @@ -33,4 +32,8 @@ jobs:
- name: Run Code Checks
run: |
make check
make check
- name: Run Bash Code Checks
run: |
make shellcheck
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}

Expand All @@ -37,4 +37,5 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python -m pip install coveralls
coveralls --service=github
coveralls --service=github
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.0.0
-----

* Repackage to hatch/pyproject
* Port to gpiod (Pi 5 support)

0.0.2
-----

Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ endif
@echo "deploy: build and upload to PyPi"
@echo "tag: tag the repository with the current version\n"

version:
@hatch version

install:
./install.sh --unstable

Expand All @@ -30,11 +33,14 @@ uninstall:

dev-deps:
python3 -m pip install -r requirements-dev.txt
sudo apt install dos2unix
sudo apt install dos2unix shellcheck

check:
@bash check.sh

shellcheck:
shellcheck *.sh

qa:
tox -e qa

Expand All @@ -44,7 +50,7 @@ pytest:
nopost:
@bash check.sh --nopost

tag:
tag: version
git tag -a "v${LIBRARY_VERSION}" -m "Version ${LIBRARY_VERSION}"

build: check
Expand Down
69 changes: 51 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,74 @@
[![PyPi Package](https://img.shields.io/pypi/v/weatherhat.svg)](https://pypi.python.org/pypi/weatherhat)
[![Python Versions](https://img.shields.io/pypi/pyversions/weatherhat.svg)](https://pypi.python.org/pypi/weatherhat)

# Pre-requisites
Weather HAT is a tidy all-in-one solution for hooking up climate and environmental sensors to a Raspberry Pi. It has a bright 1.54" LCD screen and four buttons for inputs. The onboard sensors can measure temperature, humidity, pressure and light. The RJ11 connectors will let you easily attach wind and rain sensors. It will work with any Raspberry Pi with a 40 pin header.

This library requires Python ≥3.6 so we'd recommend using it with Raspberry Pi OS Buster or later.
## Where to buy

You must enable:
* [Weather HAT](https://shop.pimoroni.com/products/weather-hat-only)
* [Weather HAT + Weather Sensors Kit](https://shop.pimoroni.com/products/weather-hat)

* i2c: `sudo raspi-config nonint do_i2c 0`
* spi: `sudo raspi-config nonint do_spi 0`
# Installing

You can optionally run `sudo raspi-config` or the graphical Raspberry Pi Configuration UI to enable interfaces.
We'd recommend using this library with Raspberry Pi OS Bookworm or later. It requires Python ≥3.7.

# Installing
## Full install (recommended):

We've created an easy installation script that will install all pre-requisites and get your Weather HAT
up and running with minimal efforts. To run it, fire up Terminal which you'll find in Menu -> Accessories -> Terminal
on your Raspberry Pi desktop, as illustrated below:

![Finding the terminal](http://get.pimoroni.com/resources/github-repo-terminal.png)

In the new terminal window type the commands exactly as it appears below (check for typos) and follow the on-screen instructions:

```bash
git clone https://github.com/pimoroni/weatherhat-python
cd weatherhat-python
./install.sh
```

**Note** Libraries will be installed in the "pimoroni" virtual environment, you will need to activate it to run examples:

Stable library from PyPi:
```
source ~/.virtualenvs/pimoroni/bin/activate
```

## Development:

If you want to contribute, or like living on the edge of your seat by having the latest code, you can install the development version like so:

```bash
git clone https://github.com/pimoroni/weatherhat-python
cd weatherhat-python
./install.sh --unstable
```

## Install stable library from PyPi and configure manually

* Set up a virtual environment: `python3 -m venv --system-site-packages $HOME/.virtualenvs/pimoroni`
* Switch to the virtual environment: `source ~/.virtualenvs/pimoroni/bin/activate`
* Install the library: `pip install weatherhat`

* Just run `pip3 install weatherhat`
In some cases you may need to us `sudo` or install pip with: `sudo apt install python3-pip`.

In some cases you may need to use `sudo` or install pip with: `sudo apt install python3-pip`
This will not make any configuration changes, so you may also need to enable:

Latest/development library from GitHub:
* i2c: `sudo raspi-config nonint do_i2c 0`
* spi: `sudo raspi-config nonint do_spi 0`

* `git clone https://github.com/pimoroni/weatherhat-python`
* `cd weatherhat-python`
* `./install.sh --unstable`
You can optionally run `sudo raspi-config` or the graphical Raspberry Pi Configuration UI to enable interfaces.

Some of the examples use additional libraries. You can install them with:
Some of the examples have additional dependencies. You can install them with:

```bash
pip3 install fonts font-manrope pyyaml adafruit-io numpy
pip install fonts font-manrope pyyaml adafruit-io numpy pillow
```

You may also need to install `libatlas-base-dev`
You may also need to install `libatlas-base-dev`:

```
sudo apt-get install libatlas-base-dev
sudo apt install libatlas-base-dev
```

# Using The Library
Expand Down
23 changes: 10 additions & 13 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# This script handles some basic QA checks on the source

NOPOST=$1
LIBRARY_NAME=`hatch project metadata name`
LIBRARY_VERSION=`hatch version | awk -F "." '{print $1"."$2"."$3}'`
POST_VERSION=`hatch version | awk -F "." '{print substr($4,0,length($4))}'`
LIBRARY_NAME=$(hatch project metadata name)
LIBRARY_VERSION=$(hatch version | awk -F "." '{print $1"."$2"."$3}')
POST_VERSION=$(hatch version | awk -F "." '{print substr($4,0,length($4))}')
TERM=${TERM:="xterm-256color"}

success() {
echo -e "$(tput setaf 2)$1$(tput sgr0)"
Expand All @@ -28,7 +29,7 @@ while [[ $# -gt 0 ]]; do
;;
*)
if [[ $1 == -* ]]; then
printf "Unrecognised option: $1\n";
printf "Unrecognised option: %s\n" "$1";
exit 1
fi
POSITIONAL_ARGS+=("$1")
Expand All @@ -39,8 +40,7 @@ done
inform "Checking $LIBRARY_NAME $LIBRARY_VERSION\n"

inform "Checking for trailing whitespace..."
grep -IUrn --color "[[:blank:]]$" --exclude-dir=dist --exclude-dir=.tox --exclude-dir=.git --exclude=PKG-INFO
if [[ $? -eq 0 ]]; then
if grep -IUrn --color "[[:blank:]]$" --exclude-dir=dist --exclude-dir=.tox --exclude-dir=.git --exclude=PKG-INFO; then
warning "Trailing whitespace found!"
exit 1
else
Expand All @@ -49,8 +49,7 @@ fi
printf "\n"

inform "Checking for DOS line-endings..."
grep -lIUrn --color $'\r' --exclude-dir=dist --exclude-dir=.tox --exclude-dir=.git --exclude=Makefile
if [[ $? -eq 0 ]]; then
if grep -lIUrn --color $'\r' --exclude-dir=dist --exclude-dir=.tox --exclude-dir=.git --exclude=Makefile; then
warning "DOS line-endings found!"
exit 1
else
Expand All @@ -59,8 +58,7 @@ fi
printf "\n"

inform "Checking CHANGELOG.md..."
cat CHANGELOG.md | grep ^${LIBRARY_VERSION} > /dev/null 2>&1
if [[ $? -eq 1 ]]; then
if ! grep "^${LIBRARY_VERSION}" CHANGELOG.md > /dev/null 2>&1; then
warning "Changes missing for version ${LIBRARY_VERSION}! Please update CHANGELOG.md."
exit 1
else
Expand All @@ -69,8 +67,7 @@ fi
printf "\n"

inform "Checking for git tag ${LIBRARY_VERSION}..."
git tag -l | grep -E "${LIBRARY_VERSION}$"
if [[ $? -eq 1 ]]; then
if ! git tag -l | grep -E "${LIBRARY_VERSION}$"; then
warning "Missing git tag for version ${LIBRARY_VERSION}"
fi
printf "\n"
Expand All @@ -84,4 +81,4 @@ if [[ $NOPOST ]]; then
else
success "OK"
fi
fi
fi
50 changes: 28 additions & 22 deletions examples/buttons.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
import signal
import select
from datetime import timedelta

import RPi.GPIO as GPIO
import gpiod
import gpiodevice
from gpiod.line import Bias, Edge

print("""buttons.py - Detect which button has been pressed
This example should demonstrate how to:
1. set up RPi.GPIO to read buttons,
1. set up gpiod to read buttons,
2. determine which button has been pressed
Press Ctrl+C to exit!
""")

IP_PU_FE = gpiod.LineSettings(edge_detection=Edge.FALLING, bias=Bias.PULL_UP, debounce_period=timedelta(milliseconds=20))

# The buttons on Weather HAT are connected to pins 5, 6, 16 and 24
BUTTONS = [5, 6, 16, 24]
# They short to ground, so we must Bias them with the PULL_UP resistor
# and watch for a falling-edge.
BUTTONS = {5: IP_PU_FE, 6: IP_PU_FE, 16: IP_PU_FE, 24: IP_PU_FE}

# These correspond to buttons A, B, X and Y respectively
LABELS = ['A', 'B', 'X', 'Y']

# Set up RPi.GPIO with the "BCM" numbering scheme
GPIO.setmode(GPIO.BCM)

# Buttons connect to ground when pressed, so we should set them up
# with a "PULL UP", which weakly pulls the input signal to 3.3V.
GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)
LABELS = {5: 'A', 6: 'B', 16: 'X', 24: 'Y'}

# Request the button pins from the gpiochip
chip = gpiodevice.find_chip_by_platform()
lines = chip.request_lines(
consumer="buttons.py",
config=BUTTONS
)

# "handle_button" will be called every time a button is pressed
# It receives one argument: the associated input pin.
def handle_button(pin):
label = LABELS[BUTTONS.index(pin)]
label = LABELS[pin]
print("Button press detected on pin: {} label: {}".format(pin, label))

# read_edge_events does not allow us to specify a timeout
# so we'll use poll to check if any events are waiting for us...
poll = select.poll()
poll.register(lines.fd, select.POLLIN)

# Loop through out buttons and attach the "handle_button" function to each
# We're watching the "FALLING" edge (transition from 3.3V to Ground) and
# picking a generous bouncetime of 100ms to smooth out button presses.
for pin in BUTTONS:
GPIO.add_event_detect(pin, GPIO.FALLING, handle_button, bouncetime=100)

# Finally, since button handlers don't require a "while True" loop,
# we pause the script to prevent it exiting immediately.
signal.pause()
# Poll for button events
while True:
if poll.poll(10):
for event in lines.read_edge_events():
handle_button(event.line_offset)
Loading

0 comments on commit e8184bd

Please sign in to comment.