8000 Add type hinting by ArchdukeTim · Pull Request #470 · robotpy/robotpy-wpilib · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Add type hinting #470

Merged
merged 15 commits into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions wpilib/wpilib/adxl345_i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
# must be accompanied by the FIRST BSD license file in the root directory of
# the project.
# ----------------------------------------------------------------------------
from typing import Tuple, Optional

import hal

from .interfaces import Accelerometer
from .i2c import I2C
from .sendablebase import SendableBase
from .interfaces import Accelerometer
from .sendablebuilder import SendableBuilder

__all__ = ["ADXL345_I2C"]

Expand Down Expand Up @@ -44,13 +45,13 @@ class Axes:
kY = 0x02
kZ = 0x04

def __init__(self, port, range, address=None):
def __init__(
self, port: I2C.Port, range: Range, address: Optional[int] = None
) -> None:
"""Constructor.

:param port: The I2C port the accelerometer is attached to.
:type port: :class:`.I2C.Port`
:param range: The range (+ or -) that the accelerometer will measure.
:type range: :class:`.ADXL345_I2C.Range`
:param address: the I2C address of the accelerometer (0x1D or 0x53)
"""
if address is None:
Expand All @@ -69,18 +70,17 @@ def __init__(self, port, range, address=None):

self.setName("ADXL345_I2C", port)

def close(self):
def close(self) -> None:
self.i2c.close()
super().close()

# Accelerometer interface

def setRange(self, range):
def setRange(self, range: Range) -> None:
"""Set the measuring range of the accelerometer.

:param range: The maximum acceleration, positive or negative, that
the accelerometer will measure.
:type range: :class:`ADXL345_I2C.Range`
"""
if range == self.Range.k2G:
value = 0
Expand All @@ -96,28 +96,28 @@ def setRange(self, range):
# Specify the data format to read
self.i2c.write(self.kDataFormatRegister, self.kDataFormat_FullRes | value)

def getX(self):
def getX(self) -> float:
"""Get the x axis acceleration

:returns: The acceleration along the x axis in g-forces
"""
return self.getAcceleration(self.Axes.kX)

def getY(self):
def getY(self) -> float:
"""Get the y axis acceleration

:returns: The acceleration along the y axis in g-forces
"""
return self.getAcceleration(self.Axes.kY)

def getZ(self):
def getZ(self) -> float:
"""Get the z axis acceleration

:returns: The acceleration along the z axis in g-forces
"""
return self.getAcceleration(self.Axes.kZ)

def getAcceleration(self, axis):
def getAcceleration(self, axis: Axes) -> float:
"""Get the acceleration of one axis in Gs.

:param axis: The axis to read from.
Expand All @@ -128,7 +128,7 @@ def getAcceleration(self, axis):
rawAccel = (data[1] << 8) | data[0]
return rawAccel * self.kGsPerLSB

def getAccelerations(self):
def getAccelerations(self) -> Tuple[float, float, float]:
"""Get the acceleration of all axes in Gs.

:returns: X,Y,Z tuple of acceleration measured on all axes of the
Expand All @@ -147,13 +147,13 @@ def getAccelerations(self):
rawData[2] * self.kGsPerLSB,
)

def _updateValues(self):
def _updateValues(self) -> None:
data = self.getAccelerations()
self._entryX.setDouble(data[0])
self._entryY.setDouble(data[1])
self._entryZ.setDouble(data[2])

def initSendable(self, builder):
def initSendable(self, builder: SendableBuilder) -> None:
builder.setSmartDashboardType("3AxisAccelerometer")
self._entryX = builder.getEntry("X")
self._entryY = builder.getEntry("Y")
Expand Down
25 changes: 12 additions & 13 deletions wpilib/wpilib/adxl345_spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
# must be accompanied by the FIRST BSD license file in the root directory of
# the project.
# ----------------------------------------------------------------------------
from typing import Tuple

import hal

from .interfaces import Accelerometer
from .spi import SPI
from .sendablebase import SendableBase
from .sendablebuilder import SendableBuilder

__all__ = ["ADXL345_SPI"]

Expand Down Expand Up @@ -48,14 +50,12 @@ class Axes:
kY = 0x02
kZ = 0x04

def __init__(self, port, range):
def __init__(self, port: SPI.Port, range: Range) -> None:
"""Constructor. Use this when the device is the first/only device on
the bus

:param port: The SPI port that the accelerometer is connected to
:type port: :class:`.SPI.Port`
:param range: The range (+ or -) that the accelerometer will measure.
:type range: :class:`.ADXL345_SPI.Range`
"""
self.spi = SPI(port)
self.spi.setClockRate(500000)
Expand All @@ -75,18 +75,17 @@ def __init__(self, port, range):

self.setName("ADXL345_SPI", port)

def close(self):
def close(self) -> None:
self.spi.close()
super().close()

# Accelerometer interface

def setRange(self, range):
def setRange(self, range: Range) -> None:
"""Set the measuring range of the accelerometer.

:param range: The maximum acceleration, positive or negative, that
the accelerometer will measure.
:type range: :class:`ADXL345_SPI.Range`
"""
if range == self.Range.k2G:
value = 0
Expand All @@ -101,28 +100,28 @@ def setRange(self, range):

