Subversion Repositories HomeAutomation

Rev

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

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