|Top Previous Next|
Let's put all the stuff from the previous pages together and actually develop a transport class! We'll build upon the Promixis PIO-1 hardware. This is a network connected multi purpose input output device. For example it has 4 IR outputs that you can use to control a TV, DVD player, cable box or anything using an IR remote. On top of that it has 3 relay outputs for things like door openers and automatic locks. The PIO-1 also has 2 serial input/outputs.
You can find the API documentation here.
First off we see that the PIO-1 employs an easy to use text based interface terminated with an end line character "\n". Next we see that the PIO-1 requires a password to be sent before it allows you to change anything. But first things first. Let's create a lua file in the examples folder called "pio1a.lua". We'll create this new file using the class structure of Lua.
The code above will provide the framework where all the functionality will be placed. Note that the code above doesn't deal with the transport functionality yet. All it does is store the address and port information and provide the class framework. Now let's add the connect function. It will create a connection object and setup it's parser and callback.
Place this code above the init function. The first line we should look at is the transport.new line. This create a TCP connection object connecting to address self.address and port self.port. It also supplies the callback for the connection events. This will allow use to handle connecting, disconnect and connect failure events.
To actually use this code we'll create a script action on the Girder tree with this code:
Running this if the IP address is correct will print a single lone "0". Looking up the status codes we find this means Promixis.Transport.IConnectionCallback.Status.CONNECTION_ESTABLISHED. So great! We have a connected object. Now we'll need to send some data over the link. This is done using transactions.
Let's implement the sendccf command of the PIO-1. The API states this to be sendccf bitmask,repeats,ccf\n Easy enough! The PIO-1 will respond with ok if the command is accepted and irdone when the IR code has been completed.
Easy as that! It's pretty much the same code as before with one significant change. When the PIO-1 returns "ok" to signal acceptance of IR code the transaction timeout is reset by returning TXR.TX_RESET_TIMEOUT.
To call this code simply add this to a lua scripting action.
The PIR-1 supports asynchronous notifications of incoming IR codes or state changes on the digital pins. Let's add a callback for the IR code notification.
This again looks similar to the previous functions. However we now do not need to set the timeout nor the data to send and set persistent to true. You can also see two new transaction return constants. TX_KEEP and TX_CONTINUE. TX_KEEP means keep the transaction in the list but do not pass the data to other transactions. TX_CONTINUE means keep the transaction yet pass the data to other transactions as well.
To use this code modify your startup lua action as follows:
The full source
You can find the latest version of the full source code in the Lua\pio1\init.lua file. Below is a copy of the file with automatic reconnecting and a few more useful functions.