38 lines
972 B
Python
38 lines
972 B
Python
import smbus
|
|
|
|
from compLib.LogstashLogging import Logging
|
|
|
|
SINGLE_ENDED = 0x84
|
|
ADDRESS = 0x48
|
|
|
|
bus = smbus.SMBus(1)
|
|
|
|
|
|
class ADC:
|
|
|
|
@staticmethod
|
|
def read(channel) -> float:
|
|
"""
|
|
Read from adc channel
|
|
0 -> Left IR Sensor
|
|
1 -> Right IR Sensor
|
|
2 -> Battery voltage / 3
|
|
:param channel: Channel between 0 and 2
|
|
:return: voltage
|
|
"""
|
|
|
|
"""Select the Command data from the given provided value above"""
|
|
command = SINGLE_ENDED | ((((channel << 2) | (channel >> 1)) & 0x07) << 4)
|
|
bus.write_byte(ADDRESS, command)
|
|
|
|
while "Koka is great":
|
|
value1 = bus.read_byte(ADDRESS)
|
|
value2 = bus.read_byte(ADDRESS)
|
|
if value1 == value2:
|
|
break
|
|
|
|
voltage = value1 / 255.0 * 3.3 # calculate the voltage value
|
|
voltage = round(voltage, 2)
|
|
|
|
Logging.get_logger().debug(f"ADC.read {channel} = {voltage}")
|
|
return voltage
|