/ adafruit_onewire / device.py
device.py
 1  # The MIT License (MIT)
 2  #
 3  # Copyright (c) 2017 Carter Nelson for Adafruit Industries
 4  #
 5  # Permission is hereby granted, free of charge, to any person obtaining a copy
 6  # of this software and associated documentation files (the "Software"), to deal
 7  # in the Software without restriction, including without limitation the rights
 8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9  # copies of the Software, and to permit persons to whom the Software is
10  # furnished to do so, subject to the following conditions:
11  #
12  # The above copyright notice and this permission notice shall be included in
13  # all copies or substantial portions of the Software.
14  #
15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  # THE SOFTWARE.
22  """
23  `adafruit_onewire.device`
24  ====================================================
25  
26  Provides access to a single device on the 1-Wire bus.
27  
28  * Author(s): Carter Nelson
29  """
30  
31  __version__ = "0.0.0-auto.0"
32  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_OneWire.git"
33  
34  _MATCH_ROM = b"\x55"
35  
36  
37  class OneWireDevice:
38      """A class to represent a single device on the 1-Wire bus."""
39  
40      def __init__(self, bus, address):
41          self._bus = bus
42          self._address = address
43  
44      def __enter__(self):
45          self._select_rom()
46          return self
47  
48      def __exit__(self, *exc):
49          return False
50  
51      def readinto(self, buf, *, start=0, end=None):
52          """
53          Read into ``buf`` from the device. The number of bytes read will be the
54          length of ``buf``.
55  
56          If ``start`` or ``end`` is provided, then the buffer will be sliced
57          as if ``buf[start:end]``. This will not cause an allocation like
58          ``buf[start:end]`` will so it saves memory.
59  
60          :param bytearray buf: buffer to write into
61          :param int start: Index to start writing at
62          :param int end: Index to write up to but not include
63          """
64          self._bus.readinto(buf, start=start, end=end)
65          if start == 0 and end is None and len(buf) >= 8:
66              if self._bus.crc8(buf):
67                  raise RuntimeError("CRC error.")
68  
69      def write(self, buf, *, start=0, end=None):
70          """
71          Write the bytes from ``buf`` to the device.
72  
73          If ``start`` or ``end`` is provided, then the buffer will be sliced
74          as if ``buffer[start:end]``. This will not cause an allocation like
75          ``buffer[start:end]`` will so it saves memory.
76  
77          :param bytearray buf: buffer containing the bytes to write
78          :param int start: Index to start writing from
79          :param int end: Index to read up to but not include
80          """
81          return self._bus.write(buf, start=start, end=end)
82  
83      def _select_rom(self):
84          self._bus.reset()
85          self.write(_MATCH_ROM)
86          self.write(self._address.rom)