Subversion Repositories HomeAutomation

Rev

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

Rev 727 Rev 732
Line 4... Line 4...
4
 * @date    2006-12-10
4
 * @date    2006-12-10
5
 *
5
 *
6
 * @author  Anders Runeson, Andreas Fritiofson
6
 * @author  Anders Runeson, Andreas Fritiofson
7
 *  
7
 *  
8
 */
8
 */
9
 
-
 
10
#include <bios.h> //FIXME: Ta bort mig, jag finns bara fšr debug av noddans RC6
-
 
11
 
9
 
12
#include "protocols.h"
10
#include "protocols.h"
13
 
11
 
14
int8_t parseProtocol(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
12
int8_t parseProtocol(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
15
    proto->protocol=IR_PROTO_UNKNOWN;
13
    proto->protocol=IR_PROTO_UNKNOWN;
Line 28... Line 26...
28
#if (IR_PROTOCOLS_USE_NEC)
26
#if (IR_PROTOCOLS_USE_NEC)
29
    if (parseNEC(buf, len, proto)==IR_OK) return IR_OK;
27
    if (parseNEC(buf, len, proto)==IR_OK) return IR_OK;
30
#endif
28
#endif
31
#if (IR_PROTOCOLS_USE_SAMSUNG)
29
#if (IR_PROTOCOLS_USE_SAMSUNG)
32
    if (parseSamsung(buf, len, proto)==IR_OK) return IR_OK;
30
    if (parseSamsung(buf, len, proto)==IR_OK) return IR_OK;
-
 
31
#endif
-
 
32
#if (IR_PROTOCOLS_USE_MARANTZ)
-
 
33
    if (parseMarantz(buf, len, proto)==IR_OK) return IR_OK;
33
#endif
34
#endif
34
    /* No protocol matched. */
35
    /* No protocol matched. */
35
    proto->protocol = IR_PROTO_UNKNOWN;
36
    proto->protocol = IR_PROTO_UNKNOWN;
36
    return IR_NOT_CORRECT_DATA;
37
    return IR_NOT_CORRECT_DATA;
37
}
38
}
38
 
39
 
39
int8_t parseHash(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
40
int8_t parseHash(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
40
    //TODO: Transform the buffer in some clever way to a 32 bit word. */
41
    //TODO: Transform the buffer in some clever way to a 32 bit word. */
41
    proto->protocol = IR_PROTO_HASH;
42
    proto->protocol = IR_PROTO_HASH;
42
    proto->timeout = 200;
43
    proto->timeout = 200;
Line 56... Line 57...
56
        return expandSharp(buf, len, proto);
57
        return expandSharp(buf, len, proto);
57
    case IR_PROTO_NEC:
58
    case IR_PROTO_NEC:
58
        return expandNEC(buf, len, proto);
59
        return expandNEC(buf, len, proto);
59
    case IR_PROTO_SAMS:
60
    case IR_PROTO_SAMS:
60
        return expandSamsung(buf, len, proto);
61
        return expandSamsung(buf, len, proto);
-
 
62
    case IR_PROTO_MARANTZ:
-
 
63
        return expandMarantz(buf, len, proto);
61
    }
64
    }
62
    /* Invalid protocol specified. */
65
    /* Invalid protocol specified. */
63
    return IR_NOT_CORRECT_DATA;
66
    return IR_NOT_CORRECT_DATA;
64
}
67
}
65
 
68
 
Line 76... Line 79...
76
 *      Pointer to protocol information
79
 *      Pointer to protocol information
77
 * @return
80
 * @return
78
 *      IR_OK if data parsed successfully, one of several errormessages if not
81
 *      IR_OK if data parsed successfully, one of several errormessages if not
79
 */
82
 */
80
int8_t parseSIRC(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
83
int8_t parseSIRC(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
81
    /* parse buf[], max is len */
84
    /* parse buf[], max is len */
82
 
85
 
83
    /* check if we have correct amount of data */
86
    /* check if we have correct amount of data */
84
    if (len != 25) {
87
    if (len != 25) {
85
        return IR_NOT_CORRECT_DATA;
88
        return IR_NOT_CORRECT_DATA;
86
    }
89
    }
87
   
90
   
Line 103... Line 106...
103
                /* write a one */
106
                /* write a one */
104
                rawbits |= 1<<((i-2)>>1);
107
                rawbits |= 1<<((i-2)>>1);
105
            } 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) {
108
            } 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) {
106
                /* do nothing, a zero is already in rawbits */
109
                /* do nothing, a zero is already in rawbits */
107
            } else {
110
            } else {
108
                return IR_NOT_CORRECT_DATA;
111
                return IR_NOT_CORRECT_DATA;
109
            }
112
            }
110
        }
113
        }
111
    }
114
    }
112
   
115
   
113
    proto->protocol = IR_PROTO_SIRC;
116
    proto->protocol = IR_PROTO_SIRC;
