Rev 835 | Rev 1320 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 835 | Rev 922 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /** |
1 | /** |
| 2 | * IR receiver and transmitter protocols. |
2 | * IR receiver and transmitter protocols. |
| 3 | * |
3 | * |
| 4 | * @date 2006-12-10 |
4 | * @date 2006-12-10 |
| 5 | * |
5 | * |
| 6 | * @author Anders Runeson, Andreas Fritiofson, Martin |
6 | * @author Anders Runeson, Andreas Fritiofson, Martin Nordn |
| 7 | * |
7 | * |
| 8 | */ |
8 | */ |
| 9 | 9 | ||
| 10 | #include "protocols.h" |
10 | #include "protocols.h" |
| - | 11 | #include <bios.h> |
|
| 11 | 12 | ||
| 12 | int8_t parseProtocol(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) { |
13 | int8_t parseProtocol(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) { |
| 13 | proto->protocol=IR_PROTO_UNKNOWN; |
14 | proto->protocol=IR_PROTO_UNKNOWN; |
| 14 | proto->data=0; |
15 | proto->data=0; |
| 15 | proto->timeout=100; |
16 | proto->timeout=100; |
| Line 190... | Line 191... | ||
| 190 | 191 | ||
| 191 | 192 | ||
| 192 | return IR_OK; |
193 | return IR_OK; |
| 193 | } |
194 | } |
| 194 | #endif |
195 | #endif |
| - | 196 | ||
| - | 197 | /** |
|
| - | 198 | * Used by the expandRC5 to ensure that we toggle the signal with each button press. |
|
| - | 199 | */ |
|
| - | 200 | int8_t rc5_toggle=0; |
|
| 195 | 201 | ||
| 196 | /** |
202 | /** |
| 197 | * Expand data from RC5 protocol |
203 | * Expand data from RC5 protocol |
| 198 | * http://www.sbprojects.com/knowledge/ir/rc5.htm |
204 | * http://www.sbprojects.com/knowledge/ir/rc5.htm |
| - | 205 | * |
|
| - | 206 | * One is defined as low then high |
|
| - | 207 | * Zero is defined as high then low |
|
| 199 | * |
208 | * |
| 200 | * @param buf |
209 | * @param buf |
| 201 | * Pointer to buffer to store the expanded data |
210 | * Pointer to buffer to store the expanded data |
| 202 | * @param len |
211 | * @param len |
| 203 | * Pointer to length of the data |
212 | * Pointer to length of the data |
| Line 205... | Line 214... | ||
| 205 | * Pointer to protocol information |
214 | * Pointer to protocol information |
| 206 | * @return |
215 | * @return |
| 207 | * IR_OK if data expanded successfully, one of several errormessages if not |
216 | * IR_OK if data expanded successfully, one of several errormessages if not |
| 208 | */ |
217 | */ |
| 209 | int8_t expandRC5(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) { |
218 | int8_t expandRC5(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) { |
| - | 219 | ||
| 210 |
|
220 | //This is the raw message that we should create the IR times for |
| - | 221 | //Lets copy the data locally to ensure that no interups will modify the vector. |
|
| 211 |
|
222 | uint16_t rawMessage=proto->data & 0x3fff; |
| - | 223 | ||
| 212 | uint8_t previousBit; |
224 | uint8_t previousBit; |
| 213 | uint16_t tempdata; |
- | |
| 214 | - | ||
| 215 | /* Set up startbit */ |
225 | /* Set up startbit */ |
| - | 226 | //Bit = 0 |
|
| - | 227 | //We start with a low signal since the diode |
|
| - | 228 | //isn't active before we send anything, |
|
| 216 | buf[0] = |
229 | buf[0] = IR_RC5_HALF_BIT;//first start bit |
| - | 230 | ||
| - | 231 | // Bit = 1 |
|
| 217 | buf[1] = IR_RC5_HALF_BIT; |
232 | buf[1] = IR_RC5_HALF_BIT; |
| 218 | buf[2] = IR_RC5_HALF_BIT;// |
233 | buf[2] = IR_RC5_HALF_BIT;//second start bit |
| - | 234 | ||
| - | 235 | if (rc5_toggle==0){ |
|
| 219 | buf[3] = IR_RC5_HALF_BIT; |
236 | buf[3] = IR_RC5_HALF_BIT; |
| 220 | buf[4] = IR_RC5_HALF_BIT;//second start bit |
- | |
| 221 | - | ||
| 222 | #define Toggle 0 //TODO: Fix toggle in some proper way |
- | |
| 223 | if (Toggle==0){ |
- | |
| 224 | buf[5] = IR_RC5_HALF_BIT; |
- | |
| 225 | buf[ |
237 | buf[4] = IR_RC5_HALF_BIT; //toggle bit (yes i know it should not be hardcoded) |
| 226 | *len = |
238 | *len = 5; |
| 227 | previousBit = 1; |
239 | previousBit = 1; //Same as last startbit |
| 228 | } else { |
240 | } else { |
| 229 | buf[4] = IR_RC5_BIT; |
- | |
| 230 | buf[5] = IR_RC5_HALF_BIT; |
- | |
| 231 | *len = 6; |
- | |
| 232 | previousBit = 0; |
- | |
| 233 | } |
- | |
| 234 | - | ||
| 235 | / |
241 | //We are reusing the signal from the previous signal |
| 236 | tempcommand = proto->data&(0x3F); |
- | |
| 237 | tempadress = (proto->data>>8)&(0x1F); |
- | |
| 238 | //TODO: This requires massive cleanup |
- | |
| 239 | - | ||
| 240 | //tempdata = ((((uint16_t)(proto->data)&(0x3F)) + ((uint16_t)((proto->data>>8)&(0x1F))<<6)))<<5; |
- | |
| 241 | - | ||
| 242 | tempdata = (uint16_t)tempcommand + ((uint16_t)tempadress<<6); |
- | |
| 243 | tempdata = tempdata<<5; |
- | |
| 244 | - | ||
| 245 | /* |
- | |
| 246 | for(uint8_t ijag t = 0; i < 11; i++) { |
- | |
| 247 | tempdata = tempdata<<1; |
- | |
| 248 |
|
242 | //and extend the time into this bit. |
| 249 | if (((tempdata&(0xA000))>>15)==1){ |
- | |
| 250 | if (previousBit == 1){//11 |
- | |
| 251 | buf[*len] = IR_RC5_HALF_BIT; |
- | |
| 252 | buf[*len+1] = IR_RC5_HALF_BIT; |
- | |
| 253 | *len = *len + 2; |
- | |
| 254 | } else {//01 |
- | |
| 255 | buf[*len-1] = IR_RC5_BIT; |
- | |
| 256 | buf[*len] = IR_RC5_HALF_BIT; |
- | |
| 257 | *len = *len + 1; |
- | |
| 258 | } |
- | |
| 259 | previousBit = 1; |
- | |
| 260 | } else {//10 |
- | |
| 261 | if (previousBit == 1){ |
- | |
| 262 |
|
243 | buf[2] = IR_RC5_BIT; |
| 263 | buf[*len] = IR_RC5_HALF_BIT; |
- | |
| 264 | *len = *len + 1; |
- | |
| 265 | } else {//00 |
- | |
| 266 |
|
244 | buf[3] = IR_RC5_HALF_BIT; |
| 267 | buf[*len+1] = IR_RC5_HALF_BIT; |
- | |
| 268 |
|
245 | *len = 4; |
| 269 | } |
- | |
| 270 |
|
246 | previousBit = 0; //Toggled from last startbit |
| 271 | } |
- | |
| 272 | } |
247 | } |
| 273 | */ |
- | |
| 274 | - | ||
| 275 | tempadress = tempadress<<2; |
- | |
| 276 | - | ||
| 277 | for(uint8_t i = 0; i < 5; i++) { |
- | |
| 278 | tempadress = tempadress<<1; |
- | |
| 279 |
|
248 | //Invert the toggle for next time |
| 280 | if (((tempadress&(0xA0))>>7)==1){ |
- | |
| 281 | if (previousBit == 1){//11 |
- | |
| 282 | buf[*len] = IR_RC5_HALF_BIT; |
- | |
| 283 | buf[*len+1] = IR_RC5_HALF_BIT; |
- | |
| 284 | *len = *len + 2; |
- | |
| 285 | } else {//01 |
- | |
| 286 | buf[*len-1] = IR_RC5_BIT; |
- | |
| 287 | buf[*len] = IR_RC5_HALF_BIT; |
- | |
| 288 | *len = *len + 1; |
- | |
| 289 | } |
- | |
| 290 | previousBit = 1; |
- | |
| 291 | } else {//10 |
- | |
| 292 | if (previousBit == 1){ |
- | |
| 293 | buf[*len-1] = IR_RC5_BIT; |
- | |
| 294 | buf[*len] = IR_RC5_HALF_BIT; |
- | |
| 295 | *len = *len + 1; |
- | |
| 296 | } else {//00 |
- | |
| 297 | buf[*len] = IR_RC5_HALF_BIT; |
- | |
| 298 | buf[*len+1] = IR_RC5_HALF_BIT; |
- | |
| 299 | *len = *len + 2; |
- | |
| 300 | } |
- | |
| 301 | previousBit = 0; |
- | |
| 302 | } |
- | |
| 303 | } |
- | |
| 304 | - | ||
| 305 | - | ||
| 306 |
|
249 | rc5_toggle=!rc5_toggle & 1; |
| 307 | 250 | ||
| 308 |
|
251 | //Decode the message |
| 309 |
|
252 | //We know that RC5 messages are 14 bits long |
| 310 |
|
253 | for(uint8_t pos=11;pos>0;pos--) |
| - | 254 | { |
|
| 311 |
|
255 | // Check the current bit |
| 312 |
|
256 | if(previousBit == ((rawMessage>>(pos-1)) & 1)) |
| - | 257 | { |
|
| 313 | buf[*len] = IR_RC5_HALF_BIT; |
258 | buf[*len]=IR_RC5_HALF_BIT; |
| 314 | buf[*len+1] = IR_RC5_HALF_BIT; |
259 | buf[*len+1]=IR_RC5_HALF_BIT; |
| 315 | *len = *len + 2; |
260 | *len=*len+2; |
| 316 | } else {//01 |
- | |
| 317 | buf[*len-1] = IR_RC5_BIT; |
- | |
| 318 | buf[*len] = IR_RC5_HALF_BIT; |
- | |
| 319 | *len = *len + 1; |
- | |
| 320 | } |
261 | } |
| 321 |
|
262 | else |
| 322 |
|
263 | { |
| - | 264 | //We are having the same signal as we ended the last bit with, |
|
| - | 265 | //Expand the time that that signal is active to cover |
|
| 323 |
|
266 | //half of this bit aswell |
| 324 | buf[*len-1] = IR_RC5_BIT; |
267 | buf[*len-1]=IR_RC5_BIT; |
| 325 | buf[*len] = IR_RC5_HALF_BIT; |
268 | buf[*len]=IR_RC5_HALF_BIT; |
| 326 | *len = *len + 1; |
269 | *len=*len+1; |
| 327 |
|
270 | |
| 328 |
|
271 | //Invert the previous bit |
| 329 |
|
272 | previousBit = (!previousBit) & 1; |
| 330 | *len = *len + 2; |
- | |
| 331 | } |
273 | } |
| 332 | previousBit = 0; |
- | |
| 333 | } |
274 | } |
| - | 275 | //We have to handle the last bit specially since we have to |
|
| - | 276 | //end with low signal on the IR diod |
|
| - | 277 | if(previousBit == 0) |
|
| - | 278 | { |
|
| - | 279 | //We have to remove the last time since that would bring us to a high signal again. |
|
| - | 280 | *len=*len-1; |
|
| - | 281 | buf[*len]=0; |
|
| 334 | } |
282 | } |
| 335 | 283 | ||
| 336 | proto->modfreq=(((F_CPU/2000)/IR_RC5_F_MOD) -1); |
284 | proto->modfreq=(((F_CPU/2000)/IR_RC5_F_MOD) -1); |
| 337 | proto->timeout=IR_RC5_TIMEOUT; |
285 | proto->timeout=IR_RC5_TIMEOUT; |
| 338 | proto->repeats=IR_RC5_REPS; |
286 | proto->repeats=IR_RC5_REPS; |
| Line 657... | Line 605... | ||
| 657 | 605 | ||
| 658 | return IR_OK; |
606 | return IR_OK; |
| 659 | } |
607 | } |
| 660 | 608 | ||
| 661 | /** |
609 | /** |
| 662 | * Expand data from Marantz. Written by Martin |
610 | * Expand data from Marantz. Written by Martin Nordn |
| 663 | * |
611 | * |
| 664 | * @param buf |
612 | * @param buf |
| 665 | * Pointer to buffer to store the expanded data |
613 | * Pointer to buffer to store the expanded data |
| 666 | * @param len |
614 | * @param len |
| 667 | * Pointer to length of the data |
615 | * Pointer to length of the data |