Oregon_NR.cpp 75.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
#include "Oregon_NR_MY.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This Arduino code is for receive and transmit data using Oregon Scientific RF protocol version 2.1 and 3.0. 
//
// Last updated: 14 October 2019
//
// The folowed sensors data format are supported.
//
// Receive and transmit:
// THGN132N (THGR122N, THGN123N),
// RTGN318,
// THGR810.

// Receive only:
// THN132N,
// WGR800,	
// UVN800.
// PCR800 added by mr.Tech since 20.02.2020
// Aslo supported self-developed sensors. Please contact author for additional infromation.
//
// This file is part of the Arduino OREGON_NR library.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// The MIT License (MIT)
//
// Copyright (c) 2019 Sergey Zawislak 
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Äàííàÿ áèáëèîòåêà Àðäóèíî ïðåäíàçíà÷åíà äëÿ ïðèåìà è ïåðåäà÷è äàííûõ â ôîðìàòå áåñïðîâîäíîãî ïðîòîêîëà Oregon Scientific v2.1 è v3.0
//
// Ïîñëåäíåå îáíîâëåíèå 14 Îêòÿáðÿ 2019
//
// Ïîääåðæèâàåòñÿ ôîðìàò ñëåäóþùèõ äàò÷èêîâ
//
// Ïðè¸ì è ïåðåäà÷à:
// THGN132N (THGR122N, THGN123N),
// RTGN318,
// THGR810.

// Òîëüîê ïðè¸ì:
// THN132N,
// WGR800,	
// UVN800.
//
// Òàêæå ïîääåðæèâàþòñÿ äàò÷èêè ñîáñòâåííîé ðàçðàáîòêè (çà äîïîëíèòåëüíîé äîêóìåíòàöåé îáðàùàòüñÿ ê àâòîðó)
//
//  Ýòîò ôàéë - ÷àñòü áèáëèîòåêè OREGON_NR
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2019 Ñåðãåé Çàâèñëÿê
//
// Äàííàÿ ëèöåíçèÿ ðàçðåøàåò ëèöàì, ïîëó÷èâøèì êîïèþ äàííîãî ïðîãðàììíîãî îáåñïå÷åíèÿ è ñîïóòñòâóþùåé äîêóìåíòàöèè 
// (â äàëüíåéøåì èìåíóåìûìè «Ïðîãðàììíîå Îáåñïå÷åíèå»), áåçâîçìåçäíî èñïîëüçîâàòü Ïðîãðàììíîå Îáåñïå÷åíèå áåç îãðàíè÷åíèé,
// âêëþ÷àÿ íåîãðàíè÷åííîå ïðàâî íà èñïîëüçîâàíèå, êîïèðîâàíèå, èçìåíåíèå, ñëèÿíèå, ïóáëèêàöèþ, ðàñïðîñòðàíåíèå, ñóáëèöåíçèðîâàíèå
// è/èëè ïðîäàæó êîïèé Ïðîãðàììíîãî Îáåñïå÷åíèÿ, à òàêæå ëèöàì, êîòîðûì ïðåäîñòàâëÿåòñÿ äàííîå Ïðîãðàììíîå Îáåñïå÷åíèå, ïðè ñîáëþäåíèè ñëåäóþùèõ óñëîâèé:
//
// Óêàçàííîå âûøå óâåäîìëåíèå îá àâòîðñêîì ïðàâå è äàííûå óñëîâèÿ äîëæíû áûòü âêëþ÷åíû âî âñå êîïèè èëè çíà÷èìûå ÷àñòè äàííîãî Ïðîãðàììíîãî Îáåñïå÷åíèÿ.
//
// ÄÀÍÍÎÅ ÏÐÎÃÐÀÌÌÍÎÅ ÎÁÅÑÏÅ×ÅÍÈÅ ÏÐÅÄÎÑÒÀÂËßÅÒÑß «ÊÀÊ ÅÑÒÜ», ÁÅÇ ÊÀÊÈÕ-ËÈÁÎ ÃÀÐÀÍÒÈÉ, ßÂÍÎ ÂÛÐÀÆÅÍÍÛÕ ÈËÈ ÏÎÄÐÀÇÓÌÅÂÀÅÌÛÕ, ÂÊËÞ×Àß ÃÀÐÀÍÒÈÈ ÒÎÂÀÐÍÎÉ 
// ÏÐÈÃÎÄÍÎÑÒÈ, ÑÎÎÒÂÅÒÑÒÂÈß ÏÎ ÅÃÎ ÊÎÍÊÐÅÒÍÎÌÓ ÍÀÇÍÀ×ÅÍÈÞ È ÎÒÑÓÒÑÒÂÈß ÍÀÐÓØÅÍÈÉ, ÍÎ ÍÅ ÎÃÐÀÍÈ×ÈÂÀßÑÜ ÈÌÈ. ÍÈ Â ÊÀÊÎÌ ÑËÓ×ÀÅ ÀÂÒÎÐÛ ÈËÈ ÏÐÀÂÎÎÁËÀÄÀÒÅËÈ 
// ÍÅ ÍÅÑÓÒ ÎÒÂÅÒÑÒÂÅÍÍÎÑÒÈ ÏÎ ÊÀÊÈÌ-ËÈÁÎ ÈÑÊÀÌ, ÇÀ ÓÙÅÐÁ ÈËÈ ÏÎ ÈÍÛÌ ÒÐÅÁÎÂÀÍÈßÌ,  ÒÎÌ ×ÈÑËÅ, ÏÐÈ ÄÅÉÑÒÂÈÈ ÊÎÍÒÐÀÊÒÀ, ÄÅËÈÊÒÅ ÈËÈ ÈÍÎÉ ÑÈÒÓÀÖÈÈ, 
// ÂÎÇÍÈÊØÈÌ ÈÇ-ÇÀ ÈÑÏÎËÜÇÎÂÀÍÈß ÏÐÎÃÐÀÌÌÍÎÃÎ ÎÁÅÑÏÅ×ÅÍÈß ÈËÈ ÈÍÛÕ ÄÅÉÑÒÂÈÉ Ñ ÏÐÎÃÐÀÌÌÍÛÌ ÎÁÅÑÏÅ×ÅÍÈÅÌ. 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//Âñ¸, ÷òî îòíîñèòñÿ ê ïðåðûâàíèþ/////////////////////////////////////
#ifndef Oregon_NR_int
#define Oregon_NR_int
static volatile unsigned long pm;
static volatile unsigned long pl, timer_mark;  

