-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransmitter.cpp
285 lines (258 loc) · 7.65 KB
/
Transmitter.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
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
#include <Arduino.h>
#include <Transmitter.h>
Transmitter::Transmitter(int frequency)
{
_pin = 0;
_pos = 0;
_manHalf = 0;
_busy = false;
_state = 0;
_active = false;
Timer2.initialize(frequency);
}
Transmitter* Transmitter::_instance;
/**
* sends a LEDBitmap of a width of 8 and a height up to 8
* via the specified LED pin.
*
* @param image The image that is to be transmitted.
*
* @return 0 if the transmission was successful,
* 1 if there is already a transmission active,
* 2 if no transmission pin has been specified.
*/
int Transmitter::sendData(LEDBitmap &image) {
#ifdef DEBUG
Serial.println("Sending image...");
#endif
return _prepFrame(image);
}
/**
* sends a string of a size up to 255 characters
* via the specified LED pin.
*
* @param image The string that is to be transmitted.
*
* @return 0 if the transmission was successful,
* 1 if there is already a transmission active,
* 2 if no transmission pin has been specified.
*/
int Transmitter::sendData(String &string) {
#ifdef DEBUG
Serial.println("Sending string...");
#endif
return _prepFrame(string);
}
/**
* prepares a LEDBitmap for transmission on the specified LED pin.
*
* @param image The image that is to be prepared.
*
* @return 0 if the transmission/preparation was successful,
* 1 if there is already a transmission active,
* 2 if no transmission pin has been specified.
*/
int Transmitter::_prepFrame(LEDBitmap &image) {
if(_busy) return 1; //no Data can be sent right now since the transmitter is already busy
if(!_pin) return 2;
/** construct the intermediate byte frame.
*
* frame structure is
* 1 octet preamble (0xA5) | 1 octet type | 2 octets size* | up to 255* octets payload | 1 octet checksum
*
* the size here consists of the total size and the height of the bitmap
* up to 8 octets in the case of a bitmap
*
* the image will be transmitted in rows
* with one row being one byte/octet
* 1 2 3 4 5 6 7 8 <-- 1st octet
* 9 10 11 12 13 14 15 16 <-- 2nd octet
* 17 18 19 20 21 22 23 24 <-- 3rd octet
* ... ...
*/
uint8_t img_size = image.getSize();
uint8_t img_height = image.getHeight();
uint8_t size = img_height + 5;
#ifdef DEBUG
Serial.print("Image size: "); Serial.print(img_size);
Serial.print("\tImage height: "); Serial.print(img_height);
Serial.print("\tFrame size: "); Serial.println(size);
#endif
uint8_t *frame = (uint8_t*)calloc(size, sizeof(uint8_t));
uint8_t checksum = 0x00;
// v-- Just a little safety net
for(uint8_t h = 0; (h < img_height) && (h < 8); h++) {
uint8_t value = image.getBitmap()[h];
checksum = (checksum + value) % 256;
frame[h+4] = value;
}
/* Fill up the rest of the image with 0 values. Legacy code.
for(uint8_t h = image.getHeight(); h < 8; h++) {
frame[h+1] = 0x00;
}
*/
// building the frame
frame[0] = PREAMBLE;
frame[1] = 2; // type = 2 = bitmap
frame[2] = img_height;
frame[3] = img_size;
frame[size-1] = checksum;
#ifdef DEBUG
Serial.println("Built frame:");
for(uint8_t i = 0; i < size; i++) {
Serial.print(frame[i], HEX); Serial.print(" ");
}
Serial.println();
#endif
_buildBitFrame(frame, size);
// set the transmitter into busy mode so the idle pattern will no longer
// be transmitted but instead the frame will be sent out.
free(frame);
_busy = true;
_pos = 0;
return 0; //preperation was successful
}
/**
* prepares a string for transmission
*
* @param str The string that is to be prepared
*
* @return 0 if the transmission/preparation was successful,
* 1 if there is already a transmission active,
* 2 if no transmission pin has been specified.
*/
int Transmitter::_prepFrame(String &string){
if(_busy) return 1; //no Data can be sent right now since the transmitter is already busy
if(!_pin) return 2;
/** construct the intermediate byte frame.
*
* frame structure is
* 1 octet preamble (0xA5) | 1 octet type | 1 octet size | up to 255* octets payload | 1 octet checksum
*
* up to 255 octets in the case of a string. One octet for each char in the string
*/
uint8_t str_size = string.length() + 1; //One more to also transmit the end character!
uint8_t size = str_size + 4;
#ifdef DEBUG
Serial.print("String size: "); Serial.print(str_size);
Serial.print("\tFrame size: "); Serial.println(size);
#endif
uint8_t* frame = (uint8_t*)calloc(size, sizeof(uint8_t));
uint8_t checksum = 0x00;
for(uint8_t c = 0; c < str_size; c++){
uint8_t value = string[c];
checksum = (checksum + value) % 256;
frame[c+3] = value;
}
frame[0] = PREAMBLE;
frame[1] = 1; //type = 1 = string
frame[2] = str_size;
frame[size-1] = checksum;
#ifdef DEBUG
Serial.println("Built frame:");
for(uint16_t i = 0; i < size; i++) {
Serial.print(frame[i], HEX); Serial.print(" ");
}
Serial.println();
#endif
_buildBitFrame(frame, size);
// set the transmitter into busy mode so the idle pattern will no longer
// be transmitted but instead the frame will be sent out.
free(frame);
_busy = true;
_pos = 0;
return 0; //preperation was successful
}
/**
* Builds a bit frame from an intermediate byte frame.
*
* @param frame A pointer to the first element of the intermediate byte frame
*
* @param size The size of the intermediate byte frame
*
* @return the position after the final position (and thus size) of the bit frame
*/
uint8_t Transmitter::_buildBitFrame(uint8_t* frame, uint8_t &size) {
/** constructs the bitwise frame for transmission. Takes the intermediate
* frame consisting of bytes and puts every bit into its own byte so it can
* be transmitted.
* Ex: 10100101 -> 0000001 00000000 00000001 00000000 00000000 00000001 00000000 000000001
*/
_frame.size = size*8;
_frame.data = (uint8_t*)malloc(_frame.size*sizeof(uint8_t));
uint8_t pos = 0;
for (uint8_t byte = 0; byte < size; byte++) {
for (uint8_t bit = 0; bit < 8; bit++) {
_frame.data[pos] = (frame[byte] >> (7 - bit)) & 0x01;
pos++;
}
}
return pos;
}
void Transmitter::transmitBit() {
if(_active) {
// Preperation
// only change the state when the manchester bit is completely transmitted.
if(!_manHalf) {
if(_busy) {
// if there is currently a transmission running
// read the next bit from the frame
if(_pos < _frame.size) {
_state = _frame.data[_pos];
_pos ++;
} else {
_busy = false;
_state = 0;
_pos = 0;
free(_frame.data);
_frame.size = 0;
}
} else { // else transmit the idle pattern
_state = 0;
}
}
// Transmission
if (!_manHalf) {
digitalWrite(_pin, !_state);
} else {
digitalWrite(_pin, _state);
}
_manHalf = !_manHalf;
} else {
digitalWrite(_pin, HIGH);
}
}
void Transmitter::start() {
_instance = this;
Timer2.attachInterrupt(_transmitBit);
_active = true;
#ifdef DEBUG
Serial.println("Transmitter started!");
#endif
}
void Transmitter::stop() {
Timer2.detachInterrupt();
_active = false;
#ifdef DEBUG
Serial.println("Transmitter stopped!");
#endif
}
int Transmitter::setFrequency(int frequency) {
if(_active) {
return -1;
}
return Timer2.initialize(frequency);
}
void Transmitter::_transmitBit() {
_instance->transmitBit();
}
bool Transmitter::lastTransmissionComplete() {
return !_busy;
}
void Transmitter::setPin(int pin) {
_pin = pin;
pinMode(_pin, OUTPUT);
}
int Transmitter::getPin() {
return _pin;
}