Subversion Repositories HomeAutomation

Rev

Rev 611 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 611 Rev 614
1
#include <inttypes.h>
1
#include <inttypes.h>
2
#include <avr/interrupt.h>
2
#include <avr/interrupt.h>
3
#include <stdio.h>
3
#include <stdio.h>
4
#include <string.h>
4
#include <string.h>
5
#include <config.h> // All configuration parameters
5
#include <config.h> // All configuration parameters
6
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
6
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
7
 
7
 
8
#include <drivers/timer/timebase.h>
8
#include <drivers/timer/timebase.h>
9
#include <drivers/ir/transceiver/irtransceiver.h>
9
#include <drivers/ir/transceiver/irtransceiver.h>
10
#include <drivers/ir/protocols.h>
10
#include <drivers/ir/protocols.h>
11
 
11
 
12
#define APP_TYPE    CAN_APPTYPES_IRRECEIVER
12
#define APP_TYPE    CAN_APPTYPES_IRRECEIVER
13
#define APP_VERSION 0x0004
13
#define APP_VERSION 0x0004
14
 
14
 
15
#define STATE_IDLE          0
15
#define STATE_IDLE          0
16
#define STATE_IR_REPEAT     1
16
#define STATE_IR_REPEAT     1
17
#define STATE_START_RECEIVE 2
17
#define STATE_START_RECEIVE 2
18
#define STATE_RECEIVING     3
18
#define STATE_RECEIVING     3
19
#define STATE_START_PAUSE   4
19
#define STATE_START_PAUSE   4
20
#define STATE_PAUSING       5
20
#define STATE_PAUSING       5
-
 
21
#define STATE_START_IDLE    6
21
 
22
 
22
#define IR_ID_DATA  0
23
#define IR_ID_DATA  0
23
#define IR_ID_RAW   1
24
#define IR_ID_RAW   1
24
#define IR_ID_DEBUG 10
25
#define IR_ID_DEBUG 10
25
 
26
 
26
// A simple message "queue", with space for one message only.
27
// A simple message "queue", with space for one message only.
27
// These are declared volatile to tell the compiler not to optimize away accesses.
28
// These are declared volatile to tell the compiler not to optimize away accesses.
28
volatile Can_Message_t rxMsg; // Message storage
29
volatile Can_Message_t rxMsg; // Message storage
29
volatile uint8_t rxMsgFull;   // Synchronization flag
30
volatile uint8_t rxMsgFull;   // Synchronization flag
30
 
31
 
31
// CAN message reception callback.
32
// CAN message reception callback.
32
// This function runs with interrupts disabled, keep it as short as possible.
33
// This function runs with interrupts disabled, keep it as short as possible.
33
void can_receive(Can_Message_t *msg) {
34
void can_receive(Can_Message_t *msg) {
34
    if (!rxMsgFull) {
35
    if (!rxMsgFull) {
35
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
36
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
36
        rxMsgFull = 1;
37
        rxMsgFull = 1;
37
    }
38
    }
38
}
39
}
39
 
40
 
