]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/bitbangio.py
Display message to linux user to use new Bitbang Library for now
[Adafruit_Blinka-hackapet.git] / src / bitbangio.py
1 """
2 `bitbangio` - Bitbanged bus protocols
3 ==============================================================
4
5 See `CircuitPython:bitbangio` in CircuitPython for more details.
6
7 * Author(s): cefn
8 """
9
10 from adafruit_blinka import Lockable, agnostic
11 import adafruit_platformdetect.constants.boards as ap_board
12
13
14 class I2C(Lockable):
15     def __init__(self, scl, sda, frequency=400000):
16         # TODO: This one is a bit questionable:
17         if agnostic.board_id == ap_board.PYBOARD:
18             raise NotImplementedError("No software I2C on {}".format(agnostic.board_id))
19         elif agnostic.detector.board.any_embedded_linux:
20             # TODO: Attempt to load this library automatically
21             raise NotImplementedError(
22                 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
23             )
24         self.init(scl, sda, frequency)
25
26     def init(self, scl, sda, frequency):
27         from machine import Pin
28         from machine import I2C as _I2C
29
30         self.deinit()
31         id = (
32             -1
33         )  # force bitbanging implementation - in future introspect platform if SDA/SCL matches hardware I2C
34         self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
35
36     def deinit(self):
37         try:
38             del self._i2c
39         except AttributeError:
40             pass
41
42     def __enter__(self):
43         return self
44
45     def __exit__(self, exc_type, exc_value, traceback):
46         self.deinit()
47
48     def scan(self):
49         return self._i2c.scan()
50
51     def readfrom_into(self, address, buffer, start=0, end=None):
52         if start is not 0 or end is not None:
53             if end is None:
54                 end = len(buffer)
55             buffer = memoryview(buffer)[start:end]
56         stop = True  # remove for efficiency later
57         return self._i2c.readfrom_into(address, buffer, stop)
58
59     def writeto(self, address, buffer, start=0, end=None, stop=True):
60         if start is not 0 or end is not None:
61             if end is None:
62                 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
63             else:
64                 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
65         return self._i2c.writeto(address, buffer, stop)
66
67
68 # TODO untested, as actually busio.SPI was on tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
69 class SPI(Lockable):
70     def __init__(self, clock, MOSI=None, MISO=None):
71         if agnostic.detector.board.any_embedded_linux:
72             # TODO: Attempt to load this library automatically
73             raise NotImplementedError(
74                 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
75             )
76         from machine import SPI
77
78         self._spi = SPI(-1)
79         self._pins = (clock, MOSI, MISO)
80
81     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
82         from machine import SPI, Pin
83
84         if self._locked:
85             # TODO verify if _spi obj 'caches' sck, mosi, miso to avoid storing in _attributeIds (duplicated in busio)
86             # i.e. #init ignores MOSI=None rather than unsetting
87             self._spi.init(
88                 baudrate=baudrate,
89                 polarity=polarity,
90                 phase=phase,
91                 bits=bits,
92                 firstbit=SPI.MSB,
93                 sck=Pin(self._pins[0].id),
94                 mosi=Pin(self._pins[1].id),
95                 miso=Pin(self._pins[2].id),
96             )
97         else:
98             raise RuntimeError("First call try_lock()")
99
100     def write(self, buf):
101         return self._spi.write(buf)
102
103     def readinto(self, buf):
104         return self.readinto(buf)
105
106     def write_readinto(self, buffer_out, buffer_in):
107         return self.write_readinto(buffer_out, buffer_in)