-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitwise.c
136 lines (112 loc) · 3.29 KB
/
bitwise.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
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
#include <stdio.h>
#include <stdint.h>
#define CRC_POLY 0x31
/* Tried using this for testing */
uint8_t crc = 0; // global, holds the crc
void send_uint8_bitstring(uint8_t intarr, int skip) {
char bitstring[30] = "";
for (int i = 7; i >= skip; i--) {
if ((intarr & (1<<i)) >> i == 0) {
printf("send_zero(); ");
} else {
printf("send_one(); ");
}
sprintf(bitstring, "%s%d", bitstring, (intarr & (1<<i)) >> i);
}
printf("\n%s\n", bitstring);
}
/* 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;
}
uint8_t crc8_update(uint8_t b)
{
uint8_t do_xor;
uint8_t reg;
reg = crc;
do_xor = (reg & 0x80);
reg <<=1;
reg |= b;
if (do_xor)
{
reg ^= CRC_POLY;
}
crc = reg;
}
void send_uint32_bitstring(uint16_t intarr, int skip) {
char bitstring[40] = "";
for (int i = (31-skip); i >= 0; i--) {
if ((intarr & (1<<i)) >> i == 0) {
printf("send_zero(); ");
} else {
printf("send_one(); ");
}
sprintf(bitstring, "%s%d", bitstring, (intarr & (1<<i)) >> i);
}
printf("\n%s\n", bitstring);
}
void send_uint16_bitstring(uint16_t intarr, int skip) {
char bitstring[30] = "";
for (int i = (15-skip); i >= 0; i--) {
if ((intarr & (1<<i)) >> i == 0) {
printf("send_zero(); ");
} else {
printf("send_one(); ");
}
sprintf(bitstring, "%s%d", bitstring, (intarr & (1<<i)) >> i);
}
printf("\n%s\n", bitstring);
}
int main () {
// protocol:
// 8 bits of preamble (255) (0b11111111)
// 12 bits of device id (0b0100 0101 1100)
// 12 bits of temperature (0b0000 1000 1111)
// 8 bits of humidity (0b01000111) = 71
// 8 bits of crc8 (0b1001 1000) this is the valid CRC for this packet
uint8_t preamble = 255;
uint16_t deviceid = 1116;
uint16_t temperature = 251;
uint8_t humidity = 69;
crc = 0;
// payload = 0000 0000 0000 0000 0000 0000 0000 0000
uint32_t payload = 0 | deviceid << 20 | temperature << 8 | humidity;
// deviceid = 0000 0000 0000 0000 0000 1011 0111 0110
// deviceid << 20 = 1011 0111 0110 0000 0000 0000 0000 0000
// payload = 1011 0111 0110 0000 0000 0000 0000 0000
// temperature = 0000 0000 0000 0000 0000 0000 1111 1011
// payload = 1011 0111 0110 0000 1111 1011 0000 0000
// humidity = xxxx xxxx xxxx xxxx xxxx xxxx 0100 0101
// payload = 1011 0111 0110 0000 1111 1011 0100 0101
// actual: 1011 0111 0110 0000 1111 1011 0100 0101
// confirmed working.
crc = crc8(payload, 4, 0x31, 0);
printf("This should be 10011000\n");
printf("It is: \n");
send_uint8_bitstring(crc, 0);
printf("\n\n");
printf("Payload: %u\n", payload);
printf("Hello\n");
/*
send_uint8_bitstring(preamble, 0);
send_uint16_bitstring(deviceid, 4);
send_uint16_bitstring(temperature, 4);
send_uint8_bitstring(humidity, 0);
*/
}