def get_interrupt_status(self): # Read IIR, LSR, and RXLVL registers n, d = self.pi.i2c_zip(self.i2c, [I2C_WRITE, 1, self.reg_conv(REG_IIR), I2C_READ, 1, I2C_START, I2C_WRITE, 1, self.reg_conv(REG_LSR), I2C_READ, 1, I2C_START, I2C_WRITE, 1, self.reg_conv(REG_RXLVL), I2C_READ, 1, I2C_END]) if n < 0: raise pigpio.error(pigpio.error_text(n)) elif n != 3: raise ValueError("unexpected number of bytes received") # Mask out two MSBs in IIR value and return tuple return (int(d[0]) & 0x3F, int(d[1]), int(d[2]))
def _write(self, addr, data): """ I2C write primitive I2C writes arbitary number of bytes to a slave device, It carries out the following steps: 1. Send start 2. Send address and R/W bit low (expecting an ack from the slave) 3. Send one or more bytes (expecting an ack after sending each byte) 4. Send stop addr: 7-bit integer, address of the slave device data: a byte or a list of bytes to write E.g. _write(0x20,0xff) # Write 0xff to a slave at address 0x20 _write(0x20,range(10)) # Write [0..9] to a slave at address 0x20 """ if isinstance(data, list): cmd = [4, addr, 2, 7, len(data)] + data + [3, 0] elif isinstance(data, int): cmd = [4, addr, 2, 7, 1, data, 3, 0] else: raise ValueError("Invalid parameter") try: self._open() ret = self.pi.bb_i2c_zip(self.sda, cmd) finally: self._close() if ret[0] != 0: import pigpio raise Exception(pigpio.error_text(ret[0]))
def _send(self, value, mode): """Send the specified value to the display with automatic 4bit / 8bit selection. The rs_mode is either ``RS_DATA`` or ``RS_INSTRUCTION``.""" # Wait, if compatibility mode is enabled if self.compat_mode and self.compat_mode_wait_time > 0: self._wait() # Assemble the parameters sent to the pigpio script params = [mode] params.extend([(value >> i) & 0x01 for i in range(8)]) # Switch off pigpio's exceptions, so that we get the return codes pigpio.exceptions = False while True: ret = self.pi.run_script(self._writescript, params) if ret >= 0: break elif ret != pigpio.PI_SCRIPT_NOT_READY: raise pigpio.error(pigpio.error_text(ret)) # If pigpio script is not ready, sleep and try again c.usleep(1) # Switch on pigpio's exceptions pigpio.exceptions = True # Record the time for the tail-end of the last send event if self.compat_mode: self.last_send_event = now()
def byte_write_verify(self, reg, byte): n, d = self.pi.i2c_zip(self.i2c, [I2C_WRITE, 2, self.reg_conv(reg), byte, I2C_READ, 1, I2C_END]) if n < 0: raise pigpio.error(pigpio.error_text(n)) elif n != 1: raise ValueError("unexpected number of bytes received") d = int(d[0]) return (d == byte, d)
def block_write(self, reg, bytestring): print(bytestring) self.pi.wave_clear() self.pi.wave_add_serial(PI_WRITE, 115200, bytestring) wid = self.pi.wave_create() cbs = self.pi.wave_send_once(wid) while self.pi.wave_tx_busy(): print("waveform tx busy ", self.pi.wave_tx_at(), " ", self.pi.wave_get_cbs()) time.sleep(.1) n, d = self.pi.i2c_zip(self.i2c, [I2C_WRITE, len(bytestring)+1, self.reg_conv(reg)] + list(bytestring) + [I2C_END]) if n < 0: raise pigpio.error(pigpio.error_text(n))
def __getitem__(self, key): self.check_key(key) cmd = [4, self.addr] # Set address cmd += [2, 7, 1, key] # write the register to read. cmd += [2, 6, 1] # read 1 byte cmd += [3, 0] # stop and end sequence (count, data) = self.pi.bb_i2c_zip(self.handle, cmd) if count < 1: raise I2cError(pigpio.error_text(count)) return data
def _send(self, value, mode): """Send the specified value to the display with automatic 4bit / 8bit selection. The rs_mode is either ``RS_DATA`` or ``RS_INSTRUCTION``.""" # Assemble the parameters sent to the pigpio script params = [mode] params.extend([(value >> i) & 0x01 for i in range(8)]) # Switch off pigpio's exceptions, so that we get the return codes pigpio.exceptions = False while True: ret = self.pi.run_script(self._writescript, params) if ret >= 0: break elif ret != pigpio.PI_SCRIPT_NOT_READY: raise pigpio.error(pigpio.error_text(ret)) # If pigpio script is not ready, sleep and try again c.usleep(1) # Switch on pigpio's exceptions pigpio.exceptions = True
def _read(self, addr, length=1): """ I2C read primitive I2C reads arbitary number of bytes from a slave device. It carries out the following steps: 1. Send start 2. Send address and R/W bit high (expecting an ack from the slave) 3. Optionally receive one or more bytes (send an ack after receiving each byte) 4. Receive the last byte (send a nack after receiving the last byte) 5. Send stop :param addr: 7-bit integer, address of the slave device :param length: non-negative integer, the number of bytes to read The return is a byte when length==1, or a list of bytes otherwise .. code-block:: python _read(0x20) # Return one byte from a slave at 0x20 _read(0x20,10) # Return 10 bytes from a slave at 0x20 """ if length < 1: raise ValueError("Invalid parameter") cmd = [4, addr, 2, 6, length, 3, 0] try: self._open() ret = self.pi.bb_i2c_zip(self.sda, cmd) finally: self._close() if ret[0] < 0: import pigpio raise Exception(pigpio.error_text(ret[0])) elif length == 1: return ret[1][0] else: return list(ret[1])
def _open(self): ret = self.pi.bb_i2c_open(self.sda, self.scl, self.baud) if ret != 0: raise Exception(pigpio.error_text(ret[0]))
# Build the radio options from our device mapping list for relay_pin, board_pin in relay_pins.iteritems(): print(""" <option value="{}">{}</option>""".format(board_pin, relay_pin)) print("""</select></td><td> <select name="action"> <option value="On">On</option> <option value="Off">Off</option> <option value="Reboot">Reboot</option> </select> </td></tr><tr><td colspan="2"><br> <input class="button" type="submit" value="Execute"> <br> <br> </form> </td></tr> """) print("""<tr><td colspan="2"> <form method="post" action="gpio.py"><br> <input class="button" type="submit" value="Refresh" name="action"> <br> </td></tr></form></table>""") if err < 0: command = command + ' <span style="color:red">(Error: {})</span>'.format(pigpio.error_text(err)) if command != "": print("<br>Last command: " + command) print("</body></html>")
def block_read(self, reg, num): n, d = self.pi.i2c_zip(self.i2c, [I2C_WRITE, 1, self.reg_conv(reg), I2C_READ, num, I2C_END]) if n < 0: raise pigpio.error(pigpio.error_text(n)) elif n != num: raise ValueError("all available bytes were not successfully read") return d
def block_write(self, reg, bytestring): n, d = self.pi.i2c_zip(self.i2c, [I2C_WRITE, 1, self.reg_conv(reg), I2C_WRITE, len(bytestring)] + list(bytestring) + [I2C_END]) if n < 0: raise pigpio.error(pigpio.error_text(n))
def mock_pigpio__u2i_exception(self, *args, **kwargs): value = func(self, *args, **kwargs) v = value if type(value) in (int, bool, float) else value[0] if v < 0: raise pigpio.error(pigpio.error_text(v)) return value