# -*- coding: utf-8 -*-
# (c) Copyright 2019 Sensirion AG, Switzerland
# from __future__ import absolute_import, division, print_function
import sys
if sys.implementation.name.lower() == "circuitpython":
import adafruit_logging as logging
else:
import logging
[docs]class IOError(OSError):
pass
log = logging.getLogger(__name__)
[docs]class I2cError(IOError):
"""
I2C error base exception.
"""
[docs] def __init__(self, received_data=None, message="I2C error."):
super(I2cError, self).__init__(message)
self.received_data = received_data
self.error_message = message
[docs]class I2cChecksumError(I2cError):
"""
I2C checksum error.
"""
[docs] def __init__(self, received_checksum, expected_checksum, received_data):
super(I2cChecksumError, self).__init__(
received_data,
"I2C error: Received wrong checksum 0x{:02X} (expected 0x{:02X})."
.format(received_checksum, expected_checksum)
)
self.received_checksum = received_checksum
self.expected_checksum = expected_checksum
[docs]class I2cTransceiveError(I2cError):
"""
I2C transceive error.
"""
[docs] def __init__(self, transceiver_error, received_data,
message="Unknown error."):
super(I2cTransceiveError, self).__init__(
received_data,
"I2C transceive failed: {}".format(message)
)
self.transceiver_error = transceiver_error
[docs]class I2cChannelDisabledError(I2cTransceiveError):
"""
I2C channel disabled error.
"""
[docs] def __init__(self, transceiver_error, received_data):
super(I2cChannelDisabledError, self).__init__(
transceiver_error,
received_data,
"Channel is disabled ({}).".format(str(transceiver_error))
)
[docs]class I2cNackError(I2cTransceiveError):
"""
I2C transceive NACK error.
"""
[docs] def __init__(self, transceiver_error, received_data):
super(I2cNackError, self).__init__(
transceiver_error,
received_data,
"NACK (byte not acknowledged)."
)
[docs]class I2cTimeoutError(I2cTransceiveError):
"""
I2C transceive timeout error.
"""
[docs] def __init__(self, transceiver_error, received_data):
super(I2cTimeoutError, self).__init__(
transceiver_error,
received_data,
"Timeout."
)