RFControl/simulate/simulate.cpp

299 lines
14 KiB
C++

#include <cstdio>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define RF_CONTROL_VARDUINO
#define MAX_RECORDINGS 255
#include "../RFControl.h"
static char sate2string[6][255] = {
"STATUS_WAITING",
"STATUS_RECORDING_0",
"STATUS_RECORDING_1",
"STATUS_RECORDING_2",
"STATUS_RECORDING_3",
"STATUS_RECORDING_END"
};
void (*sim_interruptCallback)(void);
unsigned int sim_timings[] = {
// test with 1 footer pulse
// 1, 2, 3,
// 506, 2024, 506, 2024, 506, 4301, 506, 2024, 506, 4301, 506, 2277, 506, 4301, 506, 4301,
// 506, 2277, 506, 4301, 506, 2024, 506, 2024, 506, 2024, 506, 2277, 506, 4301, 506, 2024,
// 506, 2024, 506, 2277, 506, 4301, 506, 2024, 506, 2024, 506, 4301, 506, 2024, 506, 4301,
// 506, 4301, 506, 2277, 506, 4301, 506, 4301, 506, 2024, 506, 4301, 506, 2024, 506, 2024,
// 506, 4301, 506, 4554, 506, 2024, 506, 2024, 506, 2277, 506, 2277, 506, 4301, 506, 2024,
// 506, 2024, 506, 4554, 506, 8602,
// 506, 2024, 506, 2024, 506, 4301, 506, 2024, 506, 4301, 506, 2277, 506, 4301, 506, 4301,
// 506, 2277, 506, 4301, 506, 2024, 506, 2024, 506, 2024, 506, 2277, 506, 4301, 506, 2024,
// 506, 2024, 506, 2277, 506, 4301, 506, 2024, 506, 2024, 506, 4301, 506, 2024, 506, 4301,
// 506, 4301, 506, 2277, 506, 4301, 506, 4301, 506, 2024, 506, 4301, 506, 2024, 506, 2024,
// 506, 4301, 506, 4554, 506, 2024, 506, 2024, 506, 2277, 506, 2277, 506, 4301, 506, 2024,
// 506, 2024, 506, 4554, 506, 8602,
// 506, 2024, 506, 2024, 506, 4301, 506, 2024, 506, 4301, 506, 2277, 506, 4301, 506, 4301,
// 506, 2277, 506, 4301, 506, 2024, 506, 2024, 506, 2024, 506, 2277, 506, 4301, 506, 2024,
// 506, 2024, 506, 2277, 506, 4301, 506, 2024, 506, 2024, 506, 4301, 506, 2024, 506, 4301,
// 506, 4301, 506, 2277, 506, 4301, 506, 4301, 506, 2024, 506, 4301, 506, 2024, 506, 2024,
// 506, 4301, 506, 4554, 506, 2024, 506, 2024, 506, 2277, 506, 2277, 506, 4301, 506, 2024,
// 506, 2024, 506, 4554, 506, 8602,
// 1, 2, 3
//test with multiple footer pulses
// 884, 2829, 28, 1129, 345, 4243, 157, 561, 969, 1399, 153, 6593, 599, 8500,
// 582, 2091, 648, 2007, 560, 3967, 706, 1973, 666, 4203, 676, 2080, 654, 4221, 737, 4145,
// 578, 2098, 677, 4260, 676, 1996, 604, 2096, 603, 2075, 611, 2140, 565, 2120, 799, 4079,
// 589, 2073, 608, 4346, 590, 2115, 520, 4334, 557, 2100, 584, 4376, 769, 1917, 635, 4256,
// 523, 4331, 561, 2183, 670, 4209, 544, 4352, 595, 2085, 559, 4376, 572, 2099, 602, 2099,
// 668, 4199, 662, 4273, 631, 2054, 654, 2022, 575, 2115, 600, 2161, 529, 4334, 707, 1972,
// 560, 4317, 559, 2245, 700, 8403,
// 582, 8517,
// 657, 2002, 583, 2094, 560, 4304, 615, 2076, 563, 4316, 677, 2055, 529, 4344, 600, 4282,
// 588, 2089, 620, 4319, 662, 2027, 670, 2026, 527, 2137, 591, 2160, 607, 2081, 579, 4303,
// 578, 2092, 590, 4355, 561, 2129, 575, 4284, 588, 2093, 676, 4269, 571, 2108, 544, 4323,
// 599, 4286, 541, 2210, 521, 4353, 550, 4322, 701, 1992, 520, 4413, 620, 2062, 605, 2086,
// 581, 4276, 566, 4378, 541, 2144, 561, 2125, 584, 2096, 557, 2203, 608, 4259, 584, 2101,
// 579, 4286, 589, 2211, 632, 8469,
// 576, 8537,
// 550, 2099, 696, 1985, 648, 4217, 553, 2132, 605, 4493, 584, 2165, 531, 4342, 543, 4320,
// 557, 2134, 606, 4330, 529, 2151, 545, 2138, 583, 2103, 621, 2139, 617, 2067, 637, 4238,
// 844, 1820, 726, 4223, 599, 2084, 596, 4286, 597, 2083, 556, 4374, 560, 2136, 574, 4302,
// 600, 4269, 609, 2145, 589, 4280, 629, 4239, 584, 2097, 590, 4350, 578, 2118, 538, 2137,
// 568, 4297, 665, 4279, 606, 2077, 770, 1912, 706, 1985, 587, 2153, 660, 4216, 559, 2133,
// 553, 4304, 584, 2214, 586, 8519,
// 607, 8500,
// 579, 2090, 564, 2095, 672, 4201, 574, 2108, 609, 4249, 644, 2120, 570, 4300, 640, 127,
// 4113, 503, 1149, 138, 888, 554, 4381, 551, 2138, 554, 2133, 677, 2001, 648, 2107, 539,
// 2144, 578, 4283, 565, 2130, 571, 4369, 611, 2063, 580, 4301, 557, 2131, 540, 4389, 617,
// 2077, 584, 4292, 533, 4334, 567, 2182, 640, 4222, 558, 4322, 604, 2079, 590, 4343, 582,
// 2110, 510, 2167, 623, 4240, 552, 4407, 743, 1922, 590, 2105, 604, 2061, 602, 2169, 576,
// 4288, 574, 2107, 588, 4287, 560, 2229, 556, 8552,
// 605, 8496,
// 565, 2111, 586, 2073, 584, 4365, 500, 2108, 569, 4310, 555, 2215, 639, 4210, 553, 4310,
// 581, 2111, 558, 4376, 587, 2103, 512, 2169, 622, 2059, 549, 2203, 597, 2088, 643, 4219,
// 568, 2128, 734, 4206, 593, 2085, 639, 4225, 557, 2140, 536, 4392, 565, 2111, 559, 4315,
// 551, 4336, 613, 2123, 637, 4241, 588, 4291, 569, 2106, 592, 4341, 561, 2134, 596, 2080,
// 575, 4299, 568, 4371, 590, 2090, 594, 2100, 585, 2102, 587, 2156, 596, 4282, 607, 2088,
// 559, 4293, 544, 2261, 538, 8562,
// 619, 8484,
// 571, 2299, 579, 2090, 716, 4158, 652, 2035, 566, 4290, 661, 2106, 564, 4301, 606, 4269,
// 667, 2030, 692, 4232, 589, 2093, 554, 2126, 560, 2137, 609, 2129, 678, 2010, 577, 4292,
// 587, 2100, 557, 4389, 638, 2046, 532, 4345, 545, 2124, 687, 4249, 603, 2085, 556, 4325,
// 539, 4334, 538, 2201, 621, 4247, 571, 4306, 543, 2133, 574, 4375, 619, 2057, 551, 2139,
// 531, 4330, 563, 4386, 580, 2093, 653, 2046, 604, 2065, 669, 2082, 558, 4321, 620, 2053,
// 544, 4324, 563, 2223, 601, 8495,
// 537, 56879, 567, 34691, 108, 1313, 365, 20996, 206, 1684, 305, 12412, 162, 294, 129, 5492,
// 223, 7747, 495, 1799, 346, 1550, 127, 4026, 287, 974, 107, 34, 138, 2876, 41, 3160, 94,
// 1112, 94, 793, 213, 996, 87, 5195, 180, 2689, 156, 12254, 244, 1333
// 456, 1452, 1420, 504, 464, 1476, 1444, 508, 476, 1496, 1468, 520, 480, 1516, 488, 1524,
// 484, 1524, 1504, 524, 492, 1552, 1524, 536, 500, 1576, 1548, 548, 504, 1600, 1572, 556,
// 516, 1624, 1596, 564, 524, 1652, 1620, 576, 528, 1680, 1644, 584, 540, 1704, 540, 1708,
// 544, 17368,
// 456, 1452, 1420, 504, 464, 1476, 1444, 508, 476, 1496, 1468, 520, 480, 1516, 488, 1524,
// 484, 1524, 1504, 524, 492, 1552, 1524, 536, 500, 1576, 1548, 548, 504, 1600, 1572, 556,
// 516, 1624, 1596, 564, 524, 1652, 1620, 576, 528, 1680, 1644, 584, 540, 1704, 540, 1708,
// 544, 17368,
// 456, 1452, 1420, 504, 464, 1476, 1444, 508, 476, 1496, 1468, 520, 480, 1516, 488, 1524,
// 484, 1524, 1504, 524, 492, 1552, 1524, 536, 500, 1576, 1548, 548, 504, 1600, 1572, 556,
// 516, 1624, 1596, 564, 524, 1652, 1620, 576, 528, 1680, 1644, 584, 540, 1704, 540, 1708,
// 544, 17368,
// 528, 1988, 516, 2000, 524, 3880, 536, 3888, 520, 3904, 520, 3896, 516, 2008, 516, 3896,
// 520, 3888, 516, 2024, 516, 2012, 512, 2004, 512, 3908, 512, 2004, 512, 2000, 512, 2012,
// 512, 2000, 512, 2024, 512, 3896, 516, 2008, 516, 1992, 520, 2000, 516, 2012, 512, 3924,
// 508, 3912, 508, 2028, 508, 3904, 508, 2004, 516, 3888, 516, 3908, 516, 2004, 508, 2012,
// 512, 3924, 512, 3924, 516, 2012, 512, 3916, 508, 2000, 516, 3892, 512, 2000, 512, 2020,
// 504, 2004, 508, 2008, 512, 7728, 508, 7724,
// 528, 1988, 516, 2000, 524, 3880, 536, 3888, 520, 3904, 520, 3896, 516, 2008, 516, 3896,
// 520, 3888, 516, 2024, 516, 2012, 512, 2004, 512, 3908, 512, 2004, 512, 2000, 512, 2012,
// 512, 2000, 512, 2024, 512, 3896, 516, 2008, 516, 1992, 520, 2000, 516, 2012, 512, 3924,
// 508, 3912, 508, 2028, 508, 3904, 508, 2004, 516, 3888, 516, 3908, 516, 2004, 508, 2012,
// 512, 3924, 512, 3924, 516, 2012, 512, 3916, 508, 2000, 516, 3892, 512, 2000, 512, 2020,
// 504, 2004, 508, 2008, 512, 7728, 508, 7724,
// 528, 1988, 516, 2000, 524, 3880, 536, 3888, 520, 3904, 520, 3896, 516, 2008, 516, 3896,
// 520, 3888, 516, 2024, 516, 2012, 512, 2004, 512, 3908, 512, 2004, 512, 2000, 512, 2012,
// 512, 2000, 512, 2024, 512, 3896, 516, 2008, 516, 1992, 520, 2000, 516, 2012, 512, 3924,
// 508, 3912, 508, 2028, 508, 3904, 508, 2004, 516, 3888, 516, 3908, 516, 2004, 508, 2012,
// 512, 3924, 512, 3924, 516, 2012, 512, 3916, 508, 2000, 516, 3892, 512, 2000, 512, 2020,
// 504, 2004, 508, 2008, 512, 7728, 508, 7724
/*
524, 1976, 520, 2012, 516, 3896, 520, 3908, 516, 3900, 516, 3888, 516, 2000, 520, 3900,
504, 3896, 508, 2032, 512, 2028, 492, 2004, 512, 3908, 516, 2004, 508, 2004, 508, 2016,
512, 3896, 516, 2032, 512, 3900, 512, 2012, 504, 2004, 508, 2012, 512, 2000, 512, 3916,
512, 3904, 508, 2036, 508, 3904, 512, 2004, 512, 3912, 512, 3920, 508, 2004, 512, 2012,
504, 3896, 512, 3932, 508, 2008, 508, 3924, 508, 2008, 508, 3916, 508, 3916, 508, 3924,
508, 2004, 508, 2024, 508, 7740, 496, 7728,
524, 1976, 520, 2012, 516, 3896, 520, 3908, 516, 3900, 516, 3888, 516, 2000, 520, 3900,
504, 3896, 508, 2032, 512, 2028, 492, 2004, 512, 3908, 516, 2004, 508, 2004, 508, 2016,
512, 3896, 516, 2032, 512, 3900, 512, 2012, 504, 2004, 508, 2012, 512, 2000, 512, 3916,
512, 3904, 508, 2036, 508, 3904, 512, 2004, 512, 3912, 512, 3920, 508, 2004, 512, 2012,
504, 3896, 512, 3932, 508, 2008, 508, 3924, 508, 2008, 508, 3916, 508, 3916, 508, 3924,
508, 2004, 508, 2024, 508, 7740, 496, 7728,
524, 1976, 520, 2012, 516, 3896, 520, 3908, 516, 3900, 516, 3888, 516, 2000, 520, 3900,
504, 3896, 508, 2032, 512, 2028, 492, 2004, 512, 3908, 516, 2004, 508, 2004, 508, 2016,
512, 3896, 516, 2032, 512, 3900, 512, 2012, 504, 2004, 508, 2012, 512, 2000, 512, 3916,
512, 3904, 508, 2036, 508, 3904, 512, 2004, 512, 3912, 512, 3920, 508, 2004, 512, 2012,
504, 3896, 512, 3932, 508, 2008, 508, 3924, 508, 2008, 508, 3916, 508, 3916, 508, 3924,
508, 2004, 508, 2024, 508, 7740, 496, 7728,
*/
//test with alternatly packages. pack1-pack2-pack1-pack2-pack1-pack2
43, 202, 51, 49, 180, 30, 19, 185, 743, 167, 51, 372, 175, 44, 1067, 36, 959, 912,
553, 1129, 60, 888, 49, 1185, 68, 540, 548, 1261, 67, 1171, 38, 1190, 48, 142, 507,
3962, 485, 3978, 501, 3971, 476, 3981, 478, 4026, 448, 8959,
469, 3990, 480, 2011, 467, 2025, 454, 3994, 479, 2012, 464, 4024, 443, 4005, 465, 2023,
458, 2050, 434, 4031, 435, 4006, 465, 2033, 452, 4013, 456, 2025, 455, 2043, 433, 2044,
447, 2042, 438, 2044, 438, 2053, 432, 2050, 435, 2051, 436, 2049, 435, 2043, 435, 2050,
433, 2047, 439, 2056, 430, 2049, 435, 2049, 430, 2051, 438, 2046, 442, 2045, 432, 2045,
440, 4025, 445, 2051, 439, 2032, 446, 4020, 457, 8956, 475, 8938, 494, 8928, 510, 8933,
494, 3958, 512, 1980, 484, 2001, 476, 3994, 485, 2005, 466, 4002, 467, 4017, 449, 2027,
452, 2023, 468, 4001, 467, 4006, 456, 2016, 475, 4005, 462, 4006, 458, 4024, 446, 4001,
468, 2011, 470, 2019, 465, 2026, 456, 2018, 465, 4007, 463, 4002, 469, 3996, 472, 2032,
450, 2017, 465, 2022, 458, 2036, 448, 2042, 446, 2034, 450, 2021, 458, 2032, 453, 2035,
444, 2038, 453, 2025, 451, 4012, 467, 2024, 454, 8960, 475, 8942, 488, 8951, 484, 8923,
499, 3977, 508, 1980, 480, 2004, 480, 3995, 471, 2003, 480, 4002, 463, 4013, 454, 2021,
464, 2027, 453, 4004, 465, 3999, 461, 2022, 466, 4031, 444, 2012, 472, 2030, 450, 2031,
448, 2025, 461, 2023, 459, 2037, 445, 2039, 448, 2033, 454, 2025, 455, 2035, 456, 2024,
457, 2024, 454, 2031, 455, 2021, 463, 2030, 455, 2033, 447, 2025, 464, 2039, 447, 2026,
453, 4011, 469, 2022, 452, 2023, 459, 4008, 462, 8963, 470, 8947, 488, 8957, 470, 8921,
525, 3953, 484, 1997, 508, 1979, 501, 3970, 474, 2026, 473, 3990, 461, 4001, 475, 2011,
459, 2023, 461, 4005, 487, 3985, 465, 2020, 464, 4022, 443, 4000, 467, 4000, 470, 4004,
463, 2018, 466, 2026, 457, 2022, 462, 2026, 456, 4001, 473, 3991, 474, 3998, 469, 2036,
449, 2017, 464, 2022, 470, 2011, 467, 2036, 449, 2025, 454, 2035, 451, 2022, 460, 2034,
450, 2037, 449, 2037, 452, 4008, 460, 2016, 461, 8968, 468, 8943, 482, 8954, 496, 8907,
504, 3983, 502, 1983, 479, 2000, 508, 3959, 480, 2006, 514, 3965, 461, 3996, 482, 2019,
455, 2012, 474, 4003, 458, 4005, 465, 2027, 456, 4001, 469, 2015, 470, 2017, 463, 2022,
462, 2025, 457, 2021, 463, 2026, 459, 2023, 464, 2023, 466, 2029, 447, 2020, 461, 2035,
450, 2033, 457, 2021, 458, 2034, 446, 2027, 457, 2033, 453, 2027, 473, 2025, 442, 2041,
449, 4009, 474, 2018, 450, 2023, 463, 4011, 455, 8950, 486, 8930, 496, 8929, 522, 8908,
492, 3977, 489, 1998, 482, 1999, 482, 3994, 467, 2013, 474, 3992, 480, 4011, 453, 2014,
462, 2024, 468, 4006, 459, 4014, 450, 2024, 461, 4018, 451, 4010, 462, 4005, 464, 3999,
467, 2016, 460, 2034, 456, 2018, 460, 2028, 458, 4013, 454, 4008, 469, 4002, 467, 2023,
452, 2020, 462, 2021, 473, 2014, 456, 2039, 448, 2036, 455, 2023, 459, 2028, 452, 2026,
456, 2034, 451, 2028, 455, 4009, 465, 2022, 457, 8955, 485, 16962,
881, 1248, 55, 2475, 39, 1255, 34, 1193, 33, 1051, 47, 988, 807, 911, 38, 986, 35, 1045,
59, 1075, 45, 972, 26, 895, 64, 983, 36, 860
};
size_t sim_timings_pos;
size_t sim_timings_size;
int main(int argc, const char* argv[])
{
sim_timings_pos = 0;
sim_timings_size = sizeof(sim_timings)/sizeof(unsigned int);
unsigned int pulse_length_divider = RFControl::getPulseLengthDivider();
RFControl::startReceiving(0);
while(sim_timings_pos < sim_timings_size) {
sim_interruptCallback();
if(RFControl::hasData()) {
unsigned int *timings;
unsigned int timings_size;
RFControl::getRaw(&timings, &timings_size);
printf("result: \n");
for(size_t i=0; i < timings_size; i++) {
unsigned long timing = timings[i] * pulse_length_divider;
printf("%lu ", timing);
if((i+1)%16 == 0) {
printf("\n");
}
}
printf("\n");
unsigned int buckets[8];
RFControl::compressTimings(buckets, timings, timings_size);
printf("compressed: ");
for(size_t i=0; i < 8; i++) {
unsigned long bucket = buckets[i] * pulse_length_divider;
printf("%lu ", bucket);
}
printf(" t: ");
for(size_t i=0; i < timings_size; i++) {
printf("%i", timings[i]);
}
printf("\n");
RFControl::continueReceiving();
}
}
}
void hw_attachInterrupt(uint8_t, void (*ic)(void)) {
sim_interruptCallback = ic;
}
unsigned long hw_micros(void) {
static unsigned long duration = 0;
if(sim_timings_pos < sim_timings_size) {
duration += sim_timings[sim_timings_pos++];
return duration;
} else {
printf("No timings left...\n");
}
return 0;
}
void hw_pinMode(uint8_t, uint8_t){}
void hw_digitalWrite(uint8_t, uint8_t){}
int hw_digitalRead(uint8_t){return 0;}
int hw_analogRead(uint8_t){return 0;}
void hw_analogReference(uint8_t mode){}
void hw_analogWrite(uint8_t, int){}
void hw_delayMicroseconds(unsigned int us){}
void hw_detachInterrupt(uint8_t){}
#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x0
#define OUTPUT 0x1
#define CHANGE 1
#define FALLING 2
#define RISING 3
#include "../RFControl.cpp"