-
Notifications
You must be signed in to change notification settings - Fork 0
/
FineOffset.cpp
75 lines (66 loc) · 1.66 KB
/
FineOffset.cpp
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
/*
* FineOffset - Transmit temperature and humidity using the Fine Offset
* wireless protocol.
*
* Copyright (c) 2016 Björn Stenberg <[email protected]>
* License: MIT, see file LICENSE
*
*/
#include <FineOffset.h>
FineOffset::FineOffset(int transmit_pin, int repeat_count)
{
pin = transmit_pin;
repeat = repeat_count;
pinMode(pin, OUTPUT);
}
void FineOffset::send_byte(uint8_t byte)
{
for(int i=0; i<8; i++) {
int delay = 1500;
digitalWrite(pin, HIGH);
if (byte & 0x80)
delay = 500;
delayMicroseconds(delay);
digitalWrite(pin, LOW);
delayMicroseconds(1000);
byte <<= 1;
}
}
void FineOffset::send(int device_id, float temperature, int humidity)
{
uint8_t crc;
uint8_t bytes[4];
int inttemp = round(abs(temperature * 10));
int sign = (temperature < 0) ? 1 : 0;
uint32_t data =
((long)device_id << 20) |
((long)sign << 19) |
((long)inttemp << 8) |
(long)humidity;
for (int i=0, mask=24; i<4; i++, mask-=8)
bytes[i] = (data >> mask) & 0xff;
crc = crc8(bytes, 4);
for (int r=0; r<repeat; r++) {
send_byte(0xff); // preamble
for (int i=0; i<4; i++)
send_byte(bytes[i]);
send_byte(crc);
if (repeat > 1)
delay(10);
}
}
uint8_t FineOffset::crc8( uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
// Indicated changes are from reference CRC-8 function in OneWire library
while (len--) {
uint8_t inbyte = *addr++;
for (uint8_t i = 8; i; i--) {
uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
crc <<= 1; // changed from right shift
if (mix) crc ^= 0x31;// changed from 0x8C;
inbyte <<= 1; // changed from right shift
}
}
return crc;
}