X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;ds=sidebyside;f=main.c;h=e176a1f48b7260e48333b6a640ea0058abfef16f;hb=0290a56c2b1e256e19471756921e4560310ca08c;hp=e794376fcf5a6b8da7de5fe4bb0d12d86245dadb;hpb=9b05952e09169f0d32f1dfcae129cab2fba20e95;p=beacon-rx-433.git diff --git a/main.c b/main.c index e794376..e176a1f 100644 --- a/main.c +++ b/main.c @@ -33,6 +33,11 @@ #define LED_ON() PORTB |= _BV(LED_BIT) #define LED_OFF() PORTB &= (~_BV(LED_BIT)) +#define MODE_NO_FILTER 0 +#define MODE_FILTER_THRES1 1 +#define MODE_FILTER_THRES2 2 +#define MODE_MAX 3 + static uint32_t bitfield; #ifdef HOST_VERSION @@ -344,31 +349,19 @@ static uint8_t get_input(int i) #endif } -static void set_output(int16_t i) +#ifndef HOST_VERSION +static void beep(void) { - int8_t detected = 0; - - if (cpt_filter > 250) - detected = 1; - -#ifdef HOST_VERSION - printf("%d fil_fond=%d fil_harm1=%d fil_other=%d\n", - i, fil_fond, fil_harm1, fil_other); - printf("%d pow_fond=%d pow_harm1=%d pow_other=%d cpt_filter=%d detected=%d\n", - i, pow_fond, pow_harm1, pow_other, cpt_filter, detected); -#else - if (detected) - LED_ON(); - else - LED_OFF(); + uint16_t i; - /* when we receive, output a square signal at 625 hz */ - if ((detected == 1) && (i & 4)) - BUZZER_ON(); - else - BUZZER_OFF(); -#endif + for (i = 0; i < 1000; i++) { + if (i < 250 && (i & 1)) + BUZZER_ON(); + else + BUZZER_OFF(); + } } +#endif /* The CPU runs at 8Mhz. @@ -379,7 +372,12 @@ static void set_output(int16_t i) */ int main(void) { + int8_t detected = 0; + uint8_t mode = MODE_FILTER_THRES2; int16_t i; +#ifndef HOST_VERSION + uint8_t button = 0, button_filter = 0; +#endif /* led and buzzer are outputs */ #if defined(__AVR_ATtiny45__) @@ -391,12 +389,41 @@ int main(void) while (1) { + /* wait until 200us is elapsed since previous call (5Khz) */ wait_period(); +#ifndef HOST_VERSION + /* filter button */ + button = 0; + if (BUTTON_IS_PRESSED() && button_filter < 10) { + button_filter++; + if (button_filter == 10) + button = 1; + } + else if (!BUTTON_IS_PRESSED()) + button_filter = 0; + + /* change mode if button is pressed */ + if (button) { + uint8_t j; + + mode ++; + if (mode >= MODE_MAX) + mode = 0; + + for (j = 0; j <= mode; j++) + beep(); + + /* reset input history */ + bitfield = 0; + } +#endif + /* push one bit in bitfield */ bitfield <<= 1UL; bitfield |= (uint32_t)get_input(i); + /* filter the input */ apply_filters(); mean_pow(&pow_fond, fil_fond); mean_pow(&pow_harm1, fil_harm1); @@ -411,10 +438,57 @@ int main(void) else if (cpt_filter > 0) cpt_filter--; - set_output(i); + switch (mode) { + case MODE_NO_FILTER: + case MODE_FILTER_THRES1: + /* low threshold, some risk of false positive */ + if (cpt_filter > 100) + detected = 1; + break; + case MODE_FILTER_THRES2: + /* high threshold */ + if (cpt_filter > 250) + detected = 1; + break; + default: + break; + } + +#ifdef HOST_VERSION + /* display values */ + printf("%d fil_fond=%d fil_harm1=%d fil_other=%d\n", + i, fil_fond, fil_harm1, fil_other); + printf("%d pow_fond=%d pow_harm1=%d pow_other=%d " + "cpt_filter=%d detected=%d\n", + i, pow_fond, pow_harm1, pow_other, cpt_filter, detected); +#else + if (detected) + LED_ON(); + else + LED_OFF(); + + /* if filtering is disabled, just copy the radio input + * on the buzzer */ + if (mode == MODE_NO_FILTER) { + if (RADIO_READ()) + BUZZER_ON(); + else + BUZZER_OFF(); + } + /* with filtering enabled, output a square signal at + * 625 hz if beacon is detected */ + else { + if ((detected == 1) && (i & 4)) + BUZZER_ON(); + else + BUZZER_OFF(); + } +#endif i ++; + #ifdef HOST_VERSION + /* exit when all samples are processed */ if (i >= sizeof(x)) break; #endif