Skip to content

Commit

Permalink
Change backlight to ophyd_async (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram authored Jun 10, 2024
1 parent 5166e0d commit 06ddca6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 35 deletions.
44 changes: 30 additions & 14 deletions src/dodal/devices/backlight.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
from ophyd import Component, Device, EpicsSignal, StatusBase
from enum import Enum

from ophyd_async.core import AsyncStatus, StandardReadable
from ophyd_async.epics.signal import epics_signal_rw


class BacklightPower(str, Enum):
ON = "On"
OFF = "Off"

class Backlight(Device):
"""Simple device to trigger the pneumatic in/out."""

OUT = 0
IN = 1
class BacklightPosition(str, Enum):
IN = "In"
OUT = "Out"


class Backlight(StandardReadable):
"""Simple device to trigger the pneumatic in/out."""

pos = Component(EpicsSignal, "-EA-BL-01:CTRL")
# Toggle to switch it On or Off
toggle = Component(EpicsSignal, "-EA-BLIT-01:TOGGLE")
def __init__(self, prefix: str, name: str = "") -> None:
with self.add_children_as_readables():
self.power = epics_signal_rw(BacklightPower, prefix + "-EA-BLIT-01:TOGGLE")
self.position = epics_signal_rw(
BacklightPosition, prefix + "-EA-BL-01:CTRL"
)
super().__init__(name)

def set(self, position: int) -> StatusBase:
status = self.pos.set(position)
if position == self.OUT:
status &= self.toggle.set("Off")
@AsyncStatus.wrap
async def set(self, position: BacklightPosition):
"""This setter will turn the backlight on when we move it in to the beam and off
when we move it out."""
await self.position.set(position)
if position == BacklightPosition.OUT:
await self.power.set(BacklightPower.OFF)
else:
status &= self.toggle.set("On")
return status
await self.power.set(BacklightPower.ON)
65 changes: 44 additions & 21 deletions tests/devices/unit_tests/test_backlight.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
from unittest.mock import ANY

import pytest
from bluesky import plan_stubs as bps
from bluesky.run_engine import RunEngine
from ophyd.sim import make_fake_device
from ophyd_async.core import DeviceCollector, assert_reading, set_mock_value

from dodal.devices.backlight import Backlight
from dodal.devices.backlight import Backlight, BacklightPosition, BacklightPower


@pytest.fixture
def fake_backlight() -> Backlight:
FakeBacklight = make_fake_device(Backlight)
fake_backlight: Backlight = FakeBacklight(name="backlight")
return fake_backlight


def test_backlight_can_be_written_and_read_from(fake_backlight: Backlight):
fake_backlight.pos.sim_put(fake_backlight.IN) # type: ignore
assert fake_backlight.pos.get() == fake_backlight.IN


def test_when_backlight_out_it_switches_off(fake_backlight: Backlight, RE: RunEngine):
RE(bps.mv(fake_backlight, fake_backlight.OUT))
assert fake_backlight.toggle.get() == "Off"


def test_when_backlight_in_it_switches_on(fake_backlight: Backlight, RE: RunEngine):
RE(bps.mv(fake_backlight, fake_backlight.IN))
assert fake_backlight.toggle.get() == "On"
def fake_backlight(RE: RunEngine):
with DeviceCollector(mock=True):
backlight = Backlight("", "backlight")
return backlight


async def test_backlight_can_be_written_and_read_from(fake_backlight: Backlight):
set_mock_value(fake_backlight.position, BacklightPosition.IN)
set_mock_value(fake_backlight.power, BacklightPower.ON)
await assert_reading(
fake_backlight,
{
"backlight-power": {
"value": BacklightPower.ON,
"alarm_severity": 0,
"timestamp": ANY,
},
"backlight-position": {
"value": BacklightPosition.IN,
"alarm_severity": 0,
"timestamp": ANY,
},
},
)


async def test_when_backlight_moved_out_it_switches_off(
fake_backlight: Backlight, RE: RunEngine
):
RE(bps.mv(fake_backlight, BacklightPosition.OUT))
assert await fake_backlight.position.get_value() == BacklightPosition.OUT
assert await fake_backlight.power.get_value() == BacklightPower.OFF


async def test_when_backlight_moved_in_it_switches_on(
fake_backlight: Backlight, RE: RunEngine
):
RE(bps.mv(fake_backlight, BacklightPosition.IN))
assert await fake_backlight.position.get_value() == BacklightPosition.IN
assert await fake_backlight.power.get_value() == BacklightPower.ON

0 comments on commit 06ddca6

Please sign in to comment.