Understanding SPI library ; RPI2

Thread Starter

Vindhyachal Takniki

Joined Nov 3, 2014
594
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.
 

Attachments

tjohnson

Joined Dec 23, 2014
611
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.
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:
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:
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:
Top