Skip to content

Commit e45d3ab

Browse files
committed
cleanup docstrings for Sphinx and reST
add new example to documentation remove debug code from example
1 parent 2934596 commit e45d3ab

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

adafruit_ntp.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,23 @@ class NTP: # pylint:disable=too-many-instance-attributes
165165
166166
This class uses a simple state machine to manage synchronization:
167167
- USING_CACHED_REFERENCE (state 3): The default state where the cached time reference is used.
168-
- Transitions to GETTING_SOCKET when the cache expires.
168+
169+
- Transitions to GETTING_SOCKET when the cache expires.
170+
169171
- GETTING_SOCKET (state 1): Attempts to perform a DNS lookup for the NTP server.
170-
- Transitions to GETTING_PACKET on success.
171-
- Remains in this state if retries are needed.
172+
173+
- Transitions to GETTING_PACKET on success.
174+
- Remains in this state if retries are needed.
175+
172176
- GETTING_PACKET (state 2): Sends an NTP request and waits for the response.
173-
- Transitions back to USING_CACHED_REFERENCE.
174-
- On failure, any existing cached value will continue to be used until the next scheduled
175-
synchronization.
176177
177-
The state transitions are managed by the `_update_time_sync` method, which is called if
178-
the cached time is expired when `utc_ns` is accessed.
178+
- Transitions back to USING_CACHED_REFERENCE.
179+
180+
- On failure, any existing cached value will continue to be used until the next scheduled
181+
synchronization.
182+
183+
The state transitions are managed by the ``_update_time_sync`` method, which is called if
184+
the cached time is expired when ``utc_ns`` is accessed.
179185
"""
180186

181187
# State machine values
@@ -374,15 +380,15 @@ def register_ntp_event_callback(
374380
375381
Callbacks can be used to turn off the radio to save power, initiate a network
376382
connection, or other progress monitoring processes.
377-
EG: `wifi.radio.enabled = False` or `connection_manager.connect()`
383+
EG: ``wifi.radio.enabled = False`` or ``connection_manager.connect()``
378384
379385
.. caution::
380386
381387
This implementation does not prevent duplicate registration of the same callback.
382388
All attempts to consistently identify when a callback is already registered have
383389
failed due to the limitations of the current CircuitPython implementation. Comparing
384-
the callback value directly, converting to string using `str()`, or `repr()`, or to a
385-
number using `id()` all have cases where an identical callback reference will be
390+
the callback value directly, converting to string using ``str()``, or ``repr()``, or to a
391+
number using ``id()`` all have cases where an identical callback reference will be
386392
treated as different.
387393
388394
If the same callback is registered multiple times, with the same event type, it will
@@ -391,8 +397,8 @@ def register_ntp_event_callback(
391397
:param Callable[[IntFlag, int], None] callback: The callback function to register.
392398
:param IntFlag event_types: The event types that should trigger this callback. This can
393399
be a single event type or a combination of multiple events.
394-
Defaults to `EventType.SYNC_COMPLETE`.
395-
:raises TypeError: If the `event_types` argument is not a valid event type or combination
400+
Defaults to ``EventType.SYNC_COMPLETE``.
401+
:raises TypeError: If the ``event_types`` argument is not a valid event type or combination
396402
of event types.
397403
398404
Usage examples::

docs/examples.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ Ensure your device works with this simple test that prints out the time from NTP
88
:linenos:
99

1010
Set RTC
11-
------------
11+
-------
1212

1313
Sync your CircuitPython board's realtime clock (RTC) with time from NTP.
1414

1515
.. literalinclude:: ../examples/ntp_set_rtc.py
1616
:caption: examples/ntp_set_rtc.py
1717
:linenos:
1818

19+
Using Notifications
20+
-------------------
21+
22+
Using notifications to monitor state and control power usage.
23+
24+
.. literalinclude:: ../examples/ntp_powersave.py
25+
:caption: examples/ntp_powersave.py
26+
:linenos:
27+
1928
Simple test with CPython
2029
------------------------
2130

examples/ntp_powersave.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
def on_ntp_event(event_type: EventType, next_time: int):
2323
"""Handle notifications from NTP about not using the radio for awhile."""
2424
global check_connection # pylint:disable=global-statement
25-
if event_type == EventType.NO_EVENT:
26-
print(
27-
"No Event: "
28-
f"{time.monotonic_ns() =}, {wifi.radio.enabled =}, {wifi.radio.connected =}"
29-
)
30-
return
3125
print(f"event {event_type}: Next operation scheduled at {next_time} ns")
3226
if event_type == EventType.LOOKUP_FAILED:
3327
check_connection = True
@@ -82,9 +76,5 @@ def fmt_iso(datetime: time.struct_time) -> str:
8276
print(fmt_iso(ntp.datetime))
8377
except NTPIncompleteError:
8478
print("Waiting for NTP information")
85-
except Exception as ex:
86-
print(f"{type(ex)}")
87-
print(f"Exception: {ex}")
88-
raise
8979
# other regular processing …
9080
time.sleep(1)

0 commit comments

Comments
 (0)