11Design Guide
22============
33
4- MicroPython has created a great foundation to build upon and to make it even
5- better for beginners we've created CircuitPython. This guide covers a number of
6- ways the core and libraries are geared towards beginners.
4+ This guide covers a variety of development practices for CircuitPython core and library APIs. Consistency with these practices ensures that beginners can learn a pattern once and apply it throughout the CircuitPython ecosystem.
75
86Start libraries with the cookiecutter
97-------------------------------------
@@ -164,61 +162,74 @@ After the license comment::
164162Class description
165163^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
166164
167- Documenting what the object does::
165+ At the class level document what class does and how to initialize it ::
168166
169167 class DS3231:
170- """Interface to the DS3231 RTC."""
168+ """DS3231 real-time clock.
171169
172- Renders as:
170+ :param ~busio.I2C i2c_bus: The I2C bus the DS3231 is connected to.
171+ :param int address: The I2C address of the device.
172+ """
173173
174- .. py : class :: DS3231
175- :noindex:
174+ def __init__(self, i2c_bus, address=0x40):
175+ self._i2c = i2c_bus
176176
177- Interface to the DS3231 RTC.
178177
179- Data descriptor description
180- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
178+ Renders as:
181179
182- Comment is after even though its weird::
180+ .. py :class :: DS3231(i2c_bus, address = 64 )
181+ :noindex:
183182
184- lost_power = i2c_bit.RWBit(0x0f, 7)
185- """True if the device has lost power since the time was set."""
183+ DS3231 real-time clock.
186184
187- Renders as:
185+ :param ~busio.I2C i2c_bus: The I2C bus the DS3231 is connected to.
186+ :param int address: The I2C address of the device.
188187
189- .. py : attribute :: lost_power
190- :noindex:
188+ Attributes
189+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
191190
192- True if the device has lost power since the time was set.
191+ Attributes are state on objects. (See `Getters/Setters ` above for more discussion
192+ about when to use them.) They can be defined internally in a number of different
193+ ways. Each approach is enumerated below with an explanation of where the comment
194+ goes.
193195
194- Method description
195- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
196+ Regardless of how the attribute is implemented, it should have a short
197+ description of what state it represents including the type, possible values and/or
198+ units. It should be marked as ``(read-only) `` or ``(write-only) `` at the end of
199+ the first line for attributes that are not both readable and writeable.
196200
197- First line after the method definition::
201+ Instance attributes
202+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198203
199- def turn_right(self, degrees):
200- """Turns the bot ``degrees`` right.
204+ Comment comes from after the assignment::
201205
202- :param float degrees: Degrees to turn right
206+ def __init__(self, drive_mode):
207+ self.drive_mode = drive_mode
208+ """
209+ The pin drive mode. One of:
210+
211+ - `digitalio.DriveMode.PUSH_PULL`
212+ - `digitalio.DriveMode.OPEN_DRAIN`
203213 """
204214
205215Renders as:
206216
207- .. py :method :: turn_right(degrees)
208- :noindex:
217+ .. py :attribute :: drive_mode
218+ :noindex:
209219
210- Turns the bot `` degrees `` right.
220+ The pin drive mode. One of:
211221
212- :param float degrees: Degrees to turn right
222+ - `digitalio.DriveMode.PUSH_PULL `
223+ - `digitalio.DriveMode.OPEN_DRAIN `
213224
214225Property description
215- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
226+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216227
217228Comment comes from the getter::
218229
219230 @property
220231 def datetime(self):
221- """The current date and time"""
232+ """The current date and time as a `time.struct_time`. """
222233 return self.datetime_register
223234
224235 @datetime.setter
@@ -228,9 +239,65 @@ Comment comes from the getter::
228239Renders as:
229240
230241.. py :attribute :: datetime
242+ :noindex:
243+
244+ The current date and time as a `time.struct_time `.
245+
246+ Read-only example::
247+
248+ @property
249+ def temperature(self):
250+ """
251+ The current temperature in degrees Celcius. (read-only)
252+
253+ The device may require calibration to get accurate readings.
254+ """
255+ return self._read(TEMPERATURE)
256+
257+
258+ Renders as:
259+
260+ .. py :attribute :: temperature
261+ :noindex:
262+
263+ The current temperature in degrees Celcius. (read-only)
264+
265+ The device may require calibration to get accurate readings.
266+
267+ Data descriptor description
268+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
269+
270+ Comment is after even though its weird::
271+
272+ lost_power = i2c_bit.RWBit(0x0f, 7)
273+ """True if the device has lost power since the time was set."""
274+
275+ Renders as:
276+
277+ .. py :attribute :: lost_power
278+ :noindex:
279+
280+ True if the device has lost power since the time was set.
281+
282+ Method description
283+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
284+
285+ First line after the method definition::
286+
287+ def turn_right(self, degrees):
288+ """Turns the bot ``degrees`` right.
289+
290+ :param float degrees: Degrees to turn right
291+ """
292+
293+ Renders as:
294+
295+ .. py :method :: turn_right(degrees)
231296 :noindex:
232297
233- The current date and time
298+ Turns the bot ``degrees `` right.
299+
300+ :param float degrees: Degrees to turn right
234301
235302Use BusDevice
236303--------------------------------------------------------------------------------
0 commit comments