Cubelets API Documentation 2.0
C API
Loading...
Searching...
No Matches
cubelet_routine.h File Reference
#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.
 

Function Documentation

◆ setup()

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.

#include "cubelet.h"
void setup()
{
}
void setup()
Function ran just a single time. Used for setting up variables or timers.
Definition bargraph.c:3

Function ran just a single time. Used for setting up variables or timers.

void setup()
{
}

◆ loop()

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.

  • If the brightness is below 128, it is set to 0.
  • If the brightness is 128 or greater, it applies a scaling factor to increase sensitivity.
    void loop()
    {
    unsigned int tmp_block_value = get_brightness();
    if (tmp_block_value < 128){
    tmp_block_value = 0;
    }
    else {
    tmp_block_value = (tmp_block_value - 128) * 2; // Half sensitivity
    }
    block_value = tmp_block_value;
    }
    void loop()
    The loop() function gets called repeatedly while a Cubelet is powered on.
    Definition bargraph.c:8

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.

unsigned int tmp_block_value = get_brightness();

Let's split the value handling into two parts.

  1. The first part is what happens if the value is less than 128, which is 50% brightness level. Anything lower than 128 will be considered 0.
if (tmp_block_value < 128){
tmp_block_value = 0;
}
  1. The second part is what happens when the brightness is above 128. These are the values that need to be handled. However, those values need to be remapped so they extend from 0 to 255 as expected.

If the brightness is 128 or greater, the value is adjusted by:

  • Subtracting 128 to ignore the lower brightness range.
  • Doubling the result to increase sensitivity.
else {
tmp_block_value = (tmp_block_value - 128) * 2; // Half sensitivity
}

The mapping is as follows:

[0-128] -> 0

[128-255] -> [0,255]

  1. Last step is sending the temporary brightness level to the actual block_value.
block_value = tmp_block_value;

The loop() function gets called repeatedly while a Cubelet is powered on.

  • think(): Determines the block_value.
  • act(): Lights up the LEDs based on the block_value.
void loop()
{
think();
act();
}
void act()
Using to perform an action based on a previously calculated block value.
Definition bargraph.c:19
void think()
Used in Action and Think Cubelets to calculate their block value.
Definition bargraph.c:14

The loop() function gets called repeatedly while a Cubelet is powered on.

void loop()
{
think();
act();
}

◆ sense()

void sense ( )

Optional function to include in custom programs. Typically implements setting the block value to the sensor value.

◆ think()

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:

  • How bright the LEDs are.
  • How quickly they blink.

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.

void think()
{
if (!block_value_override)
{
block_value = weighted_average();
}
}
uint8_t weighted_average(void)
Calculates a block value based on the on the weighted average of neighbors block values.

Used in Action and Think Cubelets to calculate their block value.

void think()
{
block_value = weighted_average();
}

◆ act()

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.

  1. 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.

    for (uint8_t i = 0; i < 4; i++)
    {
    // snip code for now
    // Set LEDs in mirrored pairs
    LedSet(i, r, g, b); // LED i
    LedSet(7 - i, r, g, b); // Symmetrical LED (7 - i)
    }
  2. 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.

    hueToRGB((hue + i * 64) % 255, 10, &r, &g, &b);
  3. 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.

    brightness = (uint8_t)((block_value / 2) + (rand() % (block_value / 2 + 1)));

    b. The RGB values are scaled by brightness

    r = (r * brightness) / 255;
    g = (g * brightness) / 255;
    b = (b * brightness) / 255;
  4. Setting the LEDs

    The LedSet() function lights up a specific LED with the calculated color. This creates a symmetrical pattern.

    LedSet(i, r, g, b); // LED i
    LedSet(7 - i, r, g, b); // Symmetrical LED (7 - i)
  5. Blinking Effect

    One random LED blinks brightly. The chance of blinking increases with block_value. Note: rand() % 8 selects a random LED to blink.

    if (rand() % (256 - block_value) < 5)
    {
    blink_led = rand() % 8;
    LedSetHue(blink_led, last_hue, 255);
    }
  6. Dynamic Hue and Speed

    The hue shifts slightly each loop.

    last_hue = (last_hue + 1) % 255;

    The update speed also changes with block_value:

    wait(150 - (block_value / 2));
    void wait(uint16_t delay)
    Function to delay execution for a specified amount of time.
  7. 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.

    LedRingUpdate();

And now, for the full code:

void act()
{
uint8_t r, g, b, brightness;
static uint8_t last_hue = 0;
static uint8_t blink_led = 0;
uint8_t hue = (uint8_t)(block_value * 2);
// Iterate through LEDs in mirrored pairs
for (uint8_t i = 0; i < 4; i++)
{
hueToRGB((hue + i * 64) % 255, 10, &r, &g, &b); // Generate RGB based on hue
brightness = (uint8_t)((block_value / 2) + (rand() % (block_value / 2 + 1))); // Randomized brightness
// Scale RGB by brightness
r = (r * brightness) / 255;
g = (g * brightness) / 255;
b = (b * brightness) / 255;
// Set LEDs in mirrored pairs
LedSet(i, r, g, b); // LED i
LedSet(7 - i, r, g, b); // Symmetrical LED (7 - i)
}
// Blinking effect for a random LED
if (rand() % (256 - block_value) < 5)
{
blink_led = rand() % 8;
LedSetHue(blink_led, last_hue, 255); // Set random LED to bright hue
}
last_hue = (last_hue + 1) % 255; // Increment hue for smooth transitions
wait(150 - (block_value / 2)); // Adjust speed based on block_value
LedRingUpdate();
}

Using to perform an action based on a previously calculated block value.

void act()
{
set_speaker(block_value);
}