Skip to content

Commit c7194a2

Browse files
py: refactor channel bindings internals
Signed-off-by: Ivan-Velickovic <i.velickovic@unsw.edu.au>
1 parent 2aa4ea0 commit c7194a2

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/python/module.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,27 @@ class SddfStatus(IntEnum):
282282
libsdfgen.sdfgen_sddf_lwip_serialise_config.restype = c_bool
283283
libsdfgen.sdfgen_sddf_lwip_serialise_config.argtypes = [c_void_p, c_char_p]
284284

285+
def ffi_uint8_ptr(n: Optional[int]):
286+
"""
287+
Convert an int value to a uint8_t pointer for FFI.
288+
If 'n' is None then we return None (which acts as a null pointer)
289+
"""
290+
if n is None:
291+
return None
292+
293+
return pointer(c_uint8(n))
294+
295+
296+
def ffi_bool_ptr(val: Optional[bool]):
297+
"""
298+
Convert a bool value to a bool pointer for FFI.
299+
If 'val' is None then we return None (which acts as a null pointer)
300+
"""
301+
if n is None:
302+
return None
303+
304+
return pointer(c_bool(val))
305+
285306

286307
class DeviceTree:
287308
"""
@@ -551,37 +572,23 @@ def __init__(
551572
notify_a: Optional[bool] = None,
552573
notify_b: Optional[bool] = None,
553574
) -> None:
554-
a_id_ptr = None
555-
if a_id is not None:
556-
a_id_u8 = c_uint8(a_id)
557-
a_id_ptr = pointer(a_id_u8)
558-
b_id_ptr = None
559-
if b_id is not None:
560-
b_id_u8 = c_uint8(b_id)
561-
b_id_ptr = pointer(b_id_u8)
562-
563-
pp_ptr = None
564575
if pp_a is not None:
565-
pp = c_uint8(0)
566-
pp_ptr = pointer(pp)
576+
c_pp = 0
567577
elif pp_b is not None:
568-
pp = c_uint8(1)
569-
pp_ptr = pointer(pp)
570-
571-
572-
notify_a_ptr = None
573-
notify_b_ptr = None
574-
if notify_a:
575-
notify_a_c = c_bool(notify_a)
576-
notify_a_ptr = pointer(notify_a_c)
577-
if notify_b:
578-
notify_b_c = c_bool(notify_b)
579-
notify_b_ptr = pointer(notify_b_c)
578+
c_pp = 1
580579

581580
if pp_a is not None and pp_b is not None:
582581
raise Exception("attempting to create channel with PP on both ends")
583582

584-
self._obj = libsdfgen.sdfgen_channel_create(a._obj, b._obj, a_id_ptr, b_id_ptr, notify_a_ptr, notify_b_ptr, pp_ptr)
583+
self._obj = libsdfgen.sdfgen_channel_create(
584+
a._obj,
585+
b._obj,
586+
ffi_uint8_ptr(a_id),
587+
ffi_uint8_ptr(b_id),
588+
ffi_bool_ptr(notify_a),
589+
ffi_bool_ptr(notify_b),
590+
ffi_uint8_ptr(pp_val),
591+
)
585592

586593
@property
587594
def pd_a_id(self) -> int:

0 commit comments

Comments
 (0)