Commit 8ec85006 authored by Sebastian Neuser's avatar Sebastian Neuser

WIP: Add more examples

parent 76ee1914
#include <BOB3.h>
uint16_t COLORS[] = {RED, GREEN, BLUE, FUCHSIA, VIOLET, COLERED, UNICORN};
#define NUM_COLORS (sizeof(COLORS) / sizeof(uint16_t))
uint8_t idx = NUM_COLORS - 1;
bool eyesShut()
{
static const uint16_t BLINK_INTERVAL = 1000;
static const uint16_t BLINK_LENGTH = 35;
static uint16_t blinkCounter = BLINK_INTERVAL;
static bool eyesShut = false;
--blinkCounter;
if (blinkCounter == 0) {
if (!eyesShut) {
eyesShut = true;
blinkCounter = BLINK_LENGTH;
}
else {
eyesShut = false;
blinkCounter = BLINK_INTERVAL + (random32() % BLINK_INTERVAL);
}
}
return eyesShut;
}
uint16_t filterProximity(uint16_t proximity)
{
static const uint16_t PROXIMITY_BUFFER_SIZE = 256;
static uint16_t proximityBuffer[PROXIMITY_BUFFER_SIZE] = {0,};
static uint16_t writeIndex = -1;
// 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;
}
void updateEyes()
{
uint16_t color = eyesShut() ? (uint16_t) OFF : COLORS[idx];
bob3.setEyes(color, color);
}
void setup()
{
updateEyes();
}
void loopAlice()
{
static const uint8_t PROXIMITY_ALERT_THRESHOLD = 2;
static uint8_t prev_idx = NUM_COLORS - 1;
static bool alert = false;
if (bob3.getArm(1)) {
idx += 1;
idx %= NUM_COLORS;
idx = idx ? idx : 1;
delay(200);
}
if (bob3.getArm(2)) {
bob3.transmitMessage(idx);
}
int16_t proximity = filterProximity(bob3.getIRSensor());
if (proximity > PROXIMITY_ALERT_THRESHOLD) {
if (!alert) {
alert = true;
prev_idx = idx;
idx = 0;
}
} else {
if (alert) {
alert = false;
idx = prev_idx;
}
}
updateEyes();
delay(1);
}
void loopBob()
{
int16_t rx_idx = bob3.receiveMessage(100);
if (rx_idx >= 0) {
idx = rx_idx;
}
bob3.setEyes(COLORS[idx], COLORS[idx]);
}
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