|
| 1 | +#include "direwolf.h" |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +#include "eotd_send.h" |
| 6 | +#include "audio.h" |
| 7 | +#include "gen_tone.h" |
| 8 | +#include "eotd_defs.h" |
| 9 | + |
| 10 | +#define EOTD_SILENCE_SAMPLES 1000 |
| 11 | +//#define EOTD_SEND_DEBUG |
| 12 | + |
| 13 | +static int eotd_fs[] = {1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0}; |
| 14 | +static int hotd_fs[] = {1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1 }; |
| 15 | + |
| 16 | +void my_tone_gen_put_bit (int chan, int bit) { |
| 17 | + |
| 18 | +#ifdef EOTD_SEND_DEBUG |
| 19 | + printf("mytone bit %d\n", bit); |
| 20 | +#endif |
| 21 | + tone_gen_put_bit (chan, bit); |
| 22 | +} |
| 23 | + |
| 24 | +void my_gen_tone_put_sample (int chan, int a, int sam) { |
| 25 | + |
| 26 | +#ifdef EOTD_SEND_DEBUG |
| 27 | + printf("mysilence sample %d\n", sam); |
| 28 | +#endif |
| 29 | + gen_tone_put_sample (chan, a, sam); |
| 30 | +} |
| 31 | + |
| 32 | +void send_preamble(int chan, char type) { |
| 33 | + int bit = 0; |
| 34 | + int preamble_count; |
| 35 | + int fs_count; |
| 36 | + int *fs; |
| 37 | + |
| 38 | + if (type == EOTD_TYPE_R2F) { |
| 39 | + bit = 0; |
| 40 | + preamble_count = 69; |
| 41 | + fs_count = sizeof(eotd_fs) / sizeof(int); |
| 42 | + fs = eotd_fs; |
| 43 | + } else { |
| 44 | + preamble_count = 456; |
| 45 | + fs_count = sizeof(hotd_fs) / sizeof(int); |
| 46 | + fs = hotd_fs; |
| 47 | + } |
| 48 | + |
| 49 | + for (int i = 0; i < preamble_count; i++) { |
| 50 | + my_tone_gen_put_bit (chan, bit); |
| 51 | + bit ^= 1; |
| 52 | + } |
| 53 | + |
| 54 | +#ifdef EOTD_SEND_DEBUG |
| 55 | + printf("end-of-preamble\n"); |
| 56 | +#endif |
| 57 | + // send FS |
| 58 | + for (int i = 0; i < fs_count; i++) { |
| 59 | + my_tone_gen_put_bit (chan, fs[i]); |
| 60 | + } |
| 61 | + |
| 62 | +#ifdef EOTD_SEND_DEBUG |
| 63 | + printf("end-of-fs\n"); |
| 64 | +#endif |
| 65 | +} |
| 66 | + |
| 67 | +void send_silence(int chan) { |
| 68 | + int a = ACHAN2ADEV(chan); |
| 69 | + |
| 70 | + for (int i = 0; i < EOTD_SILENCE_SAMPLES; i++) { |
| 71 | + my_gen_tone_put_sample (chan, a, 0); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +int eotd_send_block (int chan, char *str, char type) { |
| 76 | + |
| 77 | + unsigned int b[EOTD_LENGTH]; |
| 78 | + |
| 79 | + int status = sscanf(str, "%x %x %x %x %x %x %x %x", b, b+1, b+2, b+3, b+4, b+5, b+6, b+7); |
| 80 | + if (status != EOTD_LENGTH) { |
| 81 | + fprintf(stderr, "Error: expected 8, read %d", status); |
| 82 | + return -1; |
| 83 | + } |
| 84 | + |
| 85 | + send_preamble(chan, type); |
| 86 | + |
| 87 | + for (int i = 7; i >= 0; i--) { |
| 88 | + int byte = b[i]; // Copy this non-destructively so we can repeat it later, per spec. |
| 89 | + for (int j = 0; j < 8; j++) { |
| 90 | + int bit = byte & 0x01; |
| 91 | + byte >>= 1; |
| 92 | + my_tone_gen_put_bit (chan, bit); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +#ifdef EOTD_SEND_DEBUG |
| 97 | + printf("end-of-data\n"); |
| 98 | +#endif |
| 99 | + send_silence(chan); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
0 commit comments