Skip to content

Commit

Permalink
Merge pull request #1 from nelsond/v0.1.1
Browse files Browse the repository at this point in the history
Add support for error readout
  • Loading branch information
nelsond authored Jan 5, 2018
2 parents 4b6ad35 + b362f78 commit c4d91a7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 12 deletions.
92 changes: 86 additions & 6 deletions tests/mtd415t_device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,99 @@ def test_it_queries_uid_property(mtd415t_device_with_mock_serial):


# .error_flags
def test_it_raises_exception_for_error_flags(mtd415t_device_with_mock_serial):
def test_it_returns_error_flags(mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

with raises(NotImplementedError):
mtd415t.error_flags
idx = random.randint(0, 15)
mock_serial.in_buffer.append(str(1 << idx))
expected_result = tuple(True if i == idx else False for i in range(16))

assert mtd415t.error_flags == expected_result


def test_it_queries_error_flags(mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 2))
mtd415t.error_flags

assert mock_serial.out_buffer.pop() == b'E?\n'


# .errors
def test_it_raises_exception_for_errors(mtd415t_device_with_mock_serial):
def test_it_returns_not_enabled_error(mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 0))

assert mtd415t.errors == ('not enabled',)


def test_it_returns_internal_temperature_too_high_error(
mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 1))

assert mtd415t.errors == ('internal temperature too high',)


def test_it_returns_thermal_latch_up_error(mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 2))

assert mtd415t.errors == ('thermal latch-up',)


def test_it_returns_cycling_time_too_small_error(
mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 3))

assert mtd415t.errors == ('cycling time too small',)


def test_it_returns_no_sensor_error(mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

with raises(NotImplementedError):
mtd415t.errors
mock_serial.in_buffer.append(str(1 << 4))

assert mtd415t.errors == ('no sensor',)


def test_it_returns_no_tec_error(mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 5))

assert mtd415t.errors == ('no tec',)


def test_it_returns_tec_polarity_reversed_error(
mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 6))

assert mtd415t.errors == ('tec polarity reversed',)


def test_it_returns_value_out_of_range_error(mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 13))

assert mtd415t.errors == ('value out of range',)


def test_it_returns__error(mtd415t_device_with_mock_serial):
mtd415t, mock_serial = mtd415t_device_with_mock_serial

mock_serial.in_buffer.append(str(1 << 14))

assert mtd415t.errors == ('invalid command',)


# .tec_current_limit
Expand Down
21 changes: 15 additions & 6 deletions thorlabs_mtd415t/mtd415t_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MTD415TDevice(SerialDevice):
"""

# error bits, see MTD415T datasheet, p. 18
ERRORS = {
_ERRORS = {
0: 'not enabled',
1: 'internal temperature too high',
2: 'thermal latch-up',
Expand Down Expand Up @@ -132,14 +132,23 @@ def uid(self):

@property
def error_flags(self):
"""Error flags from the error register of the device"""
raise NotImplementedError(
'Error flags are not implemented yet.')
"""Error flags from the error register of the device (tuple, LSB
first)"""
err = int(self.query('E').decode('ascii'))
return tuple(c == '1' for c in reversed('{:016b}'.format(err)))

@property
def errors(self):
"""Errors from the error register of the device tuple"""
raise NotImplementedError('Errors are not implemented yet.')
"""Errors from the error register of the device (tuple)"""
flags = self.error_flags
errors = []
for idx, err in self._ERRORS.items():
if flags[idx] is False:
continue

errors.append(err)

return tuple(errors)

@property
def tec_current_limit(self):
Expand Down

0 comments on commit c4d91a7

Please sign in to comment.