114
    proto->timeout = IR_SIRC_TIMEOUT;
117
    proto->timeout = IR_SIRC_TIMEOUT;
115
    proto->data = rawbits;
118
    proto->data = rawbits;
116
   
119
   
Line 122... Line 125...
122
 * Expand data from SIRC protocol
125
 * Expand data from SIRC protocol
123
 * http://www.sbprojects.com/knowledge/ir/sirc.htm
126
 * http://www.sbprojects.com/knowledge/ir/sirc.htm
124
 *
127
 *
125
 * @param buf
128
 * @param buf
126
 *      Pointer to buffer to store the expanded data
129
 *      Pointer to buffer to store the expanded data
127
 * @param len
130
 * @param len
128
 *      Pointer to length of the data
131
 *      Pointer to length of the data
129
 * @param proto
132
 * @param proto
130
 *      Pointer to protocol information
133
 *      Pointer to protocol information
131
 * @return
134
 * @return
132
 *      IR_OK if data expanded successfully, one of several errormessages if not
135
 *      IR_OK if data expanded successfully, one of several errormessages if not
Line 138... Line 141...
138
 
141
 
139
 
142
 
140
#if (IR_PROTOCOLS_USE_RC5)
143
#if (IR_PROTOCOLS_USE_RC5)
141
/**
144
/**
142
 * Test data on RC5 protocol
145
 * Test data on RC5 protocol
143
 * http://www.sbprojects.com/knowledge/ir/rc5.htm
146
 * http://www.sbprojects.com/knowledge/ir/rc5.htm
144
 *
147
 *
145
 * @param buf
148
 * @param buf
146
 *      Pointer to buffer to where to data to parse is stored
149
 *      Pointer to buffer to where to data to parse is stored
147
 * @param len
150
 * @param len
148
 *      Length of the data
151
 *      Length of the data
149
 * @param proto
152
 * @param proto
150
 *      Pointer to protocol information
153
 *      Pointer to protocol information
151
 * @return
154
 * @return
152
 *      IR_OK if data parsed successfully, one of several errormessages if not
155
 *      IR_OK if data parsed successfully, one of several errormessages if not
153
 */
156
 */
154
int8_t parseRC5(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
157
int8_t parseRC5(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
155
    uint8_t halfbitscnt = 1;
158
    uint8_t halfbitscnt = 1;
156
    uint16_t rawbits = 0;
159
    uint16_t rawbits = 0;
157
   
160
   
158
    for (uint8_t i = 0; i<len; i++) {
161
    for (uint8_t i = 0; i<len; i++) {
Line 175... Line 178...
175
 
178
 
176
    proto->protocol=IR_PROTO_RC5;
179
    proto->protocol=IR_PROTO_RC5;
177
    proto->timeout=IR_RC5_TIMEOUT;
180
    proto->timeout=IR_RC5_TIMEOUT;
178
    //support RC5-extended keeping second startbit 
181
    //support RC5-extended keeping second startbit 
179
    //remove togglebit
182
    //remove togglebit
-
 
183
    proto->data = rawbits&0x37ff; //This seems to be wrong? Does not invert second start bit and keeps first start bit
180
    proto->data = rawbits&0x37ff;
184
    //proto->data = (rawbits&0x07ff) | ((~rawbits)&0x0100);
181
 
185
 
182
   
186
   
183
    return IR_OK;
187
    return IR_OK;
184
}
188
}
185
#endif
189
#endif
186
 
190
 
Line 199... Line 203...
199
 */
203
 */
200
int8_t expandRC5(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
204
int8_t expandRC5(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
201
    uint8_t tempadress;
205
    uint8_t tempadress;
202
    uint8_t tempcommand;
206
    uint8_t tempcommand;
203
    uint8_t previousBit;
207
    uint8_t previousBit;
-
 
208
    uint16_t tempdata;
204
   
209
   
205
    //TODO: Implement this function. Martin testar!
210
    //TODO: Implement this function. Martin testar!
206
    /* Set up startbit */
211
    /* Set up startbit */
207
    buf[0] = 0; // no start-one
212
    buf[0] = 0; // no start-one
208
    buf[1] = IR_RC5_HALF_BIT;
213
    buf[1] = IR_RC5_HALF_BIT;
Line 213... Line 218...
213
#define Toggle 0 //TODO: Fix toggle in some proper way
218
#define Toggle 0 //TODO: Fix toggle in some proper way
214
    if (Toggle==0){
219
    if (Toggle==0){
215
        buf[5] = IR_RC5_HALF_BIT;
220
        buf[5] = IR_RC5_HALF_BIT;
216
        buf[6] = IR_RC5_HALF_BIT; //toggle bit (yes i know it should not be hardcoded)
221
        buf[6] = IR_RC5_HALF_BIT; //toggle bit (yes i know it should not be hardcoded)
217
        *len = 7;
222
        *len = 7;
-
 
223
        previousBit = 1;
218
    } else {
224
    } else {
219
        buf[4] = IR_RC5_BIT;
225
        buf[4] = IR_RC5_BIT;
220
        buf[5] = IR_RC5_HALF_BIT;
226
        buf[5] = IR_RC5_HALF_BIT;
221
        *len = 6;
227
        *len = 6;
-
 
228
        previousBit = 0;
222
    }
229
    }
223
       
230
           
224
   
-
 
225
    previousBit = 1;
-
 
226
   
-
 
227
    //let's start with the adress bits..
231
    //let's start with the adress bits..
228
    tempadress = (proto->data>>8)&(0b00011111);
-
 
229
    tempcommand = proto->data&(0b00111111);
232
    tempcommand = proto->data&(0b00111111);
-
 
233
    tempadress = (proto->data>>8)&(0b00011111);
230
   
234
   
231
 
235
   
232
    Can_Message_t txMsg;
-
 
233
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
236
    //tempdata = ((((uint16_t)(proto->data)&(0x3F)) + ((uint16_t)((proto->data>>8)&(0x1F))<<6)))<<5;
234
    txMsg.DataLength = 5;
-
 
235
    txMsg.RemoteFlag = 0;
-
 
236
    txMsg.ExtendedFlag = 1;
-
 
237
    txMsg.Data.words[0] = tempadress;
-
 
238
    //txMsg.Data.words[1] = tempcommand;
-
 
239
    //txMsg.Data.words[2] = *len+3;
-
 
240
    //BIOS_CanSend(&txMsg);
-
 
241
   
237
   
-
 
238
    tempdata = (uint16_t)tempcommand + ((uint16_t)tempadress<<6);
242
    tempadress = tempadress<<2;
239
    tempdata = tempdata<<5;
243
   
240
   
-
 
241
    /*
244
    for(uint8_t i = 0; i < 5; i++) {
242
    for(uint8_t ijag t = 0; i < 11; i++) {
245
        tempadress = tempadress<<1;
243
        tempdata = tempdata<<1;
246
        //temptemp = tempadress&0xA0;
244
        //temptemp = tempadress&0xA0;
247
        if (((tempadress&(0xA0))>>7)==1){
245
        if (((tempdata&(0xA000))>>15)==1){
248
            if (previousBit == 1){//11
246
            if (previousBit == 1){//11
249
                buf[*len] = IR_RC5_HALF_BIT;
247
                buf[*len] = IR_RC5_HALF_BIT;
250
                buf[*len+1] = IR_RC5_HALF_BIT;
248
                buf[*len+1] = IR_RC5_HALF_BIT;
251
                *len = *len + 2;
249
                *len = *len + 2;
252
            } else {//01
250
            } else {//01
-
 
251
                buf[*len-1] = IR_RC5_BIT;
-
 
252
                buf[*len] = IR_RC5_HALF_BIT;
-
 
253
                *len = *len + 1;
-
 
254
            }
-
 
255
            previousBit = 1;
-
 
256
        } else {//10
-
 
257
            if (previousBit == 1){
-
 
258
                buf[*len-1] = IR_RC5_BIT;
-
 
259
                buf[*len] = IR_RC5_HALF_BIT;
-
 
260
                *len = *len + 1;
-
 
261
            } else {//00
-
 
262
                buf[*len] = IR_RC5_HALF_BIT;
-
 
263
                buf[*len+1] = IR_RC5_HALF_BIT;
-
 
264
                *len = *len + 2;
-
 
265
            }
-
 
266
            previousBit = 0;
-
 
267
        }
-
 
268
    }
-
 
269
    */
-
 
270
   
-
 
271
    tempadress = tempadress<<2;
-
 
272
   
-
 
273
    for(uint8_t i = 0; i < 5; i++) {
-
 
274
        tempadress = tempadress<<1;
-
 
275
        //temptemp = tempadress&0xA0;
-
 
276
        if (((tempadress&(0xA0))>>7)==1){
-
 
277
            if (previousBit == 1){//11
-
 
278
                buf[*len] = IR_RC5_HALF_BIT;
-
 
279
                buf[*len+1] = IR_RC5_HALF_BIT;
-
 
280
                *len = *len + 2;
-
 
281
            } else {//01
253
                buf[*len-1] = IR_RC5_BIT;
282
                buf[*len-1] = IR_RC5_BIT;
254
                buf[*len] = IR_RC5_HALF_BIT;
283
                buf[*len] = IR_RC5_HALF_BIT;
255
                *len = *len + 1;
284
                *len = *len + 1;
256
            }
285
            }
257
            previousBit = 1;
286
            previousBit = 1;
258
        } else {//10
287
        } else {//10
259
            if (previousBit == 1){
288
            if (previousBit == 1){
260
                buf[*len-1] = IR_RC5_BIT;
289
                buf[*len-1] = IR_RC5_BIT;
261
                buf[*len] = IR_RC5_HALF_BIT;
290
                buf[*len] = IR_RC5_HALF_BIT;
262
                *len = *len + 1;
291
                *len = *len + 1;
263
            } else {//00
292
            } else {//00
264
                buf[*len] = IR_RC5_HALF_BIT;
293
                buf[*len] = IR_RC5_HALF_BIT;
265
                buf[*len+1] = IR_RC5_HALF_BIT;
294
                buf[*len+1] = IR_RC5_HALF_BIT;
266
                *len = *len + 2;
295
                *len = *len + 2;
267
            }
296
            }
268
            previousBit = 0;
297
            previousBit = 0;
269
        }
298
        }
270
    }
299
    }
271
   
300
   
272
    txMsg.DataLength = 6;
-
 
273
   
301
   
274
    tempcommand = tempcommand << 1;
302
    tempcommand = tempcommand << 1;
275
   
303
   
276
    for(uint8_t i = 0; i < 6; i++) {
304
    for(uint8_t i = 0; i < 6; i++) {
277
        tempcommand = tempcommand<<1;
305
        tempcommand = tempcommand<<1;
Line 287... Line 315...
287
                *len = *len + 1;
315
                *len = *len + 1;
288
            }
316
            }
289
            previousBit = 1;
317
            previousBit = 1;
290
        } else {//10
318
        } else {//10
291
            if (previousBit == 1){
319
            if (previousBit == 1){
292
                buf[*len-1] = IR_RC5_BIT;
320
                buf[*len-1] = IR_RC5_BIT;
293
                buf[*len] = IR_RC5_HALF_BIT;
321
                buf[*len] = IR_RC5_HALF_BIT;
294
                *len = *len + 1;
322
                *len = *len + 1;
295
            } else {//00
323
            } else {//00
296
                buf[*len] = IR_RC5_HALF_BIT;
-
 
297
                buf[*len+1] = IR_RC5_HALF_BIT;
-
 
298
                *len = *len + 2;
-
 
299
            }
-
 
300
            previousBit = 0;
-
 
301
        }
-
 
302
    }
