|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2017 Carter Nelson for adafruit |
| 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 | +# pylint: disable=C0103 |
| 23 | +""" |
| 24 | +`adafruit_DS2413` |
| 25 | +==================================================== |
| 26 | +
|
| 27 | + CircuitPython driver for the DS2413 one wire 2 channel GPIO breakout. |
| 28 | +
|
| 29 | +* Author(s): Carter Nelson |
| 30 | +""" |
| 31 | + |
| 32 | +__version__ = "0.0.0-auto.0" |
| 33 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DS2413.git" |
| 34 | + |
| 35 | +from adafruit_onewire.device import OneWireDevice |
| 36 | +from micropython import const |
| 37 | + |
| 38 | +_DS2413_ACCESS_READ = b'\xF5' |
| 39 | +_DS2413_ACCESS_WRITE = b'\x5A' |
| 40 | +_DS2413_ACK_SUCCESS = b'\xAA' |
| 41 | +_DS2413_ACK_ERROR = b'\xFF' |
| 42 | +INPUT = const(0) |
| 43 | +OUTPUT = const(1) |
| 44 | + |
| 45 | +class DS2413Pin(): |
| 46 | + """Class which provides interface to single DS2413 GPIO pin.""" |
| 47 | + |
| 48 | + def __init__(self, number, host, direction=OUTPUT): |
| 49 | + if number not in (0, 1): |
| 50 | + raise ValueError("Incorrect pin number.") |
| 51 | + self._number = number |
| 52 | + self._host = host |
| 53 | + self._mask = 1 << (number * 2) |
| 54 | + self._direction = None # create it, and then... |
| 55 | + self.direction = direction # set it through setter |
| 56 | + |
| 57 | + @property |
| 58 | + def direction(self): |
| 59 | + """The direction of the pin, either INPUT or OUTPUT.""" |
| 60 | + return self._direction |
| 61 | + |
| 62 | + @direction.setter |
| 63 | + def direction(self, direction): |
| 64 | + if direction not in (INPUT, OUTPUT): |
| 65 | + raise ValueError("Incorrect direction setting.") |
| 66 | + self._direction = OUTPUT |
| 67 | + self.value = False |
| 68 | + self._direction = direction |
| 69 | + |
| 70 | + @property |
| 71 | + def value(self): |
| 72 | + """The pin state if configured as INPUT. The output latch state |
| 73 | + if configured as OUTPUT. True is HIGH/ON, False is LOW/OFF.""" |
| 74 | + # return Pin State if configured for INPUT |
| 75 | + # return Latch State if configured for OUTPUT |
| 76 | + # NOTE: logic is re-inverted to make it more normally |
| 77 | + return not self._host.pio_state & (self._mask << self._direction) |
| 78 | + |
| 79 | + @value.setter |
| 80 | + def value(self, state): |
| 81 | + # This only makes sense if the pin is configured for OUTPUT. |
| 82 | + if self._direction == INPUT: |
| 83 | + raise RuntimeError("Can't set value when pin is set to input.") |
| 84 | + # We jump through some hoops in order to only set/clear the bit |
| 85 | + # for the channel associated with this pin object. |
| 86 | + current = self._host.pio_state |
| 87 | + # PIOB Output Latch PIOA Output Latch |
| 88 | + new = (current >> 2 & 0x02) | (current >> 1 & 0x01) |
| 89 | + # To switch the output transistor on, the corresponding bit value is 0. |
| 90 | + # To switch the output transistor off (non-conducting) the bit must be 1. |
| 91 | + if state: |
| 92 | + # clear it (transistor = ON) |
| 93 | + new &= ~(1 << self._number) |
| 94 | + else: |
| 95 | + # set it (transistor = OFF) |
| 96 | + new |= 1 << self._number |
| 97 | + self._host.pio_state = new |
| 98 | + |
| 99 | +class DS2413(): |
| 100 | + """Class which provides interface to DS2413 GPIO breakout.""" |
| 101 | + |
| 102 | + def __init__(self, bus, address): |
| 103 | + if address.family_code == 0x3A: |
| 104 | + self._address = address |
| 105 | + self._device = OneWireDevice(bus, address) |
| 106 | + self._buf = bytearray(3) |
| 107 | + self._IOA = None |
| 108 | + self._IOB = None |
| 109 | + else: |
| 110 | + raise RuntimeError('Incorrect family code in device address.') |
| 111 | + |
| 112 | + @property |
| 113 | + def IOA(self): |
| 114 | + """The pin object for channel A.""" |
| 115 | + if self._IOA is None: |
| 116 | + self._IOA = DS2413Pin(0, self) |
| 117 | + return self._IOA |
| 118 | + |
| 119 | + @property |
| 120 | + def IOB(self): |
| 121 | + """The pin object for channel B.""" |
| 122 | + if self._IOB is None: |
| 123 | + self._IOB = DS2413Pin(1, self) |
| 124 | + return self._IOB |
| 125 | + |
| 126 | + @property |
| 127 | + def pio_state(self): |
| 128 | + """The state of both PIO channels.""" |
| 129 | + return self._read_status() |
| 130 | + |
| 131 | + @pio_state.setter |
| 132 | + def pio_state(self, value): |
| 133 | + return self._write_latches(value) |
| 134 | + |
| 135 | + def _read_status(self): |
| 136 | + with self._device as dev: |
| 137 | + dev.write(_DS2413_ACCESS_READ) |
| 138 | + dev.readinto(self._buf, end=1) |
| 139 | + return self._buf[0] |
| 140 | + |
| 141 | + def _write_latches(self, value): |
| 142 | + # top six bits must be 1 |
| 143 | + value |= 0xFC |
| 144 | + self._buf[0] = value |
| 145 | + self._buf[1] = ~value & 0xFF |
| 146 | + with self._device as dev: |
| 147 | + dev.write(_DS2413_ACCESS_WRITE) |
| 148 | + dev.write(self._buf, end=2) |
| 149 | + dev.readinto(self._buf, end=1) |
| 150 | + if not self._buf[0] == ord(_DS2413_ACK_SUCCESS): |
| 151 | + raise RuntimeError('ACK failure.') |
0 commit comments