-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathask.c
244 lines (240 loc) · 8.83 KB
/
ask.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
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
#include "ask_config.h"
#include "ask.h"
#include <string.h>
//################################################################################################################
bool ask_init(ask_t *ask)
{
ask->lock = false;
if (ask->fn_micros == NULL)
return false;
if (ask->fn_delay_ms == NULL)
return false;
ask->detect_busy = false;
if (ask->fn_init_rx != NULL)
ask->fn_init_rx();
if (ask->fn_read_pin != NULL)
ask->enable_rx = true;
if (ask->fn_init_tx != NULL)
ask->fn_init_tx();
if (ask->fn_write_pin != NULL)
{
if (ask->fn_delay_us == NULL)
return false;
ask->fn_write_pin(false);
}
return true;
}
//################################################################################################################
void ask_pinchange_callback(ask_t *ask)
{
if (!ask->enable_rx)
return;
if (!ask->detect_end)
{
if (!ask->detect_begin)
{
if ((ask->fn_micros() - ask->time > _ASK_MIN_NEW_FRAM_DETECT_TIME_) && ask->fn_read_pin())
{
ask->detect_begin = true;
ask->index = 0;
}
}
else
{
if ((ask->fn_micros() - ask->time > _ASK_MIN_NEW_FRAM_DETECT_TIME_) && ask->fn_read_pin())
{
ask->detect_end = true;
}
else
{
ask->buffer[ask->index] = ask->fn_micros() - ask->time;
if (ask->index < (_ASK_MAX_BYTE_LEN_ * 16 + 1))
ask->index++;
}
}
}
ask->time = ask->fn_micros();
}
//################################################################################################################
bool ask_available(ask_t *ask)
{
if (ask->lock == true)
return false;
ask->lock = true;
if (ask->index > (_ASK_MAX_BYTE_LEN_ * 16 + 1))
{
ask->detect_begin = false;
ask->detect_end = false;
ask->lock = false;
return false;
}
if (ask->detect_end && !ask->detect_busy)
{
do
{
if (ask->index < (_ASK_MIN_BYTE_LEN_ * 16 + 1))
break;
memset(ask->buffer_byte, 0, _ASK_MAX_BYTE_LEN_);
ask->buffer_time = ask->buffer[0] + ask->buffer[1];
if (ask->buffer[0] > ask->buffer[1])
{
ask->buffer_time_high[0] = ask->buffer[0] - ((_ASK_TOLERANCE_ * ask->buffer[0]) / 100);
ask->buffer_time_high[1] = ask->buffer[0] + ((_ASK_TOLERANCE_ * ask->buffer[0]) / 100);
ask->buffer_time_low[0] = ask->buffer[1] - ((_ASK_TOLERANCE_ * ask->buffer[1]) / 100);
ask->buffer_time_low[1] = ask->buffer[1] + ((_ASK_TOLERANCE_ * ask->buffer[1]) / 100);
}
else if (ask->buffer[0] < ask->buffer[1])
{
ask->buffer_time_high[0] = ask->buffer[1] - ((_ASK_TOLERANCE_ * ask->buffer[1]) / 100);
ask->buffer_time_high[1] = ask->buffer[1] + ((_ASK_TOLERANCE_ * ask->buffer[1]) / 100);
ask->buffer_time_low[0] = ask->buffer[0] - ((_ASK_TOLERANCE_ * ask->buffer[0]) / 100);
ask->buffer_time_low[1] = ask->buffer[0] + ((_ASK_TOLERANCE_ * ask->buffer[0]) / 100);
}
else
break;
ask->data_bit = 0;
memset(ask->data_byte, 0, _ASK_MAX_BYTE_LEN_);
for (uint16_t i = 0; i < ask->index - 1 ; i+=2)
{
// detect 0 data
if ((ask->buffer[i] > ask->buffer_time_low[0]) && (ask->buffer[i] < ask->buffer_time_low[1])
&& (ask->buffer[i+1] > ask->buffer_time_high[0]) && (ask->buffer[i+1] < ask->buffer_time_high[1]))
{
ask->data_bit++;
}
// detect 1 data
else if ((ask->buffer[i+1] > ask->buffer_time_low[0]) && (ask->buffer[i+1] < ask->buffer_time_low[1])
&& (ask->buffer[i] > ask->buffer_time_high[0]) && (ask->buffer[i] < ask->buffer_time_high[1]))
{
ask->data_byte[ask->data_bit / 8] |= 0x80 >> (ask->data_bit % 8);
ask->data_bit++;
}
else
break;
}
if (ask->data_bit % 8 != 0)
break;
if ((ask->data_bit > (_ASK_MAX_BYTE_LEN_ * 8)) || (ask->data_bit < (_ASK_MIN_BYTE_LEN_ * 8)))
break;
ask->detect_busy = true;
ask->lock = false;
return true;
} while (0);
ask->detect_begin = false;
ask->detect_end = false;
ask->lock = false;
return false;
}
else if (ask->detect_busy)
{
ask->lock = false;
return true;
}
ask->lock = false;
return false;
}
//################################################################################################################
void ask_wait(ask_t *ask)
{
while (ask_available(ask))
{
ask_reset_available(ask);
ask->fn_delay_ms(200);
}
}
//################################################################################################################
void ask_reset_available(ask_t *ask)
{
while (ask->lock == true)
ask->fn_delay_ms(1);
ask->lock = true;
memset(ask->buffer, 0, sizeof(ask->buffer));
ask->detect_begin = false;
ask->detect_end = false;
ask->detect_busy = false;
ask->lock = false;
}
//################################################################################################################
uint8_t ask_read_bytes(ask_t *ask, uint8_t *data)
{
while (ask->lock == true)
ask->fn_delay_ms(1);
ask->lock = true;
memcpy(data, ask->data_byte, ask->data_bit / 8);
ask->lock = false;
return ask->data_bit / 8;
}
//################################################################################################################
uint16_t ask_read_time_of_bit(ask_t *ask)
{
return ask->buffer_time;
}
//################################################################################################################
void ask_send_bytes(ask_t *ask, uint8_t *data, uint8_t len, uint32_t bit_time_micros, uint8_t try_to_send)
{
if (ask->fn_write_pin == NULL)
return;
while (ask->lock == true)
ask->fn_delay_ms(1);
ask->lock = true;
if (ask->fn_read_pin != NULL)
ask->enable_rx = false;
ask->fn_write_pin(true);
ask->fn_delay_ms(bit_time_micros * 8 / 1000);
for (uint8_t t = 0; t < try_to_send; t++)
{
ask->fn_write_pin(false);
ask->fn_delay_ms(bit_time_micros * 8 / 1000);
for (uint8_t byte = 0; byte < len; byte++)
{
for (int8_t bit = 7; bit > -1; bit--)
{
if (data[byte] & (1<<bit))
{
ask->fn_write_pin(true);
ask->fn_delay_us(bit_time_micros * 75 / 100);
ask->fn_write_pin(false);
ask->fn_delay_us(bit_time_micros * 25 / 100);
}
else
{
ask->fn_write_pin(true);
ask->fn_delay_us(bit_time_micros * 25 / 100);
ask->fn_write_pin(false);
ask->fn_delay_us(bit_time_micros * 75 / 100);
}
}
}
ask->fn_write_pin(true);
ask->fn_delay_us(bit_time_micros * 25 / 100);
ask->fn_write_pin(false);
}
if (ask->fn_read_pin != NULL)
ask->enable_rx = true;
ask->lock = false;
}
//################################################################################################################
int16_t ask_checkChannelLast4Bit(uint8_t *newCode, uint8_t *refrence, uint8_t len)
{
if (len < 1)
return -1;
uint8_t maskNew[len];
uint8_t maskRef[len];
memcpy(maskNew, newCode, len);
memcpy(maskRef, refrence, len);
maskNew[len - 1] &= 0xF0;
maskRef[len - 1] &= 0xF0;
if (memcmp(maskNew, maskRef, len) != 0)
return -1;
return newCode[len - 1] & 0x0F;
}
//################################################################################################################
int16_t ask_checkChannelLast8Bit(uint8_t *newCode, uint8_t *refrence, uint8_t len)
{
if (len < 1)
return -1;
if (memcmp(newCode, refrence, len - 1) != 0)
return -1;
return newCode[len - 1];
}
//################################################################################################################