1+ from typing import Any , Callable , Iterable
12from heapq import heappush , heappop
23from hal import report , initializeNotifier , setNotifierName , observeUserProgramStarting , updateNotifierAlarm , \
34 waitForNotifierAlarm , stopNotifier , tResourceType , tInstances
1011_kFramework_Timed = tInstances .kFramework_Timed
1112
1213class _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
5556class _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