LiBRIX₂

The LiBRIX₂ is a bundle of software libraries which makes handling the BRIX₂ hardware much easier for you. Here's an example:

If we want to blink the RGB LED in pink without the LiBRIX₂, this is what we have to do:

int blue = 13;
int red = 10;

void setup() {                
  pinMode(red, OUTPUT);    
  pinMode(blue, OUTPUT);    
}

void loop() {
  digitalWrite(red, HIGH); 
  digitalWrite(blue, HIGH); 
  delay(500);             
  digitalWrite(red, LOW); 
  digitalWrite(blue, LOW); 
  delay(500);            
}

Now WITH LiBRIX₂, the same result is achieved with this code:

#include <BRIX2.h>
BRIX2 myBrick;

void setup() {                
  myBrick.initialize();
}

void loop() {
  myBrick.setRGB(255,0,255);
  delay(500);             
  myBrick.setRGB(0,0,0); 
  delay(500);            
}

Using LiBRIX2 Step by Step

So what you do in general if you want to use any of the features of the BRIX₂ system, you include the LiBRIX₂:

#include <BRIX2.h>

void setup(){

}
void loop(){

}

After that, you create an object of the type BRIX2 and pick a name for it. That object will be your interface. Basically, this virtual object represents the actual physical BRIX₂ module and you can tell it, what to do.

#include <BRIX2.h>
BRIX2 myBrick;

void setup(){

}
void loop(){

}

The first thing that you always have to do is initialize it. Just call the BRIX2 object by it's name and tell it to initialize:

#include <BRIX2.h>
BRIX2 myBrick;

void setup(){
  myBrick.initialize();
}
void loop(){

}

After that, the BRIX2 object is ready for your commands. For example:

#include <BRIX2.h>
BRIX2 myBrick;

void setup(){
  myBrick.initialize();
  Serial1.begin(38400);
}
void loop(){
  myBrick.setHSV(25,255,255); //turn the LED orange
  int address = 0x0A; // broadcast
  char testString[45] = "The quick brown fox jumps over the lazy dog\n";
  int length = 44;
  myBrick.sendAscii(address, testString, length); // broadcast the testString via the wireless transceiver
  float voltage = myBrick.getVoltage(); //read out the current battery voltage of the BRIX₂ module
  delay(100);
}

Function overview

BRIX2.initialize()

Necessary for initialization of the module, only once.

System

The system functions can be used to configure and control aspects of the BRIX₂ Base module during runtime, for example the LEDs or the RF interface. Most of these aspects are handled by the System Controller, the secondary microcontroller inside the Base module. If the system functions are executed on the User Controller, it communicates with the System Controller which then acts accordingly, for example by reporting back the battery voltage or shutting off the status LED. Only the control functions for the RGB LED run exclusively on the User Controller.

BRIX2.setRGB(unsigned int Red, unsigned int Green, unsigned int Blue)

Takes three numbers between 0 and 255 to set each color channel of the RGB LED individually. See also https://en.wikipedia.org/wiki/Rgb

BRIX2.setHSV(unsigned int hue, unsigned int sat, unsigned int val)

Takes three numbers, hue between 0 and 360, sat and val between 0 and 255. See also https://en.wikipedia.org/wiki/HSV_color_space

BRIX2.getVoltage()

Returns the battery voltage as a float. Usually that voltage ranges from 3.0 to 4.3 Volts depending on the charge state. If it is close to 5V, your BRIX₂ module is probably plugged into a USB port. Requires Serial1 (Serial1.begin(38400);)

BRIX2.statusLedOn()

Turns on the green status LED on the BRIX₂ module. That LED is not related to the RGB LED and is supposed to indicate that the module is running. It is on by default. Requires Serial1 (Serial1.begin(38400);)

BRIX2.statusLedOff()

Turns the green status LED off. Only use this if you want to save power or have any other good reason, because you will not be able to tell of the module is on or not. (Except by observing the switch). Requires Serial1 (Serial1.begin(38400);)

