diff --git a/notecard/gpio.py b/notecard/gpio.py index 9a3dcde..c751be8 100644 --- a/notecard/gpio.py +++ b/notecard/gpio.py @@ -1,13 +1,19 @@ """GPIO abstractions for note-python.""" -from .platform import platform +import sys -if platform == 'circuitpython': +if sys.implementation.name == 'circuitpython': import digitalio -elif platform == 'micropython': +elif sys.implementation.name == 'micropython': import machine -elif platform == 'raspbian': - import RPi.GPIO as rpi_gpio +else: + try: + with open('/etc/os-release', 'r') as f: + if 'ID=raspbian' in f.read(): + raspbian = True + import RPi.GPIO as rpi_gpio + except IOError: + pass class GPIO: @@ -50,12 +56,15 @@ def setup(pin, direction, pull=None, value=None): The platform is detected internally so that the user doesn't need to write platform-specific code themselves. """ - if platform == 'circuitpython': + if sys.implementation.name == 'circuitpython': return CircuitPythonGPIO(pin, direction, pull, value) - elif platform == 'micropython': + elif sys.implementation.name == 'micropython': return MicroPythonGPIO(pin, direction, pull, value) - elif platform == 'raspbian': + elif raspbian: return RpiGPIO(pin, direction, pull, value) + else: + raise NotImplementedError( + 'GPIO not implemented for this platform.') def __init__(self, pin, direction, pull=None, value=None): """Initialize the GPIO. diff --git a/notecard/notecard.py b/notecard/notecard.py index 6cfc7c6..074ee82 100644 --- a/notecard/notecard.py +++ b/notecard/notecard.py @@ -38,11 +38,7 @@ from .transaction_manager import TransactionManager use_periphery = False -use_micropython = False use_serial_lock = False -use_circuitpython = sys.implementation.name == 'circuitpython' -use_micropython = sys.implementation.name == 'micropython' -use_rtc = not use_micropython and not use_circuitpython if sys.implementation.name == 'cpython': if sys.platform == 'linux' or sys.platform == 'linux2': @@ -52,7 +48,7 @@ use_serial_lock = True from filelock import Timeout, FileLock -use_i2c_lock = not use_periphery and not use_micropython +use_i2c_lock = not use_periphery and sys.implementation.name != 'micropython' NOTECARD_I2C_ADDRESS = 0x17 @@ -122,8 +118,7 @@ def serialTransaction(port, req, debug, txn_manager=None): if seg_len > CARD_REQUEST_SEGMENT_MAX_LEN: seg_len = CARD_REQUEST_SEGMENT_MAX_LEN - port.write(req_json[seg_off:seg_off + seg_len] - .encode('utf-8')) + port.write(req_json[seg_off:seg_off + seg_len].encode('utf-8')) seg_off += seg_len seg_left -= seg_len if seg_left == 0: @@ -152,8 +147,7 @@ def serialCommand(port, req, debug): if seg_len > CARD_REQUEST_SEGMENT_MAX_LEN: seg_len = CARD_REQUEST_SEGMENT_MAX_LEN - port.write(req_json[seg_off:seg_off + seg_len] - .encode('utf-8')) + port.write(req_json[seg_off:seg_off + seg_len].encode('utf-8')) seg_off += seg_len seg_left -= seg_len if seg_left == 0: @@ -239,13 +233,15 @@ def Transaction(self, req): if use_serial_lock: try: self.lock.acquire(timeout=5) - return serialTransaction(self.uart, req, self._debug, self._transaction_manager) + return serialTransaction(self.uart, req, self._debug, + self._transaction_manager) except Timeout: raise Exception("Notecard in use") finally: self.lock.release() else: - return serialTransaction(self.uart, req, self._debug, self._transaction_manager) + return serialTransaction(self.uart, req, self._debug, + self._transaction_manager) def Reset(self): """Reset the Notecard.""" @@ -287,10 +283,8 @@ def _sendPayload(self, json): chunk_len = min(json_left, self.max) reg = bytearray(1) reg[0] = chunk_len - write_data = bytes(json[ - chunk_offset: - chunk_offset + chunk_len - ], 'utf-8') + write_data = bytes(json[chunk_offset:chunk_offset + chunk_len], + 'utf-8') if use_periphery: msgs = [I2C.Message(reg + write_data)] self.i2c.transfer(self.addr, msgs) @@ -350,7 +344,7 @@ def Transaction(self, req): msgs = [I2C.Message(reg), I2C.Message(buf, read=True)] self.i2c.transfer(self.addr, msgs) buf = msgs[1].data - elif use_micropython: + elif sys.implementation.name == 'micropython': self.i2c.writeto(self.addr, reg, False) self.i2c.readfrom_into(self.addr, buf) else: @@ -402,7 +396,7 @@ def Reset(self): msgs = [I2C.Message(reg), I2C.Message(buf, read=True)] self.i2c.transfer(self.addr, msgs) buf = msgs[1].data - elif use_micropython: + elif sys.implementation.name == 'micropython': self.i2c.writeto(self.addr, reg, False) self.i2c.readfrom_into(self.addr, buf) else: diff --git a/notecard/platform.py b/notecard/platform.py deleted file mode 100644 index 5da6157..0000000 --- a/notecard/platform.py +++ /dev/null @@ -1,21 +0,0 @@ -"""Module for detecting the platform note-python is running on.""" - -import sys - -platform = None - -if sys.implementation.name == 'circuitpython': - import digitalio - platform = 'circuitpython' -elif sys.implementation.name == 'micropython': - import machine - platform = 'micropython' -elif sys.implementation.name == 'cpython': - try: - with open('/etc/os-release', 'r') as f: - use_raspbian = 'ID=raspbian' in f.read() - except IOError: - pass - - if use_raspbian: - platform = 'raspbian' diff --git a/notecard/timeout.py b/notecard/timeout.py index f13a399..f1761c3 100644 --- a/notecard/timeout.py +++ b/notecard/timeout.py @@ -3,12 +3,10 @@ import sys import time -from .platform import platform - -use_rtc = platform != 'micropython' and platform != 'circuitpython' +use_rtc = sys.implementation.name != 'micropython' and sys.implementation.name != 'circuitpython' if not use_rtc: - if platform == 'circuitpython': + if sys.implementation.name == 'circuitpython': import supervisor from supervisor import ticks_ms @@ -23,7 +21,7 @@ def ticks_diff(ticks1, ticks2): & _TICKS_MAX) - _TICKS_HALFPERIOD # noqa: F821 return diff - if platform == 'micropython': + if sys.implementation.name == 'micropython': from utime import ticks_diff, ticks_ms # noqa: F811