Add jumpy mode
This commit is contained in:
parent
d3c3f928b8
commit
47c93d59f7
|
@ -1,6 +1,6 @@
|
|||
PROJECT=pentabug
|
||||
|
||||
APPS = buggy mariotheme geiger ducklings wuerfel blinker
|
||||
APPS = buggy jumpy mariotheme geiger ducklings wuerfel blinker
|
||||
|
||||
OPTIMIZATION = -Os
|
||||
MCU = atmega88pa
|
||||
|
|
61
firmware/apps/jumpy.c
Normal file
61
firmware/apps/jumpy.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
#include <pentabug/app.h>
|
||||
#include <pentabug/hal.h>
|
||||
#include <pentabug/pentatonic.h>
|
||||
#include <pentabug/listen.h>
|
||||
#include <pentabug/helper.h>
|
||||
#include <pentabug/music.h>
|
||||
|
||||
#define SENSITIVITY_FACT 4
|
||||
|
||||
static uint16_t max_sound;
|
||||
static uint8_t first;
|
||||
|
||||
static void init(void) {
|
||||
pentatonic_direction(ALL_OUT);
|
||||
listen_init();
|
||||
|
||||
max_sound = 5;
|
||||
first = 0;
|
||||
}
|
||||
|
||||
static void run(void) {
|
||||
uint16_t sound = listen_measure();
|
||||
|
||||
pentatonic_all_led_set(max_sound >> 5);
|
||||
|
||||
if(!first && sound > max_sound / SENSITIVITY_FACT) {
|
||||
int8_t intensity = SENSITIVITY_FACT + 1 - max_sound / sound;
|
||||
|
||||
intensity = MIN(intensity, SENSITIVITY_FACT + 1);
|
||||
|
||||
motor_on();
|
||||
|
||||
set_note(NOTE_C, (rand() & 3) + 3);
|
||||
|
||||
for(size_t i = 0; i < intensity; ++i) {
|
||||
led_set(RIGHT, rand() & 1);
|
||||
led_set(LEFT, rand() & 1);
|
||||
|
||||
wait_ms(100);
|
||||
}
|
||||
|
||||
// turn everything off
|
||||
|
||||
stop_note();
|
||||
|
||||
motor_off();
|
||||
|
||||
led_off(RIGHT);
|
||||
led_off(LEFT);
|
||||
|
||||
wait_ms(100);
|
||||
} else {
|
||||
first = 0;
|
||||
}
|
||||
|
||||
if(sound > max_sound) {
|
||||
max_sound = sound;
|
||||
}
|
||||
}
|
||||
|
||||
REGISTER(run, init, NULL);
|
12
firmware/include/pentabug/listen.h
Normal file
12
firmware/include/pentabug/listen.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef LISTEN_H
|
||||
#define LISTEN_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void listen_init(void);
|
||||
|
||||
void listen_stop(void);
|
||||
|
||||
uint16_t listen_measure(void);
|
||||
|
||||
#endif /* LISTEN_H */
|
66
firmware/lib/listen.c
Normal file
66
firmware/lib/listen.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include <pentabug/listen.h>
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
#include <pentabug/hal.h>
|
||||
#include <pentabug/lifecycle.h>
|
||||
|
||||
void listen_init(void) {
|
||||
ADCSRA |= (1 << ADEN);
|
||||
}
|
||||
|
||||
void listen_stop(void) {
|
||||
ADCSRA &= ~(1 << ADEN);
|
||||
}
|
||||
|
||||
uint16_t listen_measure(void) {
|
||||
// save previous state
|
||||
|
||||
uint8_t prev_state = PINC & (1 << 0);
|
||||
|
||||
// set to ground for discharge
|
||||
|
||||
DDRC |= 1 << 0;
|
||||
PORTC &= ~(1 << 0);
|
||||
|
||||
// wait for discharge
|
||||
|
||||
wait_ms(1);
|
||||
|
||||
// set direction to input for measurement
|
||||
|
||||
DDRC &= ~(1 << 0);
|
||||
|
||||
// collect some sound waves
|
||||
|
||||
wait_ms(1);
|
||||
|
||||
// start measurement
|
||||
|
||||
// TODO: why can't i move this to the initialization?
|
||||
ADMUX = (1 << REFS0) | (1 << ADLAR);
|
||||
ADCSRA |= (1 << ADPS2) | (1 << ADPS1);
|
||||
|
||||
ADMUX = (ADMUX & ~(0x1F)) | 0;
|
||||
|
||||
ADCSRA |= (1 << ADSC);
|
||||
|
||||
// wait for measurement to finish
|
||||
|
||||
while (ADCSRA & (1 << ADSC)) test_stop_app();
|
||||
|
||||
uint16_t res = ADCL;
|
||||
res |= ADCH << 8;
|
||||
|
||||
// restore state
|
||||
|
||||
if(prev_state) {
|
||||
PORTC |= 1 << 0;
|
||||
}
|
||||
|
||||
DDRC |= 1 << 0;
|
||||
|
||||
// done
|
||||
|
||||
return res;
|
||||
}
|
Loading…
Reference in New Issue
Block a user