-
 
303
 
-
 
304
    /*
-
 
305
    //time for the command then!
-
 
306
    for(uint8_t i = 0; i < 6; i++) {
-
 
307
        tempcommand = tempcommand << 1;
-
 
308
        if (((tempcommand&0xA0)>>7)==1){
-
 
309
            buf[*len] = 3; //zero at the start
-
 
310
            buf[*len+1] = IR_RC5_HALF_BIT; //keep it low
-
 
311
            buf[*len+2] = IR_RC5_HALF_BIT; //and then, make it high
-
 
312
            buf[*len+3] = 3; //and then we waste another cycle low to get in sync
-
 
313
            *len = *len + 4;
-
 
314
        } else {
-
 
315
            buf[*len] = IR_RC5_HALF_BIT;
324
                buf[*len] = IR_RC5_HALF_BIT;
316
            buf[*len+1] = IR_RC5_HALF_BIT;
325
                buf[*len+1] = IR_RC5_HALF_BIT;
317
            *len = *len + 2;
326
                *len = *len + 2;
318
        }
327
            }
-
 
328
            previousBit = 0;
319
       
329
        }
320
    }  
330
    }
321
    */
331
 
322
    proto->modfreq=(((F_CPU/2000)/IR_RC5_F_MOD) -1);
332
    proto->modfreq=(((F_CPU/2000)/IR_RC5_F_MOD) -1);
323
    proto->timeout=IR_RC5_TIMEOUT;
333
    proto->timeout=IR_RC5_TIMEOUT;
324
    proto->repeats=IR_RC5_REPS;
334
    proto->repeats=IR_RC5_REPS;
325
    return IR_OK;
335
    return IR_OK;
326
    //return IR_NOT_CORRECT_DATA;
-
 
327
}
336
}
328
 
337
 
329
 
338
 
330
#if (IR_PROTOCOLS_USE_SHARP)
339
#if (IR_PROTOCOLS_USE_SHARP)
331
/**
340
/**
332
 * Test data on SHARP protocol
341
 * Test data on SHARP protocol
333
 * http://www.sbprojects.com/knowledge/ir/sharp.htm
342
 * http://www.sbprojects.com/knowledge/ir/sharp.htm
334
 *
343
 *
335
 * @param buf
344
 * @param buf
336
 *      Pointer to buffer to where to data to parse is stored
345
 *      Pointer to buffer to where to data to parse is stored
337
 * @param len
346
 * @param len
338
 *      Length of the data
347
 *      Length of the data
339
 * @param proto
348
 * @param proto
340
 *      Pointer to protocol information
349
 *      Pointer to protocol information
341
 * @return
350
 * @return
342
 *      IR_OK if data parsed successfully, one of several errormessages if not
351
 *      IR_OK if data parsed successfully, one of several errormessages if not
343
 */
352
 */
344
int8_t parseSharp(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
353
int8_t parseSharp(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
345
    /* parse buf[], max is len */
354
    /* parse buf[], max is len */
346
 
355
 
347
    /* check if we have correct amount of data */
356
    /* check if we have correct amount of data */
348
    if (len != 31) {
357
    if (len != 31) {
349
        return IR_NOT_CORRECT_DATA;
358
        return IR_NOT_CORRECT_DATA;
350
    }
359
    }
351
   
360
   
Line 358... Line 367...
358
                /* write a one */
367
                /* write a one */
359
                rawbits |= 1<<((i-1)>>1);
368
                rawbits |= 1<<((i-1)>>1);
360
            } else if (buf[i] > IR_SHARP_LOW_ZERO - IR_SHARP_LOW_ZERO/IR_SHARP_TOL_DIV && buf[i] < IR_SHARP_LOW_ZERO + IR_SHARP_LOW_ZERO/IR_SHARP_TOL_DIV) {
369
            } else if (buf[i] > IR_SHARP_LOW_ZERO - IR_SHARP_LOW_ZERO/IR_SHARP_TOL_DIV && buf[i] < IR_SHARP_LOW_ZERO + IR_SHARP_LOW_ZERO/IR_SHARP_TOL_DIV) {
361
                /* do nothing, a zero is already in rawbits */
370
                /* do nothing, a zero is already in rawbits */
362
            } else {
371
            } else {
363
                return IR_NOT_CORRECT_DATA;
372
                return IR_NOT_CORRECT_DATA;
364
            }
373
            }
365
        } else {            /* if even, ir-bit */
374
        } else {            /* if even, ir-bit */
366
            if (buf[i] > IR_SHARP_HIGH + IR_SHARP_HIGH/IR_SHARP_TOL_DIV || buf[i] < IR_SHARP_HIGH - IR_SHARP_HIGH/IR_SHARP_TOL_DIV) {
375
            if (buf[i] > IR_SHARP_HIGH + IR_SHARP_HIGH/IR_SHARP_TOL_DIV || buf[i] < IR_SHARP_HIGH - IR_SHARP_HIGH/IR_SHARP_TOL_DIV) {
367
                return IR_NOT_CORRECT_DATA;
376
                return IR_NOT_CORRECT_DATA;
368
            }
377
            }
369
        }
378
        }
Line 377... Line 386...
377
#endif
386
#endif
378
 
387
 
379
/**
388
/**
380
 * Expand data from Sharp protocol
389
 * Expand data from Sharp protocol
381
 * http://www.sbprojects.com/knowledge/ir/sharp.htm
390
 * http://www.sbprojects.com/knowledge/ir/sharp.htm
382
 *
391
 *
383
 * @param buf
392
 * @param buf
384
 *      Pointer to buffer to store the expanded data
393
 *      Pointer to buffer to store the expanded data
385
 * @param len
394
 * @param len
386
 *      Pointer to length of the data
395
 *      Pointer to length of the data
387
 * @param proto
396
 * @param proto
388
 *      Pointer to protocol information
397
 *      Pointer to protocol information
389
 * @return
398
 * @return
390
 *      IR_OK if data expanded successfully, one of several errormessages if not
399
 *      IR_OK if data expanded successfully, one of several errormessages if not
391
 */
400
 */
Line 394... Line 403...
394
    return IR_NOT_CORRECT_DATA;
403
    return IR_NOT_CORRECT_DATA;
395
}
404
}
396
 
405
 
397
 
406
 
398
#if (IR_PROTOCOLS_USE_NEC)
407
#if (IR_PROTOCOLS_USE_NEC)
399
/**
408
/**
400
 * Test data on NEC protocol
409
 * Test data on NEC protocol
401
 * http://www.sbprojects.com/knowledge/ir/nec.htm
410
 * http://www.sbprojects.com/knowledge/ir/nec.htm
402
 *
411
 *
403
 * @param buf
412
 * @param buf
404
 *      Pointer to buffer to where to data to parse is stored
413
 *      Pointer to buffer to where to data to parse is stored
Line 436... Line 445...
436
                /* write a one */
445
                /* write a one */
437
                rawbits |= 1UL<<((i-3)>>1);
446
                rawbits |= 1UL<<((i-3)>>1);
438
            } 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) {
447
            } 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) {
439
                /* do nothing, a zero is already in place */
448
                /* do nothing, a zero is already in place */
440
            } else {
449
            } else {
441
                return IR_NOT_CORRECT_DATA;
450
                return IR_NOT_CORRECT_DATA;
442
            }
451
            }
443
        } else {            /* if even, ir-bit */
452
        } else {            /* if even, ir-bit */
444
            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) {
453
            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) {
445
                return IR_NOT_CORRECT_DATA;
454
                return IR_NOT_CORRECT_DATA;
446
            }
