Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions rv3028/rv3028.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ def set_time(self, hours: int, minutes: int, seconds: int) -> None:
minutes (int): The minute value to set (0-59).
seconds (int): The second value to set (0-59).
"""
if hours < 0 or hours > 23:
raise ValueError("Hour value must be between 0 and 23")
if minutes < 0 or minutes > 59:
raise ValueError("Minute value must be between 0 and 59")
if seconds < 0 or seconds > 59:
raise ValueError("Second vaue must be between 0 and 59")

data = bytes(
[
self._int_to_bcd(seconds),
Expand Down Expand Up @@ -154,6 +161,15 @@ def set_date(self, year: int, month: int, date: int, weekday: int) -> None:
date (int): The date value to set (1-31).
weekday (int): The day of the week to set (0-6, where 0 represents Sunday).
"""
if year < 0 or year > 99:
raise ValueError("Year value must be between 0 and 99")
if month < 1 or month > 12:
raise ValueError("Month value must be between 1 and 12")
if date < 1 or date > 31:
raise ValueError("Date value must be between 1 and 31")
if weekday < 0 or weekday > 6:
raise ValueError("Weekday value must be between 0 and 6")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have these statements check if the value passed is None or would it just not accept it if one of the values in the tuple that is sent in is just None.

Example:

c.rtc.set_date(99,None,29,5)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! I think it's a good idea to enforce that the full date be set, as I don't really see a use case for partial dates, and it can lead to weird behavior that the user would have to manage. Also, the plan is to eventually replace this format with the datetime iso8601 compliant format in the future, which will require a full date anyway.

Let me know if you have a specific use case for partial dates in mind though!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good to me! I'll just rubber stamps this for now and we can circle back to full date checking in a future update.


data = bytes(
[
self._int_to_bcd(weekday),
Expand Down Expand Up @@ -196,18 +212,18 @@ def set_alarm(
Alarm hour (0-23) or None
Alarm weekday (0-6, 0=Sunday) or None
"""
# Set alarm mask to check for minute, hour, and weekday match
control2 = self._read_register(Reg.CONTROL2)[0]
self._set_flag(Reg.CONTROL2, Control2.ALARM_INT_ENABLE, Flag.SET)
self._write_register(Reg.CONTROL2, bytes([control2]))

if minute is not None and (minute < 0 or minute > 59):
raise ValueError("Invalid minute value")
if hour is not None and (hour < 0 or hour > 23):
raise ValueError("Invalid hour value")
if weekday is not None and (weekday < 0 or weekday > 6):
raise ValueError("Invalid weekday value")

# Set alarm mask to check for minute, hour, and weekday match
control2 = self._read_register(Reg.CONTROL2)[0]
self._set_flag(Reg.CONTROL2, Control2.ALARM_INT_ENABLE, Flag.SET)
self._write_register(Reg.CONTROL2, bytes([control2]))

data = bytes(
(self._int_to_bcd(param) & Alarm.VALUE)
if param is not None
Expand Down