Metadata-Version: 2.4
Name: cjn_PiTools
Version: 2.0
Summary: cjn's library of Raspberry Pi functions, drivers, tools, ...
Author-email: Chris Nelson <github@cjnaz.com>
License-Expression: MIT
Project-URL: repository, https://github.com/cjnaz/cjn_PiTools
Keywords: cjnaz,cjnfuncs,Raspberry,Raspberry Pi,RPi,luma,luma.oled,OLED,LED,blinky,DS18B20,w1_therm,ADC,ADC121C,ADC101C,ADC081C,HTU21D,MCP23008,PCA9548,TCA9548,PCA9548A,TCA9548A,SHT3,SHT30,SHT31,SHT35,Trixie,rgpio,lgpio,smbus,smbus2
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cjnfuncs>=3.2.4
Requires-Dist: lgpio>=0.2.2.0
Requires-Dist: luma.oled>=3.14
Requires-Dist: luma.core[gpio]>=2.5.1
Requires-Dist: pillow>=13.0.0
Requires-Dist: spidev>=3.8
Requires-Dist: smbus2>=0.6.1
Requires-Dist: crc>=8.0.0
Requires-Dist: RPi.GPIO>=0.7.1
Provides-Extra: rgpio
Requires-Dist: rgpio>=0.2.2.0; extra == "rgpio"
Dynamic: license-file

# cjn_PiTools - A collection of modules for Raspberry Pi projects

## cjn_PiTools is comprised of several modules (follow links to respective documentation)

NOTE:  These links point to the github repo since relative links to other .md files do not work on PyPI.

