Commit bede18e7 authored by Sebastian Neuser's avatar Sebastian Neuser
Browse files

WIP: Add more examples

parent b88b19a7
Pipeline #662 failed with stages
in 0 seconds
#include <BOB3.h>
uint16_t COLORS[] = {
OFF, RED, GREEN, BLUE, YELLOW, CYAN, FUCHSIA, ORANGE, KABARED, PURPLE, VIOLET, AQUAMARINE,
BROWN, CORAL, CORNFLOWERBLUE, STEELBLUE, ROYALBLUE, FORESTGREEN, SEAGREEN, COLERED, UNICORN,
};
uint8_t idx = sizeof(COLORS) / sizeof(uint16_t);
bool on = false;
void updateEyes()
{
uint16_t color = COLORS[idx];
bob3.setEyes(color, color);
}
void setup()
{
// bob3.enableIRSensor(true);
updateEyes();
}
void loopAlice()
{
if (bob3.getArm(1)) {
idx = random8();
idx %= sizeof(COLORS) / sizeof(uint16_t);
updateEyes();
}
if (bob3.getArm(2)) {
bob3.transmitMessage(idx);
}
delay(167);
}
void loopBob()
{
int16_t rx_idx = bob3.receiveMessage(100);
if (rx_idx >= 0) {
idx = rx_idx;
updateEyes();
}
}
void loop()
{
if (bob3.getID()) {
loopAlice();
} else {
loopBob();
}
}
#include <stdint.h>
#include <BOB3.h>
#define PROXIMITY_BUFFER_SIZE 256
uint16_t proximityBuffer[PROXIMITY_BUFFER_SIZE] = {0,};
uint16_t writeIndex = -1;
#define BLINK_INTERVAL 1000
#define BLINK_STEP_LENGTH 7
#define BLINK_STEPS 15
uint8_t blinkStep = 0;
uint16_t blinkCounter = BLINK_INTERVAL;
enum blink_phase {
BLINK_IDLE,
BLINK_SHUTTING,
BLINK_OPENING,
} blinkState = BLINK_IDLE;
#define FLASH_LENGTH 40
enum flash_phase {
FLASH_IDLE,
FLASH_ON,
FLASH_OFF,
};
struct arm {
uint16_t position;
enum flash_phase flashState;
uint16_t flashCounter;
bool on;
};
struct arm leftArm = {1, FLASH_IDLE, 0, false};
struct arm rightArm = {2, FLASH_IDLE, 0, false};
uint32_t state = 42;
uint32_t xorshift32()
{
uint32_t x = state;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
state = x;
return x;
}
void setup()
{
bob3.enableIRSensor(true);
}
uint16_t filterProximity(uint16_t proximity)
{
// Copy current proximity to ring buffer
++writeIndex;
writeIndex %= PROXIMITY_BUFFER_SIZE;
proximityBuffer[writeIndex] = proximity;
// Calculate mean proximity
proximity = 0;
for (uint16_t i = 0; i < PROXIMITY_BUFFER_SIZE; ++i) {
proximity += proximityBuffer[i];
}
proximity /= PROXIMITY_BUFFER_SIZE;
// Scale and saturate proximity
proximity = (3 * proximity) / 2;
proximity = proximity > 15 ? 15 : proximity;
return proximity;
}
uint8_t calculateBlinkFactor()
{
--blinkCounter;
if (blinkCounter == 0) {
switch (blinkState) {
case BLINK_IDLE:
blinkState = BLINK_SHUTTING;
blinkStep = BLINK_STEPS;
blinkCounter = BLINK_STEP_LENGTH;
break;
case BLINK_SHUTTING:
blinkCounter = BLINK_STEP_LENGTH;
--blinkStep;
blinkState = blinkStep
? BLINK_SHUTTING
: BLINK_OPENING;
blinkStep = blinkStep ? blinkStep : BLINK_STEPS;
break;
case BLINK_OPENING:
--blinkStep;
blinkCounter = blinkStep
? BLINK_STEP_LENGTH
: BLINK_INTERVAL + (xorshift32() >> 20);
blinkState = blinkStep
? BLINK_OPENING
: BLINK_IDLE;
break;
}
}
if (blinkState == BLINK_SHUTTING) {
return blinkStep;
}
if (blinkState == BLINK_OPENING) {
return BLINK_STEPS - blinkStep;
}
return BLINK_STEPS;
}
void updateArm(struct arm* arm)
{
if (arm->flashState == FLASH_IDLE) {
arm->on = bob3.getArm(arm->position) != 0;
if (arm->on) {
arm->flashState = FLASH_ON;
arm->flashCounter = FLASH_LENGTH;
}
}
--arm->flashCounter;
if (arm->flashCounter == 0) {
switch (arm->flashState) {
case FLASH_ON:
arm->flashState = FLASH_OFF;
arm->flashCounter = 16 * FLASH_LENGTH;
arm->on = 0;
break;
case FLASH_OFF:
arm->flashState = FLASH_IDLE;
default:
break;
}
}
}
void loop()
{
int16_t proximity = filterProximity(bob3.getIRSensor());
int16_t blinkFactor = calculateBlinkFactor();
int16_t red = (proximity * blinkFactor) / BLINK_STEPS;
int16_t green = 15 - proximity;
green = green < 0 ? 0 : green;
green = (green * blinkFactor) / BLINK_STEPS;
uint16_t color = rgb(red, green, 0);
bob3.setEyes(color, color);
updateArm(&leftArm);
updateArm(&rightArm);
bob3.setWhiteLeds(leftArm.on, rightArm.on);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment