X-Git-Url: http://git.droids-corp.org/?p=beacon-rx-433.git;a=blobdiff_plain;f=main.c;h=82f12d7cc28f1233a2ba3b0036670259c5f66a86;hp=35a86cf7de11c84fe90de9eba40665301eea7e71;hb=862393188b1eda01abbb9605e4396d9e06b825fd;hpb=0878ff0726cff2df249e065eebdbd9c35ce9071c diff --git a/main.c b/main.c index 35a86cf..82f12d7 100644 --- a/main.c +++ b/main.c @@ -27,12 +27,17 @@ #define RADIO_READ() (!!(PINB & (_BV(RADIO_BIT)))) #define BUTTON_BIT 3 -#define BUTTON_IS_PRESSED() (!!(PINB & (_BV(BUTTON_BIT)))) +#define BUTTON_IS_PRESSED() (!(PINB & (_BV(BUTTON_BIT)))) #define LED_BIT 4 #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,6 +349,21 @@ static uint8_t get_input(int i) #endif } +#ifndef HOST_VERSION +static void beep(void) +{ + uint16_t i; + + for (i = 0; i < 1000; i++) { + if (i < 250 && (i & 1)) + BUZZER_ON(); + else + BUZZER_OFF(); + wait_period(); + } +} +#endif + /* The CPU runs at 8Mhz. Fe = 5Khz @@ -354,11 +374,16 @@ static uint8_t get_input(int 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__) DDRB |= (1 << LED_BIT) | (1 << BUZZER_BIT); + PORTB |= (1 << BUTTON_BIT); /* pull up */ #endif i = 0; @@ -366,12 +391,42 @@ int main(void) while (1) { + /* wait until 200us is elapsed since previous call (5Khz) */ wait_period(); + detected = 0; + +#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); @@ -386,6 +441,22 @@ int main(void) else if (cpt_filter > 0) cpt_filter--; + 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", @@ -399,18 +470,28 @@ int main(void) else LED_OFF(); - /* output a square signal at 625 hz if beacon is - * detected */ - if ((detected == 1) && (i & 4)) - BUZZER_ON(); - else - BUZZER_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