Module | Description/Purpose
--|--
[PiBlinky](https://github.com/cjnaz/cjn_PiTools/blob/main/PiBlinky.md)       | A multiple threaded LED driver for Raspberry Pi
[rgpio_shared_gpios](https://github.com/cjnaz/cjn_PiTools/blob/main/rgpio_shared_gpios.md) | Central owner of GPIO pins for sharing across tool scripts
[PiOLED](https://github.com/cjnaz/cjn_PiTools/blob/main/PiOLED.md)           | Display multi-line messages on a shared Raspberry Pi connected OLED display
[DS18B20](https://github.com/cjnaz/cjn_PiTools/blob/main/DS18B20.md)         | DS18B20 temperature sensor library using the w1_therm kernel driver
[initW1buses](https://github.com/cjnaz/cjn_PiTools/blob/main/initW1buses.md) | Initialize the W1 buses and set write permission on found therm_bulk_read file(s)
[ADC121C](https://github.com/cjnaz/cjn_PiTools/blob/main/ADC121C.md)         | ADC121C* 12-bit ADC library for Raspberry Pi
[SHT3x](https://github.com/cjnaz/cjn_PiTools/blob/main/SHT3x.md)             | SHT3x Temperature/RH sensor library for Raspberry Pi
[HTU21D](https://github.com/cjnaz/cjn_PiTools/blob/main/HTU21D.md)           | HTU21D Temperature/RH sensor library for Raspberry Pi
[MCP23008](https://github.com/cjnaz/cjn_PiTools/blob/main/MCP23008.md)       | MCP23008 8-Bit I/O Expander library for Raspberry Pi
[PCA9548](https://github.com/cjnaz/cjn_PiTools/blob/main/PCA9548.md)         | PCA9548A/TCA9548A I2C port expander library for Raspberry Pi
[shared](https://github.com/cjnaz/cjn_PiTools/blob/main/shared.md)           | Classes and functions used across cjn_PiTools

These drivers work with `RPi.GPIO` / `smbus2` and `lgpio` / `rgpio` apis.  Using the `rgpio` api enables remote operation of GPIOs, I2C and SPI (PiOLED)

Tested on Python 3.13, Trixie (Linux 6.18.60+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.18.60-1+rpt1 (2026-11-03) aarch64 GNU/Linux)

In this documentation, "tool script" refers to a Python project that imports and uses cjn_PiTools. Some may be simple scripts, and others may themselves be installed packages.

<br/>

## Installation and usage


### If using only the RPi.GPIO and smbus2 (or lgpio) drivers (not rgpio):

    pip install cjn_PiTools
  
  - lgpio is pulled from PyPI by this install.  If that fails see the official install instructions.


### If using the rgpio driver:

There is no downside to using rgpio, other than a bit more setup complexity.

`rgpio` is required for the `rgpio_shared_gpios` module to own GPIO pins and share them across tool script processes (not possible with `smbus` or `lgpio`).
These modules are dependent on `rgpio_shared_gpios` (thus `rgpio`):

- `initW1buses` - only if GPIOs need to be sequenced before discovering `DS18B20` devices _and_ `Use_rgpio_shared_gpios` is set True in the config file
- `PiBlinky` - only if more than one process needs access to the same LED(s)


`rgpio` also optionally enables a tool script to run on one machine and access GPIO resources on another machine via a socket interface.
These file are enabled for rgpio remote access:

- PCA9548.py - Provides a CLI for setting/getting the port select register on local or remote PCA9548 chips
- PCA9548-demo.py
- PiBlinky-demo.py
- shared-demo.py
- SHT3x-demo.py
 

To install the rgpiod daemon:

    apt install rgpiod

Here's a systemd service file for starting rgpiod at boot, if needed (the above apt install may install a rgpiod.service file):

    [Unit]
    Description=        Remote GPIO Daemon
    After=              network.target

    [Service]
    Type=simple
    EnvironmentFile=    -/etc/default/rgpiod
    ExecStart=          /usr/bin/rgpiod -c ${DAEMON_CONF} -w ${DAEMON_WORK} ${DAEMON_OPTS}
    TimeoutStopSec=     20

    [Install]
    WantedBy=           multi-user.target

If you wish to allow remote access, enable it in the rgpiod configuration

    Edit /etc/default/rgpiod:
      Comment out the line DAEMON_OPTS="-l"
      Set up the DAEMON_OPTS="-x" permits and users features, as needed

Restart the rgpiod daemon

    sudo systemctl restart rgpiod

Finally, install `cjn_PiTools` with rgpio support:

    pip install cjn_PiTools[rgpio]

<br/>

## Key changes since the prior major public release (1.2.2)

V2.0 Changes
- Upgrade to Trixie and rgpio/lgpio (pigpio removed)
- Expanded i2c methods and reworked all I2C-based modules
- Added rgpio_shared_gpios to provide a gpio sharing workaround for the kernel enforcement of single process access
- Reworked initW1buses, enabling non-root access for all DS18B20 api

A fundamental change in more recent kernels is that GPIOs can be _claimed_ and by only one process at a time.  This plays havoc in having more than one tool script using PiBlinky and/or receiving notifications of button pushes, for example.  The provided solution is via the `rgpio_shared_gpios` module, which, coordinating with rgpiod, takes ownership of specified GPIOs and shares them with multiple tool scripts.  Note that I2C and SPI ports don't have the same single-owner requirement, it seems.


<br/>

## Revision history
- 2.0 261127 - Upgrade to Trixie and rgpio/lgpio (pigpio removed), expanded i2c methods, rgpio_shared_gpios, rework initW1buses
- 1.2.2 260828 - Corrected PiOLED logging of IPC handle names
- 1.2.1 260526
  - HTU21D dummy user reg reads for measurement reads stability
  - PCA9548 monitor_ch
  - ADC121C read_conversion_result force_address switch added
  - PiOLED tweaks
  - HTU21D init changed do_soft_reset > do_reset, consistent with SHT3x
- 1.2 260506
  - PiOLED uses shared memory, changed from display_file - cjnfuncs 3.2 dependency
  - PiOLED add toolname to pioled_display_driver pioled_shm_lock and pioled_go_flag .get_lock() calls
  - PiOLED taillog command
  - Debug logging of MCP23008 set_register() and set_bits() values
- 1.1.1 260401 - pyproject.toml cleanup, keywords
- 1.1 260401 - Added ADC121C, HTU21D, MCP23008, PCA9548, and SHT3x drivers
- 1.0 260207 - New.  Bundled PiBlinky, PiOLED, initW1buses, and DS18B20 modules
