How to Connect and Control an Arduino Board with Cascades

IMG_20140228_094623Hey, here is @leaschnherr and today in the context of mobile computing platform I want to explain to you how can use the USB host mode of a Z30 device to control an Arduino Uno Board. To keep it simple, my aim was just to manipulate the intensity of an external LED connected to a GPIO.

The code running on the Arduino can be used unchanged from the Dimmer example of Arduino, which you can find here with helpful explanation.

If you work with an Arduino for the first time, read the introduction at the website here. Be sure the program is running correctly and try out some examples to get used to the environment.

The Code for the BlackBerry 10 App  is a little more complex and in the first part I only want to explain, how to recognize and handle a connected USB device:

After creating a new Cascades project the first thing which has to be done is to add the Peripheral Discovery Library. In the Momentics environment just click right on the Project folder. Then choose configure->Add Library->Standard BlackBerry platform library and click ‘next’. Search for libperipheral_discovery, select it and click ‘finish’. The Library is now added automatically to the config.pri file, which gets included to the .pro file.

In the first part no GUI is needed, so you don’t have to care about it during the recognition and I’m going to explain it in the second part. The only thing which has to be done is to create an instance of a class which I called Disvover. Here is the constructor of this class:

The function bps_initialize() has to be called as once you need any services from the BPS layer. For our project we need with the Peripheral Discovery Service. Then we need to initialize the Peripheral Discovery Service themselves by calling pd_initialize(0). Then to detect connection and disconnection of the Arduino board, we need to register for the m_sigEvent and a corresponding handler (signalHandler()) via the bps_add_sigevent_handler() function. signalHandler() is now called each time, when defined events occur.

After that it is necessary to check, if the USB host mode is supported:

This could be done with a simple function called  pd_is_bus_supported()The app will run on other devices, too, but it won’t be possible to control an external USB device. If the request was successful, and the Blackberry 10 Device supports the USB host mode, the next step is to register the necessary events:

Before any method with pd_class_t as parameter is used, the function pd_is_class_supported() should be called.  In this case with PD_CLASS_SERIAL as Parameter, this is for serial communication. When this call doesn’t return an error, we go ahead and register m_sigEvent with PD_CLASS_SERIAL. Now every time, when a USB device is connected or disconnected to the Blackberry 10 device,  signalHandler() is called (see constructor):

Because the signalHandler() function has to be a static and doesn’t allow to use non-static variables, we simply use a function pointer to another function, which checks for us, whether the Arduino was connected or disconnected:

In case of a connection, a peripheral is allocated with the detected pd_peripheral_t as Parameter (For further explanation see next section). In the other case, when the Arduino was disconnected, the pd_free_peripheral() method is called and other preparations for the next connection are made (the variable m_serial is set to -1 , which is required for communication).

Our peripheral is saved as class Peripheral. This simple class contains the path, which is a property of  pd_peripheral_t itself. Later it is possible to use this class for further information. In the constructor the path is read out by a couple of constructions, which are necessary to filter the right property:

The path is one property in a long list of properties. So the first thing to do is to fetch the whole list. Then we look at each property and use the type as filter: Because our currently path the only string we will know we have the right property. Of course good programmers would check the string for example if it includes “/dev” :-)

Now it will be possible, to connect and recognize the Ardunio. How we use this interface to send instructions to the Arduino is explained in part II.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">