Skip to content

Commit 5fe90ec

Browse files
committed
Add register library topic to design guide
1 parent df52d99 commit 5fe90ec

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

docs/design_guide.rst

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,39 @@ backticks ``:class:`~adafruit_motor.servo.Servo```. You must also add the refer
494494

495495
"adafruit_motor": ("https://circuitpython.readthedocs.io/projects/motor/en/latest/", None,),
496496

497+
Use ``adafruit_register`` when possible
498+
--------------------------------------------------------------------------------
499+
`Register <https://github.com/adafruit/Adafruit_CircuitPython_Register>`_ is
500+
a foundational library that manages packing and unpacking data from device
501+
registers. When possible, use it for unpacking and packing registers. This
502+
ensures the packing code is shared amongst all registers. Furthermore, it
503+
simplifies device definitions by making them declarative (only data.)
504+
505+
*Do not* add all registers from a datasheet upfront. Instead, only add the ones
506+
necessary for the functionality the driver exposes. Adding them all will lead to
507+
unnecessary file size and API clutter. See `this video about outside-in design
508+
from @tannewt <https://www.youtube.com/watch?v=3QewiyfBQh8>`_.
509+
510+
I2C Example
511+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
512+
513+
.. code-block:: python
514+
515+
from adafruit_register import i2c_bit
516+
from adafruit_bus_device import i2c_device
517+
518+
class HelloWorldDevice:
519+
"""Device with two bits to control when the words 'hello' and 'world' are lit."""
520+
521+
hello = i2c_bit.RWBit(0x0, 0x0)
522+
"""Bit to indicate if hello is lit."""
523+
524+
world = i2c_bit.RWBit(0x1, 0x0)
525+
"""Bit to indicate if world is lit."""
526+
527+
def __init__(self, i2c, device_address=0x0):
528+
self.i2c_device = i2c_device.I2CDevice(i2c, device_address)
529+
497530
Use BusDevice
498531
--------------------------------------------------------------------------------
499532

@@ -668,8 +701,24 @@ when using ``const()``, keep in mind these general guide lines:
668701

669702
- Always use via an import, ex: ``from micropython import const``
670703
- Limit use to global (module level) variables only.
671-
- If user will not need access to variable, prefix name with a leading
672-
underscore, ex: ``_SOME_CONST``.
704+
- Only used when the user will not need access to variable and prefix name with
705+
a leading underscore, ex: ``_SOME_CONST``.
706+
707+
Example
708+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
709+
710+
.. code-block:: python
711+
712+
from adafruit_bus_device import i2c_device
713+
from micropython import const
714+
715+
_DEFAULT_I2C_ADDR = const(0x42)
716+
717+
class Widget:
718+
"""A generic widget."""
719+
720+
def __init__(self, i2c, address=_DEFAULT_I2C_ADDR):
721+
self.i2c_device = i2c_device.I2CDevice(i2c, address)
673722
674723
Libraries Examples
675724
------------------
@@ -751,6 +800,16 @@ properties.
751800
| ``sound_level`` | float | non-unit-specific sound level (monotonic but not actual decibels) |
752801
+-----------------------+-----------------------+-------------------------------------------------------------------------+
753802

803+
Driver constant naming
804+
--------------------------------------------------------------------------------
805+
806+
When adding variables for constant values for a driver. Do not include the
807+
device's name in the variable name. For example, in ``adafruit_fancy123.py``,
808+
variables should not start with ``FANCY123_``. Adding this prefix increases RAM
809+
usage and .mpy file size because variable names are preserved. User code should
810+
refer to these constants as ``adafruit_fancy123.HELLO_WORLD`` for clarity.
811+
``adafruit_fancy123.FANCY123_HELLO_WORLD`` would be overly verbose.
812+
754813
Adding native modules
755814
--------------------------------------------------------------------------------
756815

0 commit comments

Comments
 (0)