int main(int argc, char **argv) { int divisor = atoi(argv[1]); probe_mask = 1 << atoi(argv[2]); /* Byte loop */ while (1) { unsigned int byte = 0; int bit_count; /* Wait for start bit */ while (next_sample() == 1); if (average_samples(divisor-1) == 1) { /* False start bit */ continue; } /* Collect data bits, LSB first */ for (bit_count=0; bit_count<8; bit_count++) { if (average_samples(divisor)) { byte |= 1 << bit_count; } } /* Check for stop bit */ if (average_samples(divisor) == 1) { fwrite(&byte, 1, 1, stdout); fflush(stdout); } } return 0; }
int average_samples(int n_samples) { int sum = 0; int i; for (i=0; i<n_samples; i++) { sum += next_sample(); } return sum >= (n_samples/2); }
void Oscillator::refresh() { //-- Only When TS milliseconds have passed, the new sample is obtained if (next_sample()) { //-- If the oscillator is not stopped, calculate the servo position if (!_stop) { //-- Sample the sine function and set the servo pos _pos = round(_A * sin(_phase + _phase0) + _O); if (_rev) _pos=-_pos; _servo.write(_pos+90+_trim); } //-- Increment the phase //-- It is always increased, even when the oscillator is stop //-- so that the coordination is always kept _phase = _phase + _inc; } }
int main (int argc, char **argv) { fct_init(); /* A captured sFlow datagram (#14) */ u_int32_t packet[] = { htonl(0x00000005), htonl(0x00000001), htonl(0xb2116ff2), htonl(0x00000010), htonl(0x00002eb2), htonl(0x55f10676), htonl(0x00000006), htonl(0x00000001), htonl(0x000000d0), htonl(0x00003bc4), htonl(0x000001fd), htonl(0x000003e8), htonl(0x00e971b8), htonl(0x00000000), htonl(0x000001fd), htonl(0x00000000), htonl(0x00000002), htonl(0x00000001), htonl(0x00000090), htonl(0x00000001), htonl(0x00000552), htonl(0x00000004), htonl(0x00000080), htonl(0x01005e31), htonl(0x1af56c9c), htonl(0xed6213d6), htonl(0x08004500), htonl(0x054034d9), htonl(0x40003a11), htonl(0x7b3ad460), htonl(0xb312e931), htonl(0x1af5090a), htonl(0x090a052c), htonl(0x00004700), htonl(0x651ee36d), htonl(0x579e13dc), htonl(0x65de7811), htonl(0x377a5cf7), htonl(0x9e21eba7), htonl(0x0f8ebcf1), htonl(0x0ecfa7d7), htonl(0xef1810a4), htonl(0x4da7bcf3), htonl(0x3b8dbae7), htonl(0x8a9ef3c1), htonl(0x3bb875e7), htonl(0x9ab9ef3c), htonl(0x17d73cf7), htonl(0x9e06eee7), htonl(0x9ef3c73a), htonl(0xd194e203), htonl(0x01dea7f6), htonl(0xa8da1200), htonl(0x00000108), htonl(0x33cfb36f), htonl(0x000003e9), htonl(0x00000010), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000001), htonl(0x000000d0), htonl(0x00003bc5), htonl(0x000001fd), htonl(0x000003e8), htonl(0x00e975a0), htonl(0x00000000), htonl(0x000001fd), htonl(0x00000000), htonl(0x00000002), htonl(0x00000001), htonl(0x00000090), htonl(0x00000001), htonl(0x00000552), htonl(0x00000004), htonl(0x00000080), htonl(0x01005e31), htonl(0x1a8f6c9c), htonl(0xed6213d6), htonl(0x08004500), htonl(0x0540394a), htonl(0x40003a11), htonl(0x772fd460), htonl(0xb312e931), htonl(0x1a8f090a), htonl(0x090a052c), htonl(0x00004700), htonl(0x65125e8a), htonl(0x2878413c), htonl(0xa8569182), htonl(0x2f223570), htonl(0x558b4499), htonl(0x27625ad6), htonl(0xbabafb77), htonl(0x8d556e64), htonl(0xf68b6690), htonl(0x85d0d9ac), htonl(0x491de099), htonl(0x2159c756), htonl(0xeb74a2a5), htonl(0x7e019e40), htonl(0xc90f57bc), htonl(0x1e66e5b4), htonl(0x9522a3dc), htonl(0x301446f8), htonl(0x31f31f3f), htonl(0xdbf78329), htonl(0x9baf89b3), htonl(0x000003e9), htonl(0x00000010), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000001), htonl(0x000000d0), htonl(0x00003bc6), htonl(0x000001fd), htonl(0x000003e8), htonl(0x00e97988), htonl(0x00000000), htonl(0x000001fd), htonl(0x00000000), htonl(0x00000002), htonl(0x00000001), htonl(0x00000090), htonl(0x00000001), htonl(0x00000552), htonl(0x00000004), htonl(0x00000080), htonl(0x01005e31), htonl(0x1a686c9c), htonl(0xed6213d6), htonl(0x08004500), htonl(0x05403d93), htonl(0x40003a11), htonl(0x730dd460), htonl(0xb312e931), htonl(0x1a68090a), htonl(0x090a052c), htonl(0x00004700), htonl(0x651a8c9a), htonl(0xc8698831), htonl(0x20740075), htonl(0x1608600a), htonl(0x0640060e), htonl(0x40788603), htonl(0x003203a0), htonl(0x13815260), htonl(0x0d9060d2), htonl(0xf93129c1), htonl(0x078b801a), htonl(0x803a02a0), htonl(0x06600d80), htonl(0x3302843f), htonl(0x80789fb7), htonl(0x0941c6a3), htonl(0xde806e8e), htonl(0x02941392), htonl(0x19c82dc8), htonl(0x14383213), htonl(0x01007408), htonl(0x000003e9), htonl(0x00000010), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000001), htonl(0x000000d0), htonl(0x00003bc7), htonl(0x000001fd), htonl(0x000003e8), htonl(0x00e97d70), htonl(0x00000000), htonl(0x000001fd), htonl(0x00000000), htonl(0x00000002), htonl(0x00000001), htonl(0x00000090), htonl(0x00000001), htonl(0x00000552), htonl(0x00000004), htonl(0x00000080), htonl(0x01005e31), htonl(0x1a486c9c), htonl(0xed6213d6), htonl(0x08004500), htonl(0x05409771), htonl(0x40003a11), htonl(0x194ed460), htonl(0xb313e931), htonl(0x1a48090a), htonl(0x090a052c), htonl(0x00004700), htonl(0x65120000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x000003e9), htonl(0x00000010), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000001), htonl(0x000000d0), htonl(0x00003bc8), htonl(0x000001fd), htonl(0x000003e8), htonl(0x00e98158), htonl(0x00000000), htonl(0x000001fd), htonl(0x00000000), htonl(0x00000002), htonl(0x00000001), htonl(0x00000090), htonl(0x00000001), htonl(0x00000552), htonl(0x00000004), htonl(0x00000080), htonl(0x01005e31), htonl(0x1aa16c9c), htonl(0xed6213d6), htonl(0x08004500), htonl(0x05404682), htonl(0x40003a11), htonl(0x69e5d460), htonl(0xb312e931), htonl(0x1aa1090a), htonl(0x090a052c), htonl(0x00004700), htonl(0x6519ccaf), htonl(0x5e165667), htonl(0x3a001dc5), htonl(0x5faf0000), htonl(0x01081b5e), htonl(0x6563af95), htonl(0xebd15aaf), htonl(0x95baf95b), htonl(0xaf656ebc), htonl(0xcac3d7a2), htonl(0xb0000001), htonl(0x091b5f2b), htonl(0x0d5e8af5), htonl(0xecadd7ca), htonl(0xdd7cac75), htonl(0xe656339d), htonl(0x807d6bcc), htonl(0xac000001), htonl(0x0a1b5e8a), htonl(0xc55e32b2), htonl(0xaf95baf6), htonl(0x000003e9), htonl(0x00000010), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000001), htonl(0x000000d0), htonl(0x00003bc9), htonl(0x000001fd), htonl(0x000003e8), htonl(0x00e98540), htonl(0x00000000), htonl(0x000001fd), htonl(0x00000000), htonl(0x00000002), htonl(0x00000001), htonl(0x00000090), htonl(0x00000001), htonl(0x00000552), htonl(0x00000004), htonl(0x00000080), htonl(0x01005e31), htonl(0x1a906c9c), htonl(0xed6213d6), htonl(0x08004500), htonl(0x05404acb), htonl(0x40003a11), htonl(0x65add460), htonl(0xb312e931), htonl(0x1a90090a), htonl(0x090a052c), htonl(0x00004700), htonl(0x651baafe), htonl(0x0bdedfbc), htonl(0xbd602300), htonl(0x2174c3ce), htonl(0x4b2371b8), htonl(0xe6ffdcf5), htonl(0x786be4e8), htonl(0xc0fd26c2), htonl(0xeed3a46f), htonl(0x4c46f9a6), htonl(0x5db1ca70), htonl(0x79466014), htonl(0xad3298d5), htonl(0x0bb5be20), htonl(0x069402c7), htonl(0xf3a2f740), htonl(0x8b297b99), htonl(0x605378ac), htonl(0xccd10ea4), htonl(0x352cb6f5), htonl(0xc00eb400), htonl(0x000003e9), htonl(0x00000010), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000), htonl(0x00000000) }; sflow_sample_data_t *sample = NULL; sflow_flow_record_t *record = NULL; sflow_raw_header_t *header = NULL; struct sockaddr_storage ss; assert_int(1, next_sample(&packet, sizeof (packet), &sample), "Load sample #1"); assert_int(1, is_sample_format(sample, FLOW_SAMPLE), "Sample #1: format"); assert_int(15300, ntohl(((sflow_flow_sample_t *) (sample + 1))->sequence_number), "Sample #1: seq_number"); assert_int(1, next_record(sample, &record), "Load record #1:1"); assert_int(1, is_record_format(record, RAW_HEADER), "Record #1:1: format"); header = (sflow_raw_header_t *) (record + 1); assert_int(1, get_source(header, &ss), "Record #1:1: Loading source address"); assert_ip("212.96.179.18", &ss, "Record #1:1: Source address"); assert_int(1, get_destination(header, &ss), "Record #1:1: Loading destination address"); assert_ip("233.49.26.245", &ss, "Record #1:1: Destination address"); assert_int(1, next_record(sample, &record), "Load record #1:2"); assert_int(0, is_record_format(record, RAW_HEADER), "Record #1:2: format"); assert_int(0, next_record(sample, &record), "Record #1:3 should fail"); assert_int(1, next_sample(&packet, sizeof (packet), &sample), "Load sample #2"); assert_int(1, is_sample_format(sample, FLOW_SAMPLE), "Sample #2: format"); assert_int(15301, ntohl(((sflow_flow_sample_t *) (sample + 1))->sequence_number), "Sample #2: seq_number"); assert_int(1, next_sample(&packet, sizeof (packet), &sample), "Load sample #3"); assert_int(1, is_sample_format(sample, FLOW_SAMPLE), "Sample #3: format"); assert_int(15302, ntohl(((sflow_flow_sample_t *) (sample + 1))->sequence_number), "Sample #3: seq_number"); assert_int(1, next_sample(&packet, sizeof (packet), &sample), "Load sample #4"); assert_int(1, is_sample_format(sample, FLOW_SAMPLE), "Sample #4: format"); assert_int(15303, ntohl(((sflow_flow_sample_t *) (sample + 1))->sequence_number), "Sample #4: seq_number"); assert_int(1, next_sample(&packet, sizeof (packet), &sample), "Load sample #5"); assert_int(1, is_sample_format(sample, FLOW_SAMPLE), "Sample #5: format"); assert_int(15304, ntohl(((sflow_flow_sample_t *) (sample + 1))->sequence_number), "Sample #5: seq_number"); assert_int(1, next_sample(&packet, sizeof (packet), &sample), "Load sample #6"); assert_int(1, is_sample_format(sample, FLOW_SAMPLE), "Sample #6: format"); assert_int(15305, ntohl(((sflow_flow_sample_t *) (sample + 1))->sequence_number), "Sample #6: seq_number"); assert_int(0, next_sample(&packet, sizeof (packet), &sample), "Sample #7 should fail"); fct_send(); return 0; }
/* ------------------------------------------------------------------------------------------------------ Creates a byte from the waveform 0..255 new byte -1 continue -2 error -3 no more bytes --------------------------------------------------------------------------------------------------------- */ int byte_decode(int restart, int new_file) { static int t,t_active,t_byte; static int byte,n_bytes,c[6]; int i,j,k,d[7],sampleval; double x; // t = number of samples read // t_byte = sample where byte starts // t_active is > 0 when a signal has been detected if(restart) { t = t_active = 0; update_progress(0.0); update_status("Attente du signal..."); } // process next sample t++; sampleval = next_sample(new_file); if (sampleval == 40000) { return -3; } x = (double)sampleval / 32768.0; x = filter(x); x = dilate(x); x = median(x); x = make_decision(x,d); b_median(d); update_level(x); // print_deep_debug("t: %d, d6 %d, d5 %d, x %0.5f\n", t, d[6], d[5], x); // activation test if(d[6] && t_active == 0) { print_deep_debug(" - act - "); t_active = t; t_byte = 0; n_bytes = 0; update_status("Analyse du signal..."); } if(t_active == 0) return -1; // deactivation test after a long period of inactivity if(t > t_active + 10000) { update_status("Désactivation pour cause d'inactivité"); return -2; } // new byte? if(t_byte == 0 && d[5]) { // new byte print_deep_debug(" -- new byte -- "); t_active = t_byte = t; n_bytes++; byte = 0; for(i = 0;i < 6;i++) c[i] = 0; } // processing a byte? if(t_byte > 0) { j = (t - t_byte) / 88; // bit number; the duration of a bit is very close to 2ms=88.2 samples if(j >= 10) { // end of the byte print_deep_debug(" [b = %d ] ", byte); if((byte & 1) == 0) { // first bit (lsb) must be a one print_debug("Bad byte start"); return -2; } if(byte >= 512) { // last bit (msb) must be a zero print_debug("Bad byte finish"); return -2; } byte >>= 1; t_byte = 0; if(n_bytes > 5) { return byte; } if(byte != 0xAA) { // bad synchronization byte print_debug("Bad synchronization byte."); return -2; } print_debug("Found good synchronization byte."); return -1; // discard synchronization byte }