def test_transfer_multiple_bytes(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) gpio.pin_read[3] = [ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 ] result = device.transfer([0xF8, 0x1F, 0xF8]) # Verify clock self.assertListEqual(gpio.pin_written[1], [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ]) # Verify MOSI self.assertListEqual(gpio.pin_written[2], [ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 ]) # Verify MISO self.assertNotIn(3, gpio.pin_written) # Verify SS self.assertListEqual(gpio.pin_written[4], [1, 0, 1]) # Verify result self.assertEqual(result, bytearray([0x1F, 0xF8, 0x1F]))
def __init__(self, num_led, global_brightness=MAX_BRIGHTNESS, order='rgb', mosi=10, sclk=11, max_speed_hz=8000000): """Initializes the library. """ self.num_led = num_led # The number of LEDs in the Strip order = order.lower() self.rgb = RGB_MAP.get(order, RGB_MAP['rgb']) # Limit the brightness to the maximum if it's set higher if global_brightness > self.MAX_BRIGHTNESS: self.global_brightness = self.MAX_BRIGHTNESS else: self.global_brightness = global_brightness self.leds = [self.LED_START, 0, 0, 0] * self.num_led # Pixel buffer # MOSI 10 and SCLK 11 is hardware SPI, which needs to be set-up differently if mosi == 10 and sclk == 11: self.spi = SPI.SpiDev(0, 0, max_speed_hz) # Bus 0, chip select 0 else: self.spi = SPI.BitBang(GPIO.get_platform_gpio(), sclk, mosi)
def test_pin_modes_set_correctly(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) self.assertDictEqual(gpio.pin_mode, {1: GPIO.OUT, 2: GPIO.OUT, 3: GPIO.IN, 4: GPIO.OUT})
def __init__(self, count, clk=None, do=None, spi=None, gpio=None): """Initialize set of WS2801/SPI-like addressable RGB LEDs. Must specify the count of pixels, and either an explicit clk (clokc) and do (data output) line for software SPI or a spi instance for hardware SPI. """ self._spi = None if spi: # Handle hardware SPI. self._spi = spi elif clk and do: # Handle software SPI. # Default to platform GPIO if not provided. if not gpio: import Adafruit_GPIO as GPIO gpio = GPIO.get_platform_gpio() self._spi = SPI.BitBang(gpio, clk, do, None, None) else: raise ValueError('Must specify either spi for for hardware SPI or clk, and do for software SPI!') # Setup SPI interface with up to 20mhz speed. self._spi.set_clock_hz(1000000) self._spi.set_mode(0) self._spi.set_bit_order(SPI.MSBFIRST) # Setup buffer for pixel RGB data. self._count = count self._pixels = [0]*(count*3)
def __init__(self, cs, sclk=None, mosi=None, miso=None, gpio=None, spi=None): """Create an instance of the PN532 class using either software SPI (if the sclk, mosi, and miso pins are specified) or hardware SPI if a spi parameter is passed. The cs pin must be a digital GPIO pin. Optionally specify a GPIO controller to override the default that uses the board's GPIO pins. """ # Default to platform GPIO if not provided. self._gpio = gpio if self._gpio is None: self._gpio = GPIO.get_platform_gpio() # Initialize CS line. self._cs = cs self._gpio.setup(self._cs, GPIO.OUT) self._gpio.set_high(self._cs) # Setup SPI provider. if spi is not None: logger.debug('Using hardware SPI.') # Handle using hardware SPI. self._spi = spi self._spi.set_clock_hz(1000000) else: logger.debug('Using software SPI') # Handle using software SPI. Note that the CS/SS pin is not used # as it will be manually controlled by this library for better # timing. self._spi = SPI.BitBang(self._gpio, sclk, mosi, miso) # Set SPI mode and LSB first bit order. self._spi.set_mode(0) self._spi.set_bit_order(SPI.LSBFIRST)
def __init__(self, clk=None, cs=None, miso=None, mosi=None, spi=None, gpio=None): """Initialize MAX31855 device with software SPI on the specified CLK, CS, and DO pins. Alternatively can specify hardware SPI by sending an Adafruit_GPIO.SPI.SpiDev device in the spi parameter. """ self._spi = None # Handle hardware SPI if spi is not None: self._spi = spi elif clk is not None and cs is not None and miso is not None and mosi is not None: # Default to platform GPIO if not provided. if gpio is None: gpio = GPIO.get_platform_gpio() self._spi = SPI.BitBang(gpio, clk, mosi, miso, cs) else: raise ValueError( 'Must specify either spi for for hardware SPI or clk, cs, miso, and mosi for softwrare SPI!' ) self._spi.set_clock_hz(1000000) self._spi.set_mode(0) self._spi.set_bit_order(SPI.MSBFIRST)
def __init__(self, num_led, global_brightness=100, order='rgb', mosi=10, sclk=11, bus=0, device=0, max_speed_hz=8000000, led_order=None): """Initializes the library.""" rgb_map = RGB_MAP[order.lower()] self.pixel_cmd = APA102Cmd(rgb_map, global_brightness) self.leds = [Pixel.BLACK] * num_led self.led_order = led_order self._assert_led_order() self.BRIGHTNESS = APA102Cmd.BRIGHTNESS if mosi is None or mosi < 0: # Debug output # Reset leds_seq so the terminal output makes sense. self.led_order = None self.spi = debug.DummySPI(rgb_map) else: import Adafruit_GPIO.SPI as SPI # MOSI 10 and SCLK 11 is hardware SPI, which needs to be set-up differently if mosi == 10 and sclk == 11: self.spi = SPI.SpiDev(bus, device, max_speed_hz) else: import Adafruit_GPIO as GPIO self.spi = SPI.BitBang(GPIO.get_platform_gpio(), sclk, mosi)
def __init__(self, width, height, rst, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=None, i2c_address=SSD1306_I2C_ADDRESS, i2c=None): self._log = logging.getLogger('SOLED.SH1106Base') self._spi = None self._i2c = None self.width = width self.height = height self._pages = height // 8 self._buffer = [0] * (width * self._pages) # Default to platform GPIO if not provided. self._gpio = gpio if self._gpio is None: self._gpio = GPIO.get_platform_gpio() # RESET (RST/RES) Pin setup: self._rst = rst if not self._rst is None: self._gpio.setup(self._rst, GPIO.OUT) # Handle hardware SPI if spi is not None: self._log.debug('Using hardware SPI') self._spi = spi self._spi.set_clock_hz(8000000) # Handle software SPI elif sclk is not None and din is not None and cs is not None: self._log.debug('Using software SPI') self._spi = SPI.BitBang(self._gpio, sclk, din, None, cs) # Handle hardware I2C elif i2c is not None: self._log.debug('Using hardware I2C with custom I2C provider.') self._i2c = i2c.get_i2c_device(i2c_address) else: self._log.debug('Using hardware I2C with platform I2C provider.') import Adafruit_GPIO.I2C as I2C if i2c_bus is None: self._i2c = I2C.get_i2c_device(i2c_address) else: self._i2c = I2C.get_i2c_device(i2c_address, busnum=i2c_bus) # Initialize DC pin if using SPI. if self._spi is not None: if dc is None: raise ValueError('DC pin must be provided when using SPI.') self._dc = dc self._gpio.setup(self._dc, GPIO.OUT)
def __init__(self, tc_type=MAX31856_T_TYPE, avgsel=0x0, software_spi=None, hardware_spi=None, gpio=None): """ Initialize MAX31856 device with software SPI on the specified CLK, CS, and DO pins. Alternatively can specify hardware SPI by sending an SPI.SpiDev device in the spi parameter. Args: tc_type (1-byte Hex): Type of Thermocouple. Choose from class variables of the form MAX31856.MAX31856_X_TYPE. avgsel (1-byte Hex): Type of Averaging. Choose from values in CR0 table of datasheet. Default is single sample. software_spi (dict): Contains the pin assignments for software SPI, as defined below: clk (integer): Pin number for software SPI clk cs (integer): Pin number for software SPI cs do (integer): Pin number for software SPI MISO di (integer): Pin number for software SPI MOSI hardware_spi (SPI.SpiDev): If using hardware SPI, define the connection """ self._logger = logging.getLogger('Adafruit_MAX31856.MAX31856') self._spi = None self.tc_type = tc_type self.avgsel = avgsel # Handle hardware SPI if hardware_spi is not None: self._logger.debug('Using hardware SPI') self._spi = hardware_spi elif software_spi is not None: self._logger.debug('Using software SPI') # Default to platform GPIO if not provided. if gpio is None: gpio = Adafruit_GPIO.get_platform_gpio() self._spi = SPI.BitBang(gpio, software_spi['clk'], software_spi['di'], software_spi['do'], software_spi['cs']) else: raise ValueError( 'Must specify either spi for for hardware SPI or clk, cs, and do for softwrare SPI!' ) self._spi.set_clock_hz(5000000) # According to Wikipedia (on SPI) and MAX31856 Datasheet: # SPI mode 1 corresponds with correct timing, CPOL = 0, CPHA = 1 self._spi.set_mode(1) self._spi.set_bit_order(SPI.MSBFIRST) self.cr1 = ((self.avgsel << 4) + self.tc_type) # Setup for reading continuously with T-Type thermocouple self._write_register(self.MAX31856_REG_WRITE_CR0, self.MAX31856_CR0_READ_CONT) self._write_register(self.MAX31856_REG_WRITE_CR1, self.cr1)
class SSD1306Base(object): """Base class for SSD1306-based OLED displays. Implementors should subclass and provide an implementation for the _initialize function. """ def __init__(self, width, height, rst, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=None, i2c_address=SSD1306_I2C_ADDRESS, i2c=None, mcp_rst=None, mcp_address=None): self._log = logging.getLogger('Adafruit_SSD1306.SSD1306Base') self._spi = None self._i2c = None self.width = width self.height = height self._pages = height//8 self._buffer = [0]*(width*self._pages) # Default to platform GPIO if not provided. self._gpio = gpio # Instantiate MCP if rst pin is in mcp. if mcp_rst is not None and mcp_address is not None: self.__mcp_rst = mcp_rst self.__mcp = MCP.MCP23017(mcp_address, busnum=1) self.__mcp.setup(self.__mcp_rst, MCP.GPIO.OUT) if self._gpio is None: self._gpio = GPIO.get_platform_gpio() # Setup reset pin. self._rst = rst if not self._rst is None and (mcp_rst is None or mcp_address is None): self._gpio.setup(self._rst, GPIO.OUT) # Handle hardware SPI if spi is not None: self._log.debug('Using hardware SPI') self._spi = spi self._spi.set_clock_hz(8000000) # Handle software SPI elif sclk is not None and din is not None and cs is not None: self._log.debug('Using software SPI') self._spi = SPI.BitBang(self._gpio, sclk, din, None, cs) # Handle hardware I2C elif i2c is not None: self._log.debug('Using hardware I2C with custom I2C provider.') self._i2c = i2c.get_i2c_device(i2c_address) else: self._log.debug('Using hardware I2C with platform I2C provider.') if i2c_bus is None: self._i2c = I2C.get_i2c_device(i2c_address) else: self._i2c = I2C.get_i2c_device(i2c_address, busnum=i2c_bus) # Initialize DC pin if using SPI. if self._spi is not None: if dc is None: raise ValueError('DC pin must be provided when using SPI.') self._dc = dc self._gpio.setup(self._dc, GPIO.OUT)
def test_mode_0_write(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) device.write([0x1F]) # Verify clock self.assertListEqual(gpio.pin_written[1], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]) # Verify MOSI self.assertListEqual(gpio.pin_written[2], [0, 0, 0, 1, 1, 1, 1, 1]) # Verify MISO self.assertNotIn(3, gpio.pin_written) # Verify SS self.assertListEqual(gpio.pin_written[4], [1, 0, 1])
def get_spi_device(device, max_speed_hz=4000000): """Return a device to use with adafruit bus""" self.spi_acquire() try: return SPI.BitBang(self._ada_gpio, self.values["%s_pin_clk" % OID].data, self.values["%s_pin_mosi" % OID].data, self.values["%s_pin_miso" % OID].data) except Exception: logger.exception('[%s] - Exception when getting device', self.__class__.__name__) finally: self.spi_release()
def test_mode_0_read(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) gpio.pin_read[3] = [0, 0, 0, 1, 1, 1, 1, 1] result = device.read(1) # Verify clock self.assertListEqual(gpio.pin_written[1], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]) # Verify MOSI self.assertNotIn(2, gpio.pin_written) # Verify MISO self.assertNotIn(3, gpio.pin_written) # Verify SS self.assertListEqual(gpio.pin_written[4], [1, 0, 1]) # Verify result self.assertEqual(result, bytearray([0x1F]))
def __init__(self, width, height, rst, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=I2C.get_default_bus(), i2c_address=SSD1306_I2C_ADDRESS): self._log = logging.getLogger('Adafruit_SSD1306.SSD1306Base') self._spi = None self._i2c = None self.width = width self.height = height self._pages = height / 8 self._buffer = [0] * (width * self._pages) # Default to platform GPIO if not provided. self._gpio = gpio if gpio is not None else GPIO.get_platform_gpio() # Setup reset pin. self._rst = rst self._gpio.setup(self._rst, GPIO.OUT) # Handle hardware SPI if spi is not None: self._log.debug('Using hardware SPI') self._spi = spi # Handle software SPI elif sclk is not None and din is not None and cs is not None: self._log.debug('Using software SPI') self._spi = SPI.BitBang(self._gpio, sclk, din, None, cs) # Handle hardware I2C elif i2c_bus is not None: self._log.debug('Using hardware I2C') self._i2c = I2C.Device(i2c_address, i2c_bus) else: raise ValueError('Unable to determine if using SPI or I2C.') # Initialize DC pin if using SPI. if self._spi is not None: if dc is None: raise ValueError('DC pin must be provided when using SPI.') self._dc = dc self._gpio.setup(self._dc, GPIO.OUT)
def __init__(self, dc, rst, sclk=None, din=None, cs=None, gpio=None, spi=None): self._sclk = sclk self._din = din self._dc = dc self._cs = cs self._rst = rst self._gpio = gpio self._spi = spi # Default to detecting platform GPIO. if self._gpio is None: self._gpio = GPIO.get_platform_gpio() if self._rst is not None: self._gpio.setup(self._rst, GPIO.OUT) # Default to bit bang SPI. if self._spi is None: self._spi = SPI.BitBang(self._gpio, self._sclk, self._din, None, self._cs) # Set pin outputs. self._gpio.setup(self._dc, GPIO.OUT) # Initialize buffer to Adafruit logo. self._buffer = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFF, 0xFC, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, 0xC0, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x87, 0x8F, 0x9F, 0x9F, 0xFF, 0xFF, 0xFF, 0xC1, 0xC0, 0xE0, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xE0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF1, 0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x87, 0xE7, 0xFF, 0xFF, 0xFF, 0x1F, 0x1F, 0x3F, 0xF9, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x7E, 0x3F, 0x3F, 0x0F, 0x1F, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xE0, 0xF1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x1F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x1F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]
def __init__(self, clk=None, cs=None, do=None, units="c", spi=None, gpio=None): self._spi = None self.units = units # Handle hardware SPI if spi is not None: self._spi = spi elif clk is not None and cs is not None and do is not None: if gpio is None: gpio = GPIO.RPiGPIOAdapter(RPi.GPIO) self._spi = SPI.BitBang(gpio, clk, None, do, cs) else: raise ValueError('Must specify either spi') self._spi.set_clock_hz(5000000) self._spi.set_mode(0) self._spi.set_bit_order(SPI.MSBFIRST)
def __init__(self, clk=None, cs=None, do=None, spi=None, gpio=None): """Initialize MAX31855 device with software SPI on the specified CLK, CS, and DO pins. Alternatively can specify hardware SPI by sending an Adafruit_GPIO.SPI.SpiDev device in the spi parameter. """ self._logger = logging.getLogger('Adafruit_MAX31855.MAX31855') self._spi = None # Default to platform GPIO if not provided. self._gpio = gpio if gpio is not None else GPIO.get_platform_gpio() # Handle hardware SPI if spi is not None: self._logger.debug('Using hardware SPI') self._spi = spi elif clk is not None and cs is not None and do is not None: self._logger.debug('Using software SPI') self._spi = SPI.BitBang(self._gpio, clk, None, do, cs) else: raise ValueError( 'Must specify either spi for for hardware SPI or clk, cs, and do for softwrare SPI!' ) self._spi.set_mode(0) self._spi.set_bit_order(SPI.MSBFIRST)
def __init__(self, clk=None, cs=None, miso=None, mosi=None, spi=None, gpio=None): self._spi = None if spi is not None: self._spi = spi elif clk is not None and cs is not None and miso is not None and mosi is not None: if gpio is None: gpio = GPIO.get_platform_gpio() self._spi = SPI.BitBang(gpio, clk, mosi, miso, cs) else: raise ValueError( 'Must specify either spi for for hardware SPI or clk, cs, miso, and mosi for softwrare SPI!' ) self._spi.set_clock_hz(10000000) self._spi.set_mode(0) self._spi.set_bit_order(SPI.MSBFIRST)
def __init__(self, clk=None, cs=None, miso=None, mosi=None, spi=None, gpio=None, bits=10): """Initialize software SPI on the specified CLK, CS, and DO pins. Alternatively can specify hardware SPI by sending an Adafruit_GPIO.SPI.SpiDev device in the spi parameter. """ self._spi = None # Handle hardware SPI if spi is not None: self._spi = spi elif clk is not None and cs is not None and miso is not None and mosi is not None: # Default to platform GPIO if not provided. if gpio is None: gpio = GPIO.get_platform_gpio() self._spi = SPI.BitBang(gpio, clk, mosi, miso, cs) else: raise ValueError( 'Must specify either spi for for hardware SPI or clk, cs, miso, and mosi for softwrare SPI!' ) self._spi.set_clock_hz(1000000) self._spi.set_mode(0) self._spi.set_bit_order(SPI.MSBFIRST) # 10 bit (MCP3008) or 12 bit (MCP3208) resolution if (bits == 10): self.parseMCPResponse = self.parse10bit elif (bits == 12): self.parseMCPResponse = self.parse12bit else: print(' !!! MCP3x08: %i bits not supported' % (bits))
SAMPLING_RATE = float(sys.argv[4]) / 1000 REPORTING_RATE = float(sys.argv[5]) / 1000 defaultSPI = True if (len(sys.argv) > 9): SPI_CLK = int(sys.argv[6]) SPI_DI = int(sys.argv[7]) SPI_DO = int(sys.argv[8]) SPI_CS = int(sys.argv[9]) defaultSPI = False if defaultSPI: spi = SPI.SpiDev(SPI_PORT, SPI_DEVICE) else: gpio = GPIO.get_platform_gpio() spi = SPI.BitBang(gpio, SPI_CLK, SPI_DI, SPI_DO, SPI_CS) values = [] last_report = 0 def sampledData(value): global values if not math.isnan(value): values.append(value) def report(): global values, last_report if (time() - last_report > REPORTING_RATE): last_report = time() if len(values) > 0: print('{0:0.3F},{1},{2:0.3F}'.format(time(),CHANNEL, sum(values)/len(values) ))
def test_invalid_bit_order_fails(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) self.assertRaises(ValueError, device.set_bit_order, -1) self.assertRaises(ValueError, device.set_bit_order, 2)
def test_write_lsbfirst(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) device.set_bit_order(SPI.LSBFIRST) device.write([0x1F]) self.assertListEqual(gpio.pin_written[2], [1, 1, 1, 1, 1, 0, 0, 0])
def test_write_assert_deassert_ss_false(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) device.write([0x1F], assert_ss=False, deassert_ss=False) self.assertListEqual(gpio.pin_written[4], [1])
def test_ss_set_high_after_initialization(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) self.assertListEqual(gpio.pin_written[4], [1])
def __init__(self, sclk, mosi=None, miso=None, ss=None): self.bb = SPI.BitBang(gpio, sclk, mosi, miso, ss) self.bb.set_bit_order(SPI.MSBFIRST) self.bb.set_mode(2) self.bb.transfer(CONFIGURATION_COMMAND) #determine start conf self.read_length = 2
def test_read_assert_deassert_ss_false(self): gpio = MockGPIO() device = SPI.BitBang(gpio, 1, 2, 3, 4) gpio.pin_read[3] = [0, 0, 0, 1, 1, 1, 1, 1] result = device.read(1, assert_ss=False, deassert_ss=False) self.assertListEqual(gpio.pin_written[4], [1])
def __init__(self,clk=None, cs=None, miso=None, mosi=None): gpio = GPIO.get_platform_gpio() self.spi = SPI.BitBang(gpio, clk, mosi, miso, cs) self.spi.set_clock_hz(1000000) self.spi.set_mode(0) self.spi.set_bit_order(SPI.MSBFIRST)
def __init__(self, dac_model, clk=None, cs=None, do=None, spi=None, gpio=None): """The constructor for the AD56x8 class. Args: AD56x8 DAC Specific Args dac_model (str): command constant Adafruit-GPIO Specific Args clk (int): DAC value to selected channel cs (str, int): Platform specific pin selection for CS do (str, int): Platform specific pin selection for DO spi (str, int): Platform specific pin selection for CS gpio (str, int): Platform specific selection for platform GPIO Attributes: DATA_WIDTH (int): Width of DAC value in bits VREF (float): Internal VREF voltage PU_DAC_MULT (float): Default power-up DAC output value Attributes are set for the specific DAC model upon construction, which are useful for calculating the DAC value to write for a desired output voltage Since this component does not have a MISO/DOUT line, it isn't possible to determine which/any commands have been received. Use a loopback to a ADC, or get a better part. """ # Set DAC attributes specific to called model if dac_model in AD56x8_MODEL_PARAMS.keys(): setattr(self, 'device', dac_model) for key, value in AD56x8_MODEL_PARAMS[dac_model].items(): setattr(self, key, value) else: raise ValueError('AD56x8: DAC model specified not in known set') # Initialize device with software SPI on the specified CLK, # CS, and DO pins. Alternatively can specify hardware SPI by sending # an Adafruit_GPIO.SPI.SpiDev device in the spi parameter. self._spi = None # Handle hardware SPI if spi is not None: self._spi = spi elif clk is not None and cs is not None and do is not None: # Default to platform GPIO if not provided. if gpio is None: gpio = GPIO.get_platform_gpio() self._spi = SPI.BitBang(gpio, clk, do, None, cs) else: raise ValueError('Must specify either hardware spi or clk, \ cs, and do for software SPI!') # SPI Configurations self._spi.set_clock_hz(5000000) self._spi.set_mode(0) self._spi.set_bit_order(SPI.MSBFIRST)
""" test lora with spi bit-bang """ from time import sleep import Adafruit_GPIO as GPIO import Adafruit_GPIO.SPI as SPI import RPi.GPIO from utils import bytes_to_hex_str SCLK = 23 MOSI = 19 MISO = 21 SS = 24 gpio = GPIO.get_platform_gpio(mode=RPi.GPIO.BOARD) device = SPI.BitBang(gpio=gpio, sclk=SCLK, mosi=MOSI, miso=MISO, ss=SS) # reset all registers to default # revision, ret 0x12 res = device.transfer([0x42, 0]) res = bytes_to_hex_str(res) print('res {}'.format(res)) # test, ret 0x2d res = device.transfer([0x44, 0]) res = bytes_to_hex_str(res) print('res {}'.format(res))
def __init__(self, dc, rst, sclk=None, din=None, cs=None, gpio=None, spi=None, vcc=None, bl=None): self._sclk = sclk self._din = din self._dc = dc self._cs = cs self._rst = rst self._gpio = gpio self._spi = spi self._buffer = [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFF, 0xFC, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, 0xC0, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x87, 0x8F, 0x9F, 0x9F, 0xFF, 0xFF, 0xFF, 0xC1, 0xC0, 0xE0, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xE0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF1, 0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x87, 0xE7, 0xFF, 0xFF, 0xFF, 0x1F, 0x1F, 0x3F, 0xF9, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x7E, 0x3F, 0x3F, 0x0F, 0x1F, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xE0, 0xF1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x1F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x1F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] if self._gpio is None: self._gpio = GPIO.get_platform_gpio() self._gpio.setup(vcc, GPIO.OUT) self._gpio.setup(bl, GPIO.OUT) self._gpio.output(vcc, 1) self._gpio.output(bl, 1) if self._rst is not None: self._gpio.setup(self._rst, GPIO.OUT) # Default to bit bang SPI. if self._spi is None: self._spi = SPI.BitBang(self._gpio, self._sclk, self._din, None, self._cs) # Set pin outputs. self._gpio.setup(self._dc, GPIO.OUT)