pentabug/firmware/apps/buggy.c

99 lines
1.6 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <pentabug/app.h>
#include <pentabug/hal.h>
#include <pentabug/photons.h>
#include <pentabug/pentatonic.h>
2013-09-03 22:49:52 +02:00
#include <pentabug/music.h>
2013-09-06 00:35:59 +02:00
#include <pentabug/helper.h>
2013-09-03 22:10:33 +02:00
inline uint16_t biased_random(uint8_t value) {
2013-09-05 22:06:02 +02:00
return value / 8 * (rand() & 7);
2013-09-03 22:10:33 +02:00
}
static void init(void) {
pentatonic_direction(ALL_OUT);
photons_init();
}
static void run(void) {
2013-09-06 00:35:59 +02:00
if(ir_recv()) {
// receive a ir signal, react
2013-09-06 00:35:59 +02:00
motor_on();
2013-09-06 00:35:59 +02:00
led_on(RIGHT);
2013-09-03 22:10:33 +02:00
2013-09-06 00:35:59 +02:00
set_note(NOTE_B, 5);
wait_ms(200);
set_note(NOTE_F, 5);
wait_ms(100);
2013-09-03 22:10:33 +02:00
2013-09-06 00:35:59 +02:00
led_off(RIGHT);
led_on(LEFT);
set_note(NOTE_G, 5);
wait_ms(100);
set_note(NOTE_Ab, 5);
wait_ms(100);
set_note(NOTE_A, 5);
wait_ms(100);
led_off(LEFT);
motor_off();
} else if(button_clicked(RIGHT)) {
// button clicked, send ir signal and do some stuff
led_on(RIGHT);
set_note(NOTE_A, 5);
wait_ms(100);
set_note(NOTE_Ab, 5);
wait_ms(100);
set_note(NOTE_G, 5);
wait_ms(100);
led_off(RIGHT);
led_on(LEFT);
set_note(NOTE_F, 5);
wait_ms(100);
set_note(NOTE_B, 5);
wait_ms(200);
2013-09-03 22:10:33 +02:00
stop_note();
2013-09-03 21:25:07 +02:00
2013-09-06 00:35:59 +02:00
led_off(LEFT);
ir_on();
wait_ms(400);
ir_off();
wait_ms(10);
} else {
// regular bug behaviour
uint8_t light = photons_measure();
pentatonic_all_led_set(light >> 3);
motor_set(biased_random(light) > 0x40);
led_set(RIGHT, biased_random(light) > 0x20);
led_set(LEFT, biased_random(light) > 0x20);
if(biased_random(light) > 0x50) {
uint16_t tone = (biased_random(light) * 2) + 500;
set_note(tone, 0);
} else {
stop_note();
}
wait_ms(200);
}
}
REGISTER(run, init, NULL);