self.spi.write([self.kDataFormatRegister, self.kDataFormat_FullRes | value])

def getX(self):
def getX(self) -> float:
"""Get the x axis acceleration

:returns: The acceleration along the x axis in g-forces
"""
return self.getAcceleration(self.Axes.kX)

def getY(self):
def getY(self) -> float:
"""Get the y axis acceleration

:returns: The acceleration along the y axis in g-forces
"""
return self.getAcceleration(self.Axes.kY)

def getZ(self):
def getZ(self) -> float:
"""Get the z axis acceleration

:returns: The acceleration along the z axis in g-forces
"""
return self.getAcceleration(self.Axes.kZ)

def getAcceleration(self, axis):
def getAcceleration(self, axis: Axes) -> float:
"""Get the acceleration of one axis in Gs.

:param axis: The axis to read from.
Expand All @@ -138,7 +137,7 @@ def getAcceleration(self, axis):
rawAccel = (data[2] << 8) | data[1]
return rawAccel * self.kGsPerLSB

def getAccelerations(self):
def getAccelerations(self) -> Tuple[float, float, float]:
"""Get the acceleration of all axes in Gs.

:returns: X,Y,Z tuple of acceleration measured on all axes of the
Expand All @@ -161,13 +160,13 @@ def getAccelerations(self):
)

# Live Window code, only does anything if live window is activated.
def _updateValues(self):
def _updateValues(self) -> None:
data = self.getAccelerations()
self._entryX.setDouble(data[0])
self._entryY.setDouble(data[1])
self._entryZ.setDouble(data[2])

def initSendable(self, builder):
def initSendable(self, builder: SendableBuilder) -> None:
builder.setSmartDashboardType("3AxisAccelerometer")
self._entryX = builder.getEntry("X")
self._entryY = builder.getEntry("Y")
Expand Down
25 changes: 12 additions & 13 deletions wpilib/wpilib/adxl362.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
# must be accompanied by the FIRST BSD license file in the root directory of
# the project.
# ----------------------------------------------------------------------------
from typing import Tuple, Optional

import hal

from .driverstation import DriverStation
from .interfaces import Accelerometer
from .spi import SPI
from .sendablebase import SendableBase
from .sendablebuilder import SendableBuilder

__all__ = ["ADXL362"]

Expand Down Expand Up @@ -49,13 +51,11 @@ class Axes:
kY = 0x02
kZ = 0x04

def __init__(self, range, port=None):
def __init__(self, range: Range, port: Optional[SPI.Port] = None) -> None:
"""Constructor.

:param range: The range (+ or -) that the accelerometer will measure.
:type range: :class:`.ADXL362.Range`
:param port: The SPI port that the accelerometer is connected to
:type port: :class:`.SPI.Port`
"""
if port is None:
port = SPI.Port.kOnboardCS1
Expand Down Expand Up @@ -93,20 +93,19 @@ def __init__(self, range, port=None):

self.setName("ADXL362", port)

def close(self):
def close(self) -> None:
if self.spi:
self.spi.close()
self.spi = None
super().close()

# Accelerometer interface

def setRange(self, range):
def setRange(self, range: Range) -> None:
"""Set the measuring range of the accelerometer.

:param range: The maximum acceleration, positive or negative, that
the accelerometer will measure.
:type range: :class:`ADXL362.Range`
"""
if not self.spi:
return
Expand All @@ -128,28 +127,28 @@ def setRange(self, range):
[self.kRegWrite, self.kFilterCtlRegister, self.kFilterCtl_ODR_100Hz | value]
)

def getX(self):
def getX(self) -> float:
"""Get the x axis acceleration

:returns: The acceleration along the x axis in g-forces
"""
return self.getAcceleration(self.Axes.kX)

def getY(self):
def getY(self) -> float:
"""Get the y axis acceleration

:returns: The acceleration along the y axis in g-forces
"""
return self.getAcceleration(self.Axes.kY)

def getZ(self):
def getZ(self) -> float:
"""Get the z axis acceleration

:returns: The acceleration along the z axis in g-forces
"""
return self.getAcceleration(self.Axes.kZ)

def getAcceleration(self, axis):
def getAcceleration(self, axis: Axes) -> float:
"""Get the acceleration of one axis in Gs.

:param axis: The axis to read from.
Expand All @@ -164,7 +163,7 @@ def getAcceleration(self, axis):
rawAccel = (data[2] << 8) | data[1]
return rawAccel * self.gsPerLSB

def getAccelerations(self):
def getAccelerations(self) -> Tuple[float, float, float]:
"""Get the acceleration of all axes in Gs.

:returns: X,Y,Z tuple of acceleration measured on all axes in Gs.
Expand All @@ -189,13 +188,13 @@ def getAccelerations(self):
rawData[2] * self.gsPerLSB,
)

def _updateValues(self):
def _updateValues(self) -> None:
data = self.getAccelerations()
self._entryX.setDouble(data[0])
self._entryY.setDouble(data[1])
self._entryZ.setDouble(data[2])

def initSendable(self, builder):
def initSendable(self, builder: SendableBuilder) -> None:
builder.setSmartDashboardType("3AxisAccelerometer")
self._entryX = builder.getEntry("X")
self._entryY = builder.getEntry("Y")
Expand Down
Loading
0