Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions notecard/gpio.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 11 additions & 17 deletions notecard/notecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 0 additions & 21 deletions notecard/platform.py

This file was deleted.

8 changes: 3 additions & 5 deletions notecard/timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand Down