2 #include <aversive/wait.h>
10 #if defined(WAV_NOISE)
11 #include "noise.wav.c"
12 #elif defined(WAV_TONE_HIGHNOISE)
13 #include "tone-highnoise.wav.c"
14 #elif defined(WAV_TONE_LOWNOISE)
15 #include "tone-lownoise.wav.c"
16 #elif defined(WAV_TONE)
23 #define BUZZER_ON() PORTB |= _BV(BUZZER_BIT)
24 #define BUZZER_OFF() PORTB &= (~_BV(BUZZER_BIT))
27 #define RADIO_READ() (!!(PINB & (_BV(RADIO_BIT))))
30 #define BUTTON_IS_PRESSED() (!!(PINB & (_BV(BUTTON_BIT))))
33 #define LED_ON() PORTB |= _BV(LED_BIT)
34 #define LED_OFF() PORTB &= (~_BV(LED_BIT))
36 static uint32_t bitfield;
39 static int32_t fil_fond = 0;
40 static int32_t fil_harm1 = 0;
41 static int32_t fil_other = 0;
43 static int16_t fil_fond = 0;
44 static int16_t fil_harm1 = 0;
45 static int16_t fil_other = 0;
47 static int16_t pow_fond = 0;
48 static int16_t pow_harm1 = 0;
49 static int16_t pow_other = 0;
50 static int16_t cpt_filter = 0;
53 /* when compiled for host, we check that there is no saturation when
54 * applying the filter. This should not happen as the gain of the
56 int32_t saturate16(int32_t in)
60 printf("saturation\n");
62 if (fil_fond < -32768L) {
64 printf("saturation\n");
70 /* Apply the 3 filters on the bitfield. The results are stored in
71 * fil_fond, fil_harm1 and fil_other. */
72 static void apply_filters(void)
78 if (bitfield & (1UL << 0)) {
84 if (bitfield & (1UL << 1)) {
90 if (bitfield & (1UL << 2)) {
96 if (bitfield & (1UL << 3)) {
102 if (bitfield & (1UL << 4)) {
108 if (bitfield & (1UL << 5)) {
114 if (bitfield & (1UL << 6)) {
120 if (bitfield & (1UL << 7)) {
126 if (bitfield & (1UL << 8)) {
132 if (bitfield & (1UL << 9)) {
138 if (bitfield & (1UL << 10)) {
144 if (bitfield & (1UL << 11)) {
150 if (bitfield & (1UL << 12)) {
156 if (bitfield & (1UL << 13)) {
162 if (bitfield & (1UL << 14)) {
168 if (bitfield & (1UL << 15)) {
174 if (bitfield & (1UL << 16)) {
180 if (bitfield & (1UL << 17)) {
186 if (bitfield & (1UL << 18)) {
192 if (bitfield & (1UL << 19)) {
198 if (bitfield & (1UL << 20)) {
204 if (bitfield & (1UL << 21)) {
210 if (bitfield & (1UL << 22)) {
216 if (bitfield & (1UL << 23)) {
222 if (bitfield & (1UL << 24)) {
228 if (bitfield & (1UL << 25)) {
234 if (bitfield & (1UL << 26)) {
240 if (bitfield & (1UL << 27)) {
246 if (bitfield & (1UL << 28)) {
252 if (bitfield & (1UL << 29)) {
258 if (bitfield & (1UL << 30)) {
264 if (bitfield & (1UL << 31)) {
271 fil_fond = saturate16(fil_fond);
272 fil_harm1 = saturate16(fil_harm1);
273 fil_other = saturate16(fil_other);
277 /* Return abs(val) * 0.03125 + prev_out * 0.96875
278 * This is a simple "mean" filter. */
279 static inline void mean_pow(int16_t *state, int16_t val)
290 /* new += prev_out * 0.96875 */
308 #define MAX_CPT_FILTER 500
310 /* init timer (only on real hw, not on simulator which simulates an
312 static void init_timer(void)
314 #if defined(__AVR_ATtiny45__)
316 TCCR0B = (1 << CS01); /* clk/8 = 1Mhz */
320 /* wait new ech period (only on real hw, not on simulator which
321 * simulates an ATmega128). period=200 -> 5 khz */
322 static void wait_period(void)
324 #if defined(__AVR_ATtiny45__)
325 static uint8_t prev = 0;
331 } while (diff < 200);
336 /* return the input: on host, we use the table generated from the wav
337 * file, on the attiny, we use the real PIN input */
338 static uint8_t get_input(int i)
348 The CPU runs at 8Mhz.
359 /* led and buzzer are outputs */
360 #if defined(__AVR_ATtiny45__)
361 DDRB |= (1 << LED_BIT) | (1 << BUZZER_BIT);
371 /* push one bit in bitfield */
373 bitfield |= (uint32_t)get_input(i);
376 mean_pow(&pow_fond, fil_fond);
377 mean_pow(&pow_harm1, fil_harm1);
378 mean_pow(&pow_other, fil_other);
380 /* cpt_filter is increased when we consider that we are
381 * receiving the signal */
382 if ((pow_fond/3) > (pow_harm1/2) &&
383 (pow_fond/2) > pow_other &&
384 cpt_filter < MAX_CPT_FILTER)
386 else if (cpt_filter > 0)
391 printf("%d fil_fond=%d fil_harm1=%d fil_other=%d\n",
392 i, fil_fond, fil_harm1, fil_other);
393 printf("%d pow_fond=%d pow_harm1=%d pow_other=%d "
394 "cpt_filter=%d detected=%d\n",
395 i, pow_fond, pow_harm1, pow_other, cpt_filter, detected);
402 /* output a square signal at 625 hz if beacon is
404 if ((detected == 1) && (i & 4))