Oregon_TM.cpp
22.7 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
#include "Oregon_TM.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.
//
// 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 Ñåðãåé Çàâèñëÿê
//
// Äàííàÿ ëèöåíçèÿ ðàçðåøàåò ëèöàì, ïîëó÷èâøèì êîïèþ äàííîãî ïðîãðàììíîãî îáåñïå÷åíèÿ è ñîïóòñòâóþùåé äîêóìåíòàöèè
// (â äàëüíåéøåì èìåíóåìûìè «Ïðîãðàììíîå Îáåñïå÷åíèå»), áåçâîçìåçäíî èñïîëüçîâàòü Ïðîãðàììíîå Îáåñïå÷åíèå áåç îãðàíè÷åíèé,
// âêëþ÷àÿ íåîãðàíè÷åííîå ïðàâî íà èñïîëüçîâàíèå, êîïèðîâàíèå, èçìåíåíèå, ñëèÿíèå, ïóáëèêàöèþ, ðàñïðîñòðàíåíèå, ñóáëèöåíçèðîâàíèå
// è/èëè ïðîäàæó êîïèé Ïðîãðàììíîãî Îáåñïå÷åíèÿ, à òàêæå ëèöàì, êîòîðûì ïðåäîñòàâëÿåòñÿ äàííîå Ïðîãðàììíîå Îáåñïå÷åíèå, ïðè ñîáëþäåíèè ñëåäóþùèõ óñëîâèé:
//
// Óêàçàííîå âûøå óâåäîìëåíèå îá àâòîðñêîì ïðàâå è äàííûå óñëîâèÿ äîëæíû áûòü âêëþ÷åíû âî âñå êîïèè èëè çíà÷èìûå ÷àñòè äàííîãî Ïðîãðàììíîãî Îáåñïå÷åíèÿ.
//
// ÄÀÍÍÎÅ ÏÐÎÃÐÀÌÌÍÎÅ ÎÁÅÑÏÅ×ÅÍÈÅ ÏÐÅÄÎÑÒÀÂËßÅÒÑß «ÊÀÊ ÅÑÒÜ», ÁÅÇ ÊÀÊÈÕ-ËÈÁÎ ÃÀÐÀÍÒÈÉ, ßÂÍÎ ÂÛÐÀÆÅÍÍÛÕ ÈËÈ ÏÎÄÐÀÇÓÌÅÂÀÅÌÛÕ, ÂÊËÞ×Àß ÃÀÐÀÍÒÈÈ ÒÎÂÀÐÍÎÉ
// ÏÐÈÃÎÄÍÎÑÒÈ, ÑÎÎÒÂÅÒÑÒÂÈß ÏÎ ÅÃÎ ÊÎÍÊÐÅÒÍÎÌÓ ÍÀÇÍÀ×ÅÍÈÞ È ÎÒÑÓÒÑÒÂÈß ÍÀÐÓØÅÍÈÉ, ÍÎ ÍÅ ÎÃÐÀÍÈ×ÈÂÀßÑÜ ÈÌÈ. ÍÈ Â ÊÀÊÎÌ ÑËÓ×ÀÅ ÀÂÒÎÐÛ ÈËÈ ÏÐÀÂÎÎÁËÀÄÀÒÅËÈ
// ÍÅ ÍÅÑÓÒ ÎÒÂÅÒÑÒÂÅÍÍÎÑÒÈ ÏÎ ÊÀÊÈÌ-ËÈÁÎ ÈÑÊÀÌ, ÇÀ ÓÙÅÐÁ ÈËÈ ÏÎ ÈÍÛÌ ÒÐÅÁÎÂÀÍÈßÌ,  ÒÎÌ ×ÈÑËÅ, ÏÐÈ ÄÅÉÑÒÂÈÈ ÊÎÍÒÐÀÊÒÀ, ÄÅËÈÊÒÅ ÈËÈ ÈÍÎÉ ÑÈÒÓÀÖÈÈ,
// ÂÎÇÍÈÊØÈÌ ÈÇ-ÇÀ ÈÑÏÎËÜÇÎÂÀÍÈß ÏÐÎÃÐÀÌÌÍÎÃÎ ÎÁÅÑÏÅ×ÅÍÈß ÈËÈ ÈÍÛÕ ÄÅÉÑÒÂÈÉ Ñ ÏÐÎÃÐÀÌÌÍÛÌ ÎÁÅÑÏÅ×ÅÍÈÅÌ.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Конструктор
Oregon_TM::Oregon_TM(byte tr_pin)
{
TX_PIN = tr_pin;
pinMode(TX_PIN, OUTPUT);
digitalWrite(TX_PIN, LOW);
}
Oregon_TM::Oregon_TM(void)
{
pinMode(TX_PIN, OUTPUT);
digitalWrite(TX_PIN, LOW);
pinMode(7, OUTPUT); // mr.Tech
digitalWrite(7, HIGH); //mr.Tech
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Функции передатчика////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::sendZero(void)
{
if (protocol == 2){
while (time_marker + TR_TIME * 4 >= micros());
time_marker += TR_TIME * 4;
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(TR_TIME - PULSE_SHORTEN_2);
digitalWrite(TX_PIN, LOW);
delayMicroseconds(TWOTR_TIME + PULSE_SHORTEN_2);
digitalWrite(TX_PIN, HIGH);
}
if (protocol == 3)
{
if (prevstate) while (time_marker + TWOTR_TIME - PULSE_SHORTEN_3 >= micros());
else while (time_marker + TWOTR_TIME >= micros());
time_marker += TWOTR_TIME;
if (prevbit && prevstate)
{
digitalWrite(TX_PIN, LOW);
prevstate = 0;
prevbit = 0;
return;
}
if (prevbit && !prevstate)
{
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(TWOTR_TIME);
prevstate = 1;
prevbit = 0;
return;
}
if (!prevbit && prevstate)
{
digitalWrite(TX_PIN, LOW);
delayMicroseconds(TR_TIME);
digitalWrite(TX_PIN, HIGH);
prevbit = 0;
return;
}
if (!prevbit && !prevstate)
{
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(TR_TIME);
digitalWrite(TX_PIN, LOW);
prevbit = 0;
return;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::sendOne(void)
{
if (protocol == 2){
while (time_marker + TR_TIME * 4 - PULSE_SHORTEN_2>= micros());
time_marker += TR_TIME * 4;
digitalWrite(TX_PIN, LOW);
delayMicroseconds(TR_TIME + PULSE_SHORTEN_2);
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(TWOTR_TIME - PULSE_SHORTEN_2);
digitalWrite(TX_PIN, LOW);
}
if (protocol == 3)
{
if (prevstate) while (time_marker + TWOTR_TIME - PULSE_SHORTEN_3 >= micros());
else while (time_marker + TWOTR_TIME >= micros());
time_marker += TWOTR_TIME;
if (!prevbit && prevstate)
{
digitalWrite(TX_PIN, LOW);
prevstate = 0;
prevbit = 1;
return;
}
if (!prevbit && !prevstate)
{
digitalWrite(TX_PIN, HIGH);
prevstate = 1;
prevbit = 1;
return;
}
if (prevbit && prevstate)
{
digitalWrite(TX_PIN, LOW);
delayMicroseconds(TR_TIME);
digitalWrite(TX_PIN, HIGH);
prevbit = 1;
return;
}
if (prevbit && !prevstate)
{
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(TR_TIME);
digitalWrite(TX_PIN, LOW);
prevbit = 1;
return;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::sendMSB(byte data)
{
(bitRead(data, 4)) ? sendOne() : sendZero();
(bitRead(data, 5)) ? sendOne() : sendZero();
(bitRead(data, 6)) ? sendOne() : sendZero();
(bitRead(data, 7)) ? sendOne() : sendZero();
if (protocol == 2) time_marker += timing_corrector2; //Поправка на разницу тактовых частот 1024.07Гц и 1024.60Гц
if (protocol == 3) time_marker += timing_corrector3;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::sendLSB(byte data)
{
(bitRead(data, 0)) ? sendOne() : sendZero();
(bitRead(data, 1)) ? sendOne() : sendZero();
(bitRead(data, 2)) ? sendOne() : sendZero();
(bitRead(data, 3)) ? sendOne() : sendZero();
if (protocol == 2) time_marker += timing_corrector2; //Поправка на разницу тактовых частот 1024.07Гц и 1024.60Гц
if (protocol == 3) time_marker += timing_corrector3;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::sendData()
{
for(byte i = 0; i < 10; ++i)
{
sendMSB(SendBuffer[i]);
if (i < 9)
sendLSB(SendBuffer[i]);
if (protocol == 2) time_marker += 4; //Поправка на разницу тактовых частот 1024.07Гц и 1024.60Гц
//if (protocol == 3) time_marker += 4;
//Поправка на разницу тактовых частот 1024.07Гц и 1024Гц
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::sendOregon()
{
time_marker=micros();
sendPreamble();
sendLSB(0xA);
sendData();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::sendPreamble(void)
{
if (protocol == 2){
sendLSB(0xF);
sendLSB(0xF);
time_marker += 9;
sendLSB(0xF);
sendLSB(0xF);
time_marker += 9;
}
if (protocol == 3){
sendLSB(0xF);
sendLSB(0xF);
sendLSB(0xF);
sendLSB(0xF);
time_marker += 4;
sendLSB(0xF);
sendLSB(0xF);
time_marker += 3;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::calculateAndSetChecksum132(void)
{
byte CCIT_POLY = 0x07;
SendBuffer[7] &= 0xF0;
SendBuffer[8] = 0x00;
SendBuffer[9] = 0x00;
byte summ = 0x00;
byte crc = 0x3C;
byte cur_nible;
for(int i = 0; i<8; i++)
{
cur_nible = (SendBuffer[i] & 0xF0) >> 4;
summ += cur_nible;
if (i !=3)
{
crc ^= cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
}
cur_nible = SendBuffer[i] & 0x0F;
summ += cur_nible;
if (i !=2)
{
crc ^= cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
}
}
SendBuffer[7] += summ & 0x0F;
SendBuffer[8] += summ & 0xF0;
SendBuffer[8] += crc & 0x0F;
SendBuffer[9] += crc & 0xF0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::calculateAndSetChecksum318()
{
byte CCIT_POLY = 0x07;
SendBuffer[7] = SendBuffer[7] & 0xF0;
SendBuffer[8] = 0x00;
SendBuffer[9] = 0x00;
byte summ = 0x00;
byte crc = 0x00;
byte cur_nible;
for(int i = 0; i<8; i++)
{
cur_nible = (SendBuffer[i] & 0xF0) >> 4;
summ += cur_nible;
if (i !=3)
{
crc ^= cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
}
cur_nible = SendBuffer[i] & 0x0F;
summ += cur_nible;
if (i !=2)
{
crc ^= cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
}
}
SendBuffer[7] += summ & 0x0F;
SendBuffer[8] += summ & 0xF0;
SendBuffer[8] += crc & 0x0F;
SendBuffer[9] += crc & 0xF0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::calculateAndSetChecksum810()
{
byte CCIT_POLY = 0x07;
SendBuffer[7] = SendBuffer[7] & 0xF0;
SendBuffer[8] = 0x00;
SendBuffer[9] = 0x00;
byte summ = 0x00;
byte crc = 0x00;
byte cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
for(int i = 0; i<8; i++)
{
cur_nible = (SendBuffer[i] & 0xF0) >> 4;
summ += cur_nible;
crc ^= cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
cur_nible = SendBuffer[i] & 0x0F;
summ += cur_nible;
crc ^= cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
}
SendBuffer[7] += summ & 0x0F;
SendBuffer[8] += summ & 0xF0;
SendBuffer[8] += crc & 0x0F;
SendBuffer[9] += crc & 0xF0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::SendPacket()
{
if (sens_type == THGN132)
calculateAndSetChecksum132();
if (sens_type == RTGN318)
calculateAndSetChecksum318();
if (sens_type == THGR810)
calculateAndSetChecksum810();
if (sens_type == THP)
calculateAndSetChecksumTHP();
sendOregon();
digitalWrite(TX_PIN, LOW);
if (protocol == 2){
delayMicroseconds(TWOTR_TIME*15);
sendOregon();
digitalWrite(TX_PIN, LOW);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Функции кодирования данных//////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setType(word type)
{
sens_type = type;
if (type == THP)
{
SendBuffer[0] = 0x55;
return;
}
SendBuffer[0] = (type & 0xFF00) >> 8;
SendBuffer[1] = type & 0x00FF;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setChannel(byte channel)
{
byte channel_code;
if (sens_type == THGN132)
{
if (channel <= 1)
{
channel_code = 0x10;
setId(0xE3);
send_time = 39000;
}
if (channel == 2)
{
channel_code = 0x20;
setId(0xE3);
send_time = 41000;
}
if (channel == 3)
{
channel_code = 0x40;
setId(0xBB);
send_time = 43000;
}
protocol = 2;
}
if (sens_type == RTGN318)
{
if (channel <= 1)
{
channel_code = 0x10;
setId(0xF1);
send_time = 53000;
}
if (channel == 2)
{
channel_code = 0x20;
setId(0x92);
send_time = 59000;
}
if (channel == 3)
{
channel_code = 0x30;
setId(0xAA);
send_time = 61000;
}
if (channel == 4)
{
channel_code = 0x40;
setId(0x8A);
send_time = 67000;
}
if (channel >= 5)
{
channel_code = 0x50;
setId(0xB1);
send_time = 71000;
}
protocol = 2;
}
if (sens_type == THGR810)
{
if (channel <= 1)
{
channel_code = 0x10;
setId(0xCB);
send_time = 53000;
}
if (channel == 2)
{
channel_code = 0x20;
setId(0x69);
send_time = 59000;
}
if (channel == 3)
{
channel_code = 0x30;
setId(0xAA);
send_time = 61000;
}
if (channel == 4)
{
channel_code = 0x40;
setId(0x8A);
send_time = 67000;
}
if (channel == 5)
{
channel_code = 0x50;
setId(0xB1);
send_time = 71000;
}
if (channel == 6)
{
channel_code = 0x60;
send_time = 79000;
}
if (channel == 7)
{
channel_code = 0x70;
send_time = 83000;
}
if (channel == 8)
{
channel_code = 0x80;
send_time = 87000;
}
if (channel == 9)
{
channel_code = 0x90;
send_time = 91000;
}
if (channel >= 10)
{
channel_code = 0xA0;
send_time = 93000;
}
protocol = 3;
}
SendBuffer[2]&= 0x0F;
SendBuffer[2] += channel_code & 0xF0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setId(byte ID)
{
SendBuffer[2]&= 0xF0;
SendBuffer[2] += (ID & 0xF0) >> 4;
SendBuffer[3]&= 0x0F;
SendBuffer[3] += (ID & 0x0F) << 4;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setBatteryFlag(bool level)
{
SendBuffer[3] &= 0xFB;
if (level) SendBuffer[3] |= 0x04;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setStartCount(byte startcount)
{
SendBuffer[3] &= 0xF4;
if (startcount == 8) SendBuffer[3] |= 0x08;
if (startcount == 2) SendBuffer[3] |= 0x02;
if (startcount == 1) SendBuffer[3] |= 0x01;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setTemperature(float temp)
{
if(temp < 0)
{
SendBuffer[5] = 0x08;
temp *= -1;
}
else
{
SendBuffer[5] = 0x00;
}
byte tempInt = (byte) temp;
byte td = (tempInt / 10);
byte tf = tempInt - td * 10;
byte tempFloat = (temp - (float)tempInt) * 10;
SendBuffer[5] += (td << 4);
SendBuffer[4] = tf;
SendBuffer[4] |= (tempFloat << 4);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setHumidity(byte hum)
{
SendBuffer[6] = (hum/10);
SendBuffer[6] += (hum - (SendBuffer[6] * 10)) << 4;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setComfort(float temp, byte hum)
{
if (hum > 70)
{
SendBuffer[7] = 0xC0;
return;
}
if (hum < 40)
{
SendBuffer[7] = 0x80;
return;
}
if (temp > 20 && temp < 25)
{
SendBuffer[7] = 0x40;
return;
}
else SendBuffer[7] = 0x00;
return;
}
// Передача - заметка от mr.Tech //////////////////////////////////////////////////////////////////////
bool Oregon_TM::transmit()
{
if (millis() >= time_marker_send && send_time)
{
digitalWrite(7, LOW);// Включение режима TX на 7-й ноге - начало TX. Добавлено mr.Tech 09.03.2020
delay(500); // задержка перед началом преамбулы. mr.Tech
SendPacket();
time_marker_send = millis() + send_time;
digitalWrite(7, HIGH);// Отключение режима TX на 7-й ноге - конец TX. Добавлено mr.Tech 09.03.2020
return true;
}
else return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//Поддержка датчика THP
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setChannelTHP(byte channel)
{
SendBuffer[1] &= 0x0F;
SendBuffer[1] += channel << 4;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setBatteryTHP( word bat_voltage)
{
SendBuffer[6] = (bat_voltage & 0x0FF0) >> 4;
SendBuffer[7] &= 0x0F;
SendBuffer[7] += (bat_voltage & 0x000F) << 4;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setTemperatureTHP(float bme_temperature)
{
word temp_code;
if (bme_temperature < -100 || bme_temperature > 100) temp_code = 0x0FFF;
else temp_code = (word)((bme_temperature + 100) * 10);
SendBuffer[2] = temp_code & 0x00FF;
SendBuffer[1] &= 0xF0;
SendBuffer[1] += (temp_code & 0x0F00) >> 8;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setHumidityTHP(float bme_humidity)
{
word hum_code;
if (bme_humidity > 100) hum_code = 0x0FFF;
else hum_code = (word)(bme_humidity * 10);
SendBuffer[3] = (hum_code & 0x0FF0) >> 4;
SendBuffer[4] &= 0x0F;
SendBuffer[4] += (hum_code & 0x000F) << 4;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setPressureTHP(float bme_pressure)
{
word pres_code;
if (bme_pressure < 500) pres_code = 0x0000;
else pres_code = (word)((bme_pressure - 500) * 10);
SendBuffer[5] = pres_code & 0x00FF;
SendBuffer[4] &= 0xF0;
SendBuffer[4] += (pres_code & 0x0F00) >> 8;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::setErrorTHP()
{
SendBuffer[1] |= 0x0F;
SendBuffer[2] = 0xFF;
SendBuffer[3] = 0xFF;
SendBuffer[4] = 0xFF;
SendBuffer[5] = 0xFF;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Oregon_TM::calculateAndSetChecksumTHP()
{
byte CCIT_POLY = 0x07;
SendBuffer[7] = SendBuffer[7] & 0xF0;
SendBuffer[8] = 0x00;
SendBuffer[9] = 0x00;
byte summ = 0x00;
byte crc = 0x00;
byte cur_nible;
for(int i = 0; i<8; i++)
{
cur_nible = (SendBuffer[i] & 0xF0) >> 4;
summ += cur_nible;
crc ^= cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
cur_nible = SendBuffer[i] & 0x0F;
summ += cur_nible;
crc ^= cur_nible;
for(int j = 0; j < 4; j++)
if (crc & 0x80) crc = (crc << 1) ^ CCIT_POLY;
else crc <<= 1;
}
SendBuffer[7] += summ & 0x0F;
SendBuffer[8] += summ & 0xF0;
SendBuffer[8] += crc & 0x0F;
SendBuffer[9] += crc & 0xF0;
}