Subversion Repositories HomeAutomation

Rev

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

Rev 602 Rev 609
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
#include <drivers/timer/timebase.h>
7
#include <drivers/timer/timebase.h>
8
#include <drivers/ir/transceiver/irtransceiver.h>
8
#include <drivers/ir/transceiver/irtransceiver.h>
-
 
9
#include <drivers/ir/protocols.h>
9
 
10
 
10
#define APP_TYPE    CAN_APPTYPES_IRTRANSMITTER
11
#define APP_TYPE    CAN_APPTYPES_IRTRANSMITTER
11
#define APP_VERSION 0x0001
12
#define APP_VERSION 0x0002
12
 
13
 
13
#define STATE_IDLE              0
14
#define STATE_IDLE              0
14
#define STATE_START_TRANSMIT    1
15
#define STATE_START_TRANSMIT    1
15
#define STATE_TRANSMITTING      2
16
#define STATE_TRANSMITTING      2
16
#define STATE_START_PAUSE       3
17
#define STATE_START_PAUSE       3
17
#define STATE_PAUSING           4
18
#define STATE_PAUSING           4
18
#define STATE_STOP              5
19
#define STATE_STOP              5
19
 
20
 
20
// A simple message "queue", with space for one message only.
21
// A simple message "queue", with space for one message only.
21
// These are declared volatile to tell the compiler not to optimize away accesses.
22
// These are declared volatile to tell the compiler not to optimize away accesses.
22
volatile Can_Message_t rxMsg; // Message storage
23
volatile Can_Message_t rxMsg; // Message storage
23
volatile uint8_t rxMsgFull;   // Synchronization flag
24
volatile uint8_t rxMsgFull;   // Synchronization flag
24
 
25
 
25
// CAN message reception callback.
26
// CAN message reception callback.
26
// This function runs with interrupts disabled, keep it as short as possible.
27
// This function runs with interrupts disabled, keep it as short as possible.
27
void can_receive(Can_Message_t *msg) {
28
void can_receive(Can_Message_t *msg) {
28
    if (!rxMsgFull) {
29
    if (!rxMsgFull) {
29
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
30
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
30
        rxMsgFull = 1;
31
        rxMsgFull = 1;
31
    }
32
    }
32
}
33
}
33
 
34
 
