from compLib.LogstashLogging import Logging from compLib.MetricsLogging import MetricsLogging from compLib.Spi import Spi, Register SENSOR_COUNT = 5 class IRSensor(object): """Access the different IR Sensors of the robot """ @staticmethod def read(sensor: int) -> int: """Read one infrared sensor :param sensor: Which sensor to read. Between 1 and 5 :raises: IndexError :return: Sensor value. 10 bit accuracy :rtype: int """ if sensor <= 0 or sensor > SENSOR_COUNT: raise IndexError("Invalid sensor specified!") result = 0 if sensor == 1: result = Spi.read(Register.IR_1_H, 2) elif sensor == 2: result = Spi.read(Register.IR_2_H, 2) elif sensor == 3: result = Spi.read(Register.IR_3_H, 2) elif sensor == 4: result = Spi.read(Register.IR_4_H, 2) elif sensor == 5: result = Spi.read(Register.IR_5_H, 2) MetricsLogging.put("Infrared", result, sensor) return result @staticmethod def set(sensor: int, on: bool): """Turn on / off a IR emitter :param sensor: Which sensor to read. Between 1 and 5 :raises: IndexError """ if sensor <= 0 or sensor > SENSOR_COUNT: raise IndexError("Invalid sensor specified!") if sensor == 1: Spi.write(Register.IR_1_LED, 1, on) elif sensor == 2: Spi.write(Register.IR_2_LED, 1, on) elif sensor == 3: Spi.write(Register.IR_3_LED, 1, on) elif sensor == 4: Spi.write(Register.IR_4_LED, 1, on) elif sensor == 5: Spi.write(Register.IR_5_LED, 1, on)