40
int main(void)
41
int main(void)
41
{
42
{
42
    // Enable interrupts as early as possible
43
    // Enable interrupts as early as possible
43
    sei();
44
    sei();
44
   
45
   
45
    Timebase_Init();
46
    Timebase_Init();
46
    IrTransceiver_Init();
47
    IrTransceiver_Init();
47
   
48
   
48
    Can_Message_t txMsg;
49
    Can_Message_t txMsg;
49
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
50
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
50
    txMsg.DataLength = 4;
51
    txMsg.DataLength = 4;
51
    txMsg.RemoteFlag = 0;
52
    txMsg.RemoteFlag = 0;
52
    txMsg.ExtendedFlag = 1;
53
    txMsg.ExtendedFlag = 1;
53
    txMsg.Data.words[0] = APP_TYPE;
54
    txMsg.Data.words[0] = APP_TYPE;
54
    txMsg.Data.words[1] = APP_VERSION;
55
    txMsg.Data.words[1] = APP_VERSION;
55
   
56
   
56
    // Set up callback for CAN reception, this is optional if only sending is required.
57
    // Set up callback for CAN reception, this is optional if only sending is required.
57
    BIOS_CanCallback = &can_receive;
58
    BIOS_CanCallback = &can_receive;
58
    // Send CAN_NMT_APP_START
59
    // Send CAN_NMT_APP_START
59
    BIOS_CanSend(&txMsg);
60
    BIOS_CanSend(&txMsg);
60
   
61
   
61
    uint32_t time = Timebase_CurrentTime();
62
    uint32_t time = Timebase_CurrentTime();
62
    uint32_t timePauseStarted = 0;
63
    uint32_t timePauseStarted = 0;
63
   
64
   
64
    uint8_t state = STATE_IDLE;
65
    uint8_t state = STATE_IDLE;
65
   
66
   
66
    Ir_Protocol_Data_t proto;
67
    Ir_Protocol_Data_t proto;
67
    proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
68
    proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
68
    uint8_t len=0;
69
    uint8_t len=0;
69
    uint16_t rxbuffer[MAX_NR_TIMES];
70
    uint16_t rxbuffer[MAX_NR_TIMES];
70
   
71
   
71
    while (1) {
72
    while (1) {
72
        if (state == STATE_IDLE) {
73
        if (state == STATE_IDLE) {
73
            IrTransceiver_Receive_Start(rxbuffer);
74
            IrTransceiver_Receive_Start(rxbuffer);
74
            state = STATE_START_RECEIVE;
75
            state = STATE_START_RECEIVE;
75
        } else if (state == STATE_START_RECEIVE) {
76
        } else if (state == STATE_START_RECEIVE) {
76
            state = STATE_RECEIVING;
77
            state = STATE_RECEIVING;
77
        } else if (state == STATE_RECEIVING) {
78
        } else if (state == STATE_RECEIVING) {
78
            len = IrTransceiver_Receive_Poll();
79
            uint8_t res = IrTransceiver_Receive_Poll(&len);
79
            if (len > 0) {
80
            if ((res == IR_OK) && (len > 0)) {
80
                //låt protocols parsa och skicka på can
81
                //låt protocols parsa och skicka på can
81
                if (parseProtocol(rxbuffer, len, &proto) == IR_OK) {
82
                if (parseProtocol(rxbuffer, len, &proto) == IR_OK) {
82
                    if (proto.protocol != IR_PROTO_UNKNOWN) {
83
                    if (proto.protocol != IR_PROTO_UNKNOWN) {
83
                        txMsg.Data.bytes[0] = IR_BUTTON_DOWN;
84
                        txMsg.Data.bytes[0] = IR_BUTTON_DOWN;
84
                        txMsg.Data.bytes[1] = proto.protocol;
85
                        txMsg.Data.bytes[1] = proto.protocol;
85
                        txMsg.Data.bytes[2] = (proto.data>>24)&0xff;
86
                        txMsg.Data.bytes[2] = (proto.data>>24)&0xff;
86
                        txMsg.Data.bytes[3] = (proto.data>>16)&0xff;
87
                        txMsg.Data.bytes[3] = (proto.data>>16)&0xff;
87
                        txMsg.Data.bytes[4] = (proto.data>>8)&0xff;
88
                        txMsg.Data.bytes[4] = (proto.data>>8)&0xff;
88
                        txMsg.Data.bytes[5] = proto.data&0xff;
89
                        txMsg.Data.bytes[5] = proto.data&0xff;
89
                        txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
90
                        txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
90
                        txMsg.DataLength = 6;
91
                        txMsg.DataLength = 6;
91
                        BIOS_CanSend(&txMsg);
92
                        BIOS_CanSend(&txMsg);
92
                    }
93
                    }
93
                }
94
                }
94
               
95
               
95
                state = STATE_START_PAUSE;
96
                state = STATE_START_PAUSE;
96
            }
97
            }
97
        } else if (state == STATE_START_PAUSE) {
98
        } else if (state == STATE_START_PAUSE) {
-
 
99
            IrTransceiver_Receive_Start(rxbuffer);
98
            timePauseStarted = Timebase_CurrentTime();
100
            timePauseStarted = Timebase_CurrentTime();
99
            state = STATE_PAUSING;
101
            state = STATE_PAUSING;
100
        } else if (state == STATE_PAUSING) {
102
        } else if (state == STATE_PAUSING) {
101
            //resetta timebase-var om ir finns, detta bör göras snyggare
103
            //resetta timebase-var om ir finns, detta bör göras snyggare
102
            for (uint8_t i=0; i<10; i++) {
104
            /*for (uint8_t i=0; i<10; i++) {
103
                if (IrTransceiver_Receive_Pause_Poll() == 0) {
105
                if (IrTransceiver_Receive_Pause_Poll() == 0) {
104
                    break;
106
                    break;
105
                }
107
                }
106
                if (i==9) {
108
                if (i==9) {
107
                    timePauseStarted = Timebase_CurrentTime();
109
                    timePauseStarted = Timebase_CurrentTime();
108
                }
110
                }
-
 
111
            }*/
-
 
112
            uint8_t res = IrTransceiver_Receive_Poll(&len);
-
 
113
            if (res == IR_NOT_FINISHED || res == IR_OK) {
-
 
114
                timePauseStarted = Timebase_CurrentTime();
-
 
115
            }
-
 
116
            if (res == IR_OK) {
-
 
117
                IrTransceiver_Receive_Start(rxbuffer);
109
            }
118
            }
110
           
119
           
111
            time = Timebase_CurrentTime();
120
            time = Timebase_CurrentTime();
112
            if (time - timePauseStarted >= proto.timeout) {
121
            if (time - timePauseStarted >= proto.timeout) {
-
 
122
                state = STATE_START_IDLE;
-
 
123
            }
-
 
124
        } else if (state == STATE_START_IDLE) {
113
                if (proto.protocol != IR_PROTO_UNKNOWN) {
125
            if (proto.protocol != IR_PROTO_UNKNOWN) {
114
                    //skicka på can
126
                //skicka på can
115
                    txMsg.Data.bytes[0] = IR_BUTTON_UP;
127
                txMsg.Data.bytes[0] = IR_BUTTON_UP;
116
                    BIOS_CanSend(&txMsg);
128
                BIOS_CanSend(&txMsg);
117
                }
129
            }
118
                state = STATE_IDLE;
130
            state = STATE_IDLE;
119
            }
131
        }
120
        }
132
       
121
       
133
       
122
       
134
       
123
        if (rxMsgFull) {
135
        if (rxMsgFull) {
124
            /* No message reception functionality implemented */
136
            /* No message reception functionality implemented */
125
            /* Flush the received message */
137
            /* Flush the received message */
126
            rxMsgFull = 0; //  
138
            rxMsgFull = 0; //  
127
        }
139
        }
128
    }
140
    }
129
    return 0;
141
    return 0;
130
}
142
}
131
 
143