Subversion Repositories HomeAutomation

Rev

Rev 512 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 512 Rev 519
Line 7... Line 7...
7
//#include <drivers/uart/serial.h>
7
//#include <drivers/uart/serial.h>
8
#include <drivers/timer/timebase.h>
8
#include <drivers/timer/timebase.h>
9
#include <drivers/ir/receiver/irreceiver.h>
9
#include <drivers/ir/receiver/irreceiver.h>
10
 
10
 
11
#define APP_TYPE    CAN_APPTYPES_IRRECEIVER
11
#define APP_TYPE    CAN_APPTYPES_IRRECEIVER
12
#define APP_VERSION 0x0001
12
#define APP_VERSION 0x0002
13
 
13
 
14
#define STATE_IDLE      0
14
#define STATE_IDLE      0
15
#define STATE_IR_REPEAT 1
15
#define STATE_IR_REPEAT 1
16
 
16
 
17
 
17
 
Line 34... Line 34...
34
    // Enable interrupts as early as possible
34
    // Enable interrupts as early as possible
35
    sei();
35
    sei();
36
   
36
   
37
    Timebase_Init();
37
    Timebase_Init();
38
    //Serial_Init();
38
    //Serial_Init();
-
 
39
    IrReceive_Init();
39
   
40
   
40
    Can_Message_t txMsg;
41
    Can_Message_t txMsg;
41
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
42
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
42
    txMsg.DataLength = 4;
43
    txMsg.DataLength = 4;
43
    txMsg.RemoteFlag = 0;
44
    txMsg.RemoteFlag = 0;
Line 49... Line 50...
49
    BIOS_CanCallback = &can_receive;
50
    BIOS_CanCallback = &can_receive;
50
    // Send CAN_NMT_APP_START
51
    // Send CAN_NMT_APP_START
51
    BIOS_CanSend(&txMsg);
52
    BIOS_CanSend(&txMsg);
52
 
53
 
53
    txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
54
    txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
54
       
-
 
55
    //printf("AVR Test Application\n");
-
 
56
    //printf("Using AVR BIOS version %x\n", BIOS_VERSION);
-
 
57
 
-
 
58
    //txMsg.Id = (CAN_SNS << CAN_SHIFT_CLASS) | ((CAN_IR & CAN_MASK_SNS_TYPE) << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID);
-
 
59
    //txMsg.Data.dwords[0] = 0x01020304;
-
 
60
    //txMsg.DataLength = 4;
-
 
61
 
-
 
62
    IRDDR &= ~(1<<IRBIT);
-
 
63
 
55
   
64
    unsigned long time = Timebase_CurrentTime();
56
    unsigned long time = Timebase_CurrentTime();
65
    unsigned long irTimeoutTime;
57
    unsigned long irTimeoutTime = 100;
66
   
58
   
67
    uint8_t ir_protocol, ir_address, ir_command;
59
    uint8_t ir_protocol, ir_address, ir_command;
-
 
60
    uint16_t ir_timeout;
68
    uint8_t state = STATE_IDLE;
61
    uint8_t state = STATE_IDLE;
69
 
62
 
70
    while (1) {
63
    while (1) {
71
        time = Timebase_CurrentTime();
64
        time = Timebase_CurrentTime();
72
        if (state == STATE_IDLE) {
65
        if (state == STATE_IDLE) {
73
            if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) {
66
            if (IrReceive_CheckIR(&ir_protocol, &ir_address, &ir_command, &ir_timeout) == IR_OK) {
74
                txMsg.Data.bytes[0] = 0x00;
67
                txMsg.Data.bytes[0] = 0x00;
75
                txMsg.Data.bytes[1] = ir_protocol;
68
                txMsg.Data.bytes[1] = ir_protocol;
76
                txMsg.Data.bytes[2] = ir_address;
69
                txMsg.Data.bytes[2] = ir_address;
77
                txMsg.Data.bytes[3] = ir_command;
70
                txMsg.Data.bytes[3] = ir_command;
78
                txMsg.DataLength = 4;
71
                txMsg.DataLength = 4;
Line 80... Line 73...
80
                irTimeoutTime = Timebase_CurrentTime();
73
                irTimeoutTime = Timebase_CurrentTime();
81
                state = STATE_IR_REPEAT;
74
                state = STATE_IR_REPEAT;
82
            }
75
            }
83
        } else if (state == STATE_IR_REPEAT) {
76
        } else if (state == STATE_IR_REPEAT) {
84
            //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE
77
            //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE
85
            if (time - irTimeoutTime >= 105) {      //getLastProtoTimeout()
78
            if (time - irTimeoutTime >= ir_timeout) {      
86
                state = STATE_IDLE;
79
                state = STATE_IDLE;
87
                txMsg.Data.bytes[0] = 0x0f;
80
                txMsg.Data.bytes[0] = 0x0f;
88
                BIOS_CanSend(&txMsg);
81
                BIOS_CanSend(&txMsg);
89
            }
82
            }
90
            if (checkIrIdle() == IR_OK) {       //funktion som borde finnas i irreceiver.c
83
            if (IrReceive_CheckIdle() == IR_OK) {
91
                irTimeoutTime = Timebase_CurrentTime();
84
                irTimeoutTime = Timebase_CurrentTime();
92
            }
85
            }
93
        }
86
        }
94
 
87
 
95
       
88