@@ -494,6 +494,39 @@ 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 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(0x 0 , 0x 0 )
522
+ """ Bit to indicate if hello is lit."""
523
+
524
+ world = i2c_bit.RWBit(0x 1 , 0x 0 )
525
+ """ Bit to indicate if world is lit."""
526
+
527
+ def __init__ (self , i2c , device_address = 0x 0 ):
528
+ self .i2c_device = i2c_device.I2CDevice(i2c, device_address)
529
+
497
530
Use BusDevice
498
531
--------------------------------------------------------------------------------
499
532
@@ -668,8 +701,24 @@ when using ``const()``, keep in mind these general guide lines:
668
701
669
702
- Always use via an import, ex: ``from micropython import const ``
670
703
- 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(0x 42 )
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)
673
722
674
723
Libraries Examples
675
724
------------------
@@ -751,6 +800,16 @@ properties.
751
800
| ``sound_level `` | float | non-unit-specific sound level (monotonic but not actual decibels) |
752
801
+-----------------------+-----------------------+-------------------------------------------------------------------------+
753
802
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
+
754
813
Adding native modules
755
814
--------------------------------------------------------------------------------
756
815
0 commit comments