Skip to content

Commit

Permalink
Add SLM set_phases_8bit
Browse files Browse the repository at this point in the history
  • Loading branch information
dedean16 committed Nov 8, 2024
1 parent 1b136f3 commit 3da01c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions openwfs/devices/slm/slm.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,13 @@ def lookup_table(self, value: Sequence[int]):
def set_phases(self, values: ArrayLike, update=True):
self.primary_patch.set_phases(values, update)

def set_phases_8bit(self, values: ArrayLike, **kwargs):
"""
Pass 8bit unsigned integers [0, 255] as linearly mapped to phase, such that 0 -> 0, 256 -> 2π. Any other
keyword arguments will be passed to set_phases.
"""
self.set_phases(values * 2 * np.pi / 256, **kwargs)

@property
def pixels(self) -> Detector:
"""Returns a 'camera' to monitor the current value of the pixels displayed on the SLM."""
Expand Down
21 changes: 21 additions & 0 deletions tests/test_slm.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,24 @@ def test_circular_geometry(slm):
np.repeat(np.flip(np.arange(30, 70)), 1).reshape((-1, 1)),
atol=1,
)


def test_slm_lut(slm):
"""
Test the lookup table
"""
# === Test default lookup table ===
# Includes edge cases like rounding/wrapping: -0.501 -> 255, -0.499 -> 0
input_phase_a_256 = np.zeros(slm.shape)
input_phase_a_256[0:13, 0] = np.asarray([-1, -0.501, -0.499, 0, 1, 64, 255, 255.499, 255.501, 256, 257, 511, 512])
input_phases_a = input_phase_a_256 * 2 * np.pi / 256

expected_output_phases_a = np.zeros(slm.shape)
expected_output_phases_a_256 = np.asarray([255, 255, 0, 0, 1, 64, 255, 255, 0, 0, 1, 255, 0])
expected_output_phases_a[0:13, 0] = expected_output_phases_a_256 * 2 * np.pi / 256

slm.set_phases(input_phases_a)
assert np.all(np.abs(slm.pixels.read() - expected_output_phases_a_256) < 1e-6)

slm.set_phases_8bit(input_phase_a_256)
assert np.all(np.abs(slm.pixels.read() - expected_output_phases_a_256) < 1e-6)

0 comments on commit 3da01c9

Please sign in to comment.