Skip to content

Commit 1c18de9

Browse files
committed
refactor code to module
1 parent 726bcbd commit 1c18de9

File tree

6 files changed

+104
-10
lines changed

6 files changed

+104
-10
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Of course, you must import the library to use it:
7575
.. code:: python
7676
7777
import busio
78-
import adafruit_pcf8563
78+
from adafruit_pcf8563.pcf8563 import PCF8563
7979
import time
8080
8181
All the Adafruit RTC libraries take an instantiated and active I2C object
@@ -98,7 +98,7 @@ the RTC object:
9898

9999
.. code:: python
100100
101-
rtc = adafruit_pcf8563.PCF8563(i2c_bus)
101+
rtc = PCF8563(i2c_bus)
102102
103103
Date and time
104104
-------------

adafruit_pcf8563/clock.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# SPDX-FileCopyrightText: 2016 Philip R. Moyer and Radomir Dopieralski for Adafruit Industries.
2+
# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
3+
# SPDX-FileCopyrightText: Copyright (c) 2023 Bernhard Bablok
4+
#
5+
# SPDX-License-Identifier: MIT
6+
7+
"""
8+
`clock` - PCF8563 Clock module
9+
==============================
10+
11+
This class supports the clkout-feature of the PCF8563-based RTC in CircuitPython.
12+
13+
Functions are included for reading and writing registers to configure
14+
clklout frequency.
15+
16+
The class supports stand-alone usage. In this case, pass an i2-bus object
17+
to the constructor. If used together with the PCF8563 class (rtc), instantiate
18+
the rtc-object first and then pass the i2c_device attribute of the rtc
19+
to the constructor of the clock.
20+
21+
Author(s): Bernhard Bablok
22+
Date: March 2023
23+
24+
Implementation Notes
25+
--------------------
26+
27+
**Hardware:**
28+
29+
* `Seeeduino XIAO Expansion Board <https://www.adafruit.com/product/5033>`_
30+
- Works With Adafruit QT Py (Product ID: 5033)
31+
32+
**Software and Dependencies:**
33+
34+
* Adafruit CircuitPython firmware: https://github.com/adafruit/circuitpython/releases
35+
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
36+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
37+
38+
**Notes:**
39+
40+
#. Datasheet: http://cache.nxp.com/documents/data_sheet/PCF8563.pdf
41+
42+
"""
43+
44+
__version__ = "0.0.0+auto.0"
45+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PCF8563.git"
46+
47+
import time
48+
49+
from adafruit_bus_device.i2c_device import I2CDevice
50+
from adafruit_register import i2c_bit
51+
from adafruit_register import i2c_bits
52+
from micropython import const
53+
54+
try:
55+
from typing import Union
56+
from busio import I2C
57+
except ImportError:
58+
pass
59+
60+
61+
class Clock: # pylint: disable=too-few-public-methods
62+
"""Interface to the clkout of the PCF8563 RTC.
63+
64+
:param I2C i2c_bus: The I2C bus object
65+
"""
66+
67+
clockout_enabled = i2c_bit.RWBit(0x0D, 7)
68+
"""True if clockout is enabled (default). To disable clockout, set to False"""
69+
70+
clockout_frequency = i2c_bits.RWBits(2, 0x0D, 0)
71+
"""Clock output frequencies generated. Default is 32.768kHz.
72+
Possible values are as shown (selection value - frequency).
73+
00 - 32.768khz
74+
01 - 1.024kHz
75+
10 - 0.032kHz (32Hz)
76+
11 - 0.001kHz (1Hz)
77+
"""
78+
79+
CLOCKOUT_FREQ_32KHZ = const(0b00)
80+
"""Clock frequency of 32 KHz"""
81+
CLOCKOUT_FREQ_1KHZ = const(0b01)
82+
"""Clock frequency of 4 KHz"""
83+
CLOCKOUT_FREQ_32HZ = const(0b10)
84+
"""Clock frequency of 32 Hz"""
85+
CLOCKOUT_FREQ_1HZ = const(0b11)
86+
"""Clock frequency of 1 Hz"""
87+
88+
def __init__(self, i2c: Union[I2C, I2CDevice]) -> None:
89+
if isinstance(i2c, I2CDevice):
90+
self.i2c_device = i2c # reuse i2c_device (from PCF8563-instance)
91+
else:
92+
time.sleep(0.05)
93+
self.i2c_device = I2CDevice(i2c, 0x51)

adafruit_pcf8563/pcf8563.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ class is inherited by the chip-specific subclasses.
3636
3737
#. Milliseconds are not supported by this RTC.
3838
#. The alarm does not support seconds. It will always fire on full minutes.
39-
#. This RTC has a single timer. The class PCF8563_Timer implements the
39+
#. This RTC has a single timer. The class Timer implements the
4040
interface to timer-specfic registers.
41+
#. The class Clock implements the configuration of the clkout-pin.
4142
#. Datasheet: http://cache.nxp.com/documents/data_sheet/PCF8563.pdf
4243
4344
"""

adafruit_pcf8563/timer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# SPDX-License-Identifier: MIT
66

77
"""
8-
`adafruit_pcf8563_timer` - PCF8563 Timer module
9-
===============================================
8+
`timer` - PCF8563 Timer module
9+
==============================
1010
1111
This class supports the timer of the PCF8563-based RTC in CircuitPython.
1212
@@ -19,7 +19,7 @@
1919
to the constructor of the timer.
2020
2121
Author(s): Bernhard Bablok
22-
Date: November 2023
22+
Date: March 2023
2323
2424
Implementation Notes
2525
--------------------
@@ -58,7 +58,7 @@
5858
pass
5959

6060

61-
class PCF8563_Timer: # pylint: disable=too-few-public-methods
61+
class Timer: # pylint: disable=too-few-public-methods
6262
"""Interface to the timer of the PCF8563 RTC.
6363
6464
:param I2C i2c_bus: The I2C bus object

examples/pcf8563_simpletest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import board
1313
import busio
1414

15-
import adafruit_pcf8563
15+
from adafruit_pcf8563.pcf8563 import PCF8563
1616

1717
# Change to the appropriate I2C clock & data pins here!
1818
i2c_bus = busio.I2C(board.SCL, board.SDA)
1919

2020
# Create the RTC instance:
21-
rtc = adafruit_pcf8563.PCF8563(i2c_bus)
21+
rtc = PCF8563(i2c_bus)
2222

2323
# Lookup table for names of days (nicer printing).
2424
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ classifiers = [
4242
dynamic = ["dependencies", "optional-dependencies"]
4343

4444
[tool.setuptools]
45-
py-modules = ["adafruit_pcf8563"]
45+
packages = ["adafruit_pcf8563"]
4646

4747
[tool.setuptools.dynamic]
4848
dependencies = {file = ["requirements.txt"]}

0 commit comments

Comments
 (0)