455
            }
447
        }
456
        }
448
    }
457
    }
449
 
-
 
450
    proto->protocol=IR_PROTO_NEC;
-
 
451
    proto->timeout=IR_NEC_TIMEOUT;
-
 
452
    proto->data=rawbits;   
-
 
453
    return IR_OK;
-
 
454
}
-
 
455
#endif
-
 
456
 
458
 
-
 
459
    proto->protocol=IR_PROTO_NEC;
-
 
460
    proto->timeout=IR_NEC_TIMEOUT;
-
 
461
    proto->data=rawbits;   
-
 
462
    return IR_OK;
-
 
463
}
-
 
464
#endif
-
 
465
 
457
/**
466
/**
458
 * Expand data from NEC protocol
467
 * Expand data from NEC protocol
459
 * http://www.sbprojects.com/knowledge/ir/nec.htm
468
 * http://www.sbprojects.com/knowledge/ir/nec.htm
460
 *
469
 *
461
 * @param buf
470
 * @param buf
462
 *      Pointer to buffer to store the expanded data
471
 *      Pointer to buffer to store the expanded data
463
 * @param len
472
 * @param len
464
 *      Pointer to length of the data
473
 *      Pointer to length of the data
465
 * @param proto
474
 * @param proto
466
 *      Pointer to protocol information
475
 *      Pointer to protocol information
467
 * @return
476
 * @return
468
 *      IR_OK if data expanded successfully, one of several errormessages if not
477
 *      IR_OK if data expanded successfully, one of several errormessages if not
469
 */
478
 */
470
int8_t expandNEC(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
479
int8_t expandNEC(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
471
    /* Set up startbit */
480
    /* Set up startbit */
472
    buf[0] = IR_NEC_ST_BIT;
481
    buf[0] = IR_NEC_ST_BIT;
473
   
482
   
474
    if (proto->framecnt == 0) {
483
    if (proto->framecnt == 0) {
475
        buf[1] = IR_NEC_ST_PAUSE;
484
        buf[1] = IR_NEC_ST_PAUSE;
476
   
485
   
477
        *len = 67;
486
        *len = 67;
478
        for (uint8_t i = 0; i < 65; i++) {
487
        for (uint8_t i = 0; i < 65; i++) {
479
            if ((i&1) == 1) {       /* if odd, ir-pause */
488
            if ((i&1) == 1) {       /* if odd, ir-pause */
480
                if ((proto->data>>(i>>1))&1) {
489
                if ((proto->data>>(i>>1))&1) {
481
                    buf[i+2] = IR_NEC_LOW_ONE;
490
                    buf[i+2] = IR_NEC_LOW_ONE;
482
                } else {
491
                } else {
483
                    buf[i+2] = IR_NEC_LOW_ZERO;
492
                    buf[i+2] = IR_NEC_LOW_ZERO;
484
                }
493
                }
485
            } else {            /* if even, ir-bit */
494
            } else {            /* if even, ir-bit */
486
                buf[i+2] = IR_NEC_HIGH;
495
                buf[i+2] = IR_NEC_HIGH;
487
            }
496
            }
488
        }
497
        }
489
        proto->timeout=IR_NEC_TIMEOUT;
498
        proto->timeout=IR_NEC_TIMEOUT;
Line 522... Line 531...
522
        return IR_NOT_CORRECT_DATA;
531
        return IR_NOT_CORRECT_DATA;
523
    }
532
    }
524
   
533
   
525
    /* check startbit */
534
    /* check startbit */
526
    if (buf[0] > IR_SAMS_ST_BIT + IR_SAMS_ST_BIT/IR_SAMS_TOL_DIV || buf[0] < IR_SAMS_ST_BIT - IR_SAMS_ST_BIT/IR_SAMS_TOL_DIV) {
535
    if (buf[0] > IR_SAMS_ST_BIT + IR_SAMS_ST_BIT/IR_SAMS_TOL_DIV || buf[0] < IR_SAMS_ST_BIT - IR_SAMS_ST_BIT/IR_SAMS_TOL_DIV) {
527
        return IR_NOT_CORRECT_DATA;
536
        return IR_NOT_CORRECT_DATA;
528
    }
537
    }
529
 
538
 
530
    /* check pause after startbit */
539
    /* check pause after startbit */
531
    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) {
540
    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) {
532
        return IR_NOT_CORRECT_DATA;
541
        return IR_NOT_CORRECT_DATA;
533
    }
542
    }
Line 540... Line 549...
540
            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) {
549
            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) {
541
                /* write a one */
550
                /* write a one */
542
                rawbits |= 1UL<<((i-3)>>1);
551
                rawbits |= 1UL<<((i-3)>>1);
543
            } 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) {
552
            } 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) {
544
                /* do nothing, a zero is already in rawbits */
553
                /* do nothing, a zero is already in rawbits */
545
            } else {
554
            } else {
546
                return IR_NOT_CORRECT_DATA;
555
                return IR_NOT_CORRECT_DATA;
547
            }
