Skip to content

Commit

Permalink
fix CI installer and black format
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Jan 8, 2024
1 parent 3dda098 commit 9f6dc09
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 91 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ jobs:
cache: 'pip'

- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pytest
run: pip install '.[test]'

- name: Run tests
run: pytest

- name: Build
run: python setup.py build

docs:
runs-on: ubuntu-latest
steps:
Expand All @@ -37,9 +32,7 @@ jobs:
with:
python-version: "3.10"
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install -r docs/doc-requirements.txt
run: pip install '.[docs]'
- name: Build documentation
run: |
cd docs
Expand Down
8 changes: 3 additions & 5 deletions openant/base/ant.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class Ant:
_RESET_WAIT = 1

def __init__(self):

self._driver = find_driver()

self._message_queue_cond = threading.Condition()
Expand Down Expand Up @@ -100,7 +99,6 @@ def _on_acknowledge(self, message):
)

def _on_burst_data(self, message):

sequence = message._data[0] >> 5
channel = message._data[0] & 0b00011111
data = message._data[1:]
Expand All @@ -122,7 +120,6 @@ def _on_burst_data(self, message):
)

def _worker(self):

_logger.debug("Ant runner started")

while self._running:
Expand All @@ -140,7 +137,6 @@ def _worker(self):
message._id == Message.ID.BROADCAST_DATA
and message._data == self._last_data
):

