button code madness
This commit is contained in:
parent
608ad6e2fe
commit
68cd73d423
|
@ -6,20 +6,29 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
#include "freq_table.h"
|
#include "freq_table.h"
|
||||||
|
#include <lib/bughal.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static uint16_t osc0 ;
|
||||||
|
static uint16_t osc1 ;
|
||||||
|
static uint16_t sample ;
|
||||||
|
static uint8_t row ;
|
||||||
|
static uint16_t speedtime;
|
||||||
|
|
||||||
|
|
||||||
void synth_init(void){
|
void synth_init(void){
|
||||||
cli();
|
cli();
|
||||||
/*
|
osc0 = osc1 = sample = row = 0;
|
||||||
* Timer 0
|
speedtime = 3000;
|
||||||
*/
|
|
||||||
|
|
||||||
/* set timer0 to CTC & prescaler 64 → 125kHz increment */
|
/* set timer0 to CTC & prescaler 64 → 125kHz increment */
|
||||||
TCCR0A = (1 << WGM01);
|
TCCR0A = (1 << WGM01);
|
||||||
TCCR0B = (1 << CS00) | (1 << CS01);
|
TCCR0B = (1 << CS00) | (1 << CS01);
|
||||||
|
|
||||||
OCR0A = 4; /* TOP */
|
OCR0A = 4; /* TOP */
|
||||||
TCNT0 = 0;
|
TCNT0 = 0;
|
||||||
|
|
||||||
/*enable interrupt */
|
/*enable interrupt */
|
||||||
TIMSK0 |= (1 << OCIE0A);
|
TIMSK0 |= (1 << OCIE0A);
|
||||||
|
|
||||||
|
@ -35,9 +44,6 @@ void synth_deinit(void) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
SONG_LENGTH = 128,
|
SONG_LENGTH = 128,
|
||||||
xxx = 0,
|
xxx = 0,
|
||||||
|
@ -53,32 +59,43 @@ enum {
|
||||||
|
|
||||||
const char music_data[2][SONG_LENGTH] PROGMEM = {
|
const char music_data[2][SONG_LENGTH] PROGMEM = {
|
||||||
{
|
{
|
||||||
e_1, xxx, e_2, xxx, e_1, xxx, e_2, xxx, e_1, xxx, e_2, xxx, e_1, xxx, e_2, xxx,
|
e_1, xxx, e_2, xxx, e_1, xxx, e_2, xxx,
|
||||||
a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx,
|
e_1, xxx, e_2, xxx, e_1, xxx, e_2, xxx,
|
||||||
gs0, xxx, gs1, xxx, gs0, xxx, gs1, xxx, gs0, xxx, gs1, xxx, gs0, xxx, gs1, xxx,
|
a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx,
|
||||||
a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx, a_0, xxx, a_0, xxx, b_0, xxx, c_1, xxx,
|
a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx,
|
||||||
d_1, xxx, d_2, xxx, d_1, xxx, d_2, xxx, d_1, xxx, d_2, xxx, d_1, xxx, d_2, xxx,
|
gs0, xxx, gs1, xxx, gs0, xxx, gs1, xxx,
|
||||||
c_1, xxx, c_2, xxx, c_1, xxx, c_2, xxx, c_1, xxx, c_2, xxx, c_1, xxx, c_2, xxx,
|
gs0, xxx, gs1, xxx, gs0, xxx, gs1, xxx,
|
||||||
b_0, xxx, b_1, xxx, b_0, xxx, b_1, xxx, b_0, xxx, b_1, xxx, b_0, xxx, b_1, xxx,
|
a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx,
|
||||||
a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx, a_0, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
|
a_0, xxx, a_0, xxx, b_0, xxx, c_1, xxx,
|
||||||
|
d_1, xxx, d_2, xxx, d_1, xxx, d_2, xxx,
|
||||||
|
d_1, xxx, d_2, xxx, d_1, xxx, d_2, xxx,
|
||||||
|
c_1, xxx, c_2, xxx, c_1, xxx, c_2, xxx,
|
||||||
|
c_1, xxx, c_2, xxx, c_1, xxx, c_2, xxx,
|
||||||
|
b_0, xxx, b_1, xxx, b_0, xxx, b_1, xxx,
|
||||||
|
b_0, xxx, b_1, xxx, b_0, xxx, b_1, xxx,
|
||||||
|
a_0, xxx, a_1, xxx, a_0, xxx, a_1, xxx,
|
||||||
|
a_0, xxx, xxx, xxx, xxx, xxx, xxx, xxx,
|
||||||
}, {
|
}, {
|
||||||
e_3, e_3, e_3, e_3, b_2, b_2, c_3, c_3, d_3, d_3, e_3, d_3, c_3, c_3, b_2, b_2,
|
e_3, e_3, e_3, e_3, b_2, b_2, c_3, c_3,
|
||||||
a_2, a_2, a_2, a_2, a_2, a_2, c_3, c_3, e_3, e_3, e_3, e_3, d_3, d_3, c_3, c_3,
|
d_3, d_3, e_3, d_3, c_3, c_3, b_2, b_2,
|
||||||
b_2, b_2, b_2, b_2, b_2, b_2, c_3, c_3, d_3, d_3, d_3, d_3, e_3, e_3, e_3, e_3,
|
a_2, a_2, a_2, a_2, a_2, a_2, c_3, c_3,
|
||||||
c_3, c_3, c_3, c_3, a_2, a_2, a_2, a_2, a_2, a_2, a_2, a_2, a_2, a_2, a_2, a_2,
|
e_3, e_3, e_3, e_3, d_3, d_3, c_3, c_3,
|
||||||
xxx, xxx, d_3, d_3, d_3, d_3, f_3, f_3, a_3, a_3, a_3, a_3, g_3, g_3, f_3, f_3,
|
b_2, b_2, b_2, b_2, b_2, b_2, c_3, c_3,
|
||||||
e_3, e_3, e_3, e_3, e_3, e_3, c_3, c_3, e_3, e_3, e_3, e_3, d_3, d_3, c_3, c_3,
|
d_3, d_3, d_3, d_3, e_3, e_3, e_3, e_3,
|
||||||
b_2, b_2, b_2, b_2, b_2, b_2, c_3, c_3, d_3, d_3, d_3, d_3, e_3, e_3, e_3, e_3,
|
c_3, c_3, c_3, c_3, a_2, a_2, a_2, a_2,
|
||||||
c_3, c_3, c_3, c_3, a_2, a_2, a_2, a_2, a_2, a_2, a_2, a_2, xxx, xxx, xxx, xxx,
|
a_2, a_2, a_2, a_2, a_2, a_2, a_2, a_2,
|
||||||
|
xxx, xxx, d_3, d_3, d_3, d_3, f_3, f_3,
|
||||||
|
a_3, a_3, a_3, a_3, g_3, g_3, f_3, f_3,
|
||||||
|
e_3, e_3, e_3, e_3, e_3, e_3, c_3, c_3,
|
||||||
|
e_3, e_3, e_3, e_3, d_3, d_3, c_3, c_3,
|
||||||
|
b_2, b_2, b_2, b_2, b_2, b_2, c_3, c_3,
|
||||||
|
d_3, d_3, d_3, d_3, e_3, e_3, e_3, e_3,
|
||||||
|
c_3, c_3, c_3, c_3, a_2, a_2, a_2, a_2,
|
||||||
|
a_2, a_2, a_2, a_2, xxx, xxx, xxx, xxx,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static uint16_t osc0 = 0;
|
|
||||||
static uint16_t osc1 = 0;
|
|
||||||
static uint16_t sample = 0;
|
|
||||||
static uint8_t row = 0;
|
|
||||||
static uint16_t speedtime = 3000;
|
|
||||||
|
|
||||||
|
|
||||||
ISR(TIMER0_COMPA_vect,ISR_NOBLOCK)
|
ISR(TIMER0_COMPA_vect,ISR_NOBLOCK)
|
||||||
|
@ -87,12 +104,19 @@ ISR(TIMER0_COMPA_vect,ISR_NOBLOCK)
|
||||||
osc1 += pgm_read_word(&freq_table[ pgm_read_byte(&music_data[1][row])]);
|
osc1 += pgm_read_word(&freq_table[ pgm_read_byte(&music_data[1][row])]);
|
||||||
if (++sample == speedtime ) {
|
if (++sample == speedtime ) {
|
||||||
sample = 0;
|
sample = 0;
|
||||||
|
|
||||||
if (speedtime > 600) speedtime -= 4;
|
if (speedtime > 600) speedtime -= 4;
|
||||||
if (++row == SONG_LENGTH) {
|
if (++row == SONG_LENGTH) {
|
||||||
row = 0;
|
row = 0;
|
||||||
if (speedtime <= 600) speedtime = 3000;
|
if (speedtime <= 600) speedtime = 3000;
|
||||||
}
|
}
|
||||||
}
|
if (row&4){
|
||||||
|
led_on(LED_R);
|
||||||
|
led_off(LED_L);
|
||||||
|
}else{
|
||||||
|
led_on(LED_L);
|
||||||
|
led_off(LED_R);
|
||||||
|
} }
|
||||||
|
|
||||||
if (osc0 >= 0x8000) PORTB |= (1 << PORTB2);
|
if (osc0 >= 0x8000) PORTB |= (1 << PORTB2);
|
||||||
else PORTB &= ~(1<< PORTB2);
|
else PORTB &= ~(1<< PORTB2);
|
||||||
|
|
|
@ -24,8 +24,8 @@
|
||||||
#define MODE5 5
|
#define MODE5 5
|
||||||
#define NUM_MODES 6
|
#define NUM_MODES 6
|
||||||
|
|
||||||
uint8_t OpMode = MODE5;
|
uint8_t OpMode = MODE1;
|
||||||
uint8_t NextMode = MODE5;
|
uint8_t NextMode = MODE1;
|
||||||
|
|
||||||
bool mode_uninitialized = true;
|
bool mode_uninitialized = true;
|
||||||
|
|
||||||
|
@ -56,12 +56,12 @@ void modeswitch_poll(void)
|
||||||
void do_mode0(void)
|
void do_mode0(void)
|
||||||
{
|
{
|
||||||
static timer_t mytimer;
|
static timer_t mytimer;
|
||||||
static uint16_t maxval; //maximum of ADC values read during the las timer interval
|
static uint16_t maxval; //maximum of ADC values read during the las timer interval
|
||||||
static uint16_t lastmaxval; //maximum of values during last timer interval
|
static uint16_t lastmaxval; //maximum of values during last timer interval
|
||||||
uint16_t curval; //current value on D5 (one pin of the piezo,the other is on GND)
|
uint16_t curval; //current value on D5 (one pin of the piezo,the other is on GND)
|
||||||
static bool signaling; //are we currently signaling (beeping, blinking etc...)
|
static bool signaling; //are we currently signaling (beeping, blinking etc...)
|
||||||
static bool sound_on; //should sound be on when signaling
|
static bool sound_on; //should sound be on when signaling
|
||||||
static bool motor_on; //should motor be on when signaling
|
static bool motor_on; //should motor be on when signaling
|
||||||
|
|
||||||
if (mode_uninitialized) { //init after mode change
|
if (mode_uninitialized) { //init after mode change
|
||||||
maxval = 0;
|
maxval = 0;
|
||||||
|
@ -83,11 +83,11 @@ void do_mode0(void)
|
||||||
if (btn_state(BTNST_SUP, BTN_LEFT)) {
|
if (btn_state(BTNST_SUP, BTN_LEFT)) {
|
||||||
button_clear(BTN_LEFT);
|
button_clear(BTN_LEFT);
|
||||||
sound_on = !sound_on;
|
sound_on = !sound_on;
|
||||||
};
|
}
|
||||||
if (btn_state(BTNST_SUP, BTN_RIGHT)) {
|
if (btn_state(BTNST_SUP, BTN_RIGHT)) {
|
||||||
button_clear(BTN_RIGHT);
|
button_clear(BTN_RIGHT);
|
||||||
motor_on = !motor_on;
|
motor_on = !motor_on;
|
||||||
};
|
}
|
||||||
if (timer_expired(&mytimer)) {
|
if (timer_expired(&mytimer)) {
|
||||||
if (signaling) {
|
if (signaling) {
|
||||||
//turn off sound
|
//turn off sound
|
||||||
|
@ -115,15 +115,15 @@ void do_mode0(void)
|
||||||
timer_set(&mytimer, 1);
|
timer_set(&mytimer, 1);
|
||||||
}
|
}
|
||||||
lastmaxval = maxval;
|
lastmaxval = maxval;
|
||||||
maxval = 0;
|
maxval = 0;
|
||||||
} //end if soundon
|
}//end if soundon
|
||||||
|
|
||||||
} //end if timer_expired
|
}//end if timer_expired
|
||||||
|
|
||||||
} /* end mode0 */
|
} /* end mode0 */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* do crazy sytesizer mode
|
* do crazy synthesizer mode
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void do_mode1(void)
|
void do_mode1(void)
|
||||||
|
@ -131,12 +131,15 @@ void do_mode1(void)
|
||||||
if (mode_uninitialized) {
|
if (mode_uninitialized) {
|
||||||
mode_uninitialized = false;
|
mode_uninitialized = false;
|
||||||
synth_init();
|
synth_init();
|
||||||
}
|
|
||||||
|
timer_set(&mytimer,10);
|
||||||
|
}
|
||||||
|
|
||||||
/*deinialisation required*/
|
/*deinialisation required*/
|
||||||
if(OpMode != NextMode){
|
if(OpMode != NextMode){
|
||||||
synth_deinit();
|
synth_deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,14 +319,12 @@ void do_mode5(void)
|
||||||
}
|
}
|
||||||
if (timer_expired(&mytimer)) {
|
if (timer_expired(&mytimer)) {
|
||||||
if (discharge) {
|
if (discharge) {
|
||||||
//discharge LED
|
DDRC |= (1 << PORTC0) | (1 << PORTC1) | //set LED Ports to output:
|
||||||
//enable LED channels as output
|
|
||||||
DDRC |= (1 << PORTC0) | (1 << PORTC1) |
|
|
||||||
(1 << PORTC2) | (1 << PORTC3) ;
|
(1 << PORTC2) | (1 << PORTC3) ;
|
||||||
// discharge
|
// discharge
|
||||||
PORTC = (PORTC & 0b11110000);
|
PORTC = (PORTC & 0b11110000);
|
||||||
//set C0 and C2 to input (disable pullups)
|
//set C0 and C2 to input (disable pullups)
|
||||||
DDRC &= ~((1 << PORTC0) | (1 << PORTC2));
|
DDRC &= ~((1 << PORTC0) | (1 << PORTC2)); //set Led Ports to input
|
||||||
//pull ups off
|
//pull ups off
|
||||||
PORTC &= ~((1 << PORTC0) | (1 << PORTC2));
|
PORTC &= ~((1 << PORTC0) | (1 << PORTC2));
|
||||||
discharge = false;
|
discharge = false;
|
||||||
|
@ -333,6 +334,7 @@ void do_mode5(void)
|
||||||
ADCSRA |= (1 << ADSC); // start single conversion
|
ADCSRA |= (1 << ADSC); // start single conversion
|
||||||
while (ADCSRA & (1 << ADSC)) ; // wait for conversion to end
|
while (ADCSRA & (1 << ADSC)) ; // wait for conversion to end
|
||||||
led1 = ADCW; // read result
|
led1 = ADCW; // read result
|
||||||
|
|
||||||
ADMUX = (ADMUX & ~(0x1F)) | 2; // select channel 2
|
ADMUX = (ADMUX & ~(0x1F)) | 2; // select channel 2
|
||||||
ADCSRA |= (1 << ADSC); // start single conversion
|
ADCSRA |= (1 << ADSC); // start single conversion
|
||||||
while (ADCSRA & (1 << ADSC)) ; // wait for conversion to end
|
while (ADCSRA & (1 << ADSC)) ; // wait for conversion to end
|
||||||
|
@ -344,16 +346,12 @@ void do_mode5(void)
|
||||||
music_setNote(400 +((0x1ff - led1) + (0x1ff - led2)) * 5, 0);
|
music_setNote(400 +((0x1ff - led1) + (0x1ff - led2)) * 5, 0);
|
||||||
discharge = true;
|
discharge = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
timer_set(&mytimer, 2); //relaunch timer
|
timer_set(&mytimer, 2); //relaunch timer
|
||||||
}//end if timer_expired
|
}//end if timer_expired
|
||||||
|
|
||||||
}//end mode5
|
}//end mode5
|
||||||
|
|
||||||
|
|
||||||
|
void __attribute__ ((noreturn)) main(void)
|
||||||
void __attribute__ ((noreturn))
|
|
||||||
main(void)
|
|
||||||
{
|
{
|
||||||
/* hardware initialisation: */
|
/* hardware initialisation: */
|
||||||
init_leds();
|
init_leds();
|
||||||
|
@ -365,6 +363,7 @@ void __attribute__ ((noreturn))
|
||||||
timer_init();
|
timer_init();
|
||||||
music_init();
|
music_init();
|
||||||
|
|
||||||
|
|
||||||
/* here the show begins: */
|
/* here the show begins: */
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
|
@ -373,24 +372,12 @@ void __attribute__ ((noreturn))
|
||||||
button_poll();
|
button_poll();
|
||||||
modeswitch_poll();
|
modeswitch_poll();
|
||||||
switch (OpMode) {
|
switch (OpMode) {
|
||||||
case MODE1:
|
case MODE1: do_mode1() ; break ;
|
||||||
do_mode1();
|
case MODE2: do_mode2() ; break ;
|
||||||
break;
|
case MODE3: do_mode3() ; break ;
|
||||||
case MODE2:
|
case MODE4: do_mode4() ; break ;
|
||||||
do_mode2();
|
case MODE5: do_mode5() ; break ;
|
||||||
break;
|
default: do_mode0() ; break ;
|
||||||
case MODE3:
|
|
||||||
do_mode3();
|
|
||||||
break;
|
|
||||||
case MODE4:
|
|
||||||
do_mode4();
|
|
||||||
break;
|
|
||||||
case MODE5:
|
|
||||||
do_mode5();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
do_mode0();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
OpMode = NextMode;
|
OpMode = NextMode;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user