a bit code beautification
This commit is contained in:
parent
799bbdeeeb
commit
06dc89109f
|
@ -69,7 +69,7 @@ void init_mic(void){
|
|||
ADCSRB &= (1<<ACME); //leave only ACME as it is (others zerp for free running)
|
||||
ADMUX = (ADMUX & ~(0x1F)) | 5; // select channel 5
|
||||
ADCSRA |= (1<<ADSC); // start conversion
|
||||
uint16_t dummy = ADCW; //read once
|
||||
uint16_t __attribute__((unused)) dummy = ADCW; //read once
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -145,7 +145,7 @@ void button_clear(uint8_t button){
|
|||
void stateswitch(uint8_t i ){
|
||||
switch(btnstates[i])
|
||||
{
|
||||
case BTNST_NTRL: //NEUTRAL
|
||||
case BTNST_NTRL:
|
||||
if (curinput & (1<<i)){ //button down
|
||||
btncounters[i] = 0;
|
||||
btnstates[i] = BTNST_DBNC;
|
||||
|
@ -156,8 +156,7 @@ void stateswitch(uint8_t i ){
|
|||
btnstates[i] = (curinput & (1<<i))? BTNST_SDN: BTNST_NTRL;
|
||||
(btncounters[i])++;
|
||||
break;
|
||||
|
||||
case BTNST_SDN: //is shortpressed and still held down
|
||||
case BTNST_SDN:
|
||||
if (curinput & (1<<i)){
|
||||
btncounters[i]++;
|
||||
if (btncounters[i] > BTN_T_LONGFACT){ //500ms held
|
||||
|
@ -168,27 +167,27 @@ void stateswitch(uint8_t i ){
|
|||
//signal shortclick
|
||||
}
|
||||
break;
|
||||
case BTNST_LDN: //is longpressed and still held down
|
||||
case BTNST_LDN:
|
||||
if (!(curinput & (1<<i))){
|
||||
//button was released
|
||||
btnstates[i] = BTNST_LUP; //signal longpress
|
||||
}
|
||||
break;
|
||||
case BTNST_SUP: //Button came up after being pressed shortly
|
||||
case BTNST_SUP:
|
||||
if ((curinput & (1<<i))){
|
||||
//button was pressed again or is bouncing after release
|
||||
btnstates[i] = BTNST_SUPDBNC; //going in special debounce
|
||||
btncounters[i] = 0;
|
||||
}
|
||||
break;
|
||||
case BTNST_LUP: //Button came up after being pressed for a long time
|
||||
case BTNST_LUP:
|
||||
if ((curinput & (1<<i))){
|
||||
//button was pressed again or is bouncing after release
|
||||
btnstates[i] = BTNST_LUPDBNC; //going in special debounce
|
||||
btncounters[i] = 0;
|
||||
}
|
||||
break;
|
||||
case BTNST_SUPDBNC: //Button was pressed again after beeing short pressed(or is bouncing)
|
||||
case BTNST_SUPDBNC:
|
||||
if ((curinput & (1<<i))){
|
||||
//button is still pressed --> going to shortpress
|
||||
btncounters[i]++;
|
||||
|
@ -197,7 +196,7 @@ void stateswitch(uint8_t i ){
|
|||
btnstates[i] = BTNST_SUP; //nope, it was bouncing, back to old state
|
||||
}
|
||||
break;
|
||||
case BTNST_LUPDBNC: //Button was pressed again after beeing short pressed(or is bouncing)
|
||||
case BTNST_LUPDBNC:
|
||||
if ((curinput & (1<<i))){
|
||||
//button is still pressed --> going to shortpress
|
||||
btncounters[i]++;
|
||||
|
@ -209,6 +208,7 @@ void stateswitch(uint8_t i ){
|
|||
default: //curently catches nothing
|
||||
// do nothing yet
|
||||
;
|
||||
break;
|
||||
} //end switch
|
||||
timer_set(&btntimers[i], BTN_T_DEBOUNCE);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
|
||||
/* Hardware abstraction layer for Pentabug hardware */
|
||||
|
||||
enum { BUZZR_OUT, //initialize buzzer for OUTPUT mode (emmiting soundwaves)
|
||||
enum {
|
||||
BUZZR_OUT, //initialize buzzer for OUTPUT mode (emmiting soundwaves)
|
||||
BUZZR_IN //initialize buzzer for INPUT mode (registering soundwaves)
|
||||
};
|
||||
|
||||
|
@ -19,27 +20,20 @@ enum { BUZZR_OUT, //initialize buzzer for OUTPUT mode (emmiting soundwaves)
|
|||
#define BTN_BUTTONS 2 //numer of Buttons
|
||||
#define BTN_T_DEBOUNCE 5 // 50ms debounce time = minimum short press time
|
||||
#define BTN_T_LONGFACT 10 // after 10 * T_DEBOUNCE = 500ms button registers as long pressed
|
||||
|
||||
//BUTTON state machine states
|
||||
|
||||
#define BTNST_NTRL 0 // neutral - initial state nothing interesting, please go along
|
||||
|
||||
#define BTNST_DBNC 1 // debounce - pin went up, but we wait for things to stabilize and stop oscillating
|
||||
|
||||
#define BTNST_SDN 4 // affirmative, button is pressed,
|
||||
// and it's pressed no longer than
|
||||
// BTN_T_LONGFACT * BTN_T_DEBOUNCE * 10ms
|
||||
// and it's pressed no longer than
|
||||
// BTN_T_LONGFACT * BTN_T_DEBOUNCE * 10ms
|
||||
|
||||
#define BTNST_SUP 8 // and button went up after beeing pressed for a _short_ time
|
||||
|
||||
#define BTNST_LDN 16 // button is still down for more than
|
||||
//BTN_T_LONGFACT * BTN_T_DEBOUNCE * 10ms
|
||||
//BTN_T_LONGFACT * BTN_T_DEBOUNCE * 10ms
|
||||
|
||||
#define BTNST_LUP 32 // button came up after being pressed for a long time
|
||||
|
||||
#define BTNST_SUPDBNC 64 // debounce after short up
|
||||
#define BTNST_LUPDBNC 128 // debounce after long up
|
||||
|
||||
void init_leds(void);
|
||||
void led_on(int);
|
||||
void led_off(int);
|
||||
|
@ -61,7 +55,6 @@ bool switch_r(void); //switch pressed?
|
|||
void init_motor(void);
|
||||
void set_motor(int);
|
||||
|
||||
|
||||
// higher level functions for accessing switches
|
||||
|
||||
void button_poll(void); //needs to be polled in regular intervalls (lets say every 10ms)
|
||||
|
@ -72,5 +65,4 @@ void button_clear(uint8_t button);
|
|||
//test if buttonstate of btn eqals btnstate, returns true if yes
|
||||
bool btn_state(uint8_t btnstate, uint8_t btn);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
300
firmware/main.c
300
firmware/main.c
|
@ -20,28 +20,27 @@
|
|||
#define MODE4 4
|
||||
#define NUM_MODES 5
|
||||
|
||||
|
||||
|
||||
uint8_t OpMode = MODE0; //Operation mode
|
||||
bool ModeChanged = true;
|
||||
|
||||
void modeswitch_poll(void){
|
||||
if (btn_state(BTNST_LUP,BTN_LEFT)){
|
||||
void modeswitch_poll(void) {
|
||||
if (btn_state(BTNST_LUP, BTN_LEFT)) {
|
||||
//opmode -
|
||||
OpMode = (0 == OpMode) ? (NUM_MODES-1) : (OpMode-1);
|
||||
OpMode = (0 == OpMode) ? (NUM_MODES - 1) : (OpMode - 1);
|
||||
ModeChanged = true;
|
||||
button_clear(BTN_LEFT);
|
||||
};
|
||||
if (btn_state(BTNST_LUP,BTN_RIGHT)){
|
||||
if (btn_state(BTNST_LUP, BTN_RIGHT)) {
|
||||
//opmode +
|
||||
ModeChanged = true;
|
||||
OpMode = ((NUM_MODES-1) == OpMode) ? 0 : (OpMode+1);
|
||||
OpMode = ((NUM_MODES - 1) == OpMode) ? 0 : (OpMode + 1);
|
||||
button_clear(BTN_RIGHT);
|
||||
};
|
||||
return;
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
void do_mode0(void){
|
||||
void do_mode0(void) {
|
||||
static timer_t mytimer;
|
||||
static uint16_t maxval;
|
||||
static uint16_t lastmaxval;
|
||||
|
@ -50,229 +49,224 @@ void do_mode0(void){
|
|||
static bool sound_on;
|
||||
static bool motor_on;
|
||||
|
||||
if (ModeChanged){
|
||||
if (ModeChanged) {
|
||||
timer_set(&mytimer, 10);
|
||||
maxval=0;
|
||||
maxval = 0;
|
||||
ModeChanged = false;
|
||||
signaling = false;
|
||||
sound_on = true;
|
||||
motor_on = true;
|
||||
init_mic();
|
||||
maxval=0;
|
||||
maxval = 0;
|
||||
};
|
||||
|
||||
// single measurement
|
||||
curval =ADCW; // read result
|
||||
maxval = (curval>maxval) ? curval : maxval;
|
||||
|
||||
curval = ADCW; // read result
|
||||
maxval = (curval > maxval) ? curval : maxval;
|
||||
|
||||
//check for Buttons
|
||||
if (btn_state(BTNST_SUP,BTN_LEFT)) {
|
||||
if (btn_state(BTNST_SUP, BTN_LEFT)) {
|
||||
button_clear(BTN_LEFT);
|
||||
sound_on = !sound_on;
|
||||
};
|
||||
if (btn_state(BTNST_SUP,BTN_RIGHT)) {
|
||||
if (btn_state(BTNST_SUP, BTN_RIGHT)) {
|
||||
button_clear(BTN_RIGHT);
|
||||
motor_on = !motor_on;
|
||||
};
|
||||
if(timer_expired(&mytimer)){
|
||||
if (timer_expired(&mytimer)) {
|
||||
if (signaling) {
|
||||
//turn off sound
|
||||
music_setNote(NOTE_PAUSE,0); //mute
|
||||
music_setNote(NOTE_PAUSE, 0); //mute
|
||||
set_motor(MOTOR_OFF);
|
||||
//re-init mic
|
||||
init_mic();
|
||||
led_off(LED_R|LED_L);
|
||||
led_off(LED_R | LED_L);
|
||||
timer_set(&mytimer, 1);
|
||||
signaling = false;
|
||||
lastmaxval= 10000;
|
||||
lastmaxval = 10000;
|
||||
} else { //sound was off wer're in measuring mode
|
||||
if(maxval>lastmaxval){
|
||||
if (maxval > lastmaxval) {
|
||||
USART0_put_uint16(maxval);
|
||||
USART0_crlf();
|
||||
led_on(LED_R|LED_L);
|
||||
led_on(LED_R | LED_L);
|
||||
init_buzzr(); //buzzr to output
|
||||
if (sound_on) music_setNote(NOTE_C,5);
|
||||
if (motor_on) set_motor(MOTOR_ON);
|
||||
if (sound_on)
|
||||
music_setNote(NOTE_C, 5);
|
||||
if (motor_on)
|
||||
set_motor(MOTOR_ON);
|
||||
signaling = true;
|
||||
timer_set(&mytimer, 5); //sound duration
|
||||
} else {
|
||||
timer_set(&mytimer, 1);
|
||||
};
|
||||
lastmaxval=maxval;
|
||||
maxval=0;
|
||||
lastmaxval = maxval;
|
||||
maxval = 0;
|
||||
}; //end if soundon
|
||||
|
||||
}; //end if timer_expired
|
||||
|
||||
}
|
||||
;
|
||||
//end do_mode0
|
||||
|
||||
|
||||
}; //end do_mode0
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void do_mode1(void){
|
||||
void do_mode1(void) {
|
||||
static uint16_t tone;
|
||||
if (ModeChanged){
|
||||
if (ModeChanged) {
|
||||
ModeChanged = false;
|
||||
tone = 1000;
|
||||
music_setNote(tone,0);
|
||||
led_off(LED_L|LED_R);
|
||||
music_setNote(tone, 0);
|
||||
led_off(LED_L | LED_R);
|
||||
};
|
||||
if (btn_state(BTNST_SUP,BTN_LEFT)) {
|
||||
if (btn_state(BTNST_SUP, BTN_LEFT)) {
|
||||
button_clear(BTN_LEFT);
|
||||
tone += 10;
|
||||
music_setNote(tone,0);
|
||||
music_setNote(tone, 0);
|
||||
};
|
||||
if (btn_state(BTNST_SUP,BTN_RIGHT)) {
|
||||
if (btn_state(BTNST_SUP, BTN_RIGHT)) {
|
||||
button_clear(BTN_RIGHT);
|
||||
tone -= 10;
|
||||
music_setNote(tone,0);
|
||||
music_setNote(tone, 0);
|
||||
};
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
void do_mode2(void){
|
||||
void do_mode2(void) {
|
||||
static timer_t mytimer;
|
||||
uint8_t max = 50;
|
||||
uint8_t min = 5;
|
||||
uint8_t maxfreq = 6000;
|
||||
uint8_t minfreq = 2000;
|
||||
uint16_t maxfreq = 6000;
|
||||
uint16_t minfreq = 2000;
|
||||
|
||||
if (ModeChanged){
|
||||
if (ModeChanged) {
|
||||
ModeChanged = false;
|
||||
music_setNote(NOTE_PAUSE,4); //mute
|
||||
music_setNote(NOTE_PAUSE, 4); //mute
|
||||
timer_set(&mytimer, 10);
|
||||
led_off(LED_L|LED_R);
|
||||
led_off(LED_L | LED_R);
|
||||
set_motor(MOTOR_OFF);
|
||||
}
|
||||
|
||||
if(timer_expired(&mytimer)){
|
||||
if (timer_expired(&mytimer)) {
|
||||
set_motor(MOTOR_OFF);
|
||||
music_setNote(NOTE_PAUSE,0); //mute
|
||||
music_setNote(NOTE_PAUSE, 0); //mute
|
||||
// set random led
|
||||
switch(rand() % 4) {
|
||||
case 0 : led_on(LED_L); break;
|
||||
case 1 : led_on(LED_R); break;
|
||||
case 2 : led_on(LED_L|LED_R); break;
|
||||
default : led_off(LED_L|LED_R);
|
||||
switch (rand() % 4) {
|
||||
case 0:
|
||||
led_on(LED_L);
|
||||
break;
|
||||
case 1:
|
||||
led_on(LED_R);
|
||||
break;
|
||||
case 2:
|
||||
led_on(LED_L | LED_R);
|
||||
break;
|
||||
default:
|
||||
led_off(LED_L | LED_R);
|
||||
break;
|
||||
};
|
||||
// decide if to switch motor on (40% chance)
|
||||
if (rand()%5>2) set_motor(MOTOR_ON);
|
||||
if (rand() % 5 > 2)
|
||||
set_motor(MOTOR_ON);
|
||||
|
||||
//decide if to play sound (70% chance)
|
||||
if (rand()%10>2) {
|
||||
music_setNote((rand() % (maxfreq-minfreq)) + minfreq,0);
|
||||
if (rand() % 10 > 2) {
|
||||
music_setNote((rand() % (maxfreq - minfreq)) + minfreq, 0);
|
||||
|
||||
}
|
||||
|
||||
timer_set(&mytimer, (rand() % (max-min)) + min);
|
||||
timer_set(&mytimer, (rand() % (max - min)) + min);
|
||||
}; //end if timer_expired
|
||||
|
||||
}
|
||||
;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
void do_mode3(void){
|
||||
void do_mode3(void) {
|
||||
static timer_t mytimer;
|
||||
static bool blink;
|
||||
if (ModeChanged){
|
||||
if (ModeChanged) {
|
||||
ModeChanged = false;
|
||||
music_setNote(NOTE_PAUSE,4); //mute
|
||||
music_setNote(NOTE_PAUSE, 4); //mute
|
||||
set_motor(MOTOR_OFF);
|
||||
timer_set(&mytimer, 10);
|
||||
blink = false;
|
||||
};
|
||||
|
||||
if(timer_expired(&mytimer)){
|
||||
if (timer_expired(&mytimer)) {
|
||||
if (!blink) {
|
||||
//lets blink
|
||||
led_on(LED_L|LED_R);
|
||||
led_on(LED_L | LED_R);
|
||||
timer_set(&mytimer, 1);
|
||||
blink=true;
|
||||
blink = true;
|
||||
} else {
|
||||
//stop blink
|
||||
led_off(LED_L|LED_R);
|
||||
led_off(LED_L | LED_R);
|
||||
timer_set(&mytimer, 123);
|
||||
blink=false;
|
||||
blink = false;
|
||||
}
|
||||
|
||||
|
||||
} //end if timer_expired
|
||||
|
||||
if (btn_state(BTNST_SUP,BTN_LEFT)) {
|
||||
if (btn_state(BTNST_SUP, BTN_LEFT)) {
|
||||
button_clear(BTN_LEFT);
|
||||
set_motor(MOTOR_OFF);
|
||||
|
||||
};
|
||||
if (btn_state(BTNST_SUP,BTN_RIGHT)) {
|
||||
if (btn_state(BTNST_SUP, BTN_RIGHT)) {
|
||||
button_clear(BTN_RIGHT);
|
||||
set_motor(MOTOR_ON); };
|
||||
set_motor(MOTOR_ON);
|
||||
};
|
||||
|
||||
}
|
||||
;
|
||||
|
||||
|
||||
};
|
||||
|
||||
void do_mode4(void){
|
||||
void do_mode4(void) {
|
||||
uint8_t max = 200;
|
||||
uint8_t min = 10;
|
||||
|
||||
static timer_t mytimer;
|
||||
static bool blink;
|
||||
if (ModeChanged){
|
||||
music_setNote(NOTE_PAUSE,0);
|
||||
if (ModeChanged) {
|
||||
music_setNote(NOTE_PAUSE, 0);
|
||||
ModeChanged = false;
|
||||
timer_set(&mytimer, 10);
|
||||
blink = false;
|
||||
};
|
||||
if(timer_expired(&mytimer)){
|
||||
if (timer_expired(&mytimer)) {
|
||||
if (!blink) {
|
||||
//lets blink
|
||||
int i = (rand() % 3);
|
||||
switch(i) {
|
||||
case 0 : led_on(LED_L); break;
|
||||
case 1 : led_on(LED_R); break;
|
||||
default : led_on(LED_L|LED_R);
|
||||
switch (i) {
|
||||
case 0:
|
||||
led_on(LED_L);
|
||||
break;
|
||||
case 1:
|
||||
led_on(LED_R);
|
||||
break;
|
||||
default:
|
||||
led_on(LED_L | LED_R);
|
||||
break;
|
||||
};
|
||||
if (rand()%10>8) set_motor(MOTOR_ON);
|
||||
music_setNote(NOTE_C,5);
|
||||
if (rand() % 10 > 8)
|
||||
set_motor(MOTOR_ON);
|
||||
music_setNote(NOTE_C, 5);
|
||||
timer_set(&mytimer, 2);
|
||||
blink=true;
|
||||
blink = true;
|
||||
} else {
|
||||
//stop blink
|
||||
led_off(LED_L|LED_R);
|
||||
led_off(LED_L | LED_R);
|
||||
set_motor(MOTOR_OFF);
|
||||
music_setNote(NOTE_PAUSE,0);
|
||||
timer_set(&mytimer, (rand() % (max-min)) + min);
|
||||
music_setNote(NOTE_PAUSE, 0);
|
||||
timer_set(&mytimer, (rand() % (max - min)) + min);
|
||||
|
||||
blink=false;
|
||||
blink = false;
|
||||
}
|
||||
|
||||
} //end if timer_expired
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
;
|
||||
|
||||
void __attribute__((noreturn))
|
||||
main(void)
|
||||
{
|
||||
main(void) {
|
||||
/* hardware initialisation: */
|
||||
init_leds();
|
||||
init_buzzr();
|
||||
|
@ -283,74 +277,34 @@ main(void)
|
|||
timer_init();
|
||||
music_init();
|
||||
|
||||
/* here the show begins:*/
|
||||
sei();
|
||||
timer_t t;
|
||||
uint8_t ledstate_l =0;
|
||||
uint8_t ledstate_r =0;
|
||||
uint8_t motorstate =0;
|
||||
timer_set(&t, 100);
|
||||
for(;;) /* ever */ {
|
||||
/* here the show begins:*/sei();
|
||||
|
||||
for (;;) /* ever */{
|
||||
//do something
|
||||
//main polling loop;
|
||||
button_poll();
|
||||
modeswitch_poll();
|
||||
switch(OpMode){
|
||||
case MODE1 : do_mode1(); break;
|
||||
case MODE2 : do_mode2(); break;
|
||||
case MODE3 : do_mode3(); break;
|
||||
case MODE4 : do_mode4(); break;
|
||||
default : do_mode0();
|
||||
}
|
||||
/*
|
||||
|
||||
if (timer_expired(&t)){
|
||||
//while left button is pressed switch left led on
|
||||
if (btn_state(BTNST_SDN,BTN_LEFT)|btn_state(BTNST_LDN,BTN_LEFT)){
|
||||
led_on(LED_L);
|
||||
} else {
|
||||
led_off(LED_L);
|
||||
};
|
||||
if (btn_state(BTNST_SUP,BTN_LEFT)){
|
||||
music_setNote(NOTE_PAUSE,0); //mute
|
||||
button_clear(BTN_LEFT);
|
||||
switch (OpMode) {
|
||||
case MODE1:
|
||||
do_mode1();
|
||||
break;
|
||||
case MODE2:
|
||||
do_mode2();
|
||||
break;
|
||||
case MODE3:
|
||||
do_mode3();
|
||||
break;
|
||||
case MODE4:
|
||||
do_mode4();
|
||||
break;
|
||||
default:
|
||||
do_mode0();
|
||||
break;
|
||||
};
|
||||
|
||||
//when right button has been pressed short, toggle right led
|
||||
if (btn_state(BTNST_SUP,BTN_RIGHT)){
|
||||
if (ledstate_r ==0){
|
||||
ledstate_r = 1;
|
||||
led_on(LED_R);
|
||||
} else {
|
||||
ledstate_r = 0;
|
||||
led_off(LED_R);
|
||||
};
|
||||
music_setNote(500,0); //50Hz sound
|
||||
button_clear(BTN_RIGHT); //this is important, to show the event has been processed
|
||||
};
|
||||
//when right button has been pressed long, toggle motor
|
||||
if (btn_state(BTNST_LUP,BTN_RIGHT)){
|
||||
if (motorstate ==0){
|
||||
motorstate = 1;
|
||||
set_motor(MOTOR_ON);
|
||||
} else {
|
||||
motorstate = 0;
|
||||
set_motor(MOTOR_OFF);
|
||||
};
|
||||
music_setNote(NOTE_B,5); //1000Hz sound
|
||||
button_clear(BTN_RIGHT); //this is important, to show the event has been processed
|
||||
};
|
||||
|
||||
timer_set(&t, 5);
|
||||
}; //end if timer expired
|
||||
//USART0_put_uint16(0xA09F);
|
||||
//USART0_crlf();
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
/* never return 0; */
|
||||
}
|
||||
|
||||
|
||||
;
|
||||
|
||||
|
|
|
@ -1,3 +1,2 @@
|
|||
#pragma once
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user