Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |