This is part 4 of a multipart series showing you how to get started with the MetaWear platform. View the contents of the series to easily skip forwards or backwards
About Modules
Now that we have established a connection to the board, we can begin interacting with modules.
Modules are on-board sensors or features supported by the firmware.
We do this by calling MetaWearBoard.getModule
.
Let’s start with a simple example, allowing us to turn on the LED lights on the board.
First, we’ll add two buttons underneath our connect button: LED on and LED off. Add the following to activity_my.xml:
Then in our MyActivity.java file let’s import the LED module and declare the object. We’ll also import UnsupportedModuleException
for our error handling:
Then inside the connected
section of our ConnectionStateHandler
we define the module using getModule
. Note that this is wrapped in a try/catch
statement. If we tried defining the ledModule somewhere else, such as in onCreate
, it would cause an error as at that point board connection handling may not be complete:
The last thing we need to do is add the onClick
handlers, also within the connected()
code block. To quote the docs:
LED patterns function as a pulse with four main parameters to modify: rise time, high time, fall time, and duration. These parameters are modified using the ColorChannelEditor and each color channel (rgb) is configured independent of the other colors allowing you to program different patterns for each color.
So we use ColorChannelEditor
inside our onClick
and then commit the result first, then we play them:
To turn the LED off, it’s just one line of code within our second onClick
:
Go ahead and run the app - you should see the LED turn on and off when you press the LED on and LED off buttons. Make sure you connect to the board first! You always need to connect to the board before you can do anything with it. Now we’re really making progress. Spend some time playing with different settings for the LED duration, intensity and color.
In the next section we will look at streaming data.
Note You can view all these changes in the github repository on the branch version-0.3