Skip to content

Commit

Permalink
DriverADC again :-( & some other lint comments
Browse files Browse the repository at this point in the history
DriverADC tried it the hard way to allow simulated driver, although
Adafruit did their job well..
  • Loading branch information
schwabix-1311 committed Oct 8, 2024
1 parent 4dff83d commit f798064
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
33 changes: 13 additions & 20 deletions aquaPi/driver/DriverADC.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
#!/usr/bin/env python3

from enum import Enum
import logging

try:
# latest Blinka supports x86 LinuxPC, but we don't at least not chips on I²C
from adafruit_platformdetect import Detector # type: ignore[import-untyped]
if Detector().board.id == 'GENERIC_LINUX_PC':
raise NotImplementedError

# latest Blinka supports x86 LinuxPC, but we don't at least not chips on I²C
from adafruit_platformdetect import Detector # type: ignore[import-untyped]
if not Detector().board.id == 'GENERIC_LINUX_PC':
SIMULATED = False
import board # type: ignore[import-untyped]
import busio # type: ignore[import-untyped]
import adafruit_ads1x15.ads1115 as ADS
# import adafruit_ads1x15.ads1015 as ADSxx
from adafruit_ads1x15.analog_in import AnalogIn

except NotImplementedError:
else:
SIMULATED = True

class ADS(Enum): # type: ignore[no-redef]
P0, P1, P2, P3 = range(0, 4)
import board # type: ignore[import-untyped]
import busio # type: ignore[import-untyped]
from adafruit_ads1x15.ads1115 import ADS1115, P0, P1, P2, P3
from adafruit_ads1x15.analog_in import AnalogIn


from .base import (AInDriver, IoPort, PortFunc)

Expand All @@ -46,10 +39,10 @@ class DriverADS1115(AInDriver):
"""

ADDRESSES = [0x48, 0x49, 0x4A, 0x4B]
CHANNELS = [ADS.P0, ADS.P1, ADS.P2, ADS.P3] # ATM no differential channels
CHANNELS = [P0, P1, P2, P3] # ATM no differential channels

@staticmethod
def is_ads111x(ads: ADS.ADS1115) -> bool:
def is_ads111x(ads:ADS1115) -> bool:
""" check power-on register defaults of ADS1113/4/5
"""
try:
Expand Down Expand Up @@ -92,7 +85,7 @@ def find_ports() -> dict[str,IoPort]:
log.brief('Scanning I²C bus for ADS1x13/4/5 ...')
for adr in DriverADS1115.ADDRESSES:
try:
ads = ADS.ADS1115(i2c, address=adr)
ads = ADS1115(i2c, address=adr)
if DriverADS1115.is_ads111x(ads):
adc_count += 1
for ch in DriverADS1115.CHANNELS:
Expand Down Expand Up @@ -130,7 +123,7 @@ def __init__(self, cfg:dict[str,str], func:PortFunc):
self.name: str = f'ADC #{cnt} (ADS1115 @0x{adr:02X} in {inp}'

i2c = busio.I2C(board.SCL, board.SDA)
self._ads = ADS.ADS1115(i2c, address=adr, gain=abs(self.gain))
self._ads = ADS1115(i2c, address=adr, gain=abs(self.gain))
self._ana_in: AnalogIn = AnalogIn(self._ads, inp)
self._median_filter: bool = True # const ATM

Expand Down
2 changes: 1 addition & 1 deletion aquaPi/machineroom/alert_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# from time import time

from .msg_bus import (BusListener, BusRole, MsgData, MsgFilter)
from ..driver import (PortFunc, io_registry, DriverReadError)
from ..driver import (PortFunc, io_registry)


log = logging.getLogger('machineroom.alert_nodes')
Expand Down
10 changes: 6 additions & 4 deletions aquaPi/machineroom/ctrl_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,13 @@ def __init__(self, name, inputs, xscend=1, _cont=False):
self.xscend = xscend.total_seconds() / 60 / 60
self._fader_thread = None
self._fader_stop = False
self._high = 0
if not _cont:
self.data = 0
self.target = self.data
self.unit = '%'
self.clouds = []
self.cloudiness = 0

def __getstate__(self):
state = super().__getstate__()
Expand All @@ -374,7 +376,7 @@ def listen(self, msg):
self._fader_thread.join()
self.target = float(msg.data)
if self.target:
self.high = self.target
self._high = self.target
self.cloudiness = int(random.random() * 7.5)
log.brief('SunCtrl: cloudiness %d', self.cloudiness)

Expand Down Expand Up @@ -460,7 +462,7 @@ def _fader(self):
self.post(MsgData(self.id, self.data))
while now - start < xscend and not self._fader_stop:
shadow = self._calculate_clouds()
new_data = self._halfsine(now - start, xscend * 2, self.high) * shadow
new_data = self._halfsine(now - start, xscend * 2, self._high) * shadow
self._make_next_step('ascend', new_data)
now = time.time()

Expand All @@ -469,14 +471,14 @@ def _fader(self):
while not self._fader_stop:
shadow = self._calculate_clouds()
self.alert = ('\u219d', 'act') if shadow else None # rightwards wave arrow
new_data = self.high * shadow
new_data = self._high * shadow
self._make_next_step('cloudy', new_data)
now = time.time()
else:
self.alert = ('\u2198', 'act') # south east arrow
while now - start < xscend and not self._fader_stop:
shadow = self._calculate_clouds()
new_data = self._halfsine(now - start + xscend, xscend * 2, self.high) * shadow
new_data = self._halfsine(now - start + xscend, xscend * 2, self._high) * shadow
self._make_next_step('descend', new_data)
now = time.time()
self.data = 0 # end of descend
Expand Down

0 comments on commit f798064

Please sign in to comment.