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

Example code for the Bar Graph Cubelet. More...

Detailed Description

Binary

Sets the bars to display the block value in binary over 8 bits.

#include "cubelet.h"
void setup()
{
}
void loop()
{
block_value = weighted_average();
//set_binary(block_value);
//or implement it here for educational purposes:
//set bars 1-8 according to binary value
for (unsigned int i = 0; i<10; i++){
if (block_value & (1<<i)){ //Use a bit shift to create a bit mask to determine the i'th binary value.
set_bar(i+1);
}
else {
clear_bar(i+1);
}
}
}
uint8_t weighted_average(void)
Calculates a block value based on the on the weighted average of neighbors block values.
void setup()
Function ran just a single time. Used for setting up variables or timers.
Definition bargraph.c:3
void loop()
The loop() function gets called repeatedly while a Cubelet is powered on.
Definition bargraph.c:8

Digits

Displays the block value in base 10. First number to be displayed is the hundreds place value, then the tens place value, finally the ones place value. There's a small rest before displaying the next block value. 255 will therefore show one after another: 2, 5, 5, nothing

#include "cubelet.h"
void setup()
{
// intentionaly empty
}
void loop()
{
block_value = weighted_average();
unsigned int hundreds_digit = block_value/100;
unsigned int tens_digit = (block_value / 10) % 10;
unsigned int ones_digit = block_value % 10;
for (unsigned int i=0; i<4; i++){ //Four states are: 1: hundreds digit. 2: tens digit. 3: ones digit. 4: Rest.
switch (i){
case 0:
set_bar_graph(hundreds_digit);
break;
case 1:
set_bar_graph(tens_digit);
break;
case 2:
set_bar_graph(ones_digit);
break;
case 3:
set_bar_graph(0); //Rest
break;
}
wait(750);
}
}
void wait(uint16_t delay)
Function to delay execution for a specified amount of time.

Pong

#include "cubelet.h"
#include <stdbool.h>
unsigned int current_bar = 0;
bool direction_positive = true;
void setup()
{
}
void loop()
{
block_value = weighted_average();
if (direction_positive){
if (current_bar == 9) direction_positive = false; //If at the upper limit, turn around
else current_bar++; //otherwise increment current bar
}
else { //negative direction
if (current_bar == 0) direction_positive = true; //If at the lower limit, turn around
else current_bar--; //otherwise decrement current bar
}
set_bar_graph(0); //clear all bars
set_bar(current_bar); //set the current siongle bar
wait(inverse(block_value) + 15); //speed dependent on block value
}
uint8_t inverse(uint8_t bv)
Returns the inverse of the supplied block value.