From c49ffa871fb484db6c76aee164cf6a827938fc01 Mon Sep 17 00:00:00 2001 From: Tristan Warder Date: Fri, 3 May 2024 11:19:33 -0400 Subject: [PATCH 1/3] Update helpers to use socketpools Gives users a "get_socketpool" function and modifies NTP_RTC code to utilize socketpools instead of the old socket interface. --- examples/ntp_rtc_example.py | 9 +++---- p1am_200_helpers/ntp_rtc_helper.py | 14 ++++------- p1am_200_helpers/p1am_200_helpers.py | 35 +++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/examples/ntp_rtc_example.py b/examples/ntp_rtc_example.py index 958ca66..c440aa8 100644 --- a/examples/ntp_rtc_example.py +++ b/examples/ntp_rtc_example.py @@ -20,8 +20,7 @@ """ -from p1am_200_helpers import get_rtc, get_ethernet, NTP_RTC -import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket +from p1am_200_helpers import get_rtc, get_ethernet, sync_rtc, pretty_print_time # RTC / ETH rtc = get_rtc() @@ -33,12 +32,10 @@ sync_interval = 1 # Next sync in minute(s) next_sync = rtc.datetime.tm_min -# NTP RTC Object -my_ntp = NTP_RTC(socket, rtc, timezone_offset) while True: if rtc.datetime.tm_min == next_sync or rtc.datetime_compromised: print("Syncing with NTP server...") - my_ntp.sync() # Sync the RTC with the NTP server - my_ntp.pretty_print_time() + sync_rtc(timezone_offset) # Sync the RTC with the NTP server + pretty_print_time() next_sync = (rtc.datetime.tm_min + sync_interval) % 60 diff --git a/p1am_200_helpers/ntp_rtc_helper.py b/p1am_200_helpers/ntp_rtc_helper.py index 526cc33..5871380 100644 --- a/p1am_200_helpers/ntp_rtc_helper.py +++ b/p1am_200_helpers/ntp_rtc_helper.py @@ -8,6 +8,7 @@ """ import struct import time +from adafruit_wiznet5k.adafruit_wiznet5k_socketpool import SocketPoolConstants class NTPException(Exception): """Exception for NTP errors""" @@ -25,7 +26,7 @@ class NTP_RTC: """ def __init__( self, - socket, + socketpool, rtc, time_zone_offset, *, @@ -34,7 +35,7 @@ def __init__( timeout=1, # Default to 1 seconds debug = False ): - self.socket = socket + self.socketpool = socketpool self.rtc = rtc self.tz_offset = time_zone_offset * 60 * 60 # Convert to seconds self.ntp_server = ntp_server @@ -53,7 +54,7 @@ def get_epoch(self): recv_data = None attempts = 0 - client = self.socket.socket(type=self.socket.SOCK_DGRAM) + client = self.socketpool.socket(type=SocketPoolConstants.SOCK_DGRAM) client.settimeout(self.timeout) while attempts < self.retries: @@ -84,10 +85,3 @@ def set_rtc(self, epoch): current_time.tm_min, current_time.tm_sec, current_time.tm_wday, -1, -1)) self.rtc.datetime = formatted_time - - def pretty_print_time(self): - """Convert datetime to human readable time.""" - t = self.rtc.datetime - formatted_time = "Date: {}/{}/{}\nTime: {}:{:02}:{:02}".format( - t.tm_mon, t.tm_mday, t.tm_year,t.tm_hour, t.tm_min, t.tm_sec) - print(formatted_time) diff --git a/p1am_200_helpers/p1am_200_helpers.py b/p1am_200_helpers/p1am_200_helpers.py index 7baa908..e3677bd 100644 --- a/p1am_200_helpers/p1am_200_helpers.py +++ b/p1am_200_helpers/p1am_200_helpers.py @@ -22,7 +22,7 @@ import neopixel import adafruit_sdcard from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K -import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket +import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool from p1am_200_helpers.ntp_rtc_helper import NTP_RTC, NTPException __version__ = "0.0.0+auto.0" @@ -32,6 +32,7 @@ _rtc = None _eeprom = None _eth_iface = None +_eth_socket_pool = None _port_1_control = None _port_2_control = None _vfs = None @@ -153,11 +154,19 @@ def get_ethernet(dhcp=True): cs = DigitalInOut(board.D5) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) mac = get_eeprom().mac - _eth_iface = WIZNET5K(spi_bus, cs, is_dhcp=dhcp, mac=bytes(mac)) - socket.set_interface(_eth_iface) + _eth_iface = WIZNET5K(spi_bus, cs, is_dhcp=dhcp, mac=bytes(mac)) return _eth_iface -def sync_rtc(timezone_offset=-5): +def get_socketpool(): + global _eth_iface, _eth_socket_pool + if _eth_iface is None: + raise RuntimeError("Ethernet interface not initialized") + else: + if _eth_socket_pool is None: + _eth_socket_pool = socketpool.SocketPool(_eth_iface) + return _eth_socket_pool + +def sync_rtc(timezone_offset=-5, socketpool=None): """Syncs the RTC with the NTP server""" global _rtc, _eth_iface @@ -166,12 +175,26 @@ def sync_rtc(timezone_offset=-5): if _rtc is None: _rtc = get_rtc() - - ntp = NTP_RTC(socket, _rtc, timezone_offset) + if socketpool is None: + socketpool = get_socketpool() + ntp = NTP_RTC(socketpool, _rtc, timezone_offset) try: ntp.sync() except NTPException as e: print(e) return False return True + +def pretty_print_time(datetime=None): + """Convert datetime to human readable time.""" + global _rtc + if datetime is None: + if _rtc is None: + _rtc = get_rtc() + t = _rtc.datetime + else: + t = datetime + formatted_time = "Date: {}/{}/{}\nTime: {}:{:02}:{:02}".format( + t.tm_mon, t.tm_mday, t.tm_year,t.tm_hour, t.tm_min, t.tm_sec) + print(formatted_time) From f23509d4fd5215625b72cf1189e9a35fbfa26260 Mon Sep 17 00:00:00 2001 From: Tristan Warder Date: Mon, 13 May 2024 13:05:10 -0400 Subject: [PATCH 2/3] Updated helper example --- examples/helpers_example.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/examples/helpers_example.py b/examples/helpers_example.py index 88a06f9..275bf32 100644 --- a/examples/helpers_example.py +++ b/examples/helpers_example.py @@ -43,14 +43,14 @@ def main(): # RTC rtc = helper.get_rtc() - print(rtc_format_time(rtc)) # print current time + helper.pretty_print_time() # print current time # Ethernet if True: # change to false if not using the P1AM-ETH eth = helper.get_ethernet(dhcp=True) # get ethernet object print(f"IP Address is: {eth.pretty_ip(eth.ip_address)}") # print IP address helper.sync_rtc(-4) # sync RTC with NTP server, offset by -4 hours for EST - print(rtc_format_time(rtc)) # print current time + helper.pretty_print_time() # print current time # EEPROM eeprom = helper.get_eeprom() @@ -98,17 +98,7 @@ def main(): time.sleep(.01) if time.monotonic() - last_time > 5: # Every 5 seconds, print current time - t = rtc.datetime - print(f"{t.tm_hour % 12}:{t.tm_min:02}:{t.tm_sec:02}") + helper.pretty_print_time() last_time = time.monotonic() - - -def rtc_format_time(rtc): - days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") - t = rtc.datetime - current = f"The date is {days[int(t.tm_wday)]} {t.tm_mon:02}/{t.tm_mday}/{t.tm_year} " - current += f"The time is {t.tm_hour:02}:{t.tm_min:02}:{t.tm_sec:02}" - return current - main() # run main function \ No newline at end of file From 73b110530163a38ad649418c56778a828dd74447 Mon Sep 17 00:00:00 2001 From: Tristan Warder Date: Mon, 13 May 2024 13:09:13 -0400 Subject: [PATCH 3/3] Add typo I'm sorry --- p1am_200_helpers/ntp_rtc_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/p1am_200_helpers/ntp_rtc_helper.py b/p1am_200_helpers/ntp_rtc_helper.py index 5871380..d06c5a2 100644 --- a/p1am_200_helpers/ntp_rtc_helper.py +++ b/p1am_200_helpers/ntp_rtc_helper.py @@ -8,7 +8,7 @@ """ import struct import time -from adafruit_wiznet5k.adafruit_wiznet5k_socketpool import SocketPoolConstants +from adafruit_wiznet5k.adafruit_wiznet5k_socketpool import SocketPoolContants class NTPException(Exception): """Exception for NTP errors""" @@ -54,7 +54,7 @@ def get_epoch(self): recv_data = None attempts = 0 - client = self.socketpool.socket(type=SocketPoolConstants.SOCK_DGRAM) + client = self.socketpool.socket(type=SocketPoolContants.SOCK_DGRAM) client.settimeout(self.timeout) while attempts < self.retries: