Subversion Repositories HomeAutomation

Rev

Rev 1837 | Rev 1875 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1837 Rev 1874
Line 7... Line 7...
7
 *  
7
 *  
8
 */
8
 */
9
 
9
 
10
#include "protocols.h"
10
#include "protocols.h"
11
#include <bios.h>
11
#include <bios.h>
-
 
12
 
-
 
13
 
-
 
14
 
-
 
15
#include <drivers/mcu/gpio.h>
-
 
16
 
-
 
17
 
12
 
18
 
13
int8_t parseProtocol(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
19
int8_t parseProtocol(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
14
    proto->protocol=IR_PROTO_UNKNOWN;
20
    proto->protocol=IR_PROTO_UNKNOWN;
15
    proto->data=0;
21
    proto->data=0;
16
    proto->timeout=100;
22
    proto->timeout=100;
Line 75... Line 81...
75
        return expandPanasonic(buf, len, proto);
81
        return expandPanasonic(buf, len, proto);
76
    case IR_PROTO_SKY:
82
    case IR_PROTO_SKY:
77
        return expandSky(buf, len, proto);
83
        return expandSky(buf, len, proto);
78
    }
84
    }
79
    /* Invalid protocol specified. */
85
    /* Invalid protocol specified. */
80
    return IR_NOT_CORRECT_DATA;
86
    return IR_NOT_CORRECT_DATA;
81
}
87
}
82
 
88
 
83
#if (IR_PROTOCOLS_USE_SIRC)
89
#if (IR_PROTOCOLS_USE_SIRC)
84
/**
90
/**
85
 * Test data on SIRC protocol, 12-bit version
91
 * Test data on SIRC protocol, 12-bit version
86
 * http://www.sbprojects.com/knowledge/ir/sirc.htm
92
 * http://www.sbprojects.com/knowledge/ir/sirc.htm
87
 * http://picprojects.org.uk/projects/sirc/sonysirc.pdf
93
 * http://picprojects.org.uk/projects/sirc/sonysirc.pdf
Line 92... Line 98...
92
 *      Length of the data
98
 *      Length of the data
93
 * @param proto
99
 * @param proto
94
 *      Pointer to protocol information
100
 *      Pointer to protocol information
95
 * @return
101
 * @return
96
 *      IR_OK if data parsed successfully, one of several errormessages if not
102
 *      IR_OK if data parsed successfully, one of several errormessages if not
97
 */
103
 */
98
int8_t parseSIRC(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
104
int8_t parseSIRC(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
99
    /* parse buf[], max is len */
105
    /* parse buf[], max is len */
100
 
106
 
101
    /* check if we have correct amount of data.
107
    /* check if we have correct amount of data.
102
           supporting two versions of SIRC:
108
           supporting two versions of SIRC:
103
           12 bit = 25, 15 bit = 31
109
           12 bit = 25, 15 bit = 31
104
           there is also a 20 bit protocol, but we don't support it
110
           there is also a 20 bit protocol, but we don't support it
105
         */
111
         */
Line 107... Line 113...
107
        return IR_NOT_CORRECT_DATA;
113
        return IR_NOT_CORRECT_DATA;
108
    }
114
    }
109
   
115
   
110
    /* check startbit */
116
    /* check startbit */
111
    if (buf[0] > IR_SIRC_ST_BIT + IR_SIRC_ST_BIT/IR_SIRC_TOL_DIV || buf[0] < IR_SIRC_ST_BIT - IR_SIRC_ST_BIT/IR_SIRC_TOL_DIV) {
117
    if (buf[0] > IR_SIRC_ST_BIT + IR_SIRC_ST_BIT/IR_SIRC_TOL_DIV || buf[0] < IR_SIRC_ST_BIT - IR_SIRC_ST_BIT/IR_SIRC_TOL_DIV) {
112
        return IR_NOT_CORRECT_DATA;
118
        return IR_NOT_CORRECT_DATA;
113
    }
119
    }
114
   
120
   
115
    uint16_t rawbits=0;
121
    uint16_t rawbits=0;
116
   
122
   
117
    for (uint8_t i = 1; i < len; i++) {
123
    for (uint8_t i = 1; i < len; i++) {
118
        if ((i&1) == 1) {       /* if odd, ir-pause */
124
        if ((i&1) == 1) {       /* if odd, ir-pause */
119
            /* check length of pause between bits */
125
            /* check length of pause between bits */
120
            if (buf[i] > IR_SIRC_LOW + IR_SIRC_LOW/IR_SIRC_TOL_DIV || buf[i] < IR_SIRC_LOW - IR_SIRC_LOW/IR_SIRC_TOL_DIV) {
126
            if (buf[i] > IR_SIRC_LOW + IR_SIRC_LOW/IR_SIRC_TOL_DIV || buf[i] < IR_SIRC_LOW - IR_SIRC_LOW/IR_SIRC_TOL_DIV) {
121
                return IR_NOT_CORRECT_DATA;
127
                return IR_NOT_CORRECT_DATA;
122
            }
128
            }
123
        } else {            /* if even, ir-bit */
129
        } else {            /* if even, ir-bit */
124
            if (buf[i] > IR_SIRC_HIGH_ONE - IR_SIRC_HIGH_ONE/IR_SIRC_TOL_DIV && buf[i] < IR_SIRC_HIGH_ONE + IR_SIRC_HIGH_ONE/IR_SIRC_TOL_DIV) {
130
            if (buf[i] > IR_SIRC_HIGH_ONE - IR_SIRC_HIGH_ONE/IR_SIRC_TOL_DIV && buf[i] < IR_SIRC_HIGH_ONE + IR_SIRC_HIGH_ONE/IR_SIRC_TOL_DIV) {
125
                /* write a one */
131
                /* write a one */
126
                rawbits |= 1<<((i-2)>>1);
132
                rawbits |= 1<<((i-2)>>1);
127
            } else if (buf[i] > IR_SIRC_HIGH_ZERO - IR_SIRC_HIGH_ZERO/IR_SIRC_TOL_DIV && buf[i] < IR_SIRC_HIGH_ZERO + IR_SIRC_HIGH_ZERO/IR_SIRC_TOL_DIV) {
133
            } else if (buf[i] > IR_SIRC_HIGH_ZERO - IR_SIRC_HIGH_ZERO/IR_SIRC_TOL_DIV && buf[i] < IR_SIRC_HIGH_ZERO + IR_SIRC_HIGH_ZERO/IR_SIRC_TOL_DIV) {
128
                /* do nothing, a zero is already in rawbits */
134
                /* do nothing, a zero is already in rawbits */
129
            } else {
135
            } else {
130
                return IR_NOT_CORRECT_DATA;
136
                return IR_NOT_CORRECT_DATA;
131
            }
137
            }
132
        }
138
        }
133
    }
139
    }
134
   
140
   
135
    proto->protocol = IR_PROTO_SIRC;
141
    proto->protocol = IR_PROTO_SIRC;
136
    proto->timeout = IR_SIRC_TIMEOUT;
142
    proto->timeout = IR_SIRC_TIMEOUT;
137
    proto->data = rawbits;
143
    proto->data = rawbits;
138
   
144
   
139
    return IR_OK;
145
    return IR_OK;
140
}
146
}
141
#endif
147
#endif
142
 
148
 
143
/**
149
/**
144
 * Expand data from SIRC protocol
150
 * Expand data from SIRC protocol
145
 * http://www.sbprojects.com/knowledge/ir/sirc.htm
151
 * http://www.sbprojects.com/knowledge/ir/sirc.htm
146
 *
152
 *
147
 * @param buf
153
 * @param buf
148
 *      Pointer to buffer to store the expanded data
154
 *      Pointer to buffer to store the expanded data
149
 * @param len
155
 * @param len
150
 *      Pointer to length of the data
156
 *      Pointer to length of the data
151
 * @param proto
157
 * @param proto
152
 *      Pointer to protocol information
158
 *      Pointer to protocol information
153
 * @return
159
 * @return
154
 *      IR_OK if data expanded successfully, one of several errormessages if not
160
 *      IR_OK if data expanded successfully, one of several errormessages if not
155
 */
161
 */
Line 160... Line 166...
160
        /* Assume 12 bit protocol */
166
        /* Assume 12 bit protocol */
161
        *len = 25;
167
        *len = 25;
162
        /* If data to big, use 15 bit protocol */
168
        /* If data to big, use 15 bit protocol */
163
        if (proto->data > (1<<11)) { // cannot be represented by 12 bits
169
        if (proto->data > (1<<11)) { // cannot be represented by 12 bits
164
          *len = 31;
170
          *len = 31;
165
        }
171
        }
166
        for (uint8_t i = 0; i < *len-2; i++) {
172
        for (uint8_t i = 0; i < *len-2; i++) {
167
          if ((i&1) == 1) {     /* if odd, ir-pause */
173
          if ((i&1) == 1) {     /* if odd, ir-pause */
168
            buf[i+2] = IR_SIRC_LOW;
174
            buf[i+2] = IR_SIRC_LOW;
169
          } else {          /* if even, ir-bit */
175
          } else {          /* if even, ir-bit */
170
            if ((proto->data>>(i>>1))&1) {
176
            if ((proto->data>>(i>>1))&1) {
Line 190... Line 196...
190
 *
196
 *
191
 * @param buf
197
 * @param buf
192
 *      Pointer to buffer to where to data to parse is stored
198
 *      Pointer to buffer to where to data to parse is stored
193
 * @param len
199
 * @param len
194
 *      Length of the data
200
 *      Length of the data
195
 * @param proto
201
 * @param proto
196
 *      Pointer to protocol information
202
 *      Pointer to protocol information
197
 * @return
203
 * @return
198
 *      IR_OK if data parsed successfully, one of several errormessages if not
204
 *      IR_OK if data parsed successfully, one of several errormessages if not
199
 */
205
 */
200
int8_t parseRC5(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
206
int8_t parseRC5(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
Line 205... Line 211...
205
        //halfbitscnt&1==1 in the middle of bits
211
        //halfbitscnt&1==1 in the middle of bits
206
        //i&1==0 positive flank
212
        //i&1==0 positive flank
207
 
213
 
208
        if ((halfbitscnt&1)==1 && (i&1)==0) {       /* in the middle of bit AND a positve flank */
214
        if ((halfbitscnt&1)==1 && (i&1)==0) {       /* in the middle of bit AND a positve flank */
209
            rawbits |= (1<<(13-(halfbitscnt>>1)));
215
            rawbits |= (1<<(13-(halfbitscnt>>1)));
210
        }
216
        }
211
       
217
       
212
        if (buf[i] > IR_RC5_HALF_BIT - IR_RC5_HALF_BIT/IR_RC5_TOL_DIV && buf[i] < IR_RC5_HALF_BIT + IR_RC5_HALF_BIT/IR_RC5_TOL_DIV) {
218
        if (buf[i] > IR_RC5_HALF_BIT - IR_RC5_HALF_BIT/IR_RC5_TOL_DIV && buf[i] < IR_RC5_HALF_BIT + IR_RC5_HALF_BIT/IR_RC5_TOL_DIV) {
213
            halfbitscnt += 1;
219
            halfbitscnt += 1;
214
        } else if (buf[i] > IR_RC5_BIT - IR_RC5_BIT/IR_RC5_TOL_DIV && buf[i] < IR_RC5_BIT + IR_RC5_BIT/IR_RC5_TOL_DIV) {
220
        } else if (buf[i] > IR_RC5_BIT - IR_RC5_BIT/IR_RC5_TOL_DIV && buf[i] < IR_RC5_BIT + IR_RC5_BIT/IR_RC5_TOL_DIV) {
215
            halfbitscnt += 2;
221
            halfbitscnt += 2;
216
        } else {
222
        } else {
Line 251... Line 257...
251
 *      Pointer to protocol information
257
 *      Pointer to protocol information
252
 * @return
258
 * @return
253
 *      IR_OK if data expanded successfully, one of several errormessages if not
259
 *      IR_OK if data expanded successfully, one of several errormessages if not
254
 */
260
 */
255
int8_t expandRC5(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
261
int8_t expandRC5(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
256
 
262
 
257
    //This is the raw message that we should create the IR times for
263
    //This is the raw message that we should create the IR times for
258
    //Lets copy the data locally to ensure that no interups will modify the vector.
264
    //Lets copy the data locally to ensure that no interups will modify the vector.
259
    uint16_t rawMessage=proto->data & 0x3fff;
265
    uint16_t rawMessage=proto->data & 0x3fff;
260
   
266
   
261
    uint8_t previousBit;
267
    uint8_t previousBit;
Line 279... Line 285...
279
        //and extend the time into this bit.
285
        //and extend the time into this bit.
280
        buf[2] = IR_RC5_BIT;
286
        buf[2] = IR_RC5_BIT;
281
        buf[3] = IR_RC5_HALF_BIT;
287
        buf[3] = IR_RC5_HALF_BIT;
282
        *len = 4;
288
        *len = 4;
283
        previousBit = 0; //Toggled from last startbit
289
        previousBit = 0; //Toggled from last startbit
284
    }
290
    }
285
    //Invert the toggle for next time
291
    //Invert the toggle for next time
286
    rc5_toggle=!rc5_toggle & 1;
292
    rc5_toggle=!rc5_toggle & 1;
287
   
293
   
288
    //Decode the message
294
    //Decode the message
289
    //We know that RC5 messages are 14 bits long
295
    //We know that RC5 messages are 14 bits long
Line 367... Line 373...
367
        }
373
        }
368
    }
374
    }
369
   
375
   
370
    proto->protocol=IR_PROTO_SHARP;
376
    proto->protocol=IR_PROTO_SHARP;
371
    proto->timeout=IR_SHARP_TIMEOUT;
377
    proto->timeout=IR_SHARP_TIMEOUT;
372
    proto->data=rawbits;
378
    proto->data=rawbits;
373
    return IR_OK;
379
    return IR_OK;
374
}
380
}
375
#endif
381
#endif
376
 
382
 
377
/**
383
/**
378
 * Expand data from Sharp protocol
384
 * Expand data from Sharp protocol
379
 * http://www.sbprojects.com/knowledge/ir/sharp.htm
385
 * http://www.sbprojects.com/knowledge/ir/sharp.htm
380
 *
386
 *
381
 * @param buf
387
 * @param buf
382
 *      Pointer to buffer to store the expanded data
388
 *      Pointer to buffer to store the expanded data
Line 387... Line 393...
387
 * @return
393
 * @return
388
 *      IR_OK if data expanded successfully, one of several errormessages if not
394
 *      IR_OK if data expanded successfully, one of several errormessages if not
389
 */
395
 */
390
int8_t expandSharp(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
396
int8_t expandSharp(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
391
    //TODO: Implement this function.
397
    //TODO: Implement this function.
392
    return IR_NOT_CORRECT_DATA;
398
    return IR_NOT_CORRECT_DATA;
393
}
399
}
394
 
400
 
395
 
401
 
396
#if (IR_PROTOCOLS_USE_NEC)
402
#if (IR_PROTOCOLS_USE_NEC)
397
/**
403
/**
398
 * Test data on NEC protocol
404
 * Test data on NEC protocol
Line 433... Line 439...
433
            if (buf[i] > IR_NEC_LOW_ONE - IR_NEC_LOW_ONE/IR_NEC_TOL_DIV && buf[i] < IR_NEC_LOW_ONE + IR_NEC_LOW_ONE/IR_NEC_TOL_DIV) {
439
            if (buf[i] > IR_NEC_LOW_ONE - IR_NEC_LOW_ONE/IR_NEC_TOL_DIV && buf[i] < IR_NEC_LOW_ONE + IR_NEC_LOW_ONE/IR_NEC_TOL_DIV) {
434
                /* write a one */
440
                /* write a one */
435
                rawbits |= 1UL<<((i-3)>>1);
441
                rawbits |= 1UL<<((i-3)>>1);
436
            } else if (buf[i] > IR_NEC_LOW_ZERO - IR_NEC_LOW_ZERO/IR_NEC_TOL_DIV && buf[i] < IR_NEC_LOW_ZERO + IR_NEC_LOW_ZERO/IR_NEC_TOL_DIV) {
442
            } else if (buf[i] > IR_NEC_LOW_ZERO - IR_NEC_LOW_ZERO/IR_NEC_TOL_DIV && buf[i] < IR_NEC_LOW_ZERO + IR_NEC_LOW_ZERO/IR_NEC_TOL_DIV) {
437
                /* do nothing, a zero is already in place */
443
                /* do nothing, a zero is already in place */
438
            } else {
444
            } else {
439
                return IR_NOT_CORRECT_DATA;
445
                return IR_NOT_CORRECT_DATA;
440
            }
446
            }
441
        } else {            /* if even, ir-bit */
447
        } else {            /* if even, ir-bit */
442
            if (buf[i] > IR_NEC_HIGH + IR_NEC_HIGH/IR_NEC_TOL_DIV || buf[i] < IR_NEC_HIGH - IR_NEC_HIGH/IR_NEC_TOL_DIV) {
448
            if (buf[i] > IR_NEC_HIGH + IR_NEC_HIGH/IR_NEC_TOL_DIV || buf[i] < IR_NEC_HIGH - IR_NEC_HIGH/IR_NEC_TOL_DIV) {
443
                return IR_NOT_CORRECT_DATA;
449
                return IR_NOT_CORRECT_DATA;
444
            }
450
            }
445
        }
451
        }
446
    }
452
    }
447
 
453
 
448
    proto->protocol=IR_PROTO_NEC;
454
    proto->protocol=IR_PROTO_NEC;
449
    proto->timeout=IR_NEC_TIMEOUT;
455
    proto->timeout=IR_NEC_TIMEOUT;
450
    proto->data=rawbits;   
456
    proto->data=rawbits;   
451
    return IR_OK;
457
    return IR_OK;
452
}
458
}
453
#endif
459
#endif
454
 
460
 
455
/**
461
/**
456
 * Expand data from NEC protocol
462
 * Expand data from NEC protocol
457
 * http://www.sbprojects.com/knowledge/ir/nec.htm
463
 * http://www.sbprojects.com/knowledge/ir/nec.htm
458
 *
464
 *
459
 * @param buf
465
 * @param buf
Line 491... Line 497...
491
        proto->timeout=IR_NEC_ST_TIMEOUT;
497
        proto->timeout=IR_NEC_ST_TIMEOUT;
492
        *len = 3;
498
        *len = 3;
493
    }
499
    }
494
    proto->modfreq=IR_NEC_F_MOD;
500
    proto->modfreq=IR_NEC_F_MOD;
495
    proto->repeats=IR_NEC_REPS;
501
    proto->repeats=IR_NEC_REPS;
496
    return IR_OK;
502
    return IR_OK;
497
}
503
}
498
 
504
 
499
 
505
 
500
#if (IR_PROTOCOLS_USE_SAMSUNG)
506
#if (IR_PROTOCOLS_USE_SAMSUNG)
501
/**
507
/**
502
 * Test data on Samsung protocol
508
 * Test data on Samsung protocol
503
 * Very much like NEC, different start bit/pause lengths etc.
509
 * Very much like NEC, different start bit/pause lengths etc.
504
 * http://www.sbprojects.com/knowledge/ir/nec.htm
510
 * http://www.sbprojects.com/knowledge/ir/nec.htm
Line 525... Line 531...
525
        return IR_NOT_CORRECT_DATA;
531
        return IR_NOT_CORRECT_DATA;
526
    }
532
    }
527
 
533
 
528
    /* check pause after startbit */
534
    /* check pause after startbit */
529
    if (buf[1] > IR_SAMS_ST_PAUSE + IR_SAMS_ST_PAUSE/IR_SAMS_TOL_DIV || buf[1] < IR_SAMS_ST_PAUSE - IR_SAMS_ST_PAUSE/IR_SAMS_TOL_DIV) {
535
    if (buf[1] > IR_SAMS_ST_PAUSE + IR_SAMS_ST_PAUSE/IR_SAMS_TOL_DIV || buf[1] < IR_SAMS_ST_PAUSE - IR_SAMS_ST_PAUSE/IR_SAMS_TOL_DIV) {
530
        return IR_NOT_CORRECT_DATA;
536
        return IR_NOT_CORRECT_DATA;
531
    }
537
    }
532
 
538
 
533
    uint32_t rawbits = 0;
539
    uint32_t rawbits = 0;
534
   
540
   
535
    for (uint8_t i = 3; i < len; i++) {
541
    for (uint8_t i = 3; i < len; i++) {
536
        if ((i&1) == 1) {       /* if odd, ir-pause */
542
        if ((i&1) == 1) {       /* if odd, ir-pause */
Line 538... Line 544...
538
            if (buf[i] > IR_SAMS_LOW_ONE - IR_SAMS_LOW_ONE/IR_SAMS_TOL_DIV && buf[i] < IR_SAMS_LOW_ONE + IR_SAMS_LOW_ONE/IR_SAMS_TOL_DIV) {
544
            if (buf[i] > IR_SAMS_LOW_ONE - IR_SAMS_LOW_ONE/IR_SAMS_TOL_DIV && buf[i] < IR_SAMS_LOW_ONE + IR_SAMS_LOW_ONE/IR_SAMS_TOL_DIV) {
539
                /* write a one */
545
                /* write a one */
540
                rawbits |= 1UL<<((i-3)>>1);
546
                rawbits |= 1UL<<((i-3)>>1);
541
            } else if (buf[i] > IR_SAMS_LOW_ZERO - IR_SAMS_LOW_ZERO/IR_SAMS_TOL_DIV && buf[i] < IR_SAMS_LOW_ZERO + IR_SAMS_LOW_ZERO/IR_SAMS_TOL_DIV) {
547
            } else if (buf[i] > IR_SAMS_LOW_ZERO - IR_SAMS_LOW_ZERO/IR_SAMS_TOL_DIV && buf[i] < IR_SAMS_LOW_ZERO + IR_SAMS_LOW_ZERO/IR_SAMS_TOL_DIV) {
542
                /* do nothing, a zero is already in rawbits */
548
                /* do nothing, a zero is already in rawbits */
543
            } else {
549
            } else {
544
                return IR_NOT_CORRECT_DATA;
550
                return IR_NOT_CORRECT_DATA;
545
            }
551
            }
546
        } else {            /* if even, ir-bit */
552
        } else {            /* if even, ir-bit */
547
            if (buf[i] > IR_SAMS_HIGH + IR_SAMS_HIGH/IR_SAMS_TOL_DIV || buf[i] < IR_SAMS_HIGH - IR_SAMS_HIGH/IR_SAMS_TOL_DIV) {
553
            if (buf[i] > IR_SAMS_HIGH + IR_SAMS_HIGH/IR_SAMS_TOL_DIV || buf[i] < IR_SAMS_HIGH - IR_SAMS_HIGH/IR_SAMS_TOL_DIV) {
548
                return IR_NOT_CORRECT_DATA;
554
                return IR_NOT_CORRECT_DATA;
549
            }
555
            }
550
        }
556
        }
551
    }
557
    }
552
   
558
   
553
    proto->protocol=IR_PROTO_SAMS;
559
    proto->protocol=IR_PROTO_SAMS;
554
    proto->timeout=IR_SAMS_TIMEOUT;
560
    proto->timeout=IR_SAMS_TIMEOUT;
555
    proto->data=rawbits;   
561
    proto->data=rawbits;   
556
    return IR_OK;
562
    return IR_OK;
557
}
563
}
558
#endif
564
#endif
559
 
565
 
560
/**
566
/**
561
 * Expand data from Samsung protocol
567
 * Expand data from Samsung protocol
562
 * Very much like NEC, different start bit/pause lengths etc.
568
 * Very much like NEC, different start bit/pause lengths etc.
563
 * http://www.sbprojects.com/knowledge/ir/nec.htm
569
 * http://www.sbprojects.com/knowledge/ir/nec.htm
564
 *
570
 *
565
 * @param buf
571
 * @param buf
Line 616... Line 622...
616
    uint8_t halfbitscnt = 1;
622
    uint8_t halfbitscnt = 1;
617
    uint32_t rawbits = 0;
623
    uint32_t rawbits = 0;
618
   
624
   
619
    for (uint8_t i = 0; i<len; i++) {
625
    for (uint8_t i = 0; i<len; i++) {
620
        //halfbitscnt&1==1 in the middle of bits
626
        //halfbitscnt&1==1 in the middle of bits
621
        //i&1==0 positive flank
627
        //i&1==0 positive flank
622
 
628
 
623
        if ((halfbitscnt&1)==1 && (i&1)==0) {       /* in the middle of bit AND a positve flank */
629
        if ((halfbitscnt&1)==1 && (i&1)==0) {       /* in the middle of bit AND a positve flank */
624
            rawbits |= (uint32_t)1<<(19-(halfbitscnt>>1));
630
            rawbits |= (uint32_t)1<<(19-(halfbitscnt>>1));
625
        }
631
        }
626
       
632
       
Line 628... Line 634...
628
            halfbitscnt += 1;
634
            halfbitscnt += 1;
629
        } else if (buf[i] > IR_MARANTZ_BIT - IR_MARANTZ_BIT/IR_MARANTZ_TOL_DIV && buf[i] < IR_MARANTZ_BIT + IR_MARANTZ_BIT/IR_MARANTZ_TOL_DIV) {
635
        } else if (buf[i] > IR_MARANTZ_BIT - IR_MARANTZ_BIT/IR_MARANTZ_TOL_DIV && buf[i] < IR_MARANTZ_BIT + IR_MARANTZ_BIT/IR_MARANTZ_TOL_DIV) {
630
            halfbitscnt += 2;
636
            halfbitscnt += 2;
631
        } else if (buf[i] > IR_MARANTZ_BIT - IR_MARANTZ_BIT/IR_MARANTZ_TOL_DIV && buf[i] < 5*IR_MARANTZ_HALF_BIT + IR_MARANTZ_BIT/IR_MARANTZ_TOL_DIV) {
637
        } else if (buf[i] > IR_MARANTZ_BIT - IR_MARANTZ_BIT/IR_MARANTZ_TOL_DIV && buf[i] < 5*IR_MARANTZ_HALF_BIT + IR_MARANTZ_BIT/IR_MARANTZ_TOL_DIV) {
632
            halfbitscnt += 1; //It seems to work, not entirely sure of the purpose of this long zero though.
638
            halfbitscnt += 1; //It seems to work, not entirely sure of the purpose of this long zero though.
633
        } else {
639
        } else {
634
            return IR_NOT_CORRECT_DATA;
640
            return IR_NOT_CORRECT_DATA;
635
        }
641
        }
636
       
642
       
637
    }
643
    }
638
   
644
   
639
    proto->protocol=IR_PROTO_MARANTZ;
645
    proto->protocol=IR_PROTO_MARANTZ;
640
    proto->timeout=IR_MARANTZ_TIMEOUT;
646
    proto->timeout=IR_MARANTZ_TIMEOUT;
641
    proto->data = rawbits&0x0001ffff;
647
    proto->data = rawbits&0x0001ffff;
642
   
648
   
Line 644... Line 650...
644
}
650
}
645
#endif
651
#endif
646
 
652
 
647
/**
653
/**
648
 * Expand data from Marantz. Written by Martin Nordin
654
 * Expand data from Marantz. Written by Martin Nordin
649
 *
655
 *
650
 * @param buf
656
 * @param buf
651
 *      Pointer to buffer to store the expanded data
657
 *      Pointer to buffer to store the expanded data
652
 * @param len
658
 * @param len
653
 *      Pointer to length of the data
659
 *      Pointer to length of the data
654
 * @param proto
660
 * @param proto
655
 *      Pointer to protocol information
661
 *      Pointer to protocol information
Line 675... Line 681...
675
    for(uint8_t i = 0; i < 17; i++) {
681
    for(uint8_t i = 0; i < 17; i++) {
676
        tempdata = (uint32_t)tempdata<<1;
682
        tempdata = (uint32_t)tempdata<<1;
677
 
683
 
678
        if (((uint32_t)tempdata>>31)==1){
684
        if (((uint32_t)tempdata>>31)==1){
679
            if (previousBit == 1){//11
685
            if (previousBit == 1){//11
680
                buf[*len] = IR_MARANTZ_HALF_BIT;
686
                buf[*len] = IR_MARANTZ_HALF_BIT;
681
                buf[*len+1] = IR_MARANTZ_HALF_BIT;
687
                buf[*len+1] = IR_MARANTZ_HALF_BIT;
682
                *len = *len + 2;
688
                *len = *len + 2;
683
            } else {//01
689
            } else {//01
684
                buf[*len-1] = IR_MARANTZ_BIT;
690
                buf[*len-1] = IR_MARANTZ_BIT;
685
                buf[*len] = IR_MARANTZ_HALF_BIT;
691
                buf[*len] = IR_MARANTZ_HALF_BIT;
Line 705... Line 711...
705
        }
711
        }
706
    }
712
    }
707
    //make sure that we finish high by removing the last zero if needed
713
    //make sure that we finish high by removing the last zero if needed
708
    if (*len%2 == 0){
714
    if (*len%2 == 0){
709
        *len = *len - 1;
715
        *len = *len - 1;
710
    }
716
    }
711
 
717
 
712
    proto->modfreq=IR_MARANTZ_F_MOD;
718
    proto->modfreq=IR_MARANTZ_F_MOD;
713
    proto->timeout=IR_MARANTZ_TIMEOUT;
719
    proto->timeout=IR_MARANTZ_TIMEOUT;
714
    proto->repeats=IR_MARANTZ_REPS;
720
    proto->repeats=IR_MARANTZ_REPS;
715
    return IR_OK;
721
    return IR_OK;
Line 736... Line 742...
736
        return IR_NOT_CORRECT_DATA;
742
        return IR_NOT_CORRECT_DATA;
737
    }
743
    }
738
   
744
   
739
    /* check startbit */
745
    /* check startbit */
740
    if (buf[0] > IR_PANA_ST_BIT + IR_PANA_ST_BIT/IR_PANA_TOL_DIV || buf[0] < IR_PANA_ST_BIT - IR_PANA_ST_BIT/IR_PANA_TOL_DIV) {
746
    if (buf[0] > IR_PANA_ST_BIT + IR_PANA_ST_BIT/IR_PANA_TOL_DIV || buf[0] < IR_PANA_ST_BIT - IR_PANA_ST_BIT/IR_PANA_TOL_DIV) {
741
        return IR_NOT_CORRECT_DATA;
747
        return IR_NOT_CORRECT_DATA;
742
    }
748
    }
743
 
749
 
744
    /* check pause after startbit */
750
    /* check pause after startbit */
745
    if (buf[1] > IR_PANA_ST_PAUSE + IR_PANA_ST_PAUSE/IR_PANA_TOL_DIV || buf[1] < IR_PANA_ST_PAUSE - IR_PANA_ST_PAUSE/IR_PANA_TOL_DIV) {
751
    if (buf[1] > IR_PANA_ST_PAUSE + IR_PANA_ST_PAUSE/IR_PANA_TOL_DIV || buf[1] < IR_PANA_ST_PAUSE - IR_PANA_ST_PAUSE/IR_PANA_TOL_DIV) {
746
        return IR_NOT_CORRECT_DATA;
752
        return IR_NOT_CORRECT_DATA;
747
    }
753
    }
Line 768... Line 774...
768
    }
774
    }
769
   
775
   
770
    proto->protocol=IR_PROTO_PANASONIC;
776
    proto->protocol=IR_PROTO_PANASONIC;
771
    proto->timeout=IR_PANA_TIMEOUT;
777
    proto->timeout=IR_PANA_TIMEOUT;
772
    proto->data=rawbits;   
778
    proto->data=rawbits;   
773
    return IR_OK;
779
    return IR_OK;
774
}
780
}
775
#endif
781
#endif
776
 
782
 
777
/**
783
/**
778
 * Expand data from Panasonic protocol
784
 * Expand data from Panasonic protocol
Line 783... Line 789...
783
 *      Pointer to length of the data
789
 *      Pointer to length of the data
784
 * @param proto
790
 * @param proto
785
 *      Pointer to protocol information
791
 *      Pointer to protocol information
786
 * @return
792
 * @return
787
 *      IR_OK if data expanded successfully, one of several errormessages if not
793
 *      IR_OK if data expanded successfully, one of several errormessages if not
788
 */
794
 */
789
int8_t expandPanasonic(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
795
int8_t expandPanasonic(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
790
    /* Set up startbit */
796
    /* Set up startbit */
791
    buf[0] = IR_PANA_ST_BIT;
797
    buf[0] = IR_PANA_ST_BIT;
792
    buf[1] = IR_PANA_ST_PAUSE;
798
    buf[1] = IR_PANA_ST_PAUSE;
793
   
799
   
Line 798... Line 804...
798
            if ((staticBits>>(i>>1))&1) {
804
            if ((staticBits>>(i>>1))&1) {
799
                buf[i+2] = IR_PANA_LOW_ONE;
805
                buf[i+2] = IR_PANA_LOW_ONE;
800
            } else {
806
            } else {
801
                buf[i+2] = IR_PANA_LOW_ZERO;
807
                buf[i+2] = IR_PANA_LOW_ZERO;
802
            }
808
            }
803
        } else {                /* if even, ir-bit */
809
        } else {                /* if even, ir-bit */
804
            buf[i+2] = IR_PANA_HIGH;
810
            buf[i+2] = IR_PANA_HIGH;
805
        }
811
        }
806
    }
812
    }
807
   
813
   
808
    /* then add the value bits */
814
    /* then add the value bits */
809
    for (uint8_t i = 0; i < 65; i++) {
815
    for (uint8_t i = 0; i < 65; i++) {
810
        if ((i&1) == 1) {       /* if odd, ir-pause */
816
        if ((i&1) == 1) {       /* if odd, ir-pause */
Line 813... Line 819...
813
            } else {
819
            } else {
814
                buf[i+2+32] = IR_PANA_LOW_ZERO;
820
                buf[i+2+32] = IR_PANA_LOW_ZERO;
815
            }
821
            }
816
        } else {                /* if even, ir-bit */
822
        } else {                /* if even, ir-bit */
817
            buf[i+2+32] = IR_PANA_HIGH;
823
            buf[i+2+32] = IR_PANA_HIGH;
818
        }
824
        }
819
    }
825
    }
820
   
826
   
821
    *len = 99;
827
    *len = 99;
822
   
828
   
823
    proto->modfreq=IR_PANA_F_MOD;
829
    proto->modfreq=IR_PANA_F_MOD;
824
    proto->timeout=IR_PANA_TIMEOUT;
830
    proto->timeout=IR_PANA_TIMEOUT;
Line 844... Line 850...
844
    /* parse buf[], max is len */
850
    /* parse buf[], max is len */
845
 
851
 
846
    /* check startbit */
852
    /* check startbit */
847
    if (buf[0] > IR_SKY_ST_BIT + IR_SKY_ST_BIT/IR_SKY_TOL_DIV || buf[0] < IR_SKY_ST_BIT - IR_SKY_ST_BIT/IR_SKY_TOL_DIV) {
853
    if (buf[0] > IR_SKY_ST_BIT + IR_SKY_ST_BIT/IR_SKY_TOL_DIV || buf[0] < IR_SKY_ST_BIT - IR_SKY_ST_BIT/IR_SKY_TOL_DIV) {
848
        return IR_NOT_CORRECT_DATA;
854
        return IR_NOT_CORRECT_DATA;
849
    }
855
    }
850
   
856
   
851
    uint32_t rawbits=0;
857
    uint32_t rawbits=0;
852
    uint8_t current=0;
858
    uint8_t current=0;
853
    uint8_t previous=0;
859
    uint8_t previous=0;
854
    uint8_t cnt=0;
860
    uint8_t cnt=0;
Line 859... Line 865...
859
        if (buf[i] > IR_SKY_SHORT - IR_SKY_SHORT/IR_SKY_TOL_DIV && buf[i] < IR_SKY_SHORT + IR_SKY_SHORT/IR_SKY_TOL_DIV) {
865
        if (buf[i] > IR_SKY_SHORT - IR_SKY_SHORT/IR_SKY_TOL_DIV && buf[i] < IR_SKY_SHORT + IR_SKY_SHORT/IR_SKY_TOL_DIV) {
860
            current = SKYSHORT;
866
            current = SKYSHORT;
861
        }
867
        }
862
        else if (buf[i] > IR_SKY_LONG - IR_SKY_LONG/IR_SKY_TOL_DIV && buf[i] < IR_SKY_LONG + IR_SKY_LONG/IR_SKY_TOL_DIV) {
868
        else if (buf[i] > IR_SKY_LONG - IR_SKY_LONG/IR_SKY_TOL_DIV && buf[i] < IR_SKY_LONG + IR_SKY_LONG/IR_SKY_TOL_DIV) {
863
            current = SKYLONG;
869
            current = SKYLONG;
864
        }
870
        }
865
        else {
871
        else {
866
            return IR_NOT_CORRECT_DATA;
872
            return IR_NOT_CORRECT_DATA;
867
        }
873
        }
868
       
874
       
869
        /* if level is low */
875
        /* if level is low */
870
        if ((rawbits&1)==0) {
876
        if ((rawbits&1)==0) {
871
            /* and there is a long pulse */
877
            /* and there is a long pulse */
872
            if (current == SKYLONG) {
878
            if (current == SKYLONG) {
873
                /* push a one */
879
                /* push a one */
874
                rawbits = rawbits<<1;
880
                rawbits = rawbits<<1;
875
                rawbits |= 1;
881
                rawbits |= 1;
876
                cnt = 0;
882
                cnt = 0;
877
            }
883
            }
878
            else if (cnt == 0) {
884
            else if (cnt == 0) {
879
                cnt=1;
885
                cnt=1;
880
                /* push a zero */
886
                /* push a zero */
881
                rawbits = rawbits<<1;
887
                rawbits = rawbits<<1;
882
               
888
               
883
            }
889
            }
884
            else {
890
            else {
885
                cnt = 0;
891
                cnt = 0;
886
            }
892
            }
887
        }
893
        }
888
       
894
       
889
        /* if level is high */
895
        /* if level is high */
