Skip to content

Commit fc7b805

Browse files
committed
Polish the docs for ReadTheDocs
1 parent 661e380 commit fc7b805

File tree

8 files changed

+356
-30
lines changed

8 files changed

+356
-30
lines changed

README.md

-6
This file was deleted.

README.rst

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Adafruit CircuitPython BusDevice
2+
==============================
3+
4+
.. image:: https://readthedocs.org/projects/adafruit-circuitpython-bus_device/badge/?version=latest
5+
:target: https://circuitpython.readthedocs.io/projects/bus_device/en/latest/
6+
:alt: Documentation Status
7+
8+
The `I2CDevice` and `SPIDevice` helper classes make managing transaction state
9+
on a bus easy. For example, they manage locking the bus to prevent other
10+
concurrent access. For SPI devices, it manages the chip select and protocol
11+
changes such as mode. For I2C, it manages the device address.
12+
13+
API
14+
---
15+
.. toctree::
16+
:maxdepth: 3
17+
18+
adafruit_bus_device/index

adafruit_bus_device/i2c_device.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ class I2CDevice:
2525
Represents a single I2C device and manages locking the bus and the device
2626
address.
2727
28-
:param I2C i2c: The I2C bus the device is on
28+
:param ~nativeio.I2C i2c: The I2C bus the device is on
2929
:param int device_address: The 7 bit device address
3030
31-
Example::
31+
Example:
32+
33+
.. code-block:: python
3234
3335
import nativeio
3436
from board import *
@@ -44,6 +46,14 @@ class I2CDevice:
4446
device.writeto(bytes_read)
4547
"""
4648
def __init__(self, i2c, device_address):
49+
# Verify that a deivce with that address exists.
50+
while not i2c.try_lock():
51+
pass
52+
scan = i2c.scan()
53+
i2c.unlock()
54+
if device_address not in scan:
55+
raise ValueError("No i2c device at address: " + str(hex(device_address)))
56+
4757
self.i2c = i2c
4858
self.device_address = device_address
4959

@@ -56,10 +66,10 @@ def readfrom_into(self, buf, **kwargs):
5666
as if ``buf[start:end]``. This will not cause an allocation like
5767
``buf[start:end]`` will so it saves memory.
5868
59-
:param int address: 7-bit device address
60-
:param bytearray buffer: buffer to write into
61-
:param int start: Index to start writing at
62-
:param int end: Index to write up to but not include
69+
:param int address: 7-bit device address
70+
:param bytearray buffer: buffer to write into
71+
:param int start: Index to start writing at
72+
:param int end: Index to write up to but not include
6373
"""
6474
self.i2c.readfrom_into(self.device_address, buf, **kwargs)
6575

@@ -72,11 +82,10 @@ def writeto(self, buf, **kwargs):
7282
as if ``buffer[start:end]``. This will not cause an allocation like
7383
``buffer[start:end]`` will so it saves memory.
7484
75-
:param bytearray buffer: buffer containing the bytes to write
76-
:param int start: Index to start writing from
77-
:param int end: Index to read up to but not include
78-
:param bool stop: If true, output an I2C stop condition after the
79-
buffer is written
85+
:param bytearray buffer: buffer containing the bytes to write
86+
:param int start: Index to start writing from
87+
:param int end: Index to read up to but not include
88+
:param bool stop: If true, output an I2C stop condition after the buffer is written
8089
"""
8190
self.i2c.writeto(self.device_address, buf, **kwargs)
8291

adafruit_bus_device/index.rst

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Module Reference
2+
================
3+
4+
`i2c_device` - A device on an i2c bus
5+
+++++++++++++++++++++++++++++++++++++
6+
7+
.. automodule:: adafruit_bus_device.i2c_device
8+
:members:
9+
10+
`spi_device` - A device on a SPI bus
11+
++++++++++++++++++++++++++++++++++++
12+
13+
.. automodule:: adafruit_bus_device.spi_device
14+
:members:

adafruit_bus_device/spi_device.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@ class SPIDevice:
2525
Represents a single SPI device and manages locking the bus and the device
2626
address.
2727
28-
:param SPI spi: The SPI bus the device is on
28+
:param ~nativeio.SPI spi: The SPI bus the device is on
2929
:param ~microcontroller.Pin chip_select: The chip select pin
3030
31-
Example::
31+
Example:
32+
33+
.. code-block:: python
3234
3335
import nativeio
3436
from board import *
3537
from adafruit_bus_device.spi_device import I2CDevice
3638
37-
with nativeio.SPI(SCK, MOSI, MISO) as spi:
38-
device = SPIDevice(spi, D10)
39+
with nativeio.SPI(SCK, MOSI, MISO) as spi_bus:
40+
device = SPIDevice(spi_bus, D10)
3941
bytes_read = bytearray(4)
40-
with device:
41-
device.read(bytes_read)
42+
with device as spi:
43+
spi_device.read(bytes_read)
4244
# A second transaction
43-
with device:
44-
device.write(bytes_read)
45+
with device as spi:
46+
spi.write(bytes_read)
4547
"""
4648
def __init__(self, spi, chip_select, baudrate=100000, polarity=0, phase=0):
4749
self.spi = spi

0 commit comments

Comments
 (0)