![]() |
Cubelets API Documentation 2.0
C API
|
#include "communication.h"
Go to the source code of this file.
Functions | |
void | setup () |
Function ran just a single time. Used for setting up variables or timers. | |
void | loop () |
The loop() function gets called repeatedly while a Cubelet is powered on. | |
void | sense () |
Used in Sense Cubelets to read the sensor value. | |
void | think () |
Used in Action and Think Cubelets to calculate their block value. | |
void | act () |
Using to perform an action based on a previously calculated block value. | |
void setup | ( | ) |
A mandatory function that must be included in all custom programs. Allows initialization of variables, timers or behavior. Will only be called a single time.
Function ran just a single time. Used for setting up variables or timers.
Function ran just a single time. Used for setting up variables or timers.
void loop | ( | ) |
The second of two mandatory functions for a custom program. loop() gets called repeatedly while the Cubelet is powered on. Any custom behavior should be specified in this function.
The loop() function gets called repeatedly while a Cubelet is powered on.
This function reads the current brightness using the get_brightness()
function and modifies the value based on a sensitivity adjustment.
The value is obtained from the get_brightness()
function and later adjusted based on the current brightness level. tmp_block_value is a temporary variable for storing the brightness value while we do calculations on it.
Let's split the value handling into two parts.
If the brightness is 128 or greater, the value is adjusted by:
The mapping is as follows:
[0-128] -> 0
[128-255] -> [0,255]
The loop() function gets called repeatedly while a Cubelet is powered on.
The loop() function gets called repeatedly while a Cubelet is powered on.
void sense | ( | ) |
Optional function to include in custom programs. Typically implements setting the block value to the sensor value.
void think | ( | ) |
Optional function to include in custom programs. Typically implements calculating the block value for a block.
Used in Action and Think Cubelets to calculate their block value.
The think() function calculates block_value, a number that determines:
The weighted_average() function calculates this based on sensor inputs (not shown in the code). If there’s no manual override (block_value_override), it updates block_value.
Used in Action and Think Cubelets to calculate their block value.
void act | ( | ) |
Optional function to include in custom programs. Used by action blocks to perform their action after calculating their block values.
Using to perform an action based on a previously calculated block value.
This is where the magic happens! Let’s break it into smaller sections.
Symmetry in LED Colors
The 8 LEDs are arranged in pairs: LED 0 pairs with LED 7. LED 1 pairs with LED 6, and so on.
The following loop runs for half the LEDs, setting their colors and mirroring them for the other half.
Color Calculation
The hueToRGB() function converts a hue (a number between 0 and 255) into red (r), green (g), and blue (b) values.
The hue shifts slightly for each pair:
The % 255 ensures the hue wraps around within the valid range.
Brightness Adjustment
a. Brightness depends on block_value and a random factor.This ensures that brightness increases with block_value but also has some variation for a cool effect.
b. The RGB values are scaled by brightness
Setting the LEDs
The LedSet() function lights up a specific LED with the calculated color. This creates a symmetrical pattern.
Blinking Effect
One random LED blinks brightly. The chance of blinking increases with block_value. Note: rand() % 8 selects a random LED to blink.
Dynamic Hue and Speed
The hue shifts slightly each loop.
The update speed also changes with block_value:
Turn the LEDs on
The last line of code is when we tell the Cubelet to turn the LEDs on to their newest colors. Without this call, the LEDs will never update.
And now, for the full code:
Using to perform an action based on a previously calculated block value.