HomeGenie Forum

Automation Program Plugins and Wizard Scripting => Raspberry Pi GPIO/SPI/I2C => Topic started by: NickP on January 05, 2015, 03:09:11 AM

Title: PiFace Digital Driver
Post by: NickP on January 05, 2015, 03:09:11 AM
Hello

I started looking into obtaining or building a driver for Piface; below is my train of thought and am hoping somebody point me in the right direction or correct me if I have something wrong.


I looked at several posts about Piface and MCP23017 but they reference i2c and unless I am completely off track PiFace uses SPI; I have used Kingsland.PiFaceSharp successfully and it uses SPI.

HomeGenie uses raspberry sharp which supports i2c and spi but only has an extender for MCP23017 using I2C. So I wrote an equivalent to the Mcp23017I2cConnection class but using SPI connection i.e. Mcp23017SpiConnection.

How do I get my class into HomeGenie? I could rebuild raspberry sharp and overwrite the copy on my PI. Would I then have to rebuild HomeGenie or will it find the new class dynamically?

Assuming I get the class available to programs under HomeGenie. It should be a simple change to the existing MCP23017 program to switch to my SPI connection (which has all the same function calls as the I2C one). How do you develop and test those add in programs, they don't seem to be complete classes that I can paste into Visual Studio?

Thanks in advance
Nick
Title: Re: PiFace Digital Driver
Post by: Gene on January 05, 2015, 03:19:55 AM
If you already wrote the class for RaspberrySharp, you can attach it here so I will include it in the RaspberrySharp fork used for HG.
I could also push to the master RaspberrySharp repo. If you're familiar with github, suggest create your own fork as push it to the master repo.

But if you didn't write this yet, I suggest trying to implement it as a simple csharp automation program inside the HG program editor.
To see an example app using a SPI connection you can import and look the source code of the EdenV2_Module_1_1.hgx that is available for download here:

https://sourceforge.net/projects/homegenie/files/testing/

For importing it into HG use the "Import Program" option available from the Actions menu when inside any automation section group.

Cheers,
g.

Title: Re: PiFace Digital Driver
Post by: Gene on January 05, 2015, 03:31:44 AM
To answer to your last two question as well:

- if you update raspberrysharp libraries, implemented classes will be available to automation programs as well, but you will have to reference them with complete namespace as long these are not referenced here:
https://github.com/genielabs/HomeGenie/blob/master/HomeGenie/Automation/CSharpAppFactory.cs#L50

- hg csharp programs are not classes and you can't add classes there, they are most likely code snippet (see the hgx example that I pointed in the previous post), you can't use Visual Studio to develop them, but HG program editor.

g.
Title: Re: PiFace Digital Driver
Post by: NickP on January 08, 2015, 03:37:00 AM
Thanks.

I will test the class with a standalone program then upload it here.

Nick
Title: Re: PiFace Digital Driver
Post by: NickP on January 19, 2015, 11:50:14 PM
Attached is a solution with a PiFace Digital SPI driver, and a standalone demo app.
I had a go at building a new version of HomeGenie for the Pi with the PiFace dll but couldn't get the packager to work; vs2012 says "Unsupported...". I tried  a few things and ended up having to restore my Pi  :(

If you could add the dll or point me in the right direction to build a new Pi release that would be great.

Nick
Title: Re: PiFace Digital Driver
Post by: NickP on February 11, 2015, 12:48:56 AM
Trying to figure out how to build the Linux version of HG. I can build the windows version with VS; do I need to build the Linux version on Linux?
Title: Re: PiFace Digital Driver
Post by: bkenobi on February 11, 2015, 06:25:02 PM
When I have used the source to build HG, I was just modifying segments in one package or another.  As a result, I simply copied the different compiled file over to my Raspi installation path.  That approach worked fine.  I don't know how to create a distributable installation.
Title: Re: PiFace Digital Driver
Post by: Gene on February 19, 2015, 08:40:13 PM
Hi NickP,

please fork the HG RaspberrySharp-IO repository:

https://github.com/genielabs/raspberry-sharp-io

add the code there and make a pull request. That way it will be included in HG.

Cheers,
g.
Title: Re: PiFace Digital Driver
Post by: NickP on February 28, 2015, 09:54:28 PM
Pull request created :)
Title: Re: PiFace Digital Driver
Post by: Gene on March 01, 2015, 04:40:19 PM
PiFace Digital support is now included in HG r482.

http://www.homegenie.it/download.php (http://www.homegenie.it/download.php)

It could be useful to have an automation program using it (eg. virtual modules).

Cheers,
g.
Title: Re: PiFace Digital Driver
Post by: NickP on March 12, 2015, 02:54:06 AM
I'm working on one  :)
Title: Re: PiFace Digital Driver
Post by: enterprised on March 14, 2015, 06:08:00 PM
NickP,
Can you give me some pointers on how to get my Piface Digital to work with HG.

Title: Re: PiFace Digital Driver
Post by: NickP on March 16, 2015, 03:57:44 AM
Haven't been able to test yet as my PiFace has been loaned. With the latest build I think this is the basic script to manage polling the inputs. Next step is to implement the WebServiceCallReceived functionality to change the outputs. I will get to it later this week.

Nick

Code: [Select]
var moduleDomain = "Components.PiFaceDigital";

// This examples will add 16 modules of type "Switch"
// corresponding to piFace Input and Output pins
//

 var piFace = new PiFaceDigitalDevice();


var  ip_OnStateChanged = new InputPinChangedHandler ( (object sender, InputPinChangedArgs e)   =>
  {
    var moduleName = "Input" + e.pin.Id;
    if (e.pin.State)
      {
      Modules.InDomain(moduleDomain).WithName(moduleName).On();
    }
    else
    {
      Modules.InDomain(moduleDomain).WithName(moduleName).Off();
    }
  }
);

When.ProgramStopping(()=>{
  ((IDisposable)piFace).Dispose();
  return true;
});

foreach (var inputPin in piFace.InputPins)
{
  inputPin.OnStateChanged += ip_OnStateChanged;
  Program.AddVirtualModule(moduleDomain, "Input" + inputPin.Id, "Sensor", "homegenie/generic/sensor");   
}

foreach (var outputPin in piFace.OutputPins)
{
  Program.AddVirtualModule(moduleDomain, "Output" + outputPin.Id, "Switch", "");   
}


// status polling loop
while (Program.IsEnabled)
{
  Pause(0.5); // 500 ms poll resolution
  //
  piFace.PollInputPins();
}

Title: Re: PiFace Digital Driver
Post by: NickP on March 17, 2015, 03:20:00 AM
HGX - adds modules for the piFace inputs and outputs.
Note that you must enable native SPI as per the PiFace documentation. Input pin are considered on when grounded / buttons pushed.
Title: Re: PiFace Digital Driver
Post by: enterprised on March 17, 2015, 04:11:43 AM
Thanks mate, works like a charm on my PiFace Digital 2
Title: Re: PiFace Digital Driver
Post by: roelloaded on March 26, 2016, 04:57:55 PM
NickP Thanks for your work I've got to also test on Piface Relays Plus (of course only the control of the output )