Connect to the Devices
We jump into the cold water and start with our first program. We plugged in our development kit via USB and want to connect to the devices from our first program.
Last updated
Was this helpful?
We jump into the cold water and start with our first program. We plugged in our development kit via USB and want to connect to the devices from our first program.
Last updated
Was this helpful?
Learn how to connect to devices on your USB port from a JavaScript program.
In this first lesson, we want to learn how we write a program to connect to the devices from our . Once connected, we set the color of the RGB LED to green. Along the way, we introduce important concepts in programming:
The code snippet below contains the JavaScript code for the first code example. You can . Don't panic, it looks like a lot. But don't worry, for the rest of this lesson, we'll go through the code step by step and explain everything in detail.
One of these functions is initDevices()
, which tells the TDM to start the initialization process and get a connection to all devices at the computer's USB port. The TDM know how to do this, so we don't have to deal with the details how this is done.
We only need to decide when we want to call this function. In the example, we use a button named 'Establish Connection' as the trigger to execute the function. As an argument, we pass a reference to a function initDone()
, which we define starting in line 21. This function serves as a so-called callback function, and it will be called by the TDM when the initialization process finishes. This is the way the TDM tells us he is finished initializing and hands over the result.
The result of the initialization is a list of references to the connected devices, and the TDM passes this list as the single argument of the callback function. Via this argument, which we named newDevices
, we get access to all devices and their functions. To make sure we can access the devices from anywhere in our program, and not just from the initDone()
function, we store the argument on the global variable devices
that we defined earlier.