Skip to content

Commit 793e93f

Browse files
author
BiffoBear
committed
Moved debug_msg() from init.py to adafruit_wiznet5k_debug.py.
1 parent 0fd0fa2 commit 793e93f

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed

adafruit_wiznet5k/__init__.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +0,0 @@
1-
# SPDX-FileCopyrightText: 2023 Martin Stephens
2-
#
3-
# SPDX-License-Identifier: MIT
4-
5-
"""Makes a debug message function available to all modules."""
6-
try:
7-
from typing import TYPE_CHECKING, Optional, Union, Tuple, Sequence
8-
9-
if TYPE_CHECKING:
10-
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
11-
except ImportError:
12-
pass
13-
14-
import gc
15-
16-
17-
def debug_msg(
18-
message: Union[Exception, str, bytes, bytearray], debugging: bool
19-
) -> None:
20-
"""
21-
Helper function to print debugging messages. If the message is a bytes type
22-
object, create a hexdump.
23-
24-
:param Union[Exception, str, bytes, bytearray] message: The message to print.
25-
:param bool debugging: Only print if debugging is True.
26-
"""
27-
if debugging:
28-
if isinstance(message, (bytes, bytearray)):
29-
message = _hexdump(message)
30-
print(message)
31-
del message
32-
gc.collect()
33-
34-
35-
def _hexdump(src: bytes):
36-
"""
37-
Create a 16 column hexdump of a bytes object.
38-
39-
:param bytes src: The bytes object to hexdump.
40-
41-
:returns str: The hexdump.
42-
"""
43-
result = []
44-
for i in range(0, len(src), 16):
45-
chunk = src[i : i + 16]
46-
hexa = " ".join(("{:02x}".format(x) for x in chunk))
47-
text = "".join((chr(x) if 0x20 <= x < 0x7F else "." for x in chunk))
48-
result.append("{:04x} {:<48} {}".format(i, hexa, text))
49-
return "\n".join(result)

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from micropython import const
4949

5050
from adafruit_bus_device.spi_device import SPIDevice
51-
from adafruit_wiznet5k import debug_msg
51+
from adafruit_wiznet5k.adafruit_wiznet5k_debug import debug_msg
5252
import adafruit_wiznet5k.adafruit_wiznet5k_dhcp as dhcp
5353
import adafruit_wiznet5k.adafruit_wiznet5k_dns as dns
5454

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SPDX-FileCopyrightText: 2023 Martin Stephens
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""Makes a debug message function available to all modules."""
6+
try:
7+
from typing import TYPE_CHECKING, Union
8+
9+
if TYPE_CHECKING:
10+
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
11+
except ImportError:
12+
pass
13+
14+
import gc
15+
16+
17+
def debug_msg(
18+
message: Union[Exception, str, bytes, bytearray], debugging: bool
19+
) -> None:
20+
"""
21+
Helper function to print debugging messages. If the message is a bytes type
22+
object, create a hexdump.
23+
24+
:param Union[Exception, str, bytes, bytearray] message: The message to print.
25+
:param bool debugging: Only print if debugging is True.
26+
"""
27+
if debugging:
28+
if isinstance(message, (bytes, bytearray)):
29+
message = _hexdump(message)
30+
print(message)
31+
del message
32+
gc.collect()
33+
34+
35+
def _hexdump(src: bytes):
36+
"""
37+
Create a 16 column hexdump of a bytes object.
38+
39+
:param bytes src: The bytes object to hexdump.
40+
41+
:returns str: The hexdump.
42+
"""
43+
result = []
44+
for i in range(0, len(src), 16):
45+
chunk = src[i : i + 16]
46+
hexa = " ".join(("{:02x}".format(x) for x in chunk))
47+
text = "".join((chr(x) if 0x20 <= x < 0x7F else "." for x in chunk))
48+
result.append("{:04x} {:<48} {}".format(i, hexa, text))
49+
return "\n".join(result)

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import time
2929
from random import randint
3030
from micropython import const
31-
from adafruit_wiznet5k import debug_msg # pylint: disable=ungrouped-imports
31+
from adafruit_wiznet5k.adafruit_wiznet5k_debug import ( # pylint: disable=ungrouped-imports
32+
debug_msg,
33+
)
3234

3335
# DHCP State Machine
3436
_STATE_INIT = const(0x01)

0 commit comments

Comments
 (0)