I am new to RPi, I have got a RPi2 model B. I will be taking multiple analog inputs and I wish to process these signals individually, whithout being interdependent on one another. What is the best way of doing that via python??
What do you mean with "code" an input?srouth wrote:My main question here is there a way to code each channel of I/P separately, independtly on one another. Consider I have 4 inputs and I want to code them independtly at the same time. How to do that??
srouth wrote:I am new to RPi, I have got a RPi2 model B. I will be taking multiple analog inputs and I wish to process these signals individually, whithout being interdependent on one another. What is the best way of doing that via python??
Code: Select all
# pio.py # classes using Pigpio # written by Roger Woollett import pigpio as pg import socket as soc class Pio(): # to override the default host name (ip address) and port # assign to Pio.host and/or Pio.port in the calling file # static data _started = False _refcount = 0 def __init__(self): Pio._refcount += 1 self._closed = False if not Pio._started: try: Pio.host except AttributeError: Pio.host = 'localhost' try: Pio.port except AttributeError: Pio.port = 8888 # now see if daemon is running try: socket = soc.create_connection((Pio.host,Pio.port)) except: dok = False else: socket.close() dok = True if not dok: self._closed = True raise RuntimeError('Cannot connect to pigpio daemon') Pio.pi = pg.pi(Pio.host,Pio.port) Pio._started = True def close(self): Pio._refcount -= 1 self._closed = True if Pio._refcount == 0: Pio.pi.stop() Pio._started = False def __del__(self): if not self._closed: raise RuntimeError('Close not called in Pio object') class ADC(Pio): # class to access a MCP3008 A to D converter # pins used are: # Name board(gpio) - chip pin # MOSI 19(GPIO10) 11 - data in # MISO 21(GPIO9) 12 - data out # SCLK 23(GPIO11) 13 - clock # CE0 24(GPIO8) 10 - chip select # CE1 26(GPIO7) 10 - chip select on second chip # There is a second set of pins used on B+ and Pi2 # This auxilliary port is not supported as yet def __init__(self,chip): Pio.__init__(self) # chip should be 0 (chip connected to CE0) or 1 (CE1) if chip < 0 or chip > 1: Pio.close(self) raise RuntimeError('chip must be 0 (CE0) or 1 (CE1)') self.handle = Pio.pi.spi_open(chip,50000,0) def close(self): Pio.pi.spi_close(self.handle) Pio.close(self) def read(self,channel): # channel corresponds to pins 1 (ch0) to 8 (ch7) # we have to send three bytes # byte 0 has 7 zeros and a start bit # byte 1 has the top bit set to indicate # single rather than differential operation # the next three bits contain the channel # the bottom four bits are zero # byte 2 contains zeros (don't care) # 3 bytes are returned # byte 0 is ignored # byte 1 contains the high 2 bits # byte 2 contains the low 8 bits if channel < 0 or channel > 7: raise RuntimeError('channel must be 0 - 7') (count,data) = Pio.pi.spi_xfer(self.handle,[1,(8 + channel) << 4,0]) if count > 0: return (data << 8) + data else: return 0
Code: Select all
from pio import Pio,ADC