|
| 1 | +# @license |
| 2 | +# Copyright 2025 Porpoiseful LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# @fileoverview This is a class to handle port types |
| 17 | +# @author [email protected] (Alan Smith) |
| 18 | +from abc import ABC, abstractmethod |
| 19 | +from enum import Enum |
| 20 | +from typing import Self |
| 21 | + |
| 22 | +class PortType(Enum): |
| 23 | + CAN_PORT = 1 |
| 24 | + SMART_IO_PORT = 2 |
| 25 | + SMART_MOTOR_PORT = 3 |
| 26 | + SERVO_PORT = 4 |
| 27 | + I2C_PORT = 5 |
| 28 | + USB_PORT = 6 |
| 29 | + EXPANSION_HUB_MOTOR_PORT = 7 # This is the port on the expansion hub |
| 30 | + EXPANSION_HUB_SERVO_PORT = 8 # This is the port on the expansion hub |
| 31 | + |
| 32 | + BASE_COMPOUND = 256 |
| 33 | + USB_HUB = BASE_COMPOUND + 1 |
| 34 | + EXPANSION_HUB_MOTOR = BASE_COMPOUND + 2 # This is combination expansion hub and motor |
| 35 | + EXPANSION_HUB_SERVO = BASE_COMPOUND + 3 # This is combination expansion hub and servo |
| 36 | + |
| 37 | +class Port(ABC): |
| 38 | + """Abstract base class for all port types.""" |
| 39 | + |
| 40 | + def __init__(self, port_type: PortType): |
| 41 | + self.type = port_type |
| 42 | + |
| 43 | + @abstractmethod |
| 44 | + def get_all_ports(self) -> list[tuple[PortType, int]]: |
| 45 | + """Return a list of all simple ports contained in this port.""" |
| 46 | + pass |
| 47 | + |
| 48 | + def get_type(self) -> PortType: |
| 49 | + """Returns the port type""" |
| 50 | + return self.type |
| 51 | + |
| 52 | +class SimplePort(Port): |
| 53 | + def __init__(self, port_type: PortType, location: int): |
| 54 | + """ |
| 55 | + Create a simple port with a type and location. |
| 56 | +
|
| 57 | + Args: |
| 58 | + port_type: PortType for this port (must be a simple type) |
| 59 | + location: int location for this port |
| 60 | + """ |
| 61 | + if port_type.value >= PortType.BASE_COMPOUND.value: |
| 62 | + raise ValueError("Port must be of a simple type") |
| 63 | + super().__init__(port_type) |
| 64 | + self.location = location |
| 65 | + |
| 66 | + def get_all_ports(self) -> list[tuple[PortType, int]]: |
| 67 | + """Return a list containing this simple port.""" |
| 68 | + return [(self.type, self.location)] |
| 69 | + |
| 70 | + def __str__(self) -> str: |
| 71 | + return f"SimplePort({self.type}, {self.location})" |
| 72 | + |
| 73 | +class CompoundPort(Port): |
| 74 | + def __init__(self, port_type: PortType, port1: Port, port2: Port): |
| 75 | + """ |
| 76 | + Create a compound port from two other ports. |
| 77 | +
|
| 78 | + Args: |
| 79 | + port_type: PortType for this port (must be a compound type) |
| 80 | + port1: First Port for compound ports |
| 81 | + port2: Second Port for compound ports |
| 82 | + """ |
| 83 | + if port_type.value < PortType.BASE_COMPOUND.value: |
| 84 | + raise ValueError("Port must be of a compound type") |
| 85 | + super().__init__(port_type) |
| 86 | + self.port1 = port1 |
| 87 | + self.port2 = port2 |
| 88 | + |
| 89 | + def get_all_ports(self) -> list[tuple[PortType, int]]: |
| 90 | + """Return a list of all simple ports contained in this compound port.""" |
| 91 | + return self.port1.get_all_ports() + self.port2.get_all_ports() |
| 92 | + |
| 93 | + def __str__(self) -> str: |
| 94 | + return f"CompoundPort({self.type}: {self.port1}, {self.port2})" |
0 commit comments