From 572d48e8523e7cf0895803830c2d1d6cb5de8161 Mon Sep 17 00:00:00 2001 From: poesel Date: Sat, 9 Nov 2019 15:30:49 +0100 Subject: [PATCH] fixes for the ESP32 port --- terkin/device.py | 6 ++---- terkin/drivers/ds18x20.py | 14 ++++++++++++-- terkin/lib/hx711.py | 17 ++++++++++++++--- terkin/network/wifi.py | 4 ++-- terkin/sensor/core.py | 18 ++++++++++++++++-- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/terkin/device.py b/terkin/device.py index 3d2ba0fb..a574ed55 100644 --- a/terkin/device.py +++ b/terkin/device.py @@ -243,10 +243,8 @@ def add(item=''): add('=' * len(title)) # Machine runtime information. - if self.platform_info.vendor == self.platform_info.MICROPYTHON.Pycom: - frequency = machine.freq() / 1000000 - else: - frequency = machine.freq()[0] / 1000000 + frequency = machine.freq() / 1000000 + add('CPU freq {} MHz'.format(frequency)) add('Device id {}'.format(self.device_id)) add() diff --git a/terkin/drivers/ds18x20.py b/terkin/drivers/ds18x20.py index 49557cad..3d625cb6 100644 --- a/terkin/drivers/ds18x20.py +++ b/terkin/drivers/ds18x20.py @@ -8,6 +8,8 @@ from terkin import logging from terkin.sensor import AbstractSensor from terkin.sensor.core import OneWireBus +from terkin.util import get_platform_info +platform_info = get_platform_info() log = logging.getLogger(__name__) @@ -30,8 +32,16 @@ def start(self): # Initialize the DS18x20 hardware driver. try: - from onewire import DS18X20 - self.driver = DS18X20(self.bus.adapter) + if platform_info.vendor == platform_info.MICROPYTHON.Vanilla: + import ds18x20 + #self.driver = ds18x20.DS18X20(self.ow) + # TBD there is some difference between uPy & Pycom + elif platform_info.vendor == platform_info.MICROPYTHON.Pycom: + from onewire import DS18X20 + self.driver = DS18X20(self.bus.adapter) + else: + raise NotImplementedError('DS18X20 is ' + 'not implemented on this platform') return True except Exception as ex: diff --git a/terkin/lib/hx711.py b/terkin/lib/hx711.py index 9c371c01..09cd0429 100644 --- a/terkin/lib/hx711.py +++ b/terkin/lib/hx711.py @@ -5,6 +5,8 @@ import utime from terkin import logging from machine import Pin, enable_irq, disable_irq, idle +from terkin.util import get_platform_info +platform_info = get_platform_info() log = logging.getLogger(__name__) @@ -20,8 +22,16 @@ class HX711: def __init__(self, dout, pd_sck, gain=128): # Define two pins for clock and data. - self.pSCK = Pin(pd_sck, mode=Pin.OUT) - self.pOUT = Pin(dout, mode=Pin.IN, pull=Pin.PULL_UP) + if platform_info.vendor == platform_info.MICROPYTHON.Vanilla: + self.pSCK = Pin(int(pd_sck[1:]), mode=Pin.OUT) + self.pOUT = Pin(int(dout[1:]), mode=Pin.IN, pull=Pin.PULL_UP) + elif platform_info.vendor == platform_info.MICROPYTHON.Pycom: + self.pSCK = Pin(pd_sck, mode=Pin.OUT) + self.pOUT = Pin(dout, mode=Pin.IN, pull=Pin.PULL_UP) + else: + raise NotImplementedError('HX711 is ' + 'not implemented on this platform') + self.initialized = False @@ -202,7 +212,8 @@ def power_up(self): # Unfreeze pin hold when coming from deep sleep. # https://community.hiveeyes.org/t/strom-sparen-beim-einsatz-der-micropython-firmware-im-batteriebetrieb/2055/72 - self.pSCK.hold(False) + if platform_info.vendor == platform_info.MICROPYTHON.Pycom: + self.pSCK.hold(False) log.info('HX711 power up') self.pSCK.value(False) diff --git a/terkin/network/wifi.py b/terkin/network/wifi.py index c888f224..4fa414aa 100644 --- a/terkin/network/wifi.py +++ b/terkin/network/wifi.py @@ -558,8 +558,8 @@ def read(self): if platform_info.vendor == platform_info.MICROPYTHON.Vanilla: stats = {} - stats['system.wifi.channel'] = self.station.config('channel') - + # only in AP mode + #stats['system.wifi.channel'] = self.station.config('channel') try: stats['system.wifi.rssi'] = self.station.status('rssi') except: diff --git a/terkin/sensor/core.py b/terkin/sensor/core.py index b143b4f7..dd7b7e44 100644 --- a/terkin/sensor/core.py +++ b/terkin/sensor/core.py @@ -7,6 +7,8 @@ from machine import Pin, I2C from terkin import logging +from terkin.util import get_platform_info +platform_info = get_platform_info() log = logging.getLogger(__name__) @@ -275,7 +277,13 @@ def start(self): # Todo: Improve error handling. try: from onewire import OneWire - self.adapter = OneWire(Pin(self.pins['data'])) + if platform_info.vendor == platform_info.MICROPYTHON.Vanilla: + self.adapter = OneWire(Pin(int(self.pins['data'][1:]))) + elif platform_info.vendor == platform_info.MICROPYTHON.Pycom: + self.adapter = OneWire(Pin(self.pins['data'])) + else: + raise NotImplementedError('I2C Bus is ' + 'not implemented on this platform') self.scan_devices() except Exception as ex: @@ -328,7 +336,13 @@ def start(self): """ """ # Todo: Improve error handling. try: - self.adapter = I2C(self.number, mode=I2C.MASTER, pins=(self.pins['sda'], self.pins['scl']), baudrate=100000) + if platform_info.vendor == platform_info.MICROPYTHON.Vanilla: + self.adapter = I2C(self.number, sda = Pin(int(self.pins['sda'][1:])), scl = Pin(int(self.pins['scl'][1:])), freq=100000) + elif platform_info.vendor == platform_info.MICROPYTHON.Pycom: + self.adapter = I2C(self.number, mode=I2C.MASTER, pins=(self.pins['sda'], self.pins['scl']), baudrate=100000) + else: + raise NotImplementedError('I2C Bus is ' + 'not implemented on this platform') self.scan_devices() except Exception as ex: log.exc(ex, 'I2C hardware driver failed')