# Path: controller/util.py
# def mapping(x, in_min, in_max, out_min, out_max):
#     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# 
# def constrain(x, out_min, out_max):
#     return min(max(x, out_min), out_max)


import time
import logging
import re
import serial
import io
import subprocess
from abc import abstractmethod, ABC
from .util import mapping, constrain
        from lcdbackpack import LcdBackpack
                time.sleep(0.1)
        except OSError:
            logger.exception("Unable to read temperature (%s)" % self.name)
        return None


class TankSensorDevice(SensorDevice):

    def __init__(self, name, adc, channel, gain, low, high):
        super().__init__(name)
        self.__adc = adc
        self.__adc.gain = gain
        self.__channel = channel
        self.__low = low
        self.__high = high

    @property
    def value(self):
        values = []
        for _ in range(10):
            try:
                values.append(self.__adc.read(self.__channel))
                time.sleep(0.05)
            except OSError:
                logger.exception("Unable to read ADC %s" % self.name)
                time.sleep(0.5)
        # In case we got really no readings, we return 0 in order for the system to go into
        # emergency stop.
        value = sum(values) / len(values) if values else 0
        logger.debug("Tank sensor average ADC=%.2f" % value)

# Path: controller/util.py
# def mapping(x, in_min, in_max, out_min, out_max):
#     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# 
# def constrain(x, out_min, out_max):
#     return min(max(x, out_min), out_max)


# Path: controller/device.py
import time
import logging
import re
import serial
import io
import subprocess
from abc import abstractmethod, ABC
from .util import mapping, constrain
        from lcdbackpack import LcdBackpack

                time.sleep(0.1)
        except OSError:
            logger.exception("Unable to read temperature (%s)" % self.name)
        return None


class TankSensorDevice(SensorDevice):

    def __init__(self, name, adc, channel, gain, low, high):
        super().__init__(name)
        self.__adc = adc
        self.__adc.gain = gain
        self.__channel = channel
        self.__low = low
        self.__high = high

    @property
    def value(self):
        values = []
        for _ in range(10):
            try:
                values.append(self.__adc.read(self.__channel))
                time.sleep(0.05)
            except OSError:
                logger.exception("Unable to read ADC %s" % self.name)
                time.sleep(0.5)
        # In case we got really no readings, we return 0 in order for the system to go into
        # emergency stop.
        value = sum(values) / len(values) if values else 0
        logger.debug("Tank sensor average ADC=%.2f" % value)

        return constrain(mapping(value, self.__low, self.__high, 0, 100), 0, 100)