556
            }
548
        } else {            /* if even, ir-bit */
557
        } else {            /* if even, ir-bit */
549
            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) {
558
            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) {
550
                return IR_NOT_CORRECT_DATA;
559
                return IR_NOT_CORRECT_DATA;
551
            }
560
            }
Line 561... Line 570...
561
 
570
 
562
/**
571
/**
563
 * Expand data from Samsung protocol
572
 * Expand data from Samsung protocol
564
 * Very much like NEC, different start bit/pause lengths etc.
573
 * Very much like NEC, different start bit/pause lengths etc.
565
 * http://www.sbprojects.com/knowledge/ir/nec.htm
574
 * http://www.sbprojects.com/knowledge/ir/nec.htm
566
 *
575
 *
567
 * @param buf
576
 * @param buf
568
 *      Pointer to buffer to store the expanded data
577
 *      Pointer to buffer to store the expanded data
569
 * @param len
578
 * @param len
570
 *      Pointer to length of the data
579
 *      Pointer to length of the data
571
 * @param proto
580
 * @param proto
572
 *      Pointer to protocol information
581
 *      Pointer to protocol information
573
 * @return
582
 * @return
574
 *      IR_OK if data expanded successfully, one of several errormessages if not
583
 *      IR_OK if data expanded successfully, one of several errormessages if not
575
 */
584
 */
576
int8_t expandSamsung(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
585
int8_t expandSamsung(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
577
    /* Set up startbit */
586
    /* Set up startbit */
578
    buf[0] = IR_SAMS_ST_BIT;
587
    buf[0] = IR_SAMS_ST_BIT;
Line 584... Line 593...
584
                buf[i+2] = IR_SAMS_LOW_ONE;
593
                buf[i+2] = IR_SAMS_LOW_ONE;
585
            } else {
594
            } else {
586
                buf[i+2] = IR_SAMS_LOW_ZERO;
595
                buf[i+2] = IR_SAMS_LOW_ZERO;
587
            }
596
            }
588
        } else {                /* if even, ir-bit */
597
        } else {                /* if even, ir-bit */
589
            buf[i+2] = IR_SAMS_HIGH;
598
            buf[i+2] = IR_SAMS_HIGH;
590
        }
599
        }
591
    }
600
    }
592
   
601
   
593
    *len = 67;
602
    *len = 67;
594
   
603
   
595
    proto->modfreq=(((F_CPU/2000)/IR_SAMS_F_MOD) -1);
604
    proto->modfreq=(((F_CPU/2000)/IR_SAMS_F_MOD) -1);
596
    proto->timeout=IR_SAMS_TIMEOUT;
605
    proto->timeout=IR_SAMS_TIMEOUT;
597
    proto->repeats=IR_SAMS_REPS;
606
    proto->repeats=IR_SAMS_REPS;
598
    return IR_OK;
607
    return IR_OK;
599
}
608
}
-
 
609
 
-
 
610
#if (IR_PROTOCOLS_USE_MARANTZ)
-
 
611
/**
-
 
612
 * Test data on Marantz protocol
-
 
613
 * Reverse-Engineered by Noddan, very similar to RC-5.
-
 
614
 * Not tested with odd adresses since I have no remote that sends them.
-
 
615
 * Don't know what happens with the extra long bit in that case.
-
 
616
 * TODO: Replace all the RC5 with Marantz :D
-
 
617
 *
-
 
618
 * @param buf
-
 
619
 *      Pointer to buffer to where to data to parse is stored
-
 
620
 * @param len
-
 
621
 *      Length of the data
-
 
622
 * @param proto
-
 
623
 *      Pointer to protocol information
-
 
624
 * @return
-
 
625
 *      IR_OK if data parsed successfully, one of several errormessages if not
-
 
626
 */
-
 
627
int8_t parseMarantz(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto) {
-
 
628
    uint8_t halfbitscnt = 1;
-
 
629
    uint32_t rawbits = 0;
-
 
630
   
-
 
631
    for (uint8_t i = 0; i<len; i++) {
-
 
632
        //halfbitscnt&1==1 in the middle of bits
-
 
633
        //i&1==0 positive flank
-
 
634
 
-
 
635
        if ((halfbitscnt&1)==1 && (i&1)==0) {       /* in the middle of bit AND a positve flank */
-
 
636
            rawbits |= (uint32_t)1<<(19-(halfbitscnt>>1));
-
 
637
        }
-
 
638
       
-
 
639
        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) {
-
 
640
            halfbitscnt += 1;
-
 
641
        } 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) {
-
 
642
            halfbitscnt += 2;
-
 
643
        } else if (buf[i] > IR_RC5_BIT - IR_RC5_BIT/IR_RC5_TOL_DIV && buf[i] < 3*IR_RC5_BIT + IR_RC5_BIT/IR_RC5_TOL_DIV) {
-
 
644
            halfbitscnt += 1; //It seems to work, not entirely sure of the purpose of this long zero though.
-
 
645
        } else {
-
 
646
            return IR_NOT_CORRECT_DATA;
-
 
647
        }