BRIX2.RXTXOff()

Switches the RX/TX pins of the System Controller to input. This means they do not interfere anymore with Extension Modules that use hardware serial, such as the AudioAmp Module or the BLE Module. This also means that you can not talk to the System Controller anymore and you will not be able to use the wireless transceiver anymore. In order to get these functions back, you will need to perform a System Controller Reset. This will mean that the RX/TX pins are active again and interfere with said Extension Modules. You can not have it all! Requires Serial1 (Serial1.begin(38400);)

BRIX2.resetSystemController()

Performs a hard reset of the System Controller. Requires Serial1 (Serial1.begin(38400);)


RF-Transceiver

The RF Transceiver allows wireless data transmission between two BRIX₂ Base modules. The User Controller can access the RF interface only through the System Controller. If RF functions like "sendAscii" are executed on the User Controller, it sends the according data to the System Controller through serial port 1. The System Controller then reformats the data and sends it to the RF interface via SPI. On the receiving side, the RF interface picks up the data which is read from the RF interface by the System Controller. It reformats the package and sends the data to the User Controller via serial port 1.

This is why all RF transceiver commands require to activate Serial1:


void setup(){
  Serial1.begin(38400);
}

BRIX2.getAddress()

Returns the address of your module as an int. Addresses are generally referred to as hex-coded.

BRIX2.checkNode(byte address, int timeOut)

Returns a boolean that tells you if a node with address can be pinged in the wireless network. If it did not do so after timeout in milliseconds is passed, it returns false.

(This function is currently not working, as of July 2016).

BRIX2.sendAscii(byte address, char* text, byte length)

Sends an array of char (so basically a text) to a node with address. You also have to enter the length of that text. There is a usage example (RFSenderReceiverASCII) in >Examples>Wireless>. On the receiving end, only the raw text appears, without the header information and with added /r/n.

BRIX2.sendInt(byte address, int * numbers, byte length)

Sends an array of int (so basically a list of numbers) to a node with address. You also have to enter the number of integers you send. There is a usage example (RFSenderReceiverIntegers) in >Examples>Wireless>. On the receiving end, only the numbers appear, encoded as human readable ASCII, comma separated and terminated with /r/n.

BRIX2.sendFloat(byte address, int * numbers, byte length)

(This function is currently not implemented, as of July 2016).


Inertial Motion Sensor

In order to use the Inertial Motion Sensor (see also BRIX₂_Base_Module_Sensor), you have to use the InertialSensor library which is part of the LiBRIX₂ bundle. But you have to add manually to use it. It is important to also include Wire.h!. Then you just generate an object for the sensor and use it in the same way as you use the BRIX2 object:

#include <Wire.h>
#include <InertialSensor.h>
#include <BRIX2.h>
InertialSensor mySensor;
BRIX2 myBrick;

void setup(){
  myBrick.initialize();
  mySensor.initialize();
}

void loop(){
}

Check out one of the examples and use it as your code base.

InertialSensor.initialize()

Necessary for initialization of the sensor, only once.

InertialSensor.setAccelerometerRange(uint8_t accRange)

This will set the range of the Accelerometer. The default value is 0.

0 = +/- 2g
1 = +/- 4g
2 = +/- 8g
3 = +/- 16g

InertialSensor.setGyroscopeRange(uint8_t gyrRange)

Same for the gyroscope, default value is 0.

0 = +/- 250 degrees/sec
1 = +/- 500 degrees/sec
2 = +/- 1000 degrees/sec
3 = +/- 2000 degrees/sec

InertialSensor.getAccelerationX(int min, int max)
InertialSensor.getAccelerationY(int min, int max)
InertialSensor.getAccelerationZ(int min, int max)

This will give you an integer which tells you the acceleration along the X, Y or Z axis scaled to a range you set with min and max. min and max should range between -32768 and 32768.

