Skip to content

Commit

Permalink
Add some type-hinting to callback func signature.
Browse files Browse the repository at this point in the history
But we must use generic Structure because actual packet types are not known until run time.
  • Loading branch information
cboulay committed Jan 30, 2024
1 parent 9fcd739 commit 03aa6c2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 6 additions & 4 deletions pycbsdk/cbhw/device/nsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,15 @@ def _black_hole(self, pkt):
# endregion

# region AdvancedCallbacks
def register_group_callback(self, group: int, callback: Callable[[], None]) -> int:
def register_group_callback(
self, group: int, callback: Callable[[Structure], None]
) -> int:
# TODO: Make this thread safe.
self.group_callbacks[group].append(callback)
return 0

def unregister_group_callback(
self, group: int, callback: Callable[[], None]
self, group: int, callback: Callable[[Structure], None]
) -> int:
if callback in self.group_callbacks[group]:
self.group_callbacks[group].remove(callback)
Expand All @@ -454,7 +456,7 @@ def unregister_group_callback(
return -1

def register_event_callback(
self, chan_type: CBChannelType, callback: Callable[[], None]
self, chan_type: CBChannelType, callback: Callable[[Structure], None]
) -> int:
"""
:param chan_type: The type of channel this event is associated with. See CBChannelType for more info.
Expand All @@ -466,7 +468,7 @@ def register_event_callback(
return 0

def register_config_callback(
self, pkt_type: CBPacketType, callback: Callable[[], None]
self, pkt_type: CBPacketType, callback: Callable[[Structure], None]
) -> int:
# TODO: Make this thread safe.
self.config_callbacks[pkt_type].append(callback)
Expand Down
11 changes: 6 additions & 5 deletions pycbsdk/cbsdk.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ctypes import Structure
from typing import Optional
from collections.abc import Callable

Expand Down Expand Up @@ -105,31 +106,31 @@ def get_runlevel(device: NSPDevice) -> CBRunLevel:
return device.get_runlevel()


def register_spk_callback(device: NSPDevice, func: Callable[[], None]) -> int:
def register_spk_callback(device: NSPDevice, func: Callable[[Structure], None]) -> int:
return register_event_callback(device, CBChannelType.FrontEnd, func)


def register_event_callback(
device: NSPDevice, channel_type: CBChannelType, func: Callable[[], None]
device: NSPDevice, channel_type: CBChannelType, func: Callable[[Structure], None]
) -> int:
return device.register_event_callback(channel_type, func)


def register_group_callback(
device: NSPDevice, group: int, func: Callable[[], None]
device: NSPDevice, group: int, func: Callable[[Structure], None]
) -> int:
# group: 1-6 for sampling group.
return device.register_group_callback(group, func)


def unregister_group_callback(
device: NSPDevice, group: int, func: Callable[[], None]
device: NSPDevice, group: int, func: Callable[[Structure], None]
) -> int:
# group: 1-6 for sampling group.
return device.unregister_group_callback(group, func)


def register_config_callback(
device: NSPDevice, packet_type: CBPacketType, func: Callable[[], None]
device: NSPDevice, packet_type: CBPacketType, func: Callable[[Structure], None]
) -> int:
return device.register_config_callback(packet_type, func)

0 comments on commit 03aa6c2

Please sign in to comment.