@@ -494,6 +494,45 @@ backticks ``:class:`~adafruit_motor.servo.Servo```. You must also add the refer
494
494
495
495
"adafruit_motor": ("https://circuitpython.readthedocs.io/projects/motor/en/latest/", None,),
496
496
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 I2C device
501
+ registers. There is also `Register SPI <https://github.com/adafruit/Adafruit_CircuitPython_Register_SPI >`_
502
+ for SPI devices. When possible, use one of these libraries for unpacking and
503
+ packing registers. This ensures the packing code is shared amongst all
504
+ registers (even across drivers). Furthermore, it simplifies device definitions
505
+ by making them declarative (only data.)
506
+
507
+ Values with non-consecutive bits in a register or that represent FIFO endpoints
508
+ may not map well to existing register classes. In unique cases like these, it is
509
+ ok to read and write the register directly.
510
+
511
+ *Do not * add all registers from a datasheet upfront. Instead, only add the ones
512
+ necessary for the functionality the driver exposes. Adding them all will lead to
513
+ unnecessary file size and API clutter. See `this video about outside-in design
514
+ from @tannewt <https://www.youtube.com/watch?v=3QewiyfBQh8> `_.
515
+
516
+ I2C Example
517
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
518
+
519
+ .. code-block :: python
520
+
521
+ from adafruit_register import i2c_bit
522
+ from adafruit_bus_device import i2c_device
523
+
524
+ class HelloWorldDevice :
525
+ """ Device with two bits to control when the words 'hello' and 'world' are lit."""
526
+
527
+ hello = i2c_bit.RWBit(0x 0 , 0x 0 )
528
+ """ Bit to indicate if hello is lit."""
529
+
530
+ world = i2c_bit.RWBit(0x 1 , 0x 0 )
531
+ """ Bit to indicate if world is lit."""
532
+
533
+ def __init__ (self , i2c , device_address = 0x 0 ):
534
+ self .i2c_device = i2c_device.I2CDevice(i2c, device_address)
535
+
497
536
Use BusDevice
498
537
--------------------------------------------------------------------------------
499
538
@@ -668,8 +707,24 @@ when using ``const()``, keep in mind these general guide lines:
668
707
669
708
- Always use via an import, ex: ``from micropython import const ``
670
709
- 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 ``.
710
+ - Only used when the user will not need access to variable and prefix name with
711
+ a leading underscore, ex: ``_SOME_CONST ``.
712
+
713
+ Example
714
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
715
+
716
+ .. code-block :: python
717
+
718
+ from adafruit_bus_device import i2c_device
719
+ from micropython import const
720
+
721
+ _DEFAULT_I2C_ADDR = const(0x 42 )
722
+
723
+ class Widget :
724
+ """ A generic widget."""
725
+
726
+ def __init__ (self , i2c , address = _DEFAULT_I2C_ADDR ):
727
+ self .i2c_device = i2c_device.I2CDevice(i2c, address)
673
728
674
729
Libraries Examples
675
730
------------------
@@ -751,6 +806,16 @@ properties.
751
806
| ``sound_level `` | float | non-unit-specific sound level (monotonic but not actual decibels) |
752
807
+-----------------------+-----------------------+-------------------------------------------------------------------------+
753
808
809
+ Driver constant naming
810
+ --------------------------------------------------------------------------------
811
+
812
+ When adding variables for constant values for a driver. Do not include the
813
+ device's name in the variable name. For example, in ``adafruit_fancy123.py ``,
814
+ variables should not start with ``FANCY123_ ``. Adding this prefix increases RAM
815
+ usage and .mpy file size because variable names are preserved. User code should
816
+ refer to these constants as ``adafruit_fancy123.HELLO_WORLD `` for clarity.
817
+ ``adafruit_fancy123.FANCY123_HELLO_WORLD `` would be overly verbose.
818
+
754
819
Adding native modules
755
820
--------------------------------------------------------------------------------
756
821
0 commit comments