# Notifications
if message._id in [
Message.ID.STARTUP_MESSAGE,
Expand Down Expand Up @@ -318,7 +314,9 @@ def open_rx_scan_mode(self, channel=0):
:param channel int: channel number to use (doesn't really matter)
"""
message = Message(Message.ID.OPEN_RX_SCAN_MODE, [channel, 1]) # [Channel, 1-Enable]
message = Message(
Message.ID.OPEN_RX_SCAN_MODE, [channel, 1]
) # [Channel, 1-Enable]
self.write_message(message)

def close_channel(self, channel):
Expand Down
1 change: 0 additions & 1 deletion openant/base/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ def get_url(cls):
return None

def open(self):

# TODO find correct port on our own, could be done with
# serial.tools.list_ports, but that seems to have some
# problems at the moment.
Expand Down
30 changes: 17 additions & 13 deletions openant/devices/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def product_info_page_payload(self) -> List[int]:
payload[3] = int(float(self.software_ver)) * 10
except:
payload[3] = 0x00
payload[4:8] = self.serial_no.to_bytes(4, byteorder='little')
payload[4:8] = self.serial_no.to_bytes(4, byteorder="little")

return payload

Expand Down Expand Up @@ -196,7 +196,7 @@ def __init__(

self._found = False
self._attached = False
self._page_count = 0 # for interleaving pages
self._page_count = 0 # for interleaving pages

self.data = {
"common": CommonData(),
Expand All @@ -214,7 +214,6 @@ def __str__(self):

@staticmethod
def on_device_data(page: int, page_name: str, data: DeviceData):

"""Override this to capture device specific page data updates"""
assert page
assert page_name
Expand Down Expand Up @@ -250,14 +249,18 @@ def on_battery(data: BatteryData):
assert data
pass

def open_channel(self, extended=True, channel_type=None, ext_assign: Optional[int]=0x01):
def open_channel(
self, extended=True, channel_type=None, ext_assign: Optional[int] = 0x01
):
"""Configures and opens the channel for the device on the Node"""
if channel_type is None:
channel_type = Channel.Type.BIDIRECTIONAL_RECEIVE if not self.master else Channel.Type.BIDIRECTIONAL_TRANSMIT
channel_type = (
Channel.Type.BIDIRECTIONAL_RECEIVE
if not self.master
else Channel.Type.BIDIRECTIONAL_TRANSMIT
)

self.channel = self.node.new_channel(
channel_type, 0x00, ext_assign
)
self.channel = self.node.new_channel(channel_type, 0x00, ext_assign)

# configure callbacks based on if slave or master device
if not self.master:
Expand All @@ -278,7 +281,9 @@ def open_channel(self, extended=True, channel_type=None, ext_assign: Optional[in
self.channel.set_period(self.period)
self.channel.set_rf_freq(self.rf_freq)

_logger.debug(f"opening {self.name} channel #{self.channel.id}, TYPE 0x{channel_type:02x} dID {self.device_id}; dType {self.device_type}; dTrans 0x{self.trans_type:02x} {self.rf_freq} @ {self.period} ms")
_logger.debug(
f"opening {self.name} channel #{self.channel.id}, TYPE 0x{channel_type:02x} dID {self.device_id}; dType {self.device_type}; dTrans 0x{self.trans_type:02x} {self.rf_freq} @ {self.period} ms"
)
self.channel.open()

def close_channel(self):
Expand Down Expand Up @@ -315,7 +320,6 @@ def on_data(self, _):
pass

def _on_data(self, data):

# extended (> 8) has the device number and id beyond page
if len(data) > 8 and not self._attached:
device_id = data[9] + (data[10] << 8)
Expand Down Expand Up @@ -474,11 +478,11 @@ def _on_ack_data(self, data):
"""Replies to common page requests or forwards to `on_ack_data` callback"""
page = data[0]

if page == 80: # manufacturer
if page == 80: # manufacturer
payload = self.data["common"].manufacturer_page_payload()
elif page == 81: # product
elif page == 81: # product
payload = self.data["common"].product_info_page_payload()
elif page == 82: # battery
elif page == 82: # battery
# TODO
# payload = self.battery_status_page_payload()
payload = None
Expand Down
74 changes: 44 additions & 30 deletions openant/devices/controls_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ def from_byte(byte: int):
def to_byte(caps: set):
ret = 0x00
for c in caps:
ret |= (1 << c.value)
ret |= 1 << c.value
return ret

@classmethod
def _missing_(cls, _):
return ControlCapabilities.Reserved


class ControlCommand(Enum):
MenuUp = 0
MenuDown = 1
Expand All @@ -48,8 +49,8 @@ class ControlCommand(Enum):
Reset = 34
Length = 35
Lap = 36
Reserved = 0x7FFF # 5 - 31, 37 - 32767
Custom = 0xFFFE # 32768 - 65534
Reserved = 0x7FFF # 5 - 31, 37 - 32767
Custom = 0xFFFE # 32768 - 65534
NoCommand = 0xFFFF

@staticmethod
Expand All @@ -64,36 +65,34 @@ def from_int(i: int):
else:
return ControlCommand.NoCommand


class CommandStatus(Enum):
Pass = 0
Fail = 3
NotSupported = 4
Rejected = 5
Pending = 6
Reserved = 254 # 5 - 254
Reserved = 254 # 5 - 254
Uninitialized = 255

@classmethod
def _missing_(cls, _):
return CommandStatus.Uninitialized


@dataclass
class ControlsDeviceData(DeviceData):
"""ANT+ control device data"""

slave_serial: int = 0xFFFF
slave_manufacturer_id: int = 0xFFFF
capabilities: Set[ControlCapabilities] = field(
default_factory=lambda: set()
)
capabilities: Set[ControlCapabilities] = field(default_factory=lambda: set())
current_notifications: int = 0x00
command_sequence: int = 0 # inc each request
command_sequence: int = 0 # inc each request
command_status: CommandStatus = CommandStatus.Uninitialized
last_received_command_page: int = 0xFF
last_control_command: ControlCommand = ControlCommand.NoCommand
response_data: List[int] = field(
default_factory=lambda: [0xFF, 0xFF, 0xFF, 0xFF]
)
response_data: List[int] = field(default_factory=lambda: [0xFF, 0xFF, 0xFF, 0xFF])


class ControlsDevice(AntPlusDevice):
Expand Down Expand Up @@ -123,10 +122,16 @@ def on_data(self, data):

def on_tx_data(self) -> Optional[List[int]]:
# Control Device Availability, parent _on_tx_data handles common pages
return [0x02,
self.data["control"].current_notifications,
0x00, 0x00, 0x00, 0x00, 0x00, # reserved
ControlCapabilities.to_byte(self.data["control"].capabilities)]
return [
0x02,
self.data["control"].current_notifications,
0x00,
0x00,
0x00,
0x00,
0x00, # reserved
ControlCapabilities.to_byte(self.data["control"].capabilities),
]

def on_ack_data(self, data) -> Optional[List[int]]:
"""Commands are sent as ACK"""
Expand Down Expand Up @@ -155,20 +160,27 @@ def on_ack_data(self, data) -> Optional[List[int]]:
pass
# command status
elif page == 0x47:
payload = [0x47,
self.data["control"].last_received_command_page,
self.data["control"].command_sequence,
self.data["control"].command_status,
self.data["control"].response_data,
0x00, 0x00, 0x00, 0x00
]
payload = [
0x47,
self.data["control"].last_received_command_page,
self.data["control"].command_sequence,
self.data["control"].command_status,
self.data["control"].response_data,
0x00,
0x00,
0x00,
0x00,
]
payload[4:8] = self.data["control"].response_data

return payload

def _on_command(self, page: int):
# ensure command status has been set
if self.data["control"].command_status.value == CommandStatus.Uninitialized.value:
if (
self.data["control"].command_status.value
== CommandStatus.Uninitialized.value
):
self.data["control"].command_status = CommandStatus.Pass
# save last data
self.data["control"].last_received_command_page = page
Expand All @@ -186,7 +198,7 @@ def on_control_command(command: ControlCommand, raw: int):

def send_control_command_raw(self, command_int: int):
page = [0x00] * 8
page[0] = 0x49 # command page
page[0] = 0x49 # command page

data = self.data["control"]
data.command_sequence += 1
Expand All @@ -207,23 +219,25 @@ def send_control_command(self, command: ControlCommand):

# TODO command busrt


class GenericRemoteControl(ControlsDevice):
def __init__(self, node: Node, device_id = 0):
def __init__(self, node: Node, device_id=0):
super().__init__(
node,
device_id = device_id,
master = False,
device_id=device_id,
master=False,
name="generic_remote",
)

self.data["control"].capabilities.add(ControlCapabilities.GenericControl)


class GenericControllableDevice(ControlsDevice):
def __init__(self, node: Node, device_id = 0):
def __init__(self, node: Node, device_id=0):
super().__init__(
node,
device_id = device_id,
master = True,
device_id=device_id,
master=True,
name="generic_controllable_device",
)

Expand Down
14 changes: 9 additions & 5 deletions openant/devices/core_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
_logger = logging.getLogger(__name__)



class CoreTempDataQuality(Enum):
Poor = 0
Fair = 1
Expand All @@ -23,9 +22,11 @@ class CoreTempDataQuality(Enum):
def _missing_(cls, _):
return PressureSensorAlarm.Unknown


@dataclass
class CoteTemperatureData(DeviceData):
"""ANT+ core temp data"""

quality: CoreTempDataQuality = None
skin_temp: float = field(default=0, metadata={"unit": "°C"})
core_temp: float = field(default=0, metadata={"unit": "°C"})
Expand All @@ -43,7 +44,7 @@ def __init__(
node,
device_type=DeviceType.CoreTemp.value,
device_id=device_id,
period=16384, # 2Hz
period=16384, # 2Hz
name=name,
trans_type=trans_type,
)
Expand All @@ -64,8 +65,11 @@ def on_data(self, data):
# core temp main page
elif page == 0x01:
self._event_cout = data[1]
self.data["core_temp"].skin_temp = (data[3] | ((data[4] & 0xF0) << 4)) * 0.05
self.data["core_temp"].core_temp = int.from_bytes(data[6:8], byteorder="little") * 0.01
self.data["core_temp"].skin_temp = (
data[3] | ((data[4] & 0xF0) << 4)
) * 0.05
self.data["core_temp"].core_temp = (
int.from_bytes(data[6:8], byteorder="little") * 0.01
)

self.on_device_data(page, "core_temp", self.data["core_temp"])

2 changes: 1 addition & 1 deletion openant/devices/dropper_seatpost.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DelayIndicator(Enum):
class DropperSeatpostData(DeviceData):
"""ANT+ dropper seatpost data (shift)"""

configured_unlock_delay: float = field(default=0x7f, metadata={"unit": "s"})
configured_unlock_delay: float = field(default=0x7F, metadata={"unit": "s"})
delay_indicator: DelayIndicator = DelayIndicator.Unknown
valve_state: ValveState = ValveState.Unknown
lock_setting: ValveState = ValveState.Unknown
Expand Down
Loading

0 comments on commit 9f6dc09

Please sign in to comment.