890
        if ((rawbits&1)==1) {
896
        if ((rawbits&1)==1) {
891
            /* and there is a long pulse */
897
            /* and there is a long pulse */
892
            if (current == SKYLONG) {
898
            if (current == SKYLONG) {
893
                /* push a zero */
899
                /* push a zero */
894
                rawbits = rawbits<<1;
900
                rawbits = rawbits<<1;
895
               
901
               
896
                if (previous == SKYLONG) {
902
                if (previous == SKYLONG) {
897
                    cnt = 1;
903
                    cnt = 1;
898
                }
904
                }
899
                else {
905
                else {
900
                    cnt = 0;
906
                    cnt = 0;
Line 920... Line 926...
920
    proto->data = rawbits;
926
    proto->data = rawbits;
921
   
927
   
922
    return IR_OK;
928
    return IR_OK;
923
}
929
}
924
#endif
930
#endif
925
 
931
 
926
/**
932
/**
927
 * Expand data from Sky protocol
933
 * Expand data from Sky protocol
928
 *
934
 *
929
 *
935
 *
930
 * @param buf
936
 * @param buf
Line 961... Line 967...
961
 */
967
 */
962
int8_t parseNexa2(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
968
int8_t parseNexa2(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
963
    /* parse buf[], max is len */
969
    /* parse buf[], max is len */
964
 
970
 
965
    /* check if we have correct amount of data */
971
    /* check if we have correct amount of data */
966
    if (len != 146 || len != 130) {
972
    if (len != 148 && len != 132) {
967
        return IR_NOT_CORRECT_DATA;
973
        return IR_NOT_CORRECT_DATA;
968
    }
974
    }
969
   
975
 
970
    uint32_t rawbits = 0;
976
    uint32_t rawbits = 0;
971
    uint64_t rawbitsTemp = 0;
977
    uint64_t rawbitsTemp = 0;
972
    if (buf[1] < IR_NEXA2_START - IR_NEXA2_START/IR_NEXA2_TOL_DIV && buf[1] > IR_NEXA2_START + IR_NEXA2_START/IR_NEXA2_TOL_DIV) { //check start bit
978
    if (buf[0] < IR_NEXA2_START1 - IR_NEXA2_START1/IR_NEXA2_TOL_DIV && buf[0] > IR_NEXA2_START1 + IR_NEXA2_START1/IR_NEXA2_TOL_DIV) { //check start bit
973
        return IR_NOT_CORRECT_DATA;
979
        return IR_NOT_CORRECT_DATA;
974
    }
980
    }
-
 
981
    if (buf[1] < IR_NEXA2_HIGH - IR_NEXA2_HIGH/IR_NEXA2_TOL_DIV && buf[1] > IR_NEXA2_HIGH + IR_NEXA2_HIGH/IR_NEXA2_TOL_DIV) { //check start bit
-
 
982
        return IR_NOT_CORRECT_DATA;
-
 
983
    }
-
 
984
    if (buf[2] < IR_NEXA2_START2 - IR_NEXA2_START2/IR_NEXA2_TOL_DIV && buf[2] > IR_NEXA2_START2 + IR_NEXA2_START2/IR_NEXA2_TOL_DIV) { //check start bit
-
 
985
        return IR_NOT_CORRECT_DATA;
-
 
986
    }
-
 
987
 
-
 
988
gpio_set_pin(EXP_H);
975
     
989
   
976
      uint8_t bitCounter = 0;
990
    uint8_t bitCounter = 0;
977
    for (uint8_t i = 2; i < len; i++) { //Skip first bit
991
    for (uint8_t i = 3; i < len; i++) {
978
        if ((i&1) == 1) {       /* if odd, ir-pause */
992
        if ((i&1) == 0) {       /* if even, transmit */
979
            /* check length of pause between bits */
993
            /* check length of transmit pulse */
980
            if (buf[1] > IR_NEXA2_LOW_ONE - IR_NEXA2_LOW_ONE/IR_NEXA2_TOL_DIV && buf[1] < IR_NEXA2_LOW_ONE + IR_NEXA2_LOW_ONE/IR_NEXA2_TOL_DIV) {
994
            if (buf[i] > IR_NEXA2_LOW_ONE - IR_NEXA2_LOW_ONE/IR_NEXA2_TOL_DIV && buf[i] < IR_NEXA2_LOW_ONE + IR_NEXA2_LOW_ONE/IR_NEXA2_TOL_DIV) {
981
                /* write a one */
995
                /* write a one */
982
                rawbitsTemp |= 1<<(bitCounter++);
996
                rawbitsTemp |= 1<<(bitCounter++);
983
            } else if (buf[1] > IR_NEXA2_LOW_ZERO - IR_NEXA2_LOW_ZERO/IR_NEXA2_TOL_DIV && buf[1] < IR_NEXA2_LOW_ZERO + IR_NEXA2_LOW_ZERO/IR_NEXA2_TOL_DIV) {
997
            } else if (buf[i] > IR_NEXA2_LOW_ZERO - IR_NEXA2_LOW_ZERO/IR_NEXA2_TOL_DIV && buf[i] < IR_NEXA2_LOW_ZERO + IR_NEXA2_LOW_ZERO/IR_NEXA2_TOL_DIV) {
984
                /* do nothing, a zero is already in rawbits */
998
                /* do nothing, a zero is already in rawbits */
985
                bitCounter++;
999
                bitCounter++;
986
            } else {
1000
            } else {
987
                return IR_NOT_CORRECT_DATA;
1001
                return IR_NOT_CORRECT_DATA;
988
            }
1002
            }
989
            i+=2;   // skip every other bit,implement check here in the future
1003
            i+=2;   // skip every other bit, implement check here in the future
990
        } else {            /* if even, ir-bit */
1004
        } else {            /* if odd, no transmit */
991
            if (buf[1] < IR_NEXA2_HIGH - IR_NEXA2_HIGH/IR_NEXA2_TOL_DIV && buf[1] > IR_NEXA2_HIGH + IR_NEXA2_HIGH/IR_NEXA2_TOL_DIV) {
1005
            if (buf[i] < IR_NEXA2_HIGH - IR_NEXA2_HIGH/IR_NEXA2_TOL_DIV && buf[i] > IR_NEXA2_HIGH + IR_NEXA2_HIGH/IR_NEXA2_TOL_DIV) {
992
                return IR_NOT_CORRECT_DATA;
1006
                return IR_NOT_CORRECT_DATA;
993
            }
1007
            }
994
        }
1008
        }
995
    }
1009
    }
996
   
1010