Message Boards Message Boards

0
|
7872 Views
|
1 Reply
|
2 Total Likes
View groups...
Share
Share this post:

Controlling PiFace Extension-board using Mathematica

Posted 11 years ago
Hello everyone,

I have just bought the PiFace extension-board that can be placed ontop the Raspberry Pi GPIO. I have with sucess used Mathematica on the GPIO pins, and python to control the PiFace extension-board (separetely).

My question is this, whether anyone has succesfully used Mathematica directly with the PiFace? Or, if anyone (my technical expertise is limited) could tell me whether it would be possible at all?

I have tried looking for devices in Mathematica, and here GPIO shows up, but not my pifacedigitalio.

Hope someone can point me in the right direction.

Best Regards

Simon
POSTED BY: Simon P.
Posted 11 years ago
Hello again everyone,

Sorry for bumping my own post, however...

Thanks to inspiration I got from Diego Zviovich post on Sous-vide http://community.wolfram.com/groups/-/m/t/170725, it seems only natural to make a MathLink program to control the PiFace.

So, since it is snowing outside, and I have a cup of coffee in my hand, I have taken the liberty to write some code which, at least for my own basic needs, does the trick.

Initially I downloaded and installed the C-library by Thomas Macpherson, at  https://github.com/thomasmacpherson/piface. I here assume that you have succesfully installed your own PiFace and can test it with the accompanying emulator.

Firstly, I wrote the code in C, required for the MathLink. I wrote:
 #include "mathlink.h"
 #include "libpiface-1.0/pfio.h"
 
 int initializePFIO(void)
 {
     return pfio_init();
 }
 
 int deinitializePFIO(void)
{
    return pfio_deinit();
}

int leds(int led, int onOff)
{
    pfio_digital_write(led,onOff);
    return 0;
}

int switches(int pin)
{
    return pfio_digital_read(pin);
}

int relays(int relay, int onOff)
{
    pfio_digital_write(relay,onOff);
    return 0;
}

int main(int argc, char* argv[])
{
    return MLMain(argc,argv);
}
 :Begin:
 :Function: leds
 :Pattern: Leds[led_Integer, onOff_Integer]
 :Arguments: { led, onOff }
 :ArgumentTypes: { Integer, Integer }
 :ReturnType: Integer
 :End:
 :Evaluate: Leds::usage = "Leds[led_Integer, onOff_Integer] turns on/off (1/0) led x on PiFace."
 
:Begin:
:Function: initializePFIO
:Pattern: InitializePFIO[]
:Arguments: Manual
:ArgumentTypes: Manual
:ReturnType: Integer
:End:
:Evaluate: Initialize::usage = "Initialize[] initialize PiFace I/O."

:Begin:
:Function: deinitializePFIO
:Pattern: DeinitializePFIO[]
:Arguments: Manual
:ArgumentTypes: Manual
:ReturnType: Integer
:End:
:Evaluate: Initialize::usage = "Deinitialize[] deinitialize PiFace I/O."

:Begin:
:Function: relays
:Pattern: Relays[relay_Integer, onOff_Integer]
:Arguments: { relay , onOff }
:ArgumentTypes: { Integer , Integer}
:ReturnType: Integer
:End:
:Evaluate: Initialize::usage = "Relays[relay_Integer , onOff_Integer] turns on/off (1/0), relay 0 or 1 on the PiFace I/O."

:Begin:
:Function: switches
:Pattern: Switches[pin_Integer]
:Arguments: { pin }
:ArgumentTypes: { Integer }
:ReturnType: Integer
:End:
:Evaluate: Initialize::usage = "Switches[pin_Integer] reads the state of input pin x on the PiFace I/O."

I wrap this up using the mcc command, and linking the libpiface-1.0 library appropriately. Note: Dont forget that these are the paths I used, yours might be different.
./mcc -L/usr/local/lib -lpiface-1.0 -o piface_mathematica.exe piface_mathematica.tm piface_mathematica.c

This creates the required executable, which can be made available to Mathematica:

Install["/home/pi/Desktop/CProgramming/piface_mathematica.exe"]

Now it is possible to control, relays, switches and LEDS (shoudl be able to communicate with input/output pins also). As an example, the following code turns on and off the PiFace LEDS in ascending and descending sequence at 1 second intervals.
 (*Initialize PiFace Digital IO*)
 InitializePFIO[]
 
 (*Execute the LED light sequence*)
 For[i = 0, i < 8, i++, Leds[i, 1]; Pause[1];
  If[i == 7, For[i = 7, i > -1, i--, Leds[i, 0]; Pause[1];] ]]
 
 (*Turn off all LEDS*)
 Leds[#, 0] & /@ Range[0, 7]

(*Close the link*)
DeinitializePFIO[]

Hope someone can use it. Please let me know if you have suggestions/corrections to the code. 

Best Regards
Simon
POSTED BY: Simon P.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract