-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfineoffset.c
106 lines (88 loc) · 2.24 KB
/
fineoffset.c
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
/*
*
* ATMega168 as a 433MHz wireless transmitter. Can send to tellstick and other compatible
* receivers with the fineoffset protocol.
*
*/
#define F_CPU 8000000UL
#define BAUD 9600
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <util/delay.h>
void send_zero() {
PORTC &= ~(1<<PC2);
_delay_ms(1);
PORTC |= (1<<PC2);
_delay_ms(1.5);
PORTC &= ~(1<<PC2);
}
void send_one() {
PORTC &= ~(1<<PC2);
_delay_ms(1);
PORTC |= (1<<PC2);
_delay_ms(0.5);
PORTC &= ~(1<<PC2);
}
/* this is somewhat ripped from merbanan/rtl_433/src/util.c */
uint8_t crc8(uint32_t payload, unsigned nBytes, uint8_t polynomial, uint8_t init) {
uint8_t remainder = init;
unsigned byte, bit;
uint8_t current_byte;
for (byte = 0; byte < nBytes; ++byte) {
// 32-(8*(0+1))
current_byte = payload >> (32-(8*(byte+1)));
remainder ^= current_byte;
for (bit = 0; bit < 8; ++bit) {
if (remainder & 0x80) {
remainder = (remainder << 1) ^ polynomial;
}
else {
remainder = (remainder << 1);
}
}
}
return remainder;
}
void send_uint8_bitstring(uint8_t intarr, int skip) {
for (int i = 7; i >= skip; i--) {
if ((intarr & (1<<i)) >> i == 0) {
send_zero();
} else {
send_one();
}
}
}
void send_uint16_bitstring(uint16_t intarr, int skip) {
for (int i = (15-skip); i >= 0; i--) {
if ((intarr & (1<<i)) >> i == 0) {
send_zero();
} else {
send_one();
}
}
}
int main () {
DDRC |=1<<PC2; /* PC2 will now be the output pin */
uint8_t preamble = 255;
uint16_t deviceid;
uint16_t temperature;
uint8_t humidity;
uint8_t crc = 0x0;
while (1) {
deviceid = 1116;
humidity = 69;
temperature = 251;
uint32_t payload = 0 | deviceid << 20 | temperature << 8 | humidity;
crc = crc8(payload, 4, 0x31, 0); // crc of payload, 4 bytes, polynomial 0x31 x8+x5+x1+1
for (int i = 0; i <= 3; i++) { // send it three times
send_uint8_bitstring(preamble, 0);
send_uint16_bitstring(deviceid, 4);
send_uint16_bitstring(temperature, 4);
send_uint8_bitstring(humidity, 0);
send_uint8_bitstring(crc, 0);
_delay_ms(0.5);
}
_delay_ms(10000);
}
}