#if defined ( ESP8266 )
void ICACHE_RAM_ATTR receiver_interruption(void) {  
#elif defined ( ESP32 )
void ICACHE_RAM_ATTR receiver_interruption(void) {  
#else
void receiver_interruption(void) {  
#endif

  if(digitalRead(RECEIVER_PIN)){
  //Íà÷àëî èìïóëüñà
    pl = 0;
    pm = micros();  
  }
  else{
    //Êîíåö èìïóëüñà
    //Âû÷èñëÿåòñÿ âðåìÿ îêîí÷àíèÿ è äëèíà
    pl = micros() - pm;  
    //pm += pl;
  }
  //yield();
}  
#endif

//////////////////////////////////////////////////////////////////////
Oregon_NR::Oregon_NR(byte MHZ, byte MHZ_INT)
{
  INT_NO = MHZ_INT;
  RECEIVER_PIN = MHZ;
  pinMode(MHZ, INPUT); // Âûâîä, íà êîòîðûé ïîäêëþ÷¸í ïðè¸ìíèê
}

Oregon_NR::Oregon_NR(byte MHZ, byte MHZ_INT, byte led, bool pull_up)
{
  INT_NO = MHZ_INT;
  LED = led;
  PULL_UP = pull_up;
  RECEIVER_PIN = MHZ;
  pinMode(MHZ, INPUT);    // Âûâîä, íà êîòîðûé ïîäêëþ÷¸í ïðè¸ìíèê
  pinMode(LED, OUTPUT);   // Âûâîä ñâåòîäèîäà
}

//////////////////////////////////////////////////////////////////////
void Oregon_NR::start()
{
  packet_number = 0;
  packets_received = 0;
  start_pulse_cnt = 0;
  receive_status = FIND_PACKET;
  led_light(false);
  attachInterrupt(INT_NO, receiver_interruption, CHANGE);  
}
//////////////////////////////////////////////////////////////////////
void Oregon_NR::stop()
{
  detachInterrupt(INT_NO);  
}
//////////////////////////////////////////////////////////////////////
//Çàõâàò è àíàëèç ïàêåòà
//DEBUG_INFO - â Serial âûâîäèòñÿ èíôîðìàöèÿ î çàõâàòå äàííûõ
//////////////////////////////////////////////////////////////////////
void Oregon_NR::capture(bool DEBUG_INFO)
{
  ////////////////////////////////////////////////////////
  // Âîçâðàùàåìñÿ ê èñõîäíîìó ñîñòîÿíèþ
  //maybe_packet = 0;
  packets_received = 0;
  sens_type = 0;
  crc_c = 0;
  captured = 0;
  data_val = 0;
  data_val2 = 0;

  
  ////////////////////////////////////////////////////////
  //×òåíèå äàííûõ ñ ïðè¸ìíèêà
  noInterrupts();
  pulse_length = pl;
  pl = 0;
  pulse_marker = pm;
  interrupts();

  ////////////////////////////////////////////////////////
  //Ïðèø¸ë èìïóëüñ
  if (pulse_length != 0 && receive_status == FIND_PACKET){  
  //Åñëè èìïóëüñ ïðèø¸ë ñëèøêîì ïîçäíî äëÿ êîíêðåòíîé âåðñèè ïðîòîêîëà, òî ýòî ïåðâûé èìïóëüñ
    if (pulse_marker - pre_marker > (PER_LENGTH2 * 2 + LENGTH_TOLERANCE) && ver == 2) start_pulse_cnt = 0;
    if (pulse_marker - pre_marker > (PER_LENGTH3 * 2 + LENGTH_TOLERANCE) && ver == 3) start_pulse_cnt = 0;

    if (start_pulse_cnt == 0){
      ////////////////////////////////////////////////////////
      //Íàéäåíà ïåðâûé "ïðàâèëüíûé" èìïóëüñ - îïðåäåëÿåì òèï ïðîòîêîëà
      if (pulse_length < (MAX_LENGTH2 + LENGTH_TOLERANCE) && pulse_length > (MIN_LENGTH2 -  LENGTH_TOLERANCE) && catch2 ) {
        start_pulse_cnt++;
        pre_marker = pulse_marker;
        pulse_length = 0;
	ver = 2;
      }

      if (pulse_length < (MAX_LENGTH3 + LENGTH_TOLERANCE) && pulse_length > (MIN_LENGTH3 -  LENGTH_TOLERANCE) && catch3 ) {
        start_pulse_cnt++;
        pre_marker = pulse_marker;
        pulse_length = 0;
	ver = 3;
      }
    }
    else{
      ///////////////////////////////////////////////////////////
      //Íàéäåíà ñëåäóþùèé "ïðàâèëüíûé" èìïóëüñ 
      if (pulse_length <= (MAX_LENGTH2 + LENGTH_TOLERANCE) && pulse_length >= (MIN_LENGTH2 -  LENGTH_TOLERANCE) && catch2) {
        ///////////////////////////////////////////////////////////
        //Åñëè èìïóëüñ â ïðàâèëüíîì ìåñòå, òî äîáàâëÿåì ñ÷¸ò÷èê íàéäåíûõ ñòàðòîâûõ èìïóëüñîâ
        if(pulse_marker - pre_marker > (PER_LENGTH2*2-LENGTH_TOLERANCE) && pulse_marker - pre_marker < (PER_LENGTH2 * 2 + LENGTH_TOLERANCE) && ver == 2)
	{
          start_pulse_cnt++;
          pre_marker = pulse_marker;
          pulse_length = 0;
        }
        ///////////////////////////////////////////////////////////
        //Ñëåäóþùèé èìïóëüñ íàõîäèòñÿ â íåïðàâèëüíîì ìåñòå
        //Íàçíà÷àåì åãî ïåðâûì
        else
	{
          start_pulse_cnt = 1;
          pre_marker = pulse_marker;
          pulse_length = 0;
          ver = 2;
        }
      }

      if (pulse_length <= (MAX_LENGTH3 + LENGTH_TOLERANCE) && pulse_length >= (MIN_LENGTH3 -  LENGTH_TOLERANCE) && catch3) {
        ///////////////////////////////////////////////////////////
        //Åñëè èìïóëüñ â ïðàâèëüíîì ìåñòå, òî äîáàâëÿåì ñ÷¸ò÷èê íàéäåíûõ ñòàðòîâûõ èìïóëüñîâ
        if(pulse_marker - pre_marker > (PER_LENGTH3*2-LENGTH_TOLERANCE) && pulse_marker - pre_marker < (PER_LENGTH3 * 2 + LENGTH_TOLERANCE) && ver == 3)
	{
          start_pulse_cnt++;
          pre_marker = pulse_marker;
          pulse_length = 0;
        }
        ///////////////////////////////////////////////////////////
        //Ñëåäóþùèé èìïóëüñ íàõîäèòñÿ â íåïðàâèëüíîì ìåñòå
        //Íàçíà÷àåì åãî ïåðâûì
        else
	{
          start_pulse_cnt = 1;
          pre_marker = pulse_marker;
          pulse_length = 0;
          ver = 3;
        }
      }
    }
  }
//*************************************************************************************
  /////////////////////////////////////////////////////////////////////
  // Åñëè íàéäåí ïåðâûé ïàêåò è âûøëî âåðìÿ îæèäàíèÿ âòîðîãî
  // Íå æä¸ì âòîðîãî, à ïåðåõîäì â ðåæèì àíàëèçà
  // òÀÊÆÅ íå æä¸ì âòîðîãî ïàêåòà åñëè îòêëþ÷¸í ðåæèì ñðàùèâàíèÿ ïàêåòîâ
  if (packet_number == 1 && (millis() - first_packet_end) > 200) receive_status = ANALIZE_PACKETS;
  if (packet_number == 1 && (!IS_ASSEMBLE || ver == 3 )) receive_status = ANALIZE_PACKETS;

  
  //////////////////////////////////////////////////////////////////////
  //Åñëè Íàéäåíî íóæíîå êîëè÷åñòâî ïðàâèëüíûõ èìïóëüñîâ â íóæíûõ ìåñòàõ, òî âîçìîæíî ýòî ïàêåò. Íà÷èíàåì ÑÁÎÐ ÄÀÍÍÛÕ

  if (start_pulse_cnt == CATCH_PULSES && receive_status == FIND_PACKET) {
    

    start_pulse_cnt = 0;
    if (packet_number == 0)
    {
      read_tacts = collect(collect_data);
      first_packet_end = millis();
      packet_number = 1;
    }
    else
    {
      read_tacts2 = collect(collect_data2);
      packet_number = 2;
      receive_status = ANALIZE_PACKETS;
    }
  }
  //////////////////////////////////////////////////////////////////////
  // Àíàëèç äàííûõ////////////////////////////////////////////////////////
  if  (receive_status == ANALIZE_PACKETS) {     
    //////////////////////////////////////////////////////////////////////
    //Åñëè ïðèø¸ë òîëüîê êóñîê ïîñûëêè, òî íå ñòîèò è îáðàáàòûâàòü
    if ((ver ==2 && read_tacts < 136 && read_tacts2 < 136) || (ver ==3 && read_tacts < 80)) 
    {
      receive_status = FIND_PACKET;
      start_pulse_cnt = 0;  
      packet_number = 0;
       return;
    }
    //Îòêëþ÷àåì ïðåðûâàíèå, ÷òîáû ñîêðàòèòü âðåìÿ îáðàáîòêè
    detachInterrupt(INT_NO);
   
    led_light(true);
    restore_sign = 0;
    work_time = millis();   //Ðàñ÷¸ò âðåìåíè îáðàáîòêè ïàêåòà
      //Äàìï ñîáðàííûõ äàííûõ        
      //ÄËÿ ïîñûëêè áåç ïîìåõ çíà÷åíèÿ èìóðëüñîâ îëæíû áûòü ïðèìåðíî
      // v2 - 87 07  è èçðåäêà 86 06, ò.ê. äëèíà èìïóëüñà 883ìñ è 395ìñ
      // v3 - 86 06 è èçðåäêà 87 07  ò.ê. äëèíà èìïóëüñà 838 è 350ìñ
        if (DEBUG_INFO && receiver_dump){
          Serial.println(" ");
          Serial.print("SCOPE1 ");
          for(int bt = 0; bt < read_tacts; bt++){
            Serial.print((collect_data[bt] & 0xF0) >> 4, HEX);
            Serial.print(collect_data[bt] & 0x0F, HEX);
            Serial.print(' ');
          }
          Serial.println(" ");
          if ( packet_number == 2)
          {
            Serial.print("SCOPE2 ");
            for(int bt = 0; bt < read_tacts2; bt++){
              Serial.print((collect_data2[bt] & 0xF0) >> 4, HEX);
              Serial.print(collect_data2[bt] & 0x0F, HEX);
              Serial.print(' ');
            }
            Serial.println(" ");
          }

        }


    //////////////////////////////////////////////
    //Îáðàáîòêà ïåðâîé çàïèñè
    //Ðàñøèôðîâûâàåì çàïèñü. Äàííûå ñîõðàÿíåì â decode_tacts[]
    get_tacts(collect_data, read_tacts);
    bool halfshift;

    if (get_data(0, ver, collect_data) > get_data(1, ver, collect_data)){
      data_val = get_data(0, ver, collect_data);
      halfshift = 0;
    }
    else {
      data_val = get_data(1, ver, collect_data);
      halfshift = 1;
    }
    //////////////////////////////////////////////
    //Èùåì ïîçèöèþ ñèíõðîíèáëà
    synchro_pos = get_synchro_pos(collect_data);
    //////////////////////////////////////////////
    //Âûâîäèì ïîñûëêó
    if (DEBUG_INFO){
    if (packet_number == 2)   Serial.print("1)     ");
    if (packet_number == 1)   Serial.print("RESULT ");
      for(int bt = 0; bt < READ_BITS; bt++) {
        if ((ver == 3 && bt <= read_tacts) || (ver == 2 && bt <= read_tacts / 2)){
          if (collect_data[bt] > 128 + 1) Serial.print('I');
          if (collect_data[bt] < 128 - 1) Serial.print('O');
          if (collect_data[bt] == 128 + 1) Serial.print('i');
          if (collect_data[bt] == 128 - 1) Serial.print('o');
          if (collect_data[bt] == 128) Serial.print('.');
           if (receiver_dump && ver == 2) Serial.print("     ");
           if (receiver_dump && ver == 3) Serial.print("  ");
        }
        else Serial.print(' ');
      }
        Serial.print(" OSV:");
        Serial.print(ver);
        Serial.print(" SYN:");
	if (synchro_pos < 255)
        Serial.print(synchro_pos);
        else  Serial.print("NO");
        Serial.print(" TIME:");
        Serial.println (millis() / 1000);
    }
    //////////////////////////////////////////////
    //Àíàëîãè÷íî îáðàáàòûâàåì âòîðóþ çàïèñü
    if (packet_number == 2){
      get_tacts(collect_data2, read_tacts2);

      if (get_data(0, ver, collect_data2) > get_data(1, ver, collect_data2)) {
        data_val2 = get_data(0, ver, collect_data2);
        halfshift = 0;
      }
      else {
        data_val2 = get_data(1, ver, collect_data2);
        halfshift = 1;
      }

      synchro_pos2 = get_synchro_pos(collect_data2);
      if (DEBUG_INFO){
        Serial.print("2)     ");
       for(int bt = 0; bt < READ_BITS; bt++) {
        if (bt <= read_tacts2 / 2){
          if (collect_data2[bt] > 128 + 1) Serial.print('I');
          if (collect_data2[bt] < 128 - 1) Serial.print('O');
          if (collect_data2[bt] == 128 + 1) Serial.print('i');
          if (collect_data2[bt] == 128 - 1) Serial.print('o');
          if (collect_data2[bt] == 128) Serial.print('.');
           if (receiver_dump && ver == 2) Serial.print("     ");
           if (receiver_dump && ver == 3) Serial.print("  ");
        }
        else Serial.print(' ');
      }

        Serial.print(" OSV:");
        Serial.print(ver);
        Serial.print(" SYN:");
	if (synchro_pos2 < 255)
          Serial.print(synchro_pos2);
        else
          Serial.print("NO");;
        }
      }
      byte* result_data, result_data_start, aux_data;
      int correlation;
      


      //////////////////////////////////////////////
      //ÑÎÏÎÑÒÀÂËÅÍÈÅ ÏÀÊÅÒÎÂ
      //Åñëè ïàêåò îäèí, òî è ñîïîñòàâëÿòü íå èç ÷åãî
       if (packet_number == 1) result_data = collect_data;     
       //////////////////////////////////////////////
       //À âîò åñëè äâà, òî íóæíà ÑÁÎÐÊÀ ÏÀÊÅÒÀ
       //âû÷èñëÿåì îïòèìàëüíîå "ñìåùåíèå" ïàêåòîâ äðóã îòíîñèòåëüíî äðóãà
       if (packet_number == 2) {
        correlation = correlate_data(collect_data, collect_data2);
        if (DEBUG_INFO){
          Serial.print(" COR: ");
          Serial.println(correlation);
        }
        //////////////////////////////////////////////
        //Ñîáèðàåì äàííûå â ïàêåò, ãäå ñèíõðîíèáë íàéäåí ðàíüøå                
        //////////////////////////////////////////////

        if (synchro_pos >= synchro_pos2)
        {
          result_size = read_tacts2;
          result_data = collect_data2;
          correlation = -correlation;
          assemble_data(collect_data2, collect_data, correlation);
        }
        else
        {
          result_size = read_tacts;
          result_data = collect_data;
          assemble_data(collect_data, collect_data2, correlation);
        }
      }
    //////////////////////////////////////////////
    //Âûâîä ãîòîâîé ïîñûëêè
    if (DEBUG_INFO && packet_number == 2){
      Serial.print("RESULT ");
      byte* rdt = result_data;
      for(int bt = 0; bt < READ_BITS; bt++){
        if (bt <= result_size / 2){
          if (*rdt > 128 + 1) Serial.print('I');
          if (*rdt < 128 - 1) Serial.print('O');
          if (*rdt == 128 + 1) Serial.print('i');
          if (*rdt == 128 - 1) Serial.print('o');
          if (*rdt == 128) Serial.print('.');
           if (receiver_dump && ver == 2) Serial.print("     ");
           if (receiver_dump && ver == 3) Serial.print("  ");
        }
        else Serial.print(' ');
          rdt++;
      }
      Serial.println(" ");
    }



     //Ïðîâåðÿåì, äàëî ëè ÷òî-íèáóäü ñðàùèâàíèå - îòêëþ÷èë. Ýòî äà¸ò âñåãî ëèøü ôëàã, íî çàíèìàåò ìíîãî âðåìåíè
    //////////////////////////////////////////////

 //   if (get_data(halfshift, ver, result_data) > data_val && get_data(halfshift, ver, result_data) > data_val2 && ver == 2)
//	restore_sign ^= 8;

    //////////////////////////////////////////////
    //Èçâëåêàåì èç òàêòîâîé ïîñëåäîâàòåëüíîñòè áèòû
    sens_type = 0;
    if (get_info_data(result_data, packet, valid_p))
    {
      sens_type = get_sensor(packet); //Îïðåäåëÿåì òèï ïàêåòà ïî òèïó äàò÷èêà
      restore_data(packet, sens_type); // Âîññòàíàâëèâàåì äàííûå ïî òèïó äàò÷èêà
      crc_c = check_CRC(packet, sens_type); // Ïðîâåðÿåì CRC, åñëè îíî âåðíî, òî âñå ñîìíèòåëüíûå áèòû äåëàåì óâåðåííûìè
      //Åñëè íå âñå áàéòû îïðåäåëåíû óâåðåííî, íåëüçÿ ñ÷èòàòü, ÷òî ïàêåò êîððåêòíûé
      byte secresingV;
      if (sens_type == THGN132 || (sens_type & 0xFF00) == GAS) secresingV = PACKET_LENGTH - 4;
      if (sens_type == THN132) secresingV = PACKET_LENGTH - 6;
      for (byte www = 0; www < (PACKET_LENGTH - secresingV + 2); www++)
      if (valid_p[www] < 0x0f) crc_c = false;
      //Çàõâàò ïàêåòà ïðîèñõîäèò òîëüîê â ñëó÷àå, åñëè íàéäåíà ñòàðòîâàÿ ïîñëåäîâàòåëüíîñòü (íèáë ñèíõðîíèçàöèè)
      //Åñëè íå áûëî ñèíõðîíèáëîâ - òî íå î ÷åì âîîáùå ðàçãîâàðèâàòü
      if ( synchro_pos != 255 && packet_number == 1)  captured = 1;
      if ( (synchro_pos2 != 255 || synchro_pos2 != 255) && packet_number == 2)  captured = 1;
      //Çàõâàò êóñêà ïîñûëêè íå ñ÷èòàåòñÿ
      if ((ver ==2 && read_tacts < 136) || (ver ==3 && read_tacts < 80))   captured = 0;
    }
    // else if (data_val >=64 || data_val2 >=64) maybe_packet = 1;

#if ADD_SENS_SUPPORT == 1
      sens_tmp2 = 404;
      sens_CO = 255;
      sens_CH = 255;
      sens_pressure = 0;
      sens_voltage = 0;
      sens_current = 0;
      sens_ip22  = 0;
      sens_ip72 = 0;
      sens_lockalarm = 0;
      sens_pump_count = 0;
      sens_capacitance = 0;
      sens_drop_counter = 0;
#endif
    sens_id = 0;
    sens_chnl = 0;
    sens_battery = 0;
    sens_tmp = 0;
    sens_hmdty = 0;
    UV_index = 0;
    lightness = 0;
    sens_avg_ws = 0;
    sens_max_ws = 0;
    sens_wdir = 0;
    rain_rate = 0;
    total_rain = 0;

////////////////////////////////////////////////////////////////////////////////////////////////////    
//Ðàñøèôðîâêà äàò÷èêîâ Îðåãîí
////////////////////////////////////////////////////////////////////////////////////////////////////    
    if ((sens_type == THGN132 || sens_type == THN132 || (sens_type  & 0x0FFF)== RTGN318 || sens_type == THGR810) && crc_c){

      sens_id = get_id(packet);
      sens_chnl = get_channel(packet);
      sens_battery = get_battery(packet);
      sens_tmp = get_temperature(packet);
      if (sens_type == THGN132 || (sens_type  & 0x0FFF)== RTGN318 || sens_type == THGR810) 
        sens_hmdty = get_humidity(packet);
      else sens_hmdty = 0;
    }

    if (sens_type == WGR800 && crc_c){
      sens_id = get_id(packet);
      sens_battery = get_battery(packet);
      sens_avg_ws = get_avg_windspeed(packet);
      sens_max_ws = get_max_windspeed(packet);
      sens_wdir = get_winddirection(packet);
    }   

    if (sens_type == UVN800 && crc_c){
      sens_id = get_id(packet);
      sens_battery = get_battery(packet);
      UV_index = get_UV(packet);
      lightness = get_light(packet);
    }

    if (sens_type == PCR800 && crc_c){
      sens_id = get_id(packet);
      sens_battery = get_battery(packet);
      rain_rate = get_rain_rate(packet);
      total_rain = get_total_rain(packet);
    }




#if ADD_SENS_SUPPORT == 1
//////////////////////////////////////////////////////////////////////////////////////////////////    
//Ðàñøèôðîâêà êîìïëåêñíûõ ãàçîâûõ äàò÷èêîâ 
////////////////////////////////////////////////////////////////////////////////////////////////////    
    if ((sens_type & 0xFF00) == GAS && crc_c){
      sens_id = 0;
      sens_battery = 0;
      sens_chnl = get_gas_channel(packet);
      sens_tmp = get_gas_temperature_out(packet);
      if (packet[9] == 0x0F) sens_tmp = 404;
      sens_tmp2 = get_gas_temperature_in(packet);
      if (packet[12] == 0x0F) sens_tmp2 = 404;
      sens_hmdty = get_gas_hmdty(packet);
      sens_CO = get_gas_CO(packet);
      sens_CH = get_gas_CH(packet);
    }
//////////////////////////////////////////////////////////////////////////////////////////////////    
//Ðàñøèôðîâêà äàò÷èêîâ ïîæàðíîé ñèãíàëèçàöèè
////////////////////////////////////////////////////////////////////////////////////////////////////    
    if ((sens_type & 0xFF00) == FIRE && crc_c){
      sens_id = 0;
      sens_battery = 0;
           
      sens_chnl = get_gas_channel(packet);
      sens_ip22 = get_fire_ip22(packet);
      sens_ip72 = get_fire_ip72(packet);
      sens_lockalarm = get_fire_lockalarm(packet);
    }
//////////////////////////////////////////////////////////////////////////////////////////////////    
//Ðàñøèôðîâêà äàò÷èêîâ THP
////////////////////////////////////////////////////////////////////////////////////////////////////    
    if ((sens_type & 0xFF00) == THP && crc_c){
      sens_chnl = get_gas_channel(packet);
      sens_voltage = get_thp_voltage(packet);
      sens_tmp = get_thp_temperature(packet);
      sens_hmdty = get_thp_humidity(packet);
      sens_pressure = get_thp_pressure(packet);
    }

//////////////////////////////////////////////////////////////////////////////////////////////////    
//Ðàñøèôðîâêà äàò÷èêîâ òîêà è íàïðÿæåíèÿ
////////////////////////////////////////////////////////////////////////////////////////////////////    
    if ((sens_type & 0xFF00) == CURRENT && crc_c){
      sens_id = 0;
      sens_battery = 0;
           
      sens_chnl = get_gas_channel(packet);
      sens_current = get_current(packet);
      sens_voltage = get_voltage(packet);
      sens_pump_count = get_pump_count(packet);
    }

//////////////////////////////////////////////////////////////////////////////////////////////////    
//Ðàñøèôðîâêà äàò÷èêîâ îñàäêîâ åìêîñòíîãî òèïà
////////////////////////////////////////////////////////////////////////////////////////////////////    
    if ((sens_type & 0xFF00) == CAPRAIN && crc_c){
      sens_id = 0;
      sens_battery = 0;
           
      //sens_heater = 
      sens_drop_counter = get_dropcounter(packet);
      sens_capacitance = get_capacitance(packet);
    }
    
#endif
////////////////////////////////////////////////////////////////////////////////
//Îñòàëüíûå âû÷èñëåíèÿ
//Âîçâðàùàåì âñ¸ â èñõîäíîå ñîñòîÿíèå è âêëþ÷àåì ïðîñëóøèâàíèå ïðè¸ìíèêà
    work_time = millis() - work_time;
    packets_received = 0;
    if (data_val >= 64 && synchro_pos != 255) packets_received++;
    if (data_val2 >= 64 && synchro_pos2 != 255) packets_received++;
    receive_status = FIND_PACKET;
    start_pulse_cnt = 0;  
    packet_number = 0;
    led_light(false);
    //Serial.print("LED = ");
    //Serial.println(LED);
    attachInterrupt(INT_NO, receiver_interruption, CHANGE);  
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Èçâëåêàåò èç çàïèñè òàêòîâóþ ïîñëåäîâàòåëüíîñòè
//Ïàðàìåòðû: cdptr - óêàçàòåëü íà çàïèñàííóþ òàêòîâóþ ïîñëåäîâàòåëüíîñòü
//Ðåçóëüòàò ïèøåòñÿ â ìàññèâ decode_tacts
////////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_NR::get_tacts(byte* cdptr, byte bitsize){
  
//Ñáðîñ ìàññèâîâ
  for(int bt = 0 ; bt < bitsize; bt++) decode_tacts[bt] = 2;      //Èçíà÷àëüíî òàêò íåèçâåñòåí

//Ðàñøèôðîâêà òàêòîâ      
  byte* cdp = cdptr;        
  for(int bt = 0 ; bt < bitsize; bt++)
  {
      if (ver == 2 && decode_method == 1)  
    {
      if ((*cdp & 0xf0) > 0x20 && (*cdp & 0x0f) > 0x03) decode_tacts[bt] = 1;
      if ((*cdp & 0xf0) < 0x30 && (*cdp & 0x0f) < 0x05) decode_tacts[bt] = 0;
      if ((*cdp & 0xf0) < 0x20 && (*cdp & 0x0f) > 0x04) decode_tacts[bt] = 4;
      if ((*cdp & 0xf0) > 0x40 && (*cdp & 0x0f) < 0x02) decode_tacts[bt] = 3;
    }
      if (ver == 2 && decode_method == 2)  
    {
      if (*cdp == 0x88 || *cdp == 0x87  || *cdp == 0x86 || *cdp == 0x85 || *cdp == 0x84 || *cdp == 0x83 || *cdp == 0x78 ||  *cdp == 0x77 || *cdp == 0x68 || *cdp == 0x58 ) decode_tacts[bt] = 1;              // Òàêò 11 (Â ÈÄÅÀËÅ 87, ÍÎ ÈÇ ÇÀ ÑÄÂÈÃÀ ÍÀ 3 ÒÀÊÒÀ ÌÎÆÅÒ ÁÛÒÜ ÎÒ 58 À 84)
      if (*cdp == 0x00 || *cdp == 0x01  || *cdp == 0x02 || *cdp == 0x03 || *cdp == 0x10 || *cdp == 0x20 || *cdp == 0x30) decode_tacts[bt] = 0;                                                         // Òàêò 00 (Â ÈÄÅÀËÅ 00, ÍÎ ÈÇ ÇÀ ÑÄÂÈÃÀ ÍÀ 3 ÒÀÊÒÀ ÌÎÆÅÒ ÁÛÒÜ ÎÒ 30 ÄÀ 03)
      if (*cdp == 0x05 || *cdp == 0x06  || *cdp == 0x07 || *cdp == 0x08 || *cdp == 0x15 || *cdp == 0x16 || *cdp == 0x17 || *cdp == 0x24 || *cdp == 0x25 || *cdp == 0x34 || *cdp == 0x35) decode_tacts[bt] = 4; // Òàêò 01 (Â ÈÄÅÀËÅ 07, ÍÎ ÈÇ ÇÀ ÑÄÂÈÃÀ ÍÀ 3 ÒÀÊÒÀ ÌÎÆÅÒ ÁÛÒÜ ÄÎ 34)
      if (*cdp == 0x50 || *cdp == 0x60  || *cdp == 0x70 || *cdp == 0x80 || *cdp == 0x51 || *cdp == 0x61 || *cdp == 0x71 || *cdp == 0x42 || *cdp == 0x52 || *cdp == 0x43 || *cdp == 0x53) decode_tacts[bt] = 3; // Òàêò 10 (Â ÈÄÅÀËÅ 70, ÍÎ ÈÇ ÇÀ ÑÄÂÈÃÀ ÍÀ 3 ÒÀÊÒÀ ÌÎÆÅÒ ÁÛÒÜ ÄÎ 43)
    }
    if (ver == 3 && decode_method == 1)  
    {
      if ((*cdp & 0xf0) > 0x20 && (*cdp & 0x0f) > 0x04) decode_tacts[bt] = 1;
      if ((*cdp & 0xf0) < 0x30 && (*cdp & 0x0f) < 0x05) decode_tacts[bt] = 0;
      if ((*cdp & 0xf0) < 0x20 && (*cdp & 0x0f) > 0x02) decode_tacts[bt] = 4;
      if ((*cdp & 0xf0) > 0x20 && (*cdp & 0x0f) < 0x02) decode_tacts[bt] = 3;
    }
    if (ver == 3 && decode_method == 2)  
    {
      if (*cdp == 0x87 || *cdp == 0x86  || *cdp == 0x85 || *cdp == 0x84 || *cdp == 0x83 || *cdp == 0x82 || *cdp == 0x78 ||  *cdp == 0x77 || *cdp == 0x76 || *cdp == 0x68 || *cdp == 0x67 ) decode_tacts[bt] = 1;              // Òàêò 11 (Â ÈÄÅÀËÅ 87, ÍÎ ÈÇ ÇÀ ÑÄÂÈÃÀ ÍÀ 3 ÒÀÊÒÀ ÌÎÆÅÒ ÁÛÒÜ ÎÒ 58 À 84)
      if (*cdp == 0x00 || *cdp == 0x01  || *cdp == 0x02 || *cdp == 0x03 || *cdp == 0x10 || *cdp == 0x20 || *cdp == 0x30) decode_tacts[bt] = 0;                                                         // Òàêò 00 (Â ÈÄÅÀËÅ 00, ÍÎ ÈÇ ÇÀ ÑÄÂÈÃÀ ÍÀ 3 ÒÀÊÒÀ ÌÎÆÅÒ ÁÛÒÜ ÎÒ 30 ÄÀ 03)
      if (*cdp == 0x05 || *cdp == 0x06  || *cdp == 0x07 || *cdp == 0x08 || *cdp == 0x15 || *cdp == 0x16 || *cdp == 0x17 || *cdp == 0x24 || *cdp == 0x25 || *cdp == 0x34 || *cdp == 0x35) decode_tacts[bt] = 4; // Òàêò 01 (Â ÈÄÅÀËÅ 07, ÍÎ ÈÇ ÇÀ ÑÄÂÈÃÀ ÍÀ 3 ÒÀÊÒÀ ÌÎÆÅÒ ÁÛÒÜ ÄÎ 34)
      if (*cdp == 0x50 || *cdp == 0x60  || *cdp == 0x70 || *cdp == 0x80 || *cdp == 0x51 || *cdp == 0x61 || *cdp == 0x71 || *cdp == 0x42 || *cdp == 0x52 || *cdp == 0x43 || *cdp == 0x53) decode_tacts[bt] = 3; // Òàêò 10 (Â ÈÄÅÀËÅ 70, ÍÎ ÈÇ ÇÀ ÑÄÂÈÃÀ ÍÀ 3 ÒÀÊÒÀ ÌÎÆÅÒ ÁÛÒÜ ÄÎ 43)
    }
      if (decode_method == 3)  
    {
      if ((((*cdp) >> 4) + (*cdp & 0x0F)) < 5)  decode_tacts[bt] = 0;
      if (((((*cdp) >> 4) + (*cdp & 0x0F)) > 4) && ((((*cdp) >> 4) + (*cdp & 0x0F)) < 10))
	{
          if (((*cdp) >> 4) > (*cdp & 0x0f))  decode_tacts[bt] = 3;    
          if (((*cdp) >> 4) < (*cdp & 0x0f))  decode_tacts[bt] = 4;  
          if (((*cdp) >> 4) == (*cdp & 0x0f) && (*(cdp - 1) & 0x0F) < 4 )  decode_tacts[bt] = 4;  
          if (((*cdp) >> 4) == (*cdp & 0x0f) && (*(cdp - 1) & 0x0F) > 4 )  decode_tacts[bt] = 3;  
	}
      if ((((*cdp) >> 4) + (*cdp & 0x0F)) > 10) decode_tacts[bt] = 1;

    }
   *cdp++;
  }

// Ïå÷àòü ðàñøèôîðâêè
 if (receiver_dump)
 {
   byte* cdp = cdptr;        
   Serial.print("BEFORE ");


  for(int bt = 0 ; bt < bitsize; bt++)

   {

     if (decode_tacts[bt] == 1) Serial.print("II"); 
     if (decode_tacts[bt] == 0) Serial.print("OO"); 
     if (decode_tacts[bt] == 2) Serial.print("__"); 
     if (decode_tacts[bt] == 3) Serial.print("IO"); 
     if (decode_tacts[bt] == 4) Serial.print("OI"); 
     Serial.print(" "); 
     *cdp++;
  }
   Serial.println();
 }


// Ðàñøèôðîâàëè âñ¸, ÷òî ñìîãëè ñ õîäó
//Ïðîâåðÿåì äîïóñòèìà ëè òàêòîâàÿ ïîñëåäîâàòåëüíîñòü

  for(int bt = 1; bt < bitsize; bt++)
  {
//    if (decode_tacts[bt] == 2)
    {
       //Õ0 0X - íåäîïóñòèìà
      if ((decode_tacts[bt - 1]  == 0  || decode_tacts[bt - 1]  == 3) && (decode_tacts[bt] == 0 || decode_tacts[bt] == 4)) decode_tacts[bt] = 2;
       //Õ1 1X - íåäîïóñòèìà
      if ((decode_tacts[bt - 1]  == 1  || decode_tacts[bt - 1]  == 4) && (decode_tacts[bt] == 1 || decode_tacts[bt] == 3)) decode_tacts[bt] = 2;
    }
  }

//Âîññòàíîâëåíèå îäèíî÷íûõ òàêòîâ  
  for(int bt = 1; bt < (bitsize - 1); bt++)
  {
    if (decode_tacts[bt] == 2)
    {
       //Õ0 __ 0Õ
       //Õ0 11 0Õ     
      if ((decode_tacts[bt - 1]  == 0  || decode_tacts[bt - 1]  == 3) && (decode_tacts[bt + 1] == 0 || decode_tacts[bt + 1] == 4)) {
        decode_tacts[bt] = 1;
        restore_sign ^= 2;
      }
       //Õ0 __ 1Õ
       //Õ0 10 1Õ     
      if ((decode_tacts[bt - 1]  == 0  || decode_tacts[bt - 1]  == 3) && (decode_tacts[bt + 1] == 1 || decode_tacts[bt + 1] == 3)){
        decode_tacts[bt] = 3;
        restore_sign ^= 2;
        }
       //Õ1 __ 0Õ
       //Õ1 01 0Õ     
      if ((decode_tacts[bt - 1]  == 1  || decode_tacts[bt - 1]  == 4) && (decode_tacts[bt + 1] == 0 || decode_tacts[bt + 1] == 4)){
        decode_tacts[bt] = 4;
        restore_sign ^= 2;
      }
       //Õ1 __ 1Õ
       //Õ1 00 1Õ     
      if ((decode_tacts[bt - 1]  == 1  || decode_tacts[bt - 1]  == 4) && (decode_tacts[bt + 1] == 1 || decode_tacts[bt + 1] == 3)){
        decode_tacts[bt] = 0;
        restore_sign ^= 2;
      }
    }
  }

  //âîññòàíîâëåíèå ïîòåðÿííûõ ïîëóòàêòîâ
   cdp = cdptr;        
   for(int bt = 1 ; bt < (bitsize - 1); bt++)
   {
     if (decode_tacts[bt] == 2)
     {
     //Õ0 _0
     //Õ0 10
     if ((*cdp & 0x0f) < 0x05 && (decode_tacts[bt - 1] == 0 || decode_tacts[bt - 1] == 3)){
        decode_tacts[bt] = 3; 
        restore_sign ^= 1;
     }
     //Õ1 _1
     //Õ1 01
     if ((*cdp & 0x0f) > 0x04 && (decode_tacts[bt - 1] == 1 || decode_tacts[bt - 1] == 4)){
        decode_tacts[bt] = 4; 
        restore_sign ^= 1;
     }
      //0_ 0X
      //01 0X
     if ((*cdp & 0xF0) < 0x50 && (decode_tacts[bt + 1] == 0 || decode_tacts[bt + 1] == 4)){
        decode_tacts[bt] = 4; 
        restore_sign ^= 1;
     }
       //1_ 1X
      //10 1X
     if ((*cdp & 0xF0) > 0x40 && (decode_tacts[bt + 1] == 1 || decode_tacts[bt + 1] == 3)){
        decode_tacts[bt] = 3; 
        restore_sign ^= 1;
     }
    }
   *cdp++;
  }

  //Ñíîâà ïðîâåðÿåì äîïóñòèìà ëè òàêòîâàÿ ïîñëåäîâàòåëüíîñòü, à òî ÷òî ìû òàì âîññàíîâèëè - íåèçâåñòíî :)

  for(int bt = 1; bt < bitsize; bt++)
  {
    {
       //Õ0 0X - íåäîïóñòèìà
      if ((decode_tacts[bt - 1]  == 0  || decode_tacts[bt - 1]  == 3) && (decode_tacts[bt] == 0 || decode_tacts[bt] == 4)) decode_tacts[bt] = 2;
       //Õ1 1X - íåäîïóñòèìà
      if ((decode_tacts[bt - 1]  == 1  || decode_tacts[bt - 1]  == 4) && (decode_tacts[bt] == 1 || decode_tacts[bt] == 3)) decode_tacts[bt] = 2;
    }
   *cdp++;
  }
 

//Îïðåäåëåíèå âåðñèè ïàêåòà ïî ïðåàìáóëå
//Åñëè ïðåàìáóëà ðàñïîçíàíà íà íåñêîëüêî òàêòîâ óâåðåííî, òî ìîæíî ñóäèòü î âåðñèè ïàêåòà

  //001100110011 -> v2
  if (/*decode_tacts[0] == 0 && decode_tacts[1] == 1  &&*/ decode_tacts[2] == 0 && decode_tacts[3] == 1 && decode_tacts[4] == 0 && decode_tacts[5] == 1 && ver == 3){
   ver = 2;
   restore_sign ^=4;
  }
  //110011001100 -> v2
  if (/*decode_tacts[0] == 1 && decode_tacts[1] == 0  &&*/ decode_tacts[2] == 1 && decode_tacts[3] == 0 && decode_tacts[4] == 1 && decode_tacts[5] == 0 && ver == 3){
   ver = 2;
   restore_sign ^=4;
  }
  //010101010101 -> v3
  if (/*decode_tacts[0] == 4 && decode_tacts[1] == 4  &&*/ decode_tacts[2] == 4 && decode_tacts[3] == 4 && decode_tacts[4] == 4 && decode_tacts[5] == 4 && ver == 2){
   ver = 3;
   restore_sign ^=4;
  }
  //101010101010 -> v3
  if (/*decode_tacts[0] == 3 && decode_tacts[1] == 3  &&*/ decode_tacts[2] == 3 && decode_tacts[3] == 3 && decode_tacts[4] == 3 && decode_tacts[5] == 3 && ver == 2){
   ver = 3;
   restore_sign ^=4;
  }

// Ïå÷àòü ðàñøèôîðâêè
 if (receiver_dump)
 {
   byte* cdp = cdptr;        
   Serial.print("AFTER  ");
   for(int bt = 0 ; bt < bitsize; bt++)
   {
     if (decode_tacts[bt] == 1) Serial.print("II"); 
     if (decode_tacts[bt] == 0) Serial.print("OO"); 
     if (decode_tacts[bt] == 2) Serial.print("__"); 
     if (decode_tacts[bt] == 3) Serial.print("IO"); 
     if (decode_tacts[bt] == 4) Serial.print("OI"); 
     Serial.print(" "); 
     *cdp++;
  }
   Serial.println();
 }


  return;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Èçâëåêàåò èç òàêòîâîé ïîñëåäîâàòåëüíîñòè áèòîâóþ
//Ïàðàìåòðû: cdptr - óêàçàòåëü íà çàïèñàííûå äàííûå
// btt - ñìåùåíèå â òàêòàõ. Ñìåùåíèå íà òàêò ïðè àíàëèçå ìîæåò ïîììî÷ü âîññòàíîâèòü ïàêåò, ó êîòîðîãî ðàçðóøåíî íà÷àëî
// Ôóíêöèÿ âîâçðàùàåò êà÷åñòâî èëè "ãîäíîñòü" ðàñøèôðîâêè - êîëè÷åñâòî óâåðåííî óçíàíûõ òàêòîâ.
// Ñðàâíèâàÿ ãîäíîñòü ñ btt=0 è btt=1 âûáèðàåì ëó÷øóþ
////////////////////////////////////////////////////////////////////////////////////////////////////
int Oregon_NR::get_data(int btt, byte p_ver, byte* cdptr){ //btt - ñìåùåíèå íà òàêò ïðè àíàëèçå ìîæåò ïîììî÷ü âîññòàíîâèòü ïàêåò, ó êîòîðîãî ðàçðóøåíî íà÷àëî
  
  byte* cdp = cdptr;
//×èñòèì ìàññèâ
  for(int bt=0 ; bt<READ_BITS; bt++){
    
    *cdp = 128;
    cdp++;
  }
  cdp = cdptr;

  *cdp = (128+2); //Ïåðâûé áèò âñåãäà åäèíèöà!!!
  cdp++;
  int packet_validity = 0;

  if (p_ver == 2){
    for(int bt=1 ; bt < READ_BITS; bt++){
    
      if(decode_tacts[bt*2-btt]==0) *cdp-=1; // Åñëè 00 - òî âîçìîæíî çäåñü 0
      //if(decode_tacts[bt*2-btt]==0) *cdp-=1; // íà÷àëüíûé âåñ 1 îòáðàñûâàåò äàò÷èêè ñî ñáèòîé ñèíõðîíèçàöèåé. 2 - ëîâèò ñ íèìè
      if(decode_tacts[bt*2-btt]==1) *cdp+=1; // Åñëè 11 - òî ïðåäïîëîæèòåëüíî çäåñü 1
      if(decode_tacts[bt*2-2-btt]==1 && decode_tacts[bt*2-1-btt]==4) *cdp-=1; //Åñëè äî ýòîãî áûëî óâåðåííî 1101 - òî ýòî äîáàâëÿåò óâåðåííîñòè, ÷òî çäåñü 0
      if(decode_tacts[bt*2-2-btt]==0 && decode_tacts[bt*2-1-btt]==3) *cdp+=1;
      if(decode_tacts[bt*2-2-btt]==0 && decode_tacts[bt*2-1-btt]==1) *cdp-=1;
      if(decode_tacts[bt*2-2-btt]==1 && decode_tacts[bt*2-1-btt]==0) *cdp+=1;
    
      if(decode_tacts[bt*2+2-btt]==1 && decode_tacts[bt*2+1-btt]==3) *cdp-=1; // Åñëè ïîñëå ýòîãî èä¸ò 1011 - òî ýòî äîáàâëÿåò óâåðåííîñòè, ÷òî çäåñü 0
      if(decode_tacts[bt*2+2-btt]==0 && decode_tacts[bt*2+1-btt]==4) *cdp+=1;
      if(decode_tacts[bt*2+2-btt]==0 && decode_tacts[bt*2+1-btt]==1) *cdp-=1;
      if(decode_tacts[bt*2+2-btt]==1 && decode_tacts[bt*2+1-btt]==0) *cdp+=1;
    
        //Ïîäñ÷èòûâàåì êîë-âî äîñòîâåðíûõ áèò â ïàêåòå
      if (*cdp>(129))  packet_validity += *cdp - 128;
      if (*cdp<(127)) packet_validity += 128 - *cdp;
      cdp++;
    }
  return packet_validity; //âîçâðàùàåì êîë-âî äîñòîâåðíûõ áàéòîâ
  }

  if (p_ver == 3){
    for(int bt = 1 ; bt < READ_BITS; bt++){
    
      if (*(cdp - 1) > 128) // åñëè äî ýòîãî áûëà 1
      {
        if (decode_tacts[bt - btt] == 0 || decode_tacts[bt - btt] == 1) *cdp -= 2; // Åñëè 00 èëè 11 - òî çäåñü 0
        if (decode_tacts[bt - btt] == 3 || decode_tacts[bt - btt] == 4) *cdp += 2; // Åñëè 01 èëè 10 - òî çäåñü 1
      }
      if (*(cdp - 1) < 128) // åñëè äî ýòîãî áûëà 0
      {
        if (decode_tacts[bt - btt] == 0 || decode_tacts[bt - btt] == 1) *cdp += 2; // Åñëè 00 èëè 11 - òî çäåñü 1
        if (decode_tacts[bt - btt] == 3 || decode_tacts[bt - btt] == 4) *cdp -= 2; // Åñëè 01 èëè 10 - òî çäåñü 0
      }

     // åñëè äî ýòîãî íåïîíÿòíî ÷òî, òî ñêîðåå âñåãî íå óäàëîñü âîññòàíîâèòü öåëûõ äâà òàêòà. ñìîòðèì íà íåñêîëüêî áèò íàçàä

     //Âîññòàíîâëåíèå î÷åð¸äíîñòè ïðè ïðîïóñêå äóõ áèòîâ ê ðÿäó
      if (*(cdp - 1) == 128 && *(cdp - 2) == 128) 
      {
        //0 __ __ 0 - íå ìåíÿåòñÿ
        if ((decode_tacts[bt - btt] == 0 || decode_tacts[bt - btt] == 3) && (decode_tacts[bt - btt - 2] == 0 || decode_tacts[bt - btt - 2] == 4))
	{
	 if (*(cdp - 2) > 128) *cdp += 1;
	 if (*(cdp - 2) < 128) *cdp -= 1;
	}
	else 
	{
        //1 __ __ 0 èëè 0 __ __ 1- ìåíÿåòñÿ
	 if (*(cdp - 2) > 128) *cdp -= 1;
	 if (*(cdp - 2) < 128) *cdp += 1;
	}
      }

     //Âîññòàíîâëåíèå î÷åð¸äíîñòè ïðè ïðîïóñêå òð¸õ áèòîâ ê ðÿäó
      if (*(cdp - 1) == 128 && *(cdp - 2) == 128  && *(cdp - 3) == 128) 
      {
        //0 __ __ 0 - íå ìåíÿåòñÿ
        if ((decode_tacts[bt - btt] == 0 || decode_tacts[bt - btt] == 3) && (decode_tacts[bt - btt - 3] == 0 || decode_tacts[bt - btt - 3] == 4))
	{
	 if (*(cdp - 2) > 128) *cdp += 1;
	 if (*(cdp - 2) < 128) *cdp -= 1;
	}
	else 
	{
        //1 __ __ 0 èëè 0 __ __ 1- ìåíÿåòñÿ
	 if (*(cdp - 2) > 128) *cdp -= 1;
	 if (*(cdp - 2) < 128) *cdp += 1;
	}
      }

     //Âîññòàíîâëåíèå î÷åð¸äíîñòè ïðè ïðîïóñêå ÷åòûð¸õ áèòîâ ê ðÿäó
      if (*(cdp - 1) == 128 && *(cdp - 2) == 128  && *(cdp - 3) == 128  && *(cdp - 4) == 128) 
      {
        //0 __ __ 0 - íå ìåíÿåòñÿ
        if ((decode_tacts[bt - btt] == 0 || decode_tacts[bt - btt] == 3) && (decode_tacts[bt - btt - 4] == 0 || decode_tacts[bt - btt - 4] == 4))
	{
	 if (*(cdp - 2) > 128) *cdp += 1;
	 if (*(cdp - 2) < 128) *cdp -= 1;
	}
	else 
	{
        //1 __ __ 0 èëè 0 __ __ 1- ìåíÿåòñÿ
	 if (*(cdp - 2) > 128) *cdp -= 1;
	 if (*(cdp - 2) < 128) *cdp += 1;
	}
      }
      //Áîëüøå íèáëà ñ ïîìîùüþ êîíòðîëüíîé ñóììû âñ¸ ðàâíî íå âîññòàíîâèòü.


        //Ïîäñ÷èòûâàåì êîë-âî äîñòîâåðíûõ áèò â ïàêåòå
      if (*cdp>(129))  packet_validity += *cdp - 128;
      if (*cdp<(127)) packet_validity += 128 - *cdp;
      cdp++;
    }
  return packet_validity; //âîçâðàùàåì êîë-âî äîñòîâåðíûõ áàéòîâ
  }


}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Ïðîñëóøèâàíèå êàíàëà ñ ÷àñòîòîé äèñêðåòèçàöèè 16384Ãö 
//ò.å. çàïèñü ðàç â 61ìêñ
//Êàæäîìó òàêò ñîîòâåòñòâóåò áàéò. Òàêò äåëèòñÿ íà äâà ïîëóòàêòà. Â êàæäîì ïîëóòàêòå ïðîâîäèòñÿ 8 èçìåðåíèé.
//Ïðè íàëè÷èè ñèãíàëà ïðè èçìåðåíèè äîáàâëÿåòñÿ 1 ê ñîîòâåòñòâóþùåìó íèáëó. Îòñóòñòâèå ñèãíàëà - 0x00. Íàëè÷èå ñèãíàëà â òàêòå 0õ88.
//cdptr - óêàçàòåëü íà îáëàñòü ïàìÿòè, êóäà çàïèñûâàþòñÿ ñèãíàë
//dtl - óêàçàòåëü íà êîëè÷åñòâî ñ÷èòàííûõ òàêòîâ
////////////////////////////////////////////////////////////////////////////////////////////////////
int Oregon_NR::collect(byte* cdptr){
  
  bool cdp_prev_null;
  byte* cdp = cdptr;
  byte nulls_found = 0;
  byte bt2 = 0;
  //////////////////////////////////////////////////////
  //Çàïèñü íà÷èíàåì ñ ýòîãî ìîìåíòà (êîíåö ïîñëåäíåãî èìïóëüñÿ çàöåïêè + 1/16 òàêòà)
  if (ver == 2) 
  {
    pre_marker += 946; //äâà òàêòà
    *cdp = 0x87;   //Ïåðâûå äâà òàêòà èçâåñòíû - 11. Ìû æå ïîéìàëè èìïóëüñ!
    cdp++;
  }

  if (ver == 3)
  {
    pre_marker += 1434; //òðè òàêòà
    *cdp = 0x07;       //Ïåðâûå ÷åòûðå òàêòà èçâåñòíû  - 0101. Ìû æå ïîéìàëè èìïóëüñ!
    *(cdp + 1) = 0x07;  
    cdp += 2;
  }

  //////////////////////////////////////////////////////
  //Íà÷èíàåì ÷èòàòü äàííûå â ïàìÿòü
  // Ìàêñèìàëüíàÿ äëèíà ïîàñûëêà äëÿ v3 - 104ÁÈÒÀ, THN132 - 76 áÈÒ + êàê ìèíèìóì 3 áèòà 111, êîòîðûå ìû óæå íàøëè
  byte bt;

  for (bt = 0 ; bt < READ_BITS2; bt++) {        
    *cdp = 0;
    for (byte ckl = 0; ckl < 8; ckl++) {            // ×èòàåì 8 ðàç çà ïîëóòàêò
      pre_marker += 61;
      while (micros() < pre_marker);
      if (digitalRead(RECEIVER_PIN)) *cdp += 0x10;  // Èçìåðåíèÿ çàïèûâàåì â ñòàðøèé ïîëóáàéò
    }
    for (byte ckl = 0; ckl < 8; ckl++) {
      pre_marker += 61;
      while (micros() < pre_marker);
      if (digitalRead(RECEIVER_PIN)) *cdp += 1;     // Â ñëåäóþùèé ïîëóòàêò èçìåðåíèÿ çàïèûâàåì â ìëàäøèé  ïîëóáàéò. Ýòî ýêîíîìèò ïàìÿòü.

    }
    bt2++;
    // Èäåàëüíûé ïåðèîä 976.5625
    //  Êàæäûå 7 òàêòîâ äîáàâëÿòü 4ìêñ äëÿ âûðàâíèâàíèÿ ïåðèîäà ñ 976ìêñ äî 976.5714ìêñ + 0.009%
    if (bt2 == 7) 
    { 
      pre_marker += (4 + timing_correction) ;
      bt2 = 0;
    }

    /////////////////////////////////////////////
    //Åñòü âðåìÿ äî ïðèõîäà ñëåäóþùåãî ïîëóòàêòà
    //Ìîæíî ïðîâåðèòü, à íå çàêîí÷èëàñü ëè ïîñûëêà
    //Åñëè â êàíàëå ïîñëåäíåå âðåìÿ ïóñòîòà èëè ñëàáûå ïîìåõè, òî ýòî äîáàâëÿåò óâåðåííîñòè, ÷òî íàáëþäàåì îêîí÷àíèå ïàêåòà
    yield();
    if ((*cdp & 0xf0) < 0x30 && (*cdp & 0x0f) < 0x05)  nulls_found++;
    else nulls_found = 0;
    cdp++;

    /////////////////////////////////////////////
    //Åñëè áîëåå empty_space ïóñòûõ ïîëåé â çàïèñè, òî
    //ýòî âåðîÿòíåå âñåãî êîíåö ïîñûëêè. Äàëüøå ÷èòàòü íåò ñìûñëà
    //empty_space - ÷èñëî ýìïèðè÷åñêîå, çàâèñèò îò òèïà ïðè¸ìíèêà è óðîâíÿ ñèãíàëà
    //Åñëè óìåíüøèòü, âîçìîæíî ñïóòàòü ñ ïîâðåæäåíèåì ïàêåòà
    //Åñëè óâåëè÷èòü, òî ìîæíî íå ïðåêðàòèòü ÷òåíèå è íà÷àòü çàïèñûâàòü ïîìåõè
    if (nulls_found > empty_space ) return bt;










    /////////////////////////////////////////////
    //Æä¸ì ïðèõîäà âðåìåíè ñëåäóþùåãî ïîëóòàêòà

    while (micros() < pre_marker);                    
  }
  return bt;
} 
////////////////////////////////////////////////////////////////////////////////////////////////////
//Îïðåäåëåíèå ñìåùåíèÿ ïàêåòîâ äðóã îòíîñèòåëüíî äðóãà
// êà÷åñòâå ïàðàìåòðîâ ïåðåäàþòñÿ óêàçàòåëè íà ìàññèâû äàííûõ
// Âîçâàðàùàÿåòñÿ ñìåùåíèå
// >0 - âòîðîé ïàêåò íà÷àëñÿ ðàíüøå, <0 - Ïåðâûé ïàêåò íà÷àëñÿ ðàíüøå
////////////////////////////////////////////////////////////////////////////////////////////////////
int Oregon_NR::correlate_data(byte* ser1, byte* ser2){
  
  byte best_correl = 0;
  int best_shift = 0;
  byte best_correl_back = 0;
  int best_shift_back = 0;
  byte shift_score[READ_BITS];
  byte* s1;
  byte* s2;
  byte* s2t = ser2;
  //ñìåùàåì ïåðâûé ïàêåò îòíîñèòåëüíî âòîðîãî
  for (byte sht = 0; sht < READ_BITS; sht++){
    s1 = ser1;
    s2 = s2t;
    shift_score[sht] = 0;
    for(byte sp = 0; sp < READ_BITS - sht; sp++){
      if ((*s1 > (128+1) && *s2 > (128+1))||(*s1 < (128-1) && *s2 < (128-1)) ) shift_score[sht]++;
      s2++;
      s1++;
    }
    yield();
    s2t++;
  }
  for (int i = 0; i < READ_BITS; i++){

    if (shift_score[i] > best_correl){
      best_correl = shift_score[i];
      best_shift = i;
    }
  }
 
//Òåïåðü íàîáîðîò -âòðîé ïàêåò îòíîñèòåëüíî ïåðâîãî
  
  byte* s1t = ser1;
  for (byte sht = 0; sht < READ_BITS; sht++){
    s2 = ser2;
    s1 = s1t;
    shift_score[sht] = 0;
    for (byte sp = 0; sp < READ_BITS-sht; sp++){
      
      if ((*s1 > (128+1) && *s2 > (128+1))||(*s1 < (128-1) && *s2 < (128-1)) ) shift_score[sht]++;
      s2++;
      s1++;
    }
    yield();
    s1t++;
  }
// Èùåì íàèëó÷øåå ñîâïàäåíèå äëÿ îáîèõ âàðèàíòîâ 
    
  for (int i = 0; i < READ_BITS; i++){
    
    if (shift_score[i] > best_correl_back){
      best_correl_back = shift_score[i];
      best_shift_back = i;
    }
  }
  //È âîçâðàùàåì ñàìîå ëó÷øåå èç äâóõ
  if (best_correl_back > best_correl) return -best_shift_back;
  else return best_shift;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Ñáîðêà èç äâóõ ïàêåòîâ
// êà÷åñòâå ïàðàìåòðîâ ïåðåäàþòñÿ óêàçàòåëè íà ìàññèâû äàííûõ
// Ïðè÷¸ì ïåðâûì äîëæåí èäòè ðåçóëüòèðóþùèé ïàêåò, ò.å. òîò êîòîðûé èìååò áîëåå äëèííóþ ïðåàìáóëó.
//shift - ñìåùåíèå âòðîãî ïàêåòà îòíîñèòåëüíîãî ïåðâîãî
////////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_NR::assemble_data(byte* s1, byte* s2, int shift){
  
  if (shift >= 0) {
    for (int g = 0; g < shift; g++) s2++;
    for (int i = 0; i < READ_BITS - shift; i++){
      if(*s1 < (128 + 2) && *s1 > (128 - 2) && (*s2 > (128 + 1) || *s2 < (128 - 1))) {
      *s1 = *s2;
      }
      s1++;
      s2++;
    }
  }
  else {
    for (int g = 0; g < -shift; g++) s1++;
    for (int i = 0; i < READ_BITS + shift; i++){
      if(*s1 < (128 + 2) && *s1 > (128 - 2) && (*s2 > (128 + 1) || *s2 < (128 - 1))) {
      *s1 = *s2;
      }
      s1++;
      s2++;
    }
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò ïîçèöèþ ñèíõðîíèáëà â ïîñûëêå. 0xFF - íåò ñèíõðîíèáëà
// 
//code - óêàçàòåëü íà ðàñøèôðîâàííóþ áèòîâóþ ïîñëåäîâàòåëüíîñòü
//result - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
int Oregon_NR::get_synchro_pos(byte* code){
  
  bool syn_found = false;
  byte* cp = code;
  int i = 0;
  for (i = 0; i < READ_BITS - 8; i++){
    if (!consist_synchro && (*cp < 128 && *(cp + 1) > 128 && *(cp + 2) < 128 && *(cp + 3) > 128)) {
      syn_found = true;
      break;
    }

    if (consist_synchro && (*cp < 127 && *(cp + 1) > 129 && *(cp + 2) < 127 && *(cp + 3) > 129)) {
      syn_found = true;
      break;
    }
    
    cp++;
  }
  if (!syn_found) return 0xFF;
  //Ïîñëåäîâàòåëüíîñòü íàøëè, íî íàäî óáåäèòüñÿ, ÷òî ïåðåä ýòèì èä¸ò ïåðàìáóëà, ò. å. óâåðåííûå åäèíèöû 
  // Â ïðåàìáóëå íå ìîæåò áûòü íóëåé! - ýòî ãëàâíîå
  //Ïðåàìáóëó íàäî ïðîñìàòðèâàòü íà 16-3 = 13 áèòîâ íàçàä. Íó õîòÿ áû íà 10!!!
  
  for (byte g = i; i - g < 10 && g > 0; g --){
    cp --;
    if (*cp < 127) return 0xFF; // Ïåðåä ñèíõðîíèáëîì â ïðåàìáóëå íå ìîæåò áûòü óâåðåííîãî íóëÿ. Íåò òóò ñèíõðîíèáëà!
  }
  return (byte) i;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Ñîçäà¸ò êîäîâóþ ïîñûëêó
//code - óêàçàòåëü íà ðàñøèôðîâàííóþ áèòîâóþ ïîñëåäîâàòåëüíîñòü
//result - óêàçàòåëü íà êîäîâóþ ïîñûëêó
//valid - óêàçàòåëü íà êàðòó äîñòîâåðíîñòè êîäîâîé ïîñûëêè
////////////////////////////////////////////////////////////////////////////////////////////////////
int Oregon_NR::get_info_data(byte* code, byte* result, byte* valid){

  byte* rd = result;
  byte* vd = valid;
  //×èñòèì ìàññèâû
  for (int l = 0; l < PACKET_LENGTH; l++){
    *vd = 0;
    *rd = 0;
    vd++;
    rd++;
  }
  rd = result;
  vd = valid;
  
  int csm;
  for (csm = 0; csm < 30; csm++){
    if ( !consist_synchro && (*code < 128 && *(code + 1) > 128 && *(code + 2) < 128 && *(code + 3) > 128)) break; //Íàéäåíà ïîñëåäîâàòåëüíîñòü 0101 
    if (  consist_synchro && (*code < 127 && *(code + 1) > 129 && *(code + 2) < 127 && *(code + 3) > 129)) break; 
    code++;
  }
  // Ñèíõðîíèáë â ïåðâûõ 20 áèòàõ íå íàéäåí, òàêîé ïàêåò íå ðàñøèôðóåøü âî âòîðîé âåðñèè ïðîòîêîëà!
  if (ver == 2 & csm > 22) return 0; 
  // ÄËÿ òðåòüåé âåðñèè ïðîòîêîëà öèôðà èíàÿ
  if (ver == 3 & csm > 30) return 0; 
  //Ïåðåõîäèì íà íà÷àëî ñ÷èòûâàíèÿ
  code += 4;
  int ii = 0;
  for (int i = 0; i < READ_BITS - csm; i++) 
  {
    // ×òîáû íå âûéòè çà ïðåäåëû
    if (i >= PACKET_LENGTH * 4) break;
    byte multipl;
    switch (ii){
      case 0: {multipl = 0x01; break;}
      case 1: {multipl = 0x02; break;}
      case 2: {multipl = 0x04; break;}
      case 3: {multipl = 0x08; break;}
    }

    if (*code > 128 ) {
      *rd += multipl;
      *vd += multipl;
    }
    if (*code < 128 ) *vd += multipl;
    code ++;
    ii ++;
    if (ii == 4) {
      ii = 0;
      vd++;
      rd++;
    }
  }
  return 1;
}
//
////////////////////////////////////////////////////////////////////////////////////////////////////
//Ôóíêöèè ðàñøèôðîâêè äàííûõ ñ äàò÷èêîâ
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò çíà÷åíèå òåìïåðàòóðû
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_temperature(byte* oregon_data){
  if (((sens_type & 0x0FFF) == RTGN318 || sens_type == THGR810 || sens_type == THGN132 || sens_type == THN132) && crc_c)
  {
    float tmprt;
    oregon_data += 8;
    //èñïðàâëÿåì âîçìîæíûå îøèáêè:
    for (int g=0;g < 4; g++)  if (*(oregon_data + g) > 9) *(oregon_data + g) = *(oregon_data + g) - 8;
    tmprt += *(oregon_data) * 0.1;
    tmprt += *(oregon_data + 1);
    tmprt += *(oregon_data + 2) * 10;
    return (*(oregon_data + 3)) ? -tmprt : tmprt;
  }
//  else return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò òèï ñåíñîðà.
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
word Oregon_NR::get_sensor(byte* oregon_data){

    return (word)(*(oregon_data))*0x1000 + (*(oregon_data+1))*0x0100 + (*(oregon_data+2))*0x10 + *(oregon_data+3);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò çíà÷åíèå êàíàëà
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_channel(byte* oregon_data){
  if (crc_c)
  {
    byte channel = 0;  
//    word sens_type = get_sensor(oregon_data);
    if (sens_type == THGN132 || sens_type == THN132 )
    {
      switch (*(oregon_data + 4))  
      {  
      case 1:  
        channel = 1;  
        break;  
      case 2:  
        channel = 2;  
        break;  
      case 4:  
        channel = 3;  
        break;  
      }  
    }
    if ((sens_type & 0x0FFF) == RTGN318 || sens_type == THGR810 )
        channel = *(oregon_data + 4);  
    return channel;  
  }
  else return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_battery(byte* oregon_data){
  if (((sens_type & 0x0FFF) == RTGN318 || sens_type == THGR810 || sens_type == THGN132 || sens_type == THN132 ||  sens_type == WGR800 ||  sens_type == UVN800 ||  sens_type == PCR800) && crc_c)
  return (*(oregon_data+7) & 0x4) ? 0 : 1;  
  else  return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò çíà÷åíèå âëàæíîñòè
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_humidity(byte* oregon_data){

  if (((sens_type & 0x0FFF) == RTGN318 || sens_type == THGR810 || sens_type == THGN132)  && crc_c ){
    byte tmprt = 0;
    oregon_data += 12;
  //èñïðàâëÿåì âîçìîæíûå îøèáêè:
    for (int g=0; g < 2; g++)  if (*(oregon_data + g) > 9) *(oregon_data + g) = *(oregon_data + g) - 8;
    tmprt = *(oregon_data);
    tmprt += *(oregon_data + 1) * 10;
    return (float)tmprt;
  }
  else return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò id äàò÷èêà
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_id(byte* oregon_data){
  if (((sens_type & 0x0FFF) == RTGN318 || sens_type == THGR810 || sens_type == THGN132 || sens_type == THN132 ||  sens_type == WGR800 ||  sens_type == UVN800 || sens_type == PCR800) && crc_c)
  {
    byte tmprt;
    oregon_data += 5;
    tmprt = *(oregon_data) * 0x10;
    tmprt += *(oregon_data + 1);
    return tmprt;
  }
  else return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò ñðåäíåå çíà÷åíèå âåòðà â ì/c
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_avg_windspeed(byte* oregon_data)
{
  if (sens_type == WGR800 && crc_c){
    float tmprt;
    tmprt = *(oregon_data + 15) * 0x10;
    tmprt += *(oregon_data + 14);
    return tmprt / 10;
  }
  else return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò íà÷åíèå ìàêñèìàëüíîãî ïîðûâà âåòðà â ì/c
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_max_windspeed(byte* oregon_data)
{
  if (sens_type == WGR800 && crc_c){
    float tmprt;
    tmprt = *(oregon_data + 12) * 0x10;
    tmprt += *(oregon_data + 11);
    return tmprt / 10;
  }
  else return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò íàïðàâëåíèå âåòðà â êâàäðàíòàõ
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_winddirection(byte* oregon_data)
{
  if (sens_type == WGR800 && crc_c){
    byte tmprt;
    return *(oregon_data + 8) & 0x0F;
    //Êâàäðàíòû  0-N, 1-NNE, 2-NE, 3-ENE, 4-E, 5-ESE, 6-SE, 7-SSE, 8-S, 9-SSW, A-SW, B-WSW, C-W, D-WNW, E-NW,F-NNW
  }
  else return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò UV-èíäåêñ
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_UV(byte* oregon_data)
{
  if (sens_type == UVN800 && crc_c){
    byte tmprt;
    tmprt = *(oregon_data + 9) * 0x10;
    tmprt += *(oregon_data + 8);
    return tmprt;
  }
  else return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîçâðàùàåò îñâåù¸ííîñòü â óñëîâíûõ åäèíèöàõ
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_light(byte* oregon_data)
{
  if (sens_type == UVN800 && crc_c){
    byte tmprt;
    tmprt = *(oregon_data + 11) * 0x10;
    tmprt += *(oregon_data + 10);
    tmprt -= 0x6F;
    return tmprt;
  }
  else return 0;
}

// #################### mr.Tech edition 20.02.2020 ###############################################
// RAIN GAUGE#####################################################################################
float Oregon_NR::get_rain_rate(byte* oregon_data)
{
   if (sens_type == PCR800 && crc_c){
    float tmprt_;
    tmprt_ = (oregon_data[11]);
    tmprt_ += (oregon_data[10]) * 0.1;
    tmprt_ += (oregon_data[9]) * 0.01;
    tmprt_ += (oregon_data[8]) * 0.001;
    return tmprt_ * 2.54; //if your PCR800 is for metric readings
    //return tmprt_; // empirical
  }
  else return 0;
  //Serial.print(oregon_data);
}

float Oregon_NR::get_total_rain(byte* oregon_data)
{
   if (sens_type == PCR800 && crc_c){
    float tmprt;
    // tmprt = *(oregon_data + 17) * 0x2710;//0x3E8; // * 0x64; // digits before coma (ratio digit). Set quality .00000
    // tmprt += *(oregon_data + 16) * 0x3E8; //0x64 - 1000; .0 digit after coma 
    // tmprt += *(oregon_data + 15) * 0x64; //0x0A - 100; .00
    // tmprt += *(oregon_data + 14) * 0x0A; //0x0A - 10; .000
    // tmprt += *(oregon_data + 13); // .0000
    // // tmprt += (oregon_data[12]) * 0.0001;
    // return tmprt / 10000 * 2.54; //if your PCR800 is designed for metric measurement
    tmprt = *(oregon_data + 17);//0x3E8; // * 0x64; // digits before coma (ratio digit). Set quality .00000
    tmprt += *(oregon_data + 16) * 0.1; //0x64 - 1000; .0 digit after coma 
    tmprt += *(oregon_data + 15) * 0.01; //0x0A - 100; .00
    tmprt += *(oregon_data + 14) * 0.001; //0x0A - 10; .000
    tmprt += *(oregon_data + 13) * 0.0001; // .0000
    tmprt += (oregon_data[12]) * 0.00001;
    return tmprt * 2.54; //if your PCR800 is for metric readings
    //return tmprt_; // empirical
  }
  else return 0;
}
// ###############################################################################################


////////////////////////////////////////////////////////////////////////////////////////////////////
//Ïðîâåðêà CRC
//oregon_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
bool Oregon_NR::check_CRC(byte* oregon_data, word sens_type){

  byte* pp=oregon_data;
  byte crc, resived_crc, truecrc, resived_truecrc, i;
  crc=0;
  byte CCIT_POLY = 0x07;
  
  if (sens_type==THGN132){
 //CHKSUM 1...15 
 //CRC 1...5,8...15 STARTSUM = 3Ch, POLY = 07h
    truecrc = 0x3C;
    for(int x=0; x < 15; x++){
      crc += *pp;
      if ( x != 5 && x != 6){
        truecrc ^= *pp;
        for(i = 0; i<4; i++) 
          if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
          else truecrc <<= 1;
      }
      pp++;  
    }
    for(i = 0; i<4; i++) 
      if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
      else truecrc <<= 1;

    resived_crc = (*(oregon_data+15))+(*(oregon_data+16))*0x10;
    resived_truecrc = (*(oregon_data+17))+(*(oregon_data+18))*0x10;
    received_CRC = truecrc;
    return (resived_crc == crc && resived_truecrc == truecrc)? 1 : 0;
  }

  if ((sens_type & 0x0FFF) == RTGN318){
 //CHKSUM 1...15 
 //CRC 1...5,8...15 STARTSUM = 00h, POLY = 07h
    truecrc = 0x0;
    for(int x=0; x<15; x++){
      crc += *pp;
      if ( x != 5 && x != 6){
        truecrc ^= *pp;
        for(i = 0; i<4; i++) 
          if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
          else truecrc <<= 1;
      }
      pp++;  
    }
    for(i = 0; i<4; i++) 
      if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
      else truecrc <<= 1;

    resived_crc = (*(oregon_data+15))+(*(oregon_data+16))*0x10;
    resived_truecrc = (*(oregon_data+17))+(*(oregon_data+18))*0x10;
    received_CRC = truecrc;
    return (resived_crc == crc && resived_truecrc == truecrc)? 1 : 0;
  }

  if (sens_type == THGR810){
 //CHKSUM 1...15 
 //CRC 00h,1...15 STARTSUM = 00h, POLY = 07h
    truecrc = 0x0;
    for(i = 0; i<4; i++) 
      if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
      else truecrc <<= 1;
    for(int x=0; x<15; x++){
      crc += *pp;
      truecrc ^= *pp;
      for(i = 0; i<4; i++) 
        if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
        else truecrc <<= 1;
      pp++;  
    }
    for(i = 0; i<4; i++) 
      if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
      else truecrc <<= 1;

    resived_crc = (*(oregon_data+15))+(*(oregon_data+16))*0x10;
    resived_truecrc = (*(oregon_data+17))+(*(oregon_data+18))*0x10;
    received_CRC = truecrc;
    return (resived_crc == crc && resived_truecrc == truecrc)? 1 : 0;
  }

  if (sens_type == UVN800 ){
 //CHKSUM 1...13 
 //CRC 00h,1...13 STARTSUM = 00h, POLY = 07h
    truecrc = 0x0;
    for(i = 0; i<4; i++) 
      if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
      else truecrc <<= 1;
    for(int x=0; x<13; x++){
      crc += *pp;
      truecrc ^= *pp;
      for(i = 0; i<4; i++) 
        if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
        else truecrc <<= 1;
      pp++;  
    }
    for(i = 0; i<4; i++) 
      if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
      else truecrc <<= 1;

    resived_crc = (*(oregon_data+13))+(*(oregon_data+14))*0x10;
    resived_truecrc = (*(oregon_data+15))+(*(oregon_data+16))*0x10;
    received_CRC = truecrc;
    return (resived_crc == crc && resived_truecrc == truecrc)? 1 : 0;
  }


  if (sens_type == WGR800){
   //CHKSUM 1...17
    for(int x=0; x < 17; x++){ 
      crc += *pp;
      pp++;  
    }

    resived_crc = (*(oregon_data+17))+(*(oregon_data+18))*0x10;
    return (resived_crc == crc)? 1 : 0;
  }
  //### mr.Tech 20.02.2020 ######################
  if (sens_type == PCR800){
   //CHKSUM 1...18
    for(int x=0; x < 18; x++){ 
      crc += *pp;
      pp++;  
    }

    resived_crc = (*(oregon_data+18))+(*(oregon_data+19))*0x10;
    return (resived_crc == crc)? 1 : 0;
  }
  //##################################################

  if (sens_type==THN132){

 //CHKSUM 1...12 
 //CRC 1...5,8...12 STARTSUM = D6h, POLY = 07h
    truecrc = 0xD6;
    for(int x=0; x<12; x++){
      crc += *pp;
      if ( x != 5 && x != 6){
        truecrc ^= *pp;
        for(i = 0; i<4; i++) 
          if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
          else truecrc <<= 1;
      }
      pp++;  
    }
    for(i = 0; i<4; i++) 
      if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
      else truecrc <<= 1;

    resived_crc = (*(oregon_data+12))+(*(oregon_data+13))*0x10;
    resived_truecrc = (*(oregon_data+14))+(*(oregon_data+15))*0x10;
    received_CRC = truecrc;
    return (resived_crc == crc && resived_truecrc == truecrc)? 1 : 0;
  }

#ifdef ADD_SENS_SUPPORT == 1

  if ((sens_type & 0xFF00) == GAS || (sens_type & 0xFF00) == THP || (sens_type & 0xFF00) == FIRE || (sens_type & 0xFF00) == CURRENT  || (sens_type & 0xFF00) == CAPRAIN){
    truecrc = 0x00;
    for(int x=0; x<15; x++){
      crc += *pp;
      truecrc ^= *pp;
      for(i = 0; i<4; i++) 
        if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
        else truecrc <<= 1;
      pp++;  
    }
    for(i = 0; i<4; i++) 
      if(truecrc & 0x80) truecrc = (truecrc << 1) ^ CCIT_POLY;
      else truecrc <<= 1;

    resived_crc = (*(oregon_data+15))+(*(oregon_data+16))*0x10;
    resived_truecrc = (*(oregon_data+17))+(*(oregon_data+18))*0x10;
    received_CRC = truecrc;
    return (resived_crc == crc && resived_truecrc == truecrc)? 1 : 0;

  }
#endif

  return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Âîñòàíîâëåíèå äàííûõ ïî òèïó äàò÷èêà
////////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_NR::restore_data(byte* oregon_data, word sens_type){
  
  byte* pp=oregon_data;
  if (sens_type==THGN132){
    pp+=8;
    for(int x=0; x<6; x++){
      if(*pp>9 && x!=3) *pp-=8;
      pp++;  
    }
  }
  if (sens_type==THN132){
    pp+=8;
    for(int x=0; x<3; x++){
      if(*pp>9) *pp-=8;
      pp++;  
    }
  }
  return;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_NR::led_light(bool led_on) {
  if (LED != 0xFF) { 
    if (PULL_UP && led_on) digitalWrite(LED, LOW);
    if (PULL_UP && !led_on) digitalWrite(LED, HIGH);
    if (!PULL_UP && led_on) digitalWrite(LED, HIGH);
    if (!PULL_UP && !led_on) digitalWrite(LED, LOW);
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//
#if ADD_SENS_SUPPORT == 1
////////////////////////////////////////////////////////////////////////////////////////////////////
//Ôóíêöèè ðàñøèôðîâêè äàííûõ äàò÷èêîâ GAS
//gas_data - óêàçàòåëü íà êîäîâóþ ïîñûëêó
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_gas_channel(byte* gas_data){

  return gas_data[2];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_gas_temperature_out(byte* gas_data){

  int temperat = gas_data[9] * 0x0100 + gas_data[8] * 0x0010 + gas_data[7];
  return ((float)(-1000 + temperat)) / 10;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_gas_temperature_in(byte* gas_data){

  int temperat = gas_data[12] * 0x0100 + gas_data[11] * 0x0010 + gas_data[10];
  return ((float)(-1000 + temperat)) / 10;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_gas_hmdty(byte* gas_data){

  return gas_data[14] * 0x10 + gas_data[13];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_gas_CO(byte* gas_data){

  return gas_data[6] * 0x10 + gas_data[5];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_gas_CH(byte* gas_data){

  return gas_data[4] * 0x10 + gas_data[3];
}

////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_fire_ip22(byte* fire_data){

  return fire_data[4] * 0x10 + fire_data[5];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_fire_ip72(byte* fire_data){

  return fire_data[6] * 0x10 + fire_data[7];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
byte Oregon_NR::get_fire_lockalarm(byte* fire_data){

  return fire_data[8] * 0x10 + fire_data[9];
}


////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_current(byte* current_data){

  return ((float)(current_data[4] * 0x1000 + current_data[5] * 0x0100  + current_data[6] * 0x0010  + current_data[7])) / 1000;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_voltage(byte* voltage_data){

  return ((float)(voltage_data[8] * 0x1000 + voltage_data[9] * 0x0100  + voltage_data[10] * 0x0010  + voltage_data[11])) / 10;
}

word Oregon_NR::get_pump_count(byte* voltage_data){

  return (voltage_data[12] * 0x0100 + voltage_data[13] * 0x0010  + voltage_data[14]);
}
unsigned long Oregon_NR::get_dropcounter(byte* packetdata){

  return (packetdata[10] * 0x10000  + packetdata[11] * 0x1000  + packetdata[12] * 0x100 + packetdata[13] * 0x10 + packetdata[14]);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
int Oregon_NR::get_capacitance(byte* packetdata){

  return (packetdata[6] * 0x1000  + packetdata[7] * 0x100  + packetdata[8] * 0x10 + packetdata[9]);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_thp_temperature(byte* current_data){

  return (float)(current_data[3] * 0x0100 + current_data[4] * 0x0010  + current_data[5] * 0x0001) / 10 - 100;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_thp_pressure(byte* current_data){

  return (float)(current_data[9] * 0x0100 + current_data[10] * 0x0010  + current_data[11] * 0x0001) / 10 + 500;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_thp_voltage(byte* current_data){

  return (float)(current_data[12] * 0x0100 + current_data[13] * 0x0010  + current_data[14] * 0x0001) * 0.01;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float Oregon_NR::get_thp_humidity(byte* current_data){

  return (float)(current_data[6] * 0x0100 + current_data[7] * 0x0010  + current_data[8] * 0x0001) / 10;
}
#endif