本文整理汇总了Python中spidev.SpiDev类的典型用法代码示例。如果您正苦于以下问题:Python SpiDev类的具体用法?Python SpiDev怎么用?Python SpiDev使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SpiDev类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MCP3008
class MCP3008(object):
"""
MCP3008 ADC (Analogue-to-Digital converter).
"""
def __init__(self, bus=0, device=0, channel=0):
self.bus = bus
self.device = device
self.channel = channel
self.spi = SpiDev()
def __enter__(self):
self.open()
return self
def open(self):
self.spi.open(self.bus, self.device)
def read(self):
adc = self.spi.xfer2([1, (8 + self.channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data
def __exit__(self, type, value, traceback):
self.close()
def close(self):
self.spi.close()
示例2: ad7606
class ad7606():
try:
start_acq_fd = open("/sys/class/gpio/gpio145/value", 'w', 0)
except IOError as e:
print("Error opening start acquire gpio pin: %s" % e)
raise
def __init__(self, dev):
self.dev = SpiDev()
try:
self.dev.open(3,dev)
self.dev.mode = 2
except IOError as e:
print("Error opening /dev/spidev3.%d: %s" % (dev, e))
raise
def trigger_acquire(self):
self.start_acq_fd.write(b'1')
self.start_acq_fd.write(b'0')
def read(self):
self.trigger_acquire()
buf = self.dev.readbytes(16)
samples = [0,0,0,0,0,0,0,0]
for i in xrange(8):
samples[i] = buf[2*i] << 8 | buf[2*i+1] << 0
return samples
示例3: dac8568
class dac8568():
def __init__(self, dev):
self.dev = SpiDev()
try:
self.dev.open(4,dev)
except IOError as e:
print("Error opening /dev/spidev4.%d: %s" % (dev, e))
raise
def build_command(self, control, addr, data):
prefix = 0
features = 0
cmd = [0,0,0,0]
cmd[3] = (0xf & features) << 0 | (0xf & data) << 4
cmd[2] = (0x0ff0 & data) >> 4
cmd[1] = (0xf000 & data) >> 12 | (0xf & addr) << 4
cmd[0] = (0xf & control) << 0 | (0xf & prefix) << 4
return cmd
# indexed by label number, mapping to DAC channel number
channel_map = (0,2,4,6,7,5,3,1)
write_mode = { 'WRITE': 0x0,
'UPDATE': 0x1,
'WRITE_UPDATE_ALL': 0x2,
'WRITE_UPDATE': 0x3 }
def write(self, addr, data, mode = write_mode['WRITE_UPDATE']):
addr = self.channel_map[addr]
cmd = self.build_command(mode, addr, data)
self.dev.writebytes(cmd)
示例4: AnalogInputDevice
class AnalogInputDevice(CompositeDevice):
"""
Represents an analog input device connected to SPI (serial interface).
"""
def __init__(self, device=0, bits=None):
if bits is None:
raise InputDeviceError('you must specify the bit resolution of the device')
if device not in (0, 1):
raise InputDeviceError('device must be 0 or 1')
self._device = device
self._bits = bits
self._spi = SpiDev()
self._spi.open(0, self.device)
super(AnalogInputDevice, self).__init__()
def close(self):
"""
Shut down the device and release all associated resources.
"""
if self._spi:
s = self._spi
self._spi = None
s.close()
super(AnalogInputDevice, self).close()
@property
def bus(self):
"""
The SPI bus that the device is connected to. As the Pi only has a
single (user accessible) SPI bus, this always returns 0.
"""
return 0
@property
def device(self):
"""
The select pin that the device is connected to. The Pi has two select
pins so this will be 0 or 1.
"""
return self._device
def _read(self):
raise NotImplementedError
@property
def value(self):
"""
A value read from the device. This will be a floating point value
between 0 and 1 (scaled according to the number of bits supported by
the device).
"""
return self._read() / (2**self._bits - 1)
示例5: SpiDevice
class SpiDevice(object):
def __init__(self, bus, device):
self.spi = SpiDev()
self.bus = bus
self.device = device
def init(self):
self.spi.open(self.bus, self.device)
def transfer(self, data):
return self.spi.xfer2(data)
def close(self):
self.spi.close()
示例6: run
def run():
"""
Launches the main loop
"""
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ALERT_PIN, GPIO.OUT)
try:
spi = SpiDev()
spi.open(0, 0)
api = XivelyAPIClient(XIVELY_API_KEY)
feed = api.feeds.get(XIVELY_FEED_ID)
moisture = get_datastream(feed, "water")
light = get_datastream(feed, "light")
temp = get_datastream(feed, "temp")
sent_notification = False
while True:
moisture.current_value = readadc(spi, ADC_MOISTURE_PIN)
moisture.at = datetime.utcnow()
light.current_value = readadc(spi, ADC_LIGHT_PIN)
light.at = datetime.utcnow()
temp.current_value = "%.2f" % convert_temp(readadc(spi, ADC_TMP_PIN))
temp.at = datetime.utcnow()
if(DEBUG):
print("Moisture: %d, light: %d, temp: %s" % (
moisture.current_value,
light.current_value,
temp.current_value))
if(moisture.current_value < MOISTURE_THRESHOLD):
if(not sent_notification):
send_pushover_msg(
"Please water your plant: %s" % moisture.current_value)
sent_notification = True
GPIO.output(ALERT_PIN, GPIO.HIGH)
else:
sent_notification = False
GPIO.output(ALERT_PIN, GPIO.LOW)
try:
moisture.update()
light.update()
temp.update()
except Exception as e:
print("%s" % e.strerror)
time.sleep(UPDATE_DELAY)
finally:
GPIO.cleanup()
示例7: __init__
def __init__(self, bus = 0, channel_select = 1):
self.bus = SpiDev()
self.bus.open(bus,channel_select)
self.reset()
self.buff=[]
self.run = True
self.timestamp = time()
示例8: __init__
class MCP3008:
def __init__(self, bus = 0, device = 0):
self.bus, self.device = bus, device
self.spi = SpiDev()
self.open()
def open(self):
self.spi.open(self.bus, self.device)
def read(self, channel = 0):
adc = self.spi.xfer2([1, (8 + channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data
def close(self):
self.spi.close()
示例9: __init__
def __init__(self, dev):
self.dev = SpiDev()
try:
self.dev.open(4,dev)
except IOError as e:
print("Error opening /dev/spidev4.%d: %s" % (dev, e))
raise
示例10: __init__
def __init__(self, device=0, bits=None):
if bits is None:
raise InputDeviceError("you must specify the bit resolution of the device")
if device not in (0, 1):
raise InputDeviceError("device must be 0 or 1")
self._device = device
self._bits = bits
self._spi = SpiDev()
self._spi.open(0, self.device)
示例11: __init__
def __init__(self, device=0, bits=None):
if bits is None:
raise InputDeviceError('you must specify the bit resolution of the device')
if device not in (0, 1):
raise InputDeviceError('device must be 0 or 1')
self._device = device
self._bits = bits
self._spi = SpiDev()
self._spi.open(0, self.device)
super(AnalogInputDevice, self).__init__()
示例12: AnalogInputDevice
class AnalogInputDevice(object):
"""
Represents an analog input device connected to SPI (serial interface).
"""
def __init__(self, device=0, bits=None):
if bits is None:
raise InputDeviceError("you must specify the bit resolution of the device")
if device not in (0, 1):
raise InputDeviceError("device must be 0 or 1")
self._device = device
self._bits = bits
self._spi = SpiDev()
self._spi.open(0, self.device)
def close(self):
if self._spi:
s = self._spi
self._spi = None
s.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_tb):
self.close()
@property
def bus(self):
return 0
@property
def device(self):
return self._device
def _read(self):
raise NotImplementedError
@property
def value(self):
return self._read() / (2 ** self._bits - 1)
示例13: MCP3008
class MCP3008(object):
"""Class for MCP3008 ADC"""
def __init__(self, port=0, device=0):
self.spi = SpiDev()
# connect spi object to specified spi device
self.spi.open(port, device)
def readValueChannel(self, channel=0):
"""
read SPI data from MCP3008 on channel -> digital value
spi.xfer2() send three bytes to the device
the first byte is 1 -> 00000001
the second byte is 8 + channel and left shift with 4 bits
the third byte is 0 -> 00000000
the device return 3 bytes as responce
"""
# perform spi transaction
adc = self.spi.xfer2([1, (8 + channel) <<4, 0])
# extract value from data bytes
data = ((adc[1] & 3) << 8) + adc[2]
return data
def readVoltChannel(self, channel=0, vmax=3.3, places=5):
"""
read the digital data from MCP3008 and convert it to voltage
MCP3008: 10bit ADC -> value in number range 0-1023
spi value -> voltage
0 -> 0v
1023 -> vmax
"""
# read spi digital value
adc = self.spi.xfer2([1, (8 + channel) <<4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
# convert it to voltage
volts = (data * vmax) / float(1023)
# round to specified number of decimal places
volts = round(volts, places)
return volts
示例14: __init__
class MCP3008:
def __init__(self, bus = 0, device = 0, channel = 0):
self.bus, self.device, self.channel = bus, device, channel
self.spi = SpiDev()
def __enter__(self):
self.open()
return self
def open(self):
self.spi.open(self.bus, self.device)
def read(self):
adc = self.spi.xfer2([1, (8 + self.channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data
def __exit__(self, type, value, traceback):
self.close()
def close(self):
self.spi.close()
示例15: __init__
def __init__(self, device=0, chipselect=0, debug=False):
self.device = device
self.chipselect = chipselect
self.debug = debug
try:
try:
# Initialize GPIOs
GPIO.setmode(GPIO.BCM) # Use Broadcom numbers
GPIO.setwarnings(False)
GPIO.setup(self.GPIO_RESET, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self.GPIO_IRQ, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
GPIO.add_event_detect(self.GPIO_IRQ, GPIO.RISING)
time.sleep(0.05)
except RuntimeError as e:
raise PhyError("Failed to initialize GPIOs: %s" %\
str(e))
# Initialize SPI
try:
self.spi = SpiDev()
self.spi.open(device, chipselect)
except IOError as e:
raise PhyError("Failed to open SPI device %d.%d: %s" %\
(device, chipselect, str(e)))
try:
self.spi.mode = 0;
self.spi.bits_per_word = 8;
self.spi.cshigh = False
self.spi.lsbfirst = False
self.spi.max_speed_hz = 200000;
except IOError as e:
try:
self.spi.close()
self.spi = None
except:
pass
raise PhyError("Failed to configure SPI device %d.%d: %s" %\
(device, chipselect, str(e)))
# Get the controller out of hardware reset
GPIO.output(self.GPIO_RESET, GPIO.HIGH)
time.sleep(0.2)
# Send a software reset
self.sendReset()
# Upload default config
self.profibusSetPhyConfig()
except:
GPIO.cleanup()
raise