-
 
648
       
-
 
649
    }
-
 
650
   
-
 
651
    //rawbits = (uint32_t)1<<17;
-
 
652
    proto->protocol=IR_PROTO_MARANTZ;
-
 
653
    proto->timeout=IR_RC5_TIMEOUT;
-
 
654
    //support RC5-extended keeping second startbit 
-
 
655
    //remove togglebit
-
 
656
    //proto->data = rawbits&0x37ff;
-
 
657
    proto->data = rawbits&0x0001ffff;
-
 
658
   
-
 
659
    return IR_OK;
-
 
660
}
-
 
661
 
-
 
662
/**
-
 
663
 * Expand data from Marantz
-
 
664
 *
-
 
665
 * @param buf
-
 
666
 *      Pointer to buffer to store the expanded data
-
 
667
 * @param len
-
 
668
 *      Pointer to length of the data
-
 
669
 * @param proto
-
 
670
 *      Pointer to protocol information
-
 
671
 * @return
-
 
672
 *      IR_OK if data expanded successfully, one of several errormessages if not
-
 
673
 */
-
 
674
int8_t expandMarantz(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto) {
-
 
675
    uint8_t previousBit;
-
 
676
    uint32_t tempdata;
-
 
677
    uint32_t tempvariabel; //bra att ha eftersom jag inte har koll pŒ alla typecastingar
-
 
678
   
-
 
679
    //TODO: Implement this function. Martin testar!
-
 
680
    /* Set up startbits */
-
 
681
    buf[0] = IR_RC5_HALF_BIT;//first start bit
-
 
682
    buf[1] = IR_RC5_HALF_BIT;
-
 
683
    buf[2] = IR_RC5_HALF_BIT;//second start bit
-
 
684
    //TODO: Toggle bit should be better, not hard-coded
-
 
685
    buf[3] = IR_RC5_HALF_BIT;
-
 
686
    buf[4] = IR_RC5_HALF_BIT;//toggle bit
-
 
687
    *len=5;
-
 
688
    previousBit = 1;
-
 
689
   
-
 
690
    tempdata = proto->data;
-
 
691
   
-
 
692
    tempdata = (uint32_t)tempdata<<14; //MSB at position 32. 
-
 
693
   
-
 
694
    for(uint8_t ij = 0; ij < 17; ij++) {
-
 
695
        tempdata = (uint32_t)tempdata<<1;
-
 
696
        tempvariabel = (uint32_t)tempdata&(0x8000);
-
 
697
        tempvariabel = (uint32_t)tempdata>>31;
-
 
698
        if ((uint32_t)tempvariabel==1){
-
 
699
            if (previousBit == 1){//11
-
 
700
                buf[*len] = IR_RC5_HALF_BIT;
-
 
701
                buf[*len+1] = IR_RC5_HALF_BIT;
-
 
702
                *len = *len + 2;
-
 
703
            } else {//01
-
 
704
                buf[*len-1] = IR_RC5_BIT;
-
 
705
                buf[*len] = IR_RC5_HALF_BIT;
-
 
706
                *len = *len + 1;
-
 
707
            }
-
 
708
            previousBit = 1;
-
 
709
        } else {
-
 
710
            if (previousBit == 1){//10
-
 
711
                buf[*len-1] = IR_RC5_BIT;
-
 
712
                buf[*len] = IR_RC5_HALF_BIT;
-
 
713
                *len = *len + 1;
-
 
714
            } else {//00
-
 
715
                buf[*len] = IR_RC5_HALF_BIT;
-
 
716
                if (ij==4){
-
 
717
                    buf[*len+1] = IR_RC5_HALF_BIT*5;
-
 
718
                } else {
-
 
719
                    buf[*len+1] = IR_RC5_HALF_BIT;
-
 
720
                }
-
 
721
               
-
 
722
                *len = *len + 2;
-
 
723
            }
-
 
724
            previousBit = 0;
-
 
725
        }
-
 
726
    }
-
 
727
    //make sure that we finish high by removing the last zero if needed
-
 
728
    if (*len%2 == 0){
-
 
729
        *len = *len - 1;
-
 
730
    }
-
 
731
 
-
 
732
    proto->modfreq=(((F_CPU/2000)/IR_RC5_F_MOD) -1);
-
 
733
    proto->timeout=IR_RC5_TIMEOUT;
-
 
734
    proto->repeats=IR_RC5_REPS;
-
 
735
    return IR_OK;
-
 
736
}
-
 
737
 
-
 
738
 
-
 
739
#endif