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)
26 static uint32_t bitfield;
29 static int32_t fil_fond = 0;
30 static int32_t fil_harm1 = 0;
31 static int32_t fil_other = 0;
33 static int16_t fil_fond = 0;
34 static int16_t fil_harm1 = 0;
35 static int16_t fil_other = 0;
37 static int16_t pow_fond = 0;
38 static int16_t pow_harm1 = 0;
39 static int16_t pow_other = 0;
40 static int16_t cpt_filter = 0;
43 /* when compiled for host, we check that there is no saturation when
44 * applying the filter. This should not happen as the gain of the
46 int32_t saturate16(int32_t in)
50 printf("saturation\n");
52 if (fil_fond < -32768L) {
54 printf("saturation\n");
60 /* Apply the 3 filters on the bitfield. The results are stored in
61 * fil_fond, fil_harm1 and fil_other. */
62 static void apply_filters(void)
68 if (bitfield & (1UL << 0)) {
74 if (bitfield & (1UL << 1)) {
80 if (bitfield & (1UL << 2)) {
86 if (bitfield & (1UL << 3)) {
92 if (bitfield & (1UL << 4)) {
98 if (bitfield & (1UL << 5)) {
104 if (bitfield & (1UL << 6)) {
110 if (bitfield & (1UL << 7)) {
116 if (bitfield & (1UL << 8)) {
122 if (bitfield & (1UL << 9)) {
128 if (bitfield & (1UL << 10)) {
134 if (bitfield & (1UL << 11)) {
140 if (bitfield & (1UL << 12)) {
146 if (bitfield & (1UL << 13)) {
152 if (bitfield & (1UL << 14)) {
158 if (bitfield & (1UL << 15)) {
164 if (bitfield & (1UL << 16)) {
170 if (bitfield & (1UL << 17)) {
176 if (bitfield & (1UL << 18)) {
182 if (bitfield & (1UL << 19)) {
188 if (bitfield & (1UL << 20)) {
194 if (bitfield & (1UL << 21)) {
200 if (bitfield & (1UL << 22)) {
206 if (bitfield & (1UL << 23)) {
212 if (bitfield & (1UL << 24)) {
218 if (bitfield & (1UL << 25)) {
224 if (bitfield & (1UL << 26)) {
230 if (bitfield & (1UL << 27)) {
236 if (bitfield & (1UL << 28)) {
242 if (bitfield & (1UL << 29)) {
248 if (bitfield & (1UL << 30)) {
254 if (bitfield & (1UL << 31)) {
261 fil_fond = saturate16(fil_fond);
262 fil_harm1 = saturate16(fil_harm1);
263 fil_other = saturate16(fil_other);
267 /* Return abs(val) * 0.03125 + prev_out * 0.96875
268 * This is a simple "mean" filter. */
269 static inline void mean_pow(int16_t *state, int16_t val)
280 /* new += prev_out * 0.96875 */
298 #define MAX_CPT_FILTER 500
300 /* init timer (only on real hw, not on simulator which simulates an
302 static void init_timer(void)
304 #if defined(__AVR_ATtiny45__)
306 TCCR0B = (1 << CS01); /* clk/8 = 1Mhz */
310 /* wait new ech period (only on real hw, not on simulator which
311 * simulates an ATmega128). period=200 -> 5 khz */
312 static void wait_period(void)
314 #if defined(__AVR_ATtiny45__)
315 static uint8_t prev = 0;
321 } while (diff < 200);
326 /* return the input: on host, we use the table generated from the wav
327 * file, on the attiny, we use the real PIN input */
328 static uint8_t get_input(int i)
333 return !!(PINB & RADIO_BIT);
337 static void set_output(int16_t i)
341 if (cpt_filter > 250)
345 printf("%d fil_fond=%d fil_harm1=%d fil_other=%d\n",
346 i, fil_fond, fil_harm1, fil_other);
347 printf("%d pow_fond=%d pow_harm1=%d pow_other=%d cpt_filter=%d detected=%d\n",
348 i, pow_fond, pow_harm1, pow_other, cpt_filter, detected);
350 /* when we receive, output a square signal at 625 hz */
351 if ((detected == 1) && (i & 4))
352 PORTB |= (1 << BUZ_BIT);
354 PORTB &= ~(1 << BUZ_BIT);
359 The CPU runs at 8Mhz.
369 /* led and buzzer are outputs */
370 #if defined(__AVR_ATtiny45__)
371 DDRB |= (1 << LED_BIT) | (1 << BUZ_BIT);
375 PORTB |= (1 << LED_BIT);
377 PORTB &= ~(1 << LED_BIT);
388 /* push one bit in bitfield */
390 bitfield |= (uint32_t)get_input(i);
393 mean_pow(&pow_fond, fil_fond);
394 mean_pow(&pow_harm1, fil_harm1);
395 mean_pow(&pow_other, fil_other);
397 /* cpt_filter is increased when we consider that we are
398 * receiving the signal */
399 if ((pow_fond/3) > (pow_harm1/2) &&
400 (pow_fond/2) > pow_other &&
401 cpt_filter < MAX_CPT_FILTER)
403 else if (cpt_filter > 0)