Skip to content

Commit 7522ec2

Browse files
committed
Cleaning up code.
Signed-off-by: Mike Stitt <[email protected]>
1 parent 958c909 commit 7522ec2

File tree

2 files changed

+50
-64
lines changed

2 files changed

+50
-64
lines changed

subprojects/robotpy-wpilib/wpilib/iterativerobotpy.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,74 +37,74 @@ def __init__(self, period: float):
3737
self._teleopPeriodicHasRun: bool = False
3838
self._testPeriodicHasRun: bool = False
3939

40-
def robotInit(self):
40+
def robotInit(self) -> None:
4141
pass
4242

43-
def driverStationConnected(self):
43+
def driverStationConnected(self) -> None:
4444
pass
4545

46-
def simulationInit(self):
46+
def simulationInit(self) -> None:
4747
pass
4848

49-
def disabledInit(self):
49+
def disabledInit(self) -> None:
5050
pass
5151

52-
def autonomousInit(self):
52+
def autonomousInit(self) -> None:
5353
pass
5454

55-
def teleopInit(self):
55+
def teleopInit(self) -> None:
5656
pass
5757

58-
def testInit(self):
58+
def testInit(self) -> None:
5959
pass
6060

61-
def robotPeriodic(self):
61+
def robotPeriodic(self) -> None:
6262
if not self._robotPeriodicHasRun:
6363
print(f"Default RobotPeriodic() method...Override me!")
6464
self._robotPeriodicHasRun = True
6565

66-
def simulationPeriodic(self):
66+
def simulationPeriodic(self) -> None:
6767
if not self._simulationPeriodicHasRun:
6868
print(f"Default simulationPeriodic() method...Override me!")
6969
self._simulationPeriodicHasRun = True
7070

71-
def disabledPeriodic(self):
71+
def disabledPeriodic(self) -> None:
7272
if not self._disabledPeriodicHasRun:
7373
print(f"Default disabledPeriodic() method...Override me!")
7474
self._disabledPeriodicHasRun = True
7575

76-
def autonomousPeriodic(self):
76+
def autonomousPeriodic(self) -> None:
7777
if not self._autonomousPeriodicHasRun:
7878
print(f"Default autonomousPeriodic() method...Override me!")
7979
self._autonomousPeriodicHasRun = True
8080

81-
def teleopPeriodic(self):
81+
def teleopPeriodic(self)->None:
8282
if not self._teleopPeriodicHasRun:
8383
print(f"Default teleopPeriodic() method...Override me!")
8484
self._teleopPeriodicHasRun = True
8585

86-
def testPeriodic(self):
86+
def testPeriodic(self)->None:
8787
if not self._testPeriodicHasRun:
8888
print(f"Default testPeriodic() method...Override me!")
8989
self._teleopPeriodicHasRun = True
9090

91-
def disabledExit(self):
91+
def disabledExit(self)->None:
9292
pass
9393

94-
def autonomousExit(self):
94+
def autonomousExit(self)->None:
9595
pass
9696

97-
def teleopExit(self):
97+
def teleopExit(self)->None:
9898
pass
9999

100-
def testExit(self):
100+
def testExit(self)->None:
101101
pass
102102

103103
# todo @Deprecated(forRemoval=true, since="2025")
104-
def setNetworkTablesFlushEnabled(self, enabled: bool):
104+
def setNetworkTablesFlushEnabled(self, enabled: bool)->None:
105105
self._ntFlushEnabled = enabled
106106

107-
def enableLiveWindowInTest(self, testLW: bool):
107+
def enableLiveWindowInTest(self, testLW: bool)->None:
108108
if self.isTestEnabled():
109109
raise RuntimeError("Can't configure test mode while in test mode!")
110110
if not self._reportedLw and testLW:
@@ -118,7 +118,7 @@ def isLiveWindowEnabledInTest(self) -> bool:
118118
def getPeriod(self) -> float:
119119
return self._periodS
120120

121-
def loopFunc(self):
121+
def loopFunc(self)->None:
122122
DriverStation.refreshData()
123123
self.watchdog.reset()
124124

@@ -248,10 +248,10 @@ def loopFunc(self):
248248
if self.watchdog.isExpired():
249249
self.printWatchdogEpochs()
250250

251-
def printLoopOverrunMessage(self):
251+
def printLoopOverrunMessage(self)->None:
252252
reportWarning(
253253
f"Loop time of {self.watchdog.getTimeout()}s overrun", False
254254
)
255255

256-
def printWatchdogEpochs(self):
256+
def printWatchdogEpochs(self)->None:
257257
self.watchdog.printEpochs()

subprojects/robotpy-wpilib/wpilib/timedrobotpy.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Any, Callable, Iterable
12
from heapq import heappush, heappop
23
from hal import report, initializeNotifier, setNotifierName, observeUserProgramStarting, updateNotifierAlarm, \
34
waitForNotifierAlarm, stopNotifier, tResourceType, tInstances
@@ -10,20 +11,20 @@
1011
_kFramework_Timed = tInstances.kFramework_Timed
1112

1213
class _Callback:
13-
def __init__(self, func, periodUs: int, expirationUs: int):
14+
def __init__(self, func: Callable[[],None], periodUs: int, expirationUs: int):
1415
self.func = func
1516
self._periodUs = periodUs
1617
self.expirationUs = expirationUs
1718