34
int main(void)
35
int main(void)
35
{
36
{
36
    // Enable interrupts as early as possible
37
    // Enable interrupts as early as possible
37
    sei();
38
    sei();
38
   
39
   
39
    Timebase_Init();
40
    Timebase_Init();
40
    IrTransceiver_Init();
41
    IrTransceiver_Init();
41
   
42
   
42
    Can_Message_t txMsg;
43
    Can_Message_t txMsg;
43
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
44
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
44
    txMsg.DataLength = 4;
45
    txMsg.DataLength = 4;
45
    txMsg.RemoteFlag = 0;
46
    txMsg.RemoteFlag = 0;
46
    txMsg.ExtendedFlag = 1;
47
    txMsg.ExtendedFlag = 1;
47
    txMsg.Data.words[0] = APP_TYPE;
48
    txMsg.Data.words[0] = APP_TYPE;
48
    txMsg.Data.words[1] = APP_VERSION;
49
    txMsg.Data.words[1] = APP_VERSION;
49
   
50
   
50
    // Set up callback for CAN reception, this is optional if only sending is required.
51
    // Set up callback for CAN reception, this is optional if only sending is required.
51
    BIOS_CanCallback = &can_receive;
52
    BIOS_CanCallback = &can_receive;
52
    // Send CAN_NMT_APP_START
53
    // Send CAN_NMT_APP_START
53
    BIOS_CanSend(&txMsg);
54
    BIOS_CanSend(&txMsg);
54
 
55
 
55
    uint32_t time = Timebase_CurrentTime();
56
    uint32_t time = Timebase_CurrentTime();
56
    uint32_t timePauseStarted = 0;
57
    uint32_t timePauseStarted = 0;
57
   
58
   
58
    uint8_t state=STATE_IDLE;
59
    uint8_t state=STATE_IDLE;
59
    uint8_t repeatcnt=0;
60
    uint8_t repeatcnt=0;
60
    uint8_t stop=0;
61
    uint8_t stop=0;
-
 
62
    Ir_Protocol_Data_t proto;
61
    uint8_t protocol=0; uint32_t ir_data=0; uint16_t timeout=0; uint8_t repeats=0;
63
    proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
-
 
64
    uint8_t len=0;
-
 
65
   
-
 
66
    uint16_t txbuffer[MAX_NR_TIMES];
62
   
67
   
63
    while (1) {
68
    while (1) {
64
        time = Timebase_CurrentTime();
69
        time = Timebase_CurrentTime();
65
        if (state == STATE_IDLE) {
70
        if (state == STATE_IDLE) {
66
        } else if (state == STATE_START_TRANSMIT) {
71
        } else if (state == STATE_START_TRANSMIT) {
-
 
72
            if (expandProtocol(txbuffer, &len, &proto) == IR_OK) {
67
            if (IrTransceiver_Transmit(protocol, ir_data, &timeout, &repeats) == IR_OK) {
73
                if (IrTransceiver_Transmit(txbuffer, len, proto.modfreq) == IR_OK) {
68
                state = STATE_TRANSMITTING;
74
                    state = STATE_TRANSMITTING;
-
 
75
                } else {
-
 
76
                    state = STATE_IDLE;
-
 
77
                }
69
            } else {
78
            } else {
70
                state = STATE_IDLE;
79
                state = STATE_IDLE;
71
            }
80
            }
72
        } else if (state == STATE_TRANSMITTING) {
81
        } else if (state == STATE_TRANSMITTING) {
73
            //polla om den är klar, om klar gå till STATE_START_PAUSE
82
            //polla om den är klar, om klar gå till STATE_START_PAUSE
74
            if (IrTransceiver_Transmit_Poll() != IR_NOT_FINISHED) {
83
            if (IrTransceiver_Transmit_Poll() != IR_NOT_FINISHED) {
75
                state = STATE_START_PAUSE;
84
                state = STATE_START_PAUSE;
76
            }
85
            }
77
        } else if (state == STATE_START_PAUSE) {
86
        } else if (state == STATE_START_PAUSE) {
78
            if (repeatcnt<repeats) {
87
            if (repeatcnt<proto.repeats) {
79
                repeatcnt++;
88
                repeatcnt++;
80
            }
89
            }
81
            timePauseStarted = Timebase_CurrentTime();
90
            timePauseStarted = Timebase_CurrentTime();
82
            state = STATE_PAUSING;
91
            state = STATE_PAUSING;
83
        } else if (state == STATE_PAUSING) {
92
        } else if (state == STATE_PAUSING) {
84
            //när timeout har gått (timebase) så gå till STATE_START_TRANSMIT
93
            //när timeout har gått (timebase) så gå till STATE_START_TRANSMIT
85
            if (time - timePauseStarted >= timeout) {
94
            if (time - timePauseStarted >= proto.timeout) {
86
                state = STATE_START_TRANSMIT;
95
                state = STATE_START_TRANSMIT;
87
            }
96
            }
88
            if (stop == 1 && repeatcnt >= repeats) {
97
            if (stop == 1 && repeatcnt >= proto.repeats) {
89
                state = STATE_STOP;
98
                state = STATE_STOP;
90
            }
99
            }
91
 
-
 
92
        } else if (state == STATE_STOP) {
100
        } else if (state == STATE_STOP) {
93
            stop = 0;
101
            stop = 0;
94
            timeout = 0;
102
            proto.timeout = 0;
95
            repeatcnt = 0;
103
            repeatcnt = 0;
96
            state = STATE_IDLE;
104
            state = STATE_IDLE;
97
        }
105
        }
98
 
106
 
99
       
107
       
100
        if (rxMsgFull) {
108
        if (rxMsgFull) {
101
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT) {
109
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT) {
102
                uint16_t acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE);
110
                uint16_t acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE);
103
                uint8_t actid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
111
                uint8_t actid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
104
               
112
               
105
                if (rxMsg.DataLength==6) {
113
                if (rxMsg.DataLength==6) {
106
                    if (acttype == ACT_TYPE_IR) {
114
                    if (acttype == ACT_TYPE_IR) {
107
                        if (state == STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_DOWN) {
115
                        if (state == STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_DOWN) {
108
                            protocol = rxMsg.Data.bytes[1];
116
                            proto.protocol = rxMsg.Data.bytes[1];
109
                            ir_data = rxMsg.Data.bytes[5];
117
                            proto.data = rxMsg.Data.bytes[5];
110
                            ir_data |= (rxMsg.Data.bytes[4]<<8);
118
                            proto.data |= (rxMsg.Data.bytes[4]<<8);
111
                            ir_data |= ((uint32_t)rxMsg.Data.bytes[3]<<16);
119
                            proto.data |= ((uint32_t)rxMsg.Data.bytes[3]<<16);
112
                            ir_data |= ((uint32_t)rxMsg.Data.bytes[2]<<24);
120
                            proto.data |= ((uint32_t)rxMsg.Data.bytes[2]<<24);
113
 
121
 
114
                            state = STATE_START_TRANSMIT;
122
                            state = STATE_START_TRANSMIT;
115
                        } else if (state != STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_UP) {
123
                        } else if (state != STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_UP) {
116
                            stop = 1;
124
                            stop = 1;
117
                        }
125
                        }
118
                    }
126
                    }
119
                }
127
                }
120
            }
128
            }
121
           
129
           
122
            // Flush the received message
130
            // Flush the received message
123
            rxMsgFull = 0; //  
131
            rxMsgFull = 0; //  
124
        }
132
        }
125
    }
133
    }
126
   
134
   
127
    return 0;
135
    return 0;
128
}
136
}
129
 
137