Skip to content

Commit 574f32e

Browse files
penguinlandnjooma
authored andcommitted
[RSDK-5810] remove impossible-to-implement functions from digital interrupts (#503)
Within digital interrupts, the `value()` method will get the number of interrupts that have occurred. It's a good thing to have; we'll keep it. However, - `add_callback()` cannot be implemented without changing the GPRC interface, which will require a scope doc, which will require planning how to do this right. Get rid of it for now: digital interrupts aren't really part of the API yet. - `add_post_processor()` doesn't exist outside of raspberry pi boards; get rid of it. - `tick()` is only used in tests (to simulate the interrupt firing). Keep it in `MockDigitalInterrupt`, but get rid of it in the "real" code. So, how do you use digital interrupts outside of the RDK server? Unfortunately, you kinda can't, and have never been able to. You can repeatedly check its value and do something if you notice the value has changed, but a proper GRPC interface for digital interrupts doesn't currently exist. We shouldn't pretend that it does. It ought to come sometime in 2024.
1 parent a87e436 commit 574f32e

File tree

5 files changed

+8
-89
lines changed

5 files changed

+8
-89
lines changed

examples/server/v1/components.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import AsyncIterator
1111

1212
from datetime import timedelta
13-
from multiprocessing import Lock, Queue
13+
from multiprocessing import Lock
1414
from pathlib import Path
1515
from typing import Any, Dict, List, Mapping, Optional, Tuple
1616

@@ -20,7 +20,6 @@
2020
from viam.components.audio_input import AudioInput
2121
from viam.components.base import Base
2222
from viam.components.board import Board
23-
from viam.components.board.board import PostProcessor
2423
from viam.components.camera import Camera
2524
from viam.components.encoder import Encoder
2625
from viam.components.gantry import Gantry
@@ -241,27 +240,12 @@ async def read(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -> int:
241240

242241
class ExampleDigitalInterrupt(Board.DigitalInterrupt):
243242
def __init__(self, name: str):
244-
self.high = False
245-
self.last_tick = 0
246243
self.num_ticks = 0
247-
self.callbacks: List[Queue] = []
248-
self.post_processors: List[PostProcessor] = []
249244
super().__init__(name)
250245

251246
async def value(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -> int:
252247
return self.num_ticks
253248

254-
async def tick(self, high: bool, nanos: int):
255-
self.high = high
256-
self.last_tick = nanos
257-
self.num_ticks += 1
258-
259-
async def add_callback(self, queue: Queue):
260-
self.callbacks.append(queue)
261-
262-
async def add_post_processor(self, processor: PostProcessor):
263-
self.post_processors.append(processor)
264-
265249

266250
class ExampleGPIOPin(Board.GPIOPin):
267251
def __init__(self, name: str):

src/viam/components/board/board.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import abc
22
from datetime import timedelta
3-
from multiprocessing import Queue
4-
from typing import Any, Callable, Dict, Final, List, Optional
3+
from typing import Any, Dict, Final, List, Optional
54

65
from viam.proto.common import BoardStatus
76
from viam.proto.component.board import PowerMode
87
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
98

109
from ..component_base import ComponentBase
1110

12-
PostProcessor = Callable[[int], int]
13-
1411

1512
class Board(ComponentBase):
1613
"""
@@ -57,41 +54,6 @@ async def value(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Option
5754
"""
5855
...
5956

60-
@abc.abstractmethod
61-
async def tick(self, high: bool, nanos: int):
62-
"""
63-
This method is to be called either manually if the interrupt
64-
is a proxy to some real hardware interrupt or for tests.
65-
66-
Args:
67-
high (bool): If the signal of the interrupt is high.
68-
nanos (int): Nanoseconds from an arbitrary point in time,
69-
but always increasing and always needs to be accurate.
70-
Using ``time.time_ns()`` would be acceptable.
71-
"""
72-
...
73-
74-
@abc.abstractmethod
75-
async def add_callback(self, queue: Queue):
76-
"""
77-
Add a callback to be sent the low/high value on ``tick()``.
78-
79-
Args:
80-
queue (Queue): The receiving queue.
81-
"""
82-
...
83-
84-
@abc.abstractmethod
85-
async def add_post_processor(self, processor: PostProcessor):
86-
"""
87-
Add a post processor that should be used to modify what
88-
is returned by ``self.value()``
89-
90-
Args:
91-
processor (PostProcessor): The post processor to add.
92-
"""
93-
...
94-
9557
class GPIOPin(ComponentBase):
9658
"""
9759
Abstract representation of an individual GPIO pin on a board

src/viam/components/board/client.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import timedelta
2-
from multiprocessing import Queue
32
from typing import Any, Dict, List, Mapping, Optional
43

54
from google.protobuf.duration_pb2 import Duration
@@ -31,7 +30,6 @@
3130
from viam.utils import ValueTypes, dict_to_struct, get_geometries, struct_to_dict
3231

3332
from . import Board
34-
from .board import PostProcessor
3533

3634

3735
class AnalogReaderClient(Board.AnalogReader):
@@ -59,15 +57,6 @@ async def value(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Option
5957
response: GetDigitalInterruptValueResponse = await self.board.client.GetDigitalInterruptValue(request, timeout=timeout)
6058
return response.value
6159

62-
async def tick(self, high: bool, nanos: int):
63-
raise NotImplementedError()
64-
65-
async def add_callback(self, queue: Queue):
66-
raise NotImplementedError()
67-
68-
async def add_post_processor(self, processor: PostProcessor):
69-
raise NotImplementedError()
70-
7160

7261
class GPIOPinClient(Board.GPIOPin):
7362
def __init__(self, name: str, board: "BoardClient"):

tests/mocks/components.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from dataclasses import dataclass
99
from datetime import datetime, timedelta
10-
from multiprocessing import Queue
1110
from secrets import choice
1211
from typing import Any, Dict, List, Mapping, Optional, Tuple, Union
1312

@@ -18,7 +17,6 @@
1817
from viam.components.audio_input import AudioInput
1918
from viam.components.base import Base
2019
from viam.components.board import Board
21-
from viam.components.board.board import PostProcessor
2220
from viam.components.camera import Camera, DistortionParameters, IntrinsicParameters
2321
from viam.components.encoder import Encoder
2422
from viam.components.gantry import Gantry
@@ -262,27 +260,16 @@ def __init__(self, name: str):
262260
self.high = False
263261
self.last_tick = 0
264262
self.num_ticks = 0
265-
self.callbacks: List[Queue] = []
266-
self.post_processors: List[PostProcessor] = []
267-
self.timeout: Optional[float] = None
268263
super().__init__(name)
269264

270265
async def value(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> int:
271-
self.extra = extra
272-
self.timeout = timeout
273266
return self.num_ticks
274267

275-
async def tick(self, high: bool, nanos: int):
268+
async def tick(self, high: bool, nanos: int): # Call this to get the mock interrupt to change
276269
self.high = high
277270
self.last_tick = nanos
278271
self.num_ticks += 1
279272

280-
async def add_callback(self, queue: Queue):
281-
self.callbacks.append(queue)
282-
283-
async def add_post_processor(self, processor: PostProcessor):
284-
self.post_processors.append(processor)
285-
286273

287274
class MockGPIOPin(Board.GPIOPin):
288275
def __init__(self, name: str):

tests/test_board.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,18 @@ async def test_get_digital_interrupt_value(self, board: MockBoard, service: Boar
175175
async with ChannelFor([service]) as channel:
176176
client = BoardServiceStub(channel)
177177

178+
request = GetDigitalInterruptValueRequest(
179+
board_name=board.name, digital_interrupt_name="dne"
180+
)
178181
with pytest.raises(GRPCError, match=r".*Status.NOT_FOUND.*"):
179-
request = GetDigitalInterruptValueRequest(board_name=board.name, digital_interrupt_name="dne")
180182
await client.GetDigitalInterruptValue(request)
181183

182-
extra = {"foo": "bar", "baz": [1, 2, 3]}
183184
request = GetDigitalInterruptValueRequest(
184-
board_name=board.name, digital_interrupt_name="interrupt1", extra=dict_to_struct(extra)
185+
board_name=board.name, digital_interrupt_name="interrupt1"
185186
)
186-
response: GetDigitalInterruptValueResponse = await client.GetDigitalInterruptValue(request, timeout=18.2)
187+
response: GetDigitalInterruptValueResponse = await client.GetDigitalInterruptValue(request)
187188
assert response.value == 0
188189

189-
interrupt = cast(MockDigitalInterrupt, board.digital_interrupts["interrupt1"])
190-
assert interrupt.extra == extra
191-
assert interrupt.timeout == loose_approx(18.2)
192-
193190
@pytest.mark.asyncio
194191
async def test_set_gpio(self, board: MockBoard, service: BoardRPCService):
195192
async with ChannelFor([service]) as channel:

0 commit comments

Comments
 (0)