|
| 1 | +""" |
| 2 | +VEML7700 luminosity sensor |
| 3 | +""" |
| 4 | +from typing import cast |
| 5 | + |
| 6 | +from ...types import CerberusSchemaType, ConfigType, SensorValueType |
| 7 | +from . import GenericSensor |
| 8 | + |
| 9 | +REQUIREMENTS = ("adafruit-circuitpython-veml7700",) |
| 10 | +CONFIG_SCHEMA: CerberusSchemaType = { |
| 11 | + "chip_addr": { |
| 12 | + "type": 'integer', |
| 13 | + "required": False, |
| 14 | + "empty": False, |
| 15 | + "default": '0x10'}, |
| 16 | + "integration_time": { |
| 17 | + "required": False, |
| 18 | + "empty": False, |
| 19 | + "allowed": [25, 50, 100, 200, 400, 800], |
| 20 | + "default": 25, |
| 21 | + }, |
| 22 | + "gain": { |
| 23 | + "required": False, |
| 24 | + "empty": False, |
| 25 | + "allowed": [0.125, 0.25, 1, 2], |
| 26 | + "default": 0.125, |
| 27 | + }, |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +class Sensor(GenericSensor): |
| 32 | + """ |
| 33 | + Implementation of Sensor class for the Adafruit_VEML7700 |
| 34 | + """ |
| 35 | + |
| 36 | + SENSOR_SCHEMA: CerberusSchemaType = { |
| 37 | + "type": { |
| 38 | + "type": 'string', |
| 39 | + "required": False, |
| 40 | + "empty": False, |
| 41 | + "allowed": ['light', 'lux', 'lux_corrected'], |
| 42 | + "default": 'lux_corrected', |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + def setup_module(self) -> None: |
| 47 | + # pylint: disable=import-outside-toplevel,attribute-defined-outside-init |
| 48 | + # pylint: disable=import-error,no-member |
| 49 | + import board # type: ignore |
| 50 | + import busio # type: ignore |
| 51 | + import adafruit_veml7700 # type: ignore |
| 52 | + # Create the I2C bus |
| 53 | + self.i2c = busio.I2C(board.SCL, board.SDA) |
| 54 | + |
| 55 | + # Convert sensor address from hex to dec |
| 56 | + self.address = int(0x10) |
| 57 | + if self.config["chip_addr"]: |
| 58 | + self.address = int(self.config["chip_addr"]) |
| 59 | + |
| 60 | + self.veml7700 = adafruit_veml7700.VEML7700(self.i2c, self.address) |
| 61 | + |
| 62 | + # Set gain |
| 63 | + gains = { |
| 64 | + "0.125": 'ALS_GAIN_1_8', |
| 65 | + "0.25": 'ALS_GAIN_1_4', |
| 66 | + "1": 'ALS_GAIN_1', |
| 67 | + "2": 'ALS_GAIN_2', |
| 68 | + } |
| 69 | + if 'gain' in self.config: |
| 70 | + self.veml7700.light_gain = getattr(self.veml7700, gains[str(self.config['gain'])]) |
| 71 | + |
| 72 | + # Set integration time |
| 73 | + ints = { |
| 74 | + "25": 'ALS_25MS', |
| 75 | + "50": 'ALS_50MS', |
| 76 | + "100": 'ALS_100MS', |
| 77 | + "200": 'ALS_200MS', |
| 78 | + "400": 'ALS_400MS', |
| 79 | + "800": 'ALS_800MS', |
| 80 | + } |
| 81 | + if 'integration_time' in self.config: |
| 82 | + self.veml7700.light_integration_time = getattr(self.veml7700, |
| 83 | + ints[str(self.config['integration_time'])]) |
| 84 | + |
| 85 | + #print("veml7700 Gain = {}".format(self.veml7700.light_gain)) |
| 86 | + #print("veml7700 Integration time = {}".format(self.veml7700.light_integration_time)) |
| 87 | + |
| 88 | + def get_value(self, sens_conf: ConfigType) -> SensorValueType: |
| 89 | + # pylint: disable=import-outside-toplevel,attribute-defined-outside-init |
| 90 | + # pylint: disable=import-error,no-member |
| 91 | + sens_type = sens_conf["type"] |
| 92 | + data = { |
| 93 | + "light": self.veml7700.light, |
| 94 | + "lux": self.veml7700.lux, |
| 95 | + "lux_corrected": "-1", |
| 96 | + } |
| 97 | + |
| 98 | + if data['lux'] > 1000: |
| 99 | + data['lux_corrected'] = (6.0135e-13 * data['lux'] ** 4) + \ |
| 100 | + (-9.3924e-9 * data['lux'] ** 3) + \ |
| 101 | + (8.1488e-5 * data['lux'] ** 2) + \ |
| 102 | + (1.0023 * data['lux']) |
| 103 | + else: |
| 104 | + data['lux_corrected'] = data['lux'] |
| 105 | + |
| 106 | + return cast( |
| 107 | + float, |
| 108 | + data[sens_type], |
| 109 | + ) |
0 commit comments