Skip to content

Commit

Permalink
Merge pull request #3 from facts-engineering/wiznet5k-socketpool-migr…
Browse files Browse the repository at this point in the history
…ation

Update library to Wiznet5k's socketpool interface
  • Loading branch information
AdamCummick authored May 13, 2024
2 parents 0d58f55 + 73b1105 commit 7c6f947
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
16 changes: 3 additions & 13 deletions examples/helpers_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
9 changes: 3 additions & 6 deletions examples/ntp_rtc_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
14 changes: 4 additions & 10 deletions p1am_200_helpers/ntp_rtc_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import struct
import time
from adafruit_wiznet5k.adafruit_wiznet5k_socketpool import SocketPoolContants

class NTPException(Exception):
"""Exception for NTP errors"""
Expand All @@ -25,7 +26,7 @@ class NTP_RTC:
"""
def __init__(
self,
socket,
socketpool,
rtc,
time_zone_offset,
*,
Expand All @@ -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
Expand All @@ -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=SocketPoolContants.SOCK_DGRAM)
client.settimeout(self.timeout)

while attempts < self.retries:
Expand Down Expand Up @@ -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)
35 changes: 29 additions & 6 deletions p1am_200_helpers/p1am_200_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)

0 comments on commit 7c6f947

Please sign in to comment.