Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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