@@ -38,16 +38,27 @@ provide `__enter__` and `__exit__` to create a context manager usable with `with
38
38
39
39
## Verify your device
40
40
41
- Make sure device you are talking to is the device you expect. If not, raise a
42
- ValueError. Beware that I2C addresses can be identical on different devices so
43
- read registers you know to make sure they match your expectation. Validating
44
- this upfront will help catch mistakes.
41
+ Whenever possible, make sure device you are talking to is the device you expect.
42
+ If not, raise a ValueError. Beware that I2C addresses can be identical on
43
+ different devices so read registers you know to make sure they match your
44
+ expectation. Validating this upfront will help catch mistakes.
45
45
46
46
## Getters/Setters
47
47
48
48
When designing a driver for a device, use properties for device state and use
49
- methods for actions that the device performs. Doing this well helps beginners
50
- understand when to use what. It is also consistent with
49
+ methods for sequences of abstract actions that the device performs. State is a
50
+ property of the device as a whole that exists regardless of what the code is
51
+ doing. This includes things like temperature, time, sound, light and the state
52
+ of a switch. For a more complete list see the sensor properties bullet below.
53
+
54
+ Another way to separate state from actions is that state is usually something
55
+ the user can sense themselves by sight or feel for example. Actions are
56
+ something the user can watch. The device does this and then this.
57
+
58
+ Making this separation clear to the user will help beginners understand when to
59
+ use what.
60
+
61
+ Here is more info on properties from
51
62
[ Python] ( https://docs.python.org/3/library/functions.html#property ) .
52
63
53
64
## Design for compatibility with CPython
@@ -61,14 +72,27 @@ modules to add extra functionality. By distinguishing API boundaries at modules
61
72
you increase the likelihood that incorrect expectations are found on import and
62
73
not randomly during runtime.
63
74
75
+ ### Example
76
+ When adding extra functionality to CircuitPython to mimic what a normal
77
+ operating system would do, either copy an existing CPython API (for example file
78
+ writing) or create a separate module to achieve what you want. For example,
79
+ mounting and unmount drives is not a part of CPython so it should be done in a
80
+ module, such as a new ` filesystem ` , that is only available in CircuitPython.
81
+ That way when someone moves the code to CPython they know what parts need to be
82
+ adapted.
83
+
64
84
## Document inline
65
85
66
86
Whenever possible, document your code right next to the code that implements it.
67
87
This makes it more likely to stay up to date with the implementation itself. Use
68
88
Sphinx's automodule to format these all nicely in ReadTheDocs. The cookiecutter
69
89
helps set these up.
70
90
71
- Use [ rST] ( http://docutils.sourceforge.net/docs/user/rst/quickref.html ) for markup.
91
+ Use [ Sphinx flavor rST] ( http://openalea.gforge.inria.fr/doc/openalea/doc/_build/html/source/sphinx/rest_syntax.html ) for markup.
92
+
93
+ Lots of documentation is a good thing but it can take a lot of space. To
94
+ minimize the space used on disk and on load, distribute the library as both .py
95
+ and .mpy, MicroPython and CircuitPython's bytecode format that omits comments.
72
96
73
97
### Module description
74
98
@@ -148,6 +172,9 @@ object with methods that read or write into the buffer instead of creating new
148
172
objects. ` nativeio ` classes are design to read and write to subsections of
149
173
buffers.
150
174
175
+ Its ok to allocate an object to return to the user. Just beware of causing more
176
+ than one allocation per call due to internal logic.
177
+
151
178
** However** , this is a memory tradeoff so do not do it for large or rarely used
152
179
buffers.
153
180
0 commit comments