1819
@classmethod
1920
def makeCallBack(cls,
20-
func,
21+
func: Callable[[],None],
2122
startTimeUs: int,
2223
periodUs: int,
23-
offsetUs: int):
24+
offsetUs: int) -> "_Callback":
2425

2526
callback = _Callback(
26-
func,
27+
func=func,
2728
periodUs=periodUs,
2829
expirationUs=startTimeUs
2930
)
@@ -42,42 +43,42 @@ def calcFutureExpirationUs(self, currentTimeUs: int) -> int:
4243
return self.expirationUs + self._periodUs + \
4344
((currentTimeUs - self.expirationUs) // self._periodUs) * self._periodUs
4445

45-
def setNextStartTimeUs(self, currentTimeUs: int):
46+
def setNextStartTimeUs(self, currentTimeUs: int) -> None:
4647
self.expirationUs = self.calcFutureExpirationUs(currentTimeUs)
4748

48-
def __lt__(self, other):
49+
def __lt__(self, other) -> bool:
4950
return self.expirationUs < other.expirationUs
5051

51-
def __bool__(self):
52+
def __bool__(self) -> bool:
5253
return True
5354

5455

5556
class _OrderedList:
5657
def __init__(self):
57-
self._data = []
58+
self._data: list[Any] = []
5859

59-
def add(self, item):
60+
def add(self, item: Any) -> None:
6061
heappush(self._data, item)
6162

62-
def pop(self):
63+
def pop(self) -> Any:
6364
return heappop(self._data)
6465

65-
def peek(self):
66+
def peek(self) -> Any|None:
6667
if self._data:
6768
return self._data[0]
6869
else:
6970
return None
7071

71-
def __len__(self):
72+
def __len__(self) -> int:
7273
return len(self._data)
7374

74-
def __iter__(self):
75+
def __iter__(self) -> Iterable[Any]:
7576
return iter(sorted(self._data))
7677

77-
def __contains__(self, item):
78+
def __contains__(self, item) -> bool:
7879
return item in self._data
7980

80-
def __str__(self):
81+
def __str__(self) -> str:
8182
return str(sorted(self._data))
8283

8384

@@ -93,13 +94,11 @@ def __init__(self, period: float = 0.020):
9394

9495
self._notifier, status = initializeNotifier()
9596
if status != 0:
96-
message = f"initializeNotifier() returned {status} {self._notifier}"
97-
#raise RuntimeError(message) # todo
97+
raise RuntimeError(f"initializeNotifier() returned {self._notifier}, {status}")
9898

9999
status = setNotifierName(self._notifier, "TimedRobot")
100100
if status != 0:
101-
message = f"setNotifierName() returned {status}"
102-
#raise RuntimeError(message) # todo
101+
raise RuntimeError(f"setNotifierName() returned {status}")
103102

104103
report(_kResourceType_Framework, _kFramework_Timed)
105104

@@ -123,13 +122,11 @@ def startCompetition(self) -> None:
123122

124123
status = updateNotifierAlarm(self._notifier, callback.expirationUs)
125124
if status != 0:
126-
message = f"updateNotifierAlarm() returned {status}"
127-
#raise RuntimeError(message) # todo
125+
raise RuntimeError(f"updateNotifierAlarm() returned {status}")
128126

129127
currentTimeUs, status = waitForNotifierAlarm(self._notifier)
130128
if status != 0:
131-
message = f"waitForNotifierAlarm() returned currentTimeUs={currentTimeUs} status={status}"
132-
#raise RuntimeError(message) # todo
129+
raise RuntimeError(f"waitForNotifierAlarm() returned currentTimeUs={currentTimeUs} status={status}")
133130

134131
if currentTimeUs == 0:
135132
# when HAL_StopNotifier(self.notifier) is called the above waitForNotifierAlarm
@@ -145,33 +142,22 @@ def startCompetition(self) -> None:
145142
callback = self._callbacks.pop()
146143
self._runCallbackAndReschedule(callback, currentTimeUs)
147144

148-
def _runCallbackAndReschedule(self, callback, currentTimeUs:int):
145+
def _runCallbackAndReschedule(self, callback: Callable[[],None], currentTimeUs: int) -> None:
149146
callback.func()
150147
callback.setNextStartTimeUs(currentTimeUs)
151148
self._callbacks.add(callback)
152149

153-
def endCompetition(self):
150+
def endCompetition(self) -> None:
154151
stopNotifier(self._notifier)
155152

156-
"""
157-
todo this doesn't really translate to python (is it really needed?):
158-
159-
TimedRobot::~TimedRobot() {
160-
if (m_notifier != HAL_kInvalidHandle) {
161-
int32_t status = 0;
162-
HAL_StopNotifier(m_notifier, &status);
163-
FRC_ReportError(status, "StopNotifier");
164-
}
165-
}
166-
"""
167-
168-
def getLoopStartTime(self):
169-
return self.loopStartTimeUs/1e6 # todo units are seconds
153+
def getLoopStartTime(self) -> float:
154+
return self.loopStartTimeUs/1e6 # units are seconds
170155

171156
def addPeriodic(self,
172-
callback, # todo typehint
173-
period: float, # todo units seconds
174-
offset: float = 0.0): # todo units seconds
157+
callback: Callable[[],None],
158+
period: float, # units are seconds
159+
offset: float = 0.0 # units are seconds
160+
) -> None:
175161
self._callbacks.add(
176162
_Callback.makeCallBack(
177163
callback,

0 commit comments

Comments
 (0)