InertialSensor.getRotationX(int min, int max)
InertialSensor.getRotationY(int min, int max)
InertialSensor.getRotationZ(int min, int max)

This will give you an integer which tells you the angular velocity around the X, Y or Z axis scaled to a range you set with min and max. min and max should range between -32768 and 32768.

InertialSensor.getAccelerationX()
InertialSensor.getAccelerationY()
InertialSensor.getAccelerationZ()

This will give you an integer which tells you the acceleration along the X, Y or Z axis as a value between -32768 and 32768

InertialSensor.getRotationX()
InertialSensor.getRotationY()
InertialSensor.getRotationZ()

This will give you an integer which tells you the angular velocity around the X, Y or Z axis as a value between -32768 and 32768


AmbiSense Extension

In order to use the AmbiSense Extension, you have to add the according lib along with wire.h:


#include <Wire.h> // If you need Wire.h ALWAYS include it first!
#include <BRIX2.h>
#include <AmbiSenseExtension.h>

// Create Objects for Ambisense Extension and BRIX₂ Module
AmbiSenseExtension myAmbi;
BRIX2 myBrick;

void setup(){
  // Initialize the BRIX₂ Module and the AmbiSense Extension
  myBrick.initialize();
  myAmbi.initialize();  
}

void loop(){
}

AmbiSenseExtension.initialize()

Necessary for initialization of the sensor, only once.

AmbiSenseExtension.getVisible(int min, int max)
AmbiSenseExtension.getInfrared(int min, int max)
AmbiSenseExtension.getFullSpectrum(int min, int max)

These functions provide the readings of visible light, infrared light and the full spectrum in a user-defined range from min to max. min and max should range between -32768 and 32768.

AmbiSenseExtension.getVisible()
AmbiSenseExtension.getInfrared()
AmbiSenseExtension.getFullSpectrum()

These functions provide the full range readings of visible light, infrared light or the full spectrum (0-65535 each)


Proximity Extension

In order to use the Proximity Extension, you have to add the according lib along with wire.h:


#include <Wire.h> // If you need Wire.h ALWAYS include it first!
#include <BRIX2.h>
#include <ProximityExtension.h>  // let's play with the Proximity Extension

// generate Objects for Extension Module and BRIX₂ Module
ProximityExtension myProx;
BRIX2 myBrick;

void setup(){
  // initialize ALL the things!
  myBrick.initialize();
  myProx.initialize();
}

void loop(){
}

ProximityExtension.initialize()

Necessary for initialization of the sensor, only once.

ProximityExtension.getProximity(int min, int max);

These function provides the proximity reading in a user-defined range from min to max. min and max shoud range between -32768 and 32768. Results may vary slightly.

ProximityExtension.getProximity();

These function provides the full range proximity reading (around 5000-32000)


VibroSound Extension

In order to use the VibroSound Extension, you have to add the according lib:


#include <BRIX2.h>
#include <VibroSoundExtension.h>

BRIX2 myBrick;
VibroSoundExtension myVibroSound;

void setup(){
  myBrick.initialize();
  myVibroSound.initialize();
}

void loop(){
}

VibroSoundExtension.initialize()

Necessary for initialization of the Extension, only once.

VibroSoundExtension.setVibration(char intensity)

Adjust the vibration intensity (0-255). The module will keep vibrating until you set another value.

VibroSoundExtension.Shake(char intensity, int duration)

The module will vibrate with given intensity (0-255) for a given number of milliseconds (0-32767) and then stop. This will override all vibration settings done with VibroSoundExtension.setVibration(char intensity).

VibroSoundExtension.setPitch(int pitch)

This is basically a wrapper for tone() on the correct pin. Pitch is given in Hz.

VibroSoundExtension.Beep(int pitch, int duration)

The module will beep in the given pitch (in Hz) for a given duration in milliseconds (0-32767). This is a wrapper for tone() with given duration on the correct pin.

VibroSoundExtension.noBeep()

Stop all the beeps. This is a wrapper for noTone()