Understanding SPI library ; RPI2

Discussion in 'Programmer's Corner' started by Vindhyachal Takniki, Jul 7, 2015.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    1. I am using RPi2 with raspbian loaded. I am using SPI library from here:
    http://www.raspberrypi-spy.co.uk/2013/10/analogue-sensors-on-the-raspberry-pi-using-an-mcp3008/

    Also .c file I have attached.

    2. Problem is I don't understand how to call functions here.
    e.g This is how tx/rx function is defined:

    static PyObject *SpiDev_xfer2(SpiDevObject *self, PyObject *args)

    It has two struct arguments. But when function is called, it is called as:

    adc = spi.xfer2([1,2,3])

    where argument contains list of values you want to tx & it reurns valye rx.
     
  2. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,908
    2,169
    Is this a Python programming question or a SPI transfer question?
     
  3. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    python programming question
     
  4. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    The standard way of writing this code is to call the instance of the class directly, which is accomplished using the self word. This is how it is done in your example of calling the function:
    Code (Text):
    1. adc = spi.xfer2([1,2,3])
    where there are actually two arguments being passed to the function, not just one, even thought it may not look like it. The first argument is the spi instance of the class.

    A logically equivalent (but non-standard and therefore not recommended) version of this statement is:
    Code (Text):
    1. adc = SpiDev_xfer2.xfer2(spi, [1,2,3])
    where the class (NOT a specific instance of it) is being called, and the first argument passed is again an instance of it. Here you can clearly see that there are two arguments being passed.

    I would recommend that you study how OOP works in Python.
     
    Last edited: Jul 7, 2015
    Vindhyachal Takniki likes this.
  5. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
Loading...