Cubelets API Documentation 2.0
C API
Loading...
Searching...
No Matches
brt_LowSensitivity.c File Reference

Lowers the sensitivity of the Brightness Cubelet/Hat by half. More...

#include "cubelet.h"

Functions

void setup ()
 Initialization function called once at the start. The first line includes the header required for all cubelets The setup() Function runs once when the program starts. Here, it’s empty because we don't have any initialization.
 
void loop ()
 Continuously checks and adjusts the brightness level.
 

Detailed Description

The Goal

We want to change the Brightness cubelet/hat so that it becomes less sensitive to lower illumination while providing a bit more sensitivity to higher illuminations.

Workflow

  1. The setup() function initializes the system (empty in this case).
  2. The loop() function continuously calculates the new value.

Understanding the Code

Function Documentation

◆ setup()

void setup ( )

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

◆ loop()

void loop ( )

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;