Rev 603 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 603 | Rev 611 | ||
|---|---|---|---|
| Line 2... | Line 2... | ||
| 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/uart/serial.h> |
- | |
| - | 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 |
|
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 |
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 |
|
| - | 18 | #define STATE_RECEIVING 3 |
|
| - | 19 | #define STATE_START_PAUSE 4 |
|
| - | 20 | #define STATE_PAUSING 5 |
|
| 17 | 21 | ||
| 18 | #define IR_ID_DATA 0 |
22 | #define IR_ID_DATA 0 |
| 19 | #define IR_ID_RAW 1 |
23 | #define IR_ID_RAW 1 |
| 20 | #define IR_ID_DEBUG 10 |
24 | #define IR_ID_DEBUG 10 |
| 21 | 25 | ||
| Line 37... | Line 41... | ||
| 37 | { |
41 | { |
| 38 | // Enable interrupts as early as possible |
42 | // Enable interrupts as early as possible |
| 39 | sei(); |
43 | sei(); |
| 40 | 44 | ||
| 41 | Timebase_Init(); |
45 | Timebase_Init(); |
| 42 | //Serial_Init(); |
- | |
| 43 |
|
46 | IrTransceiver_Init(); |
| 44 | 47 | ||
| 45 | Can_Message_t txMsg; |
48 | Can_Message_t txMsg; |
| 46 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
49 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
| 47 | txMsg.DataLength = 4; |
50 | txMsg.DataLength = 4; |
| 48 | txMsg.RemoteFlag = 0; |
51 | txMsg.RemoteFlag = 0; |
| Line 53... | Line 56... | ||
| 53 | // Set up callback for CAN reception, this is optional if only sending is required. |
56 | // Set up callback for CAN reception, this is optional if only sending is required. |
| 54 | BIOS_CanCallback = &can_receive; |
57 | BIOS_CanCallback = &can_receive; |
| 55 | // Send CAN_NMT_APP_START |
58 | // Send CAN_NMT_APP_START |
| 56 | BIOS_CanSend(&txMsg); |
59 | BIOS_CanSend(&txMsg); |
| 57 | 60 | ||
| 58 |
|
61 | uint32_t time = Timebase_CurrentTime(); |
| - | 62 | uint32_t timePauseStarted = 0; |
|
| 59 | 63 | ||
| 60 | unsigned long time = Timebase_CurrentTime(); |
- | |
| 61 | unsigned long irTimeoutTime = 100; |
- | |
| 62 | - | ||
| 63 | uint8_t ir_protocol; |
- | |
| 64 | //uint8_t ir_address, ir_command; |
- | |
| 65 | uint32_t ir_data; |
- | |
| 66 | uint16_t ir_timeout; |
- | |
| 67 | uint8_t state = STATE_IDLE; |
64 | uint8_t state = STATE_IDLE; |
| - | 65 | ||
| - | 66 | Ir_Protocol_Data_t proto; |
|
| - | 67 | proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0; |
|
| - | 68 | uint8_t len=0; |
|
| - | 69 | uint16_t rxbuffer[MAX_NR_TIMES]; |
|
| 68 | 70 | ||
| 69 | while (1) { |
71 | while (1) { |
| 70 | time = Timebase_CurrentTime(); |
- | |
| 71 | - | ||
| 72 | if (state == STATE_IDLE) { |
72 | if (state == STATE_IDLE) { |
| - | 73 | IrTransceiver_Receive_Start(rxbuffer); |
|
| - | 74 | state = STATE_START_RECEIVE; |
|
| - | 75 | } else if (state == STATE_START_RECEIVE) { |
|
| 73 |
|
76 | state = STATE_RECEIVING; |
| 74 |
|
77 | } else if (state == STATE_RECEIVING) { |
| 75 |
|
78 | len = IrTransceiver_Receive_Poll(); |
| 76 | if ( |
79 | if (len > 0) { |
| 77 |
|
80 | //låt protocols parsa och skicka på can |
| - | 81 | if (parseProtocol(rxbuffer, len, &proto) == IR_OK) { |
|
| - | 82 | if (proto.protocol != IR_PROTO_UNKNOWN) { |
|
| 78 | txMsg.Data.bytes[0] = IR_BUTTON_DOWN; |
83 | txMsg.Data.bytes[0] = IR_BUTTON_DOWN; |
| 79 | txMsg.Data |
84 | txMsg.Data.bytes[1] = proto.protocol; |
| 80 | txMsg.Data |
85 | txMsg.Data.bytes[2] = (proto.data>>24)&0xff; |
| 81 | txMsg.Data |
86 | txMsg.Data.bytes[3] = (proto.data>>16)&0xff; |
| 82 | txMsg.Data |
87 | txMsg.Data.bytes[4] = (proto.data>>8)&0xff; |
| 83 | txMsg.Data |
88 | txMsg.Data.bytes[5] = proto.data&0xff; |
| 84 | 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)); |
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)); |
| 85 | txMsg.DataLength = 6; |
90 | txMsg.DataLength = 6; |
| 86 | BIOS_CanSend(&txMsg); |
91 | BIOS_CanSend(&txMsg); |
| - | 92 | } |
|
| 87 | 93 | } |
|
| 88 |
|
94 | |
| 89 | irTimeoutTime = Timebase_CurrentTime(); |
- | |
| 90 | state = |
95 | state = STATE_START_PAUSE; |
| 91 | 96 | } |
|
| 92 |
|
97 | } else if (state == STATE_START_PAUSE) { |
| 93 |
|
98 | timePauseStarted = Timebase_CurrentTime(); |
| 94 | ir_protocol = IR_PROTO_UNKNOWN; //used in STATE_IR_REPEATE, to know if to send a release |
- | |
| 95 |
|
99 | state = STATE_PAUSING; |
| 96 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_RAW<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
- | |
| 97 |
|
100 | } else if (state == STATE_PAUSING) { |
| 98 |
|
101 | //resetta timebase-var om ir finns, detta bör göras snyggare |
| 99 |
|
102 | for (uint8_t i=0; i<10; i++) { |
| 100 |
|
103 | if (IrTransceiver_Receive_Pause_Poll() == 0) { |
| 101 | txMsg.Data.words[1] = getRawData(index+1); |
- | |
| 102 | txMsg.Data.words[2] = getRawData(index+2); |
- | |
| 103 | txMsg.Data.words[3] = getRawData(index+3); |
- | |
| 104 | 104 | break; |
|
| 105 | /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ |
- | |
| 106 | //while (BIOS_CanSend(&txMsg) != CAN_OK) {} |
- | |
| 107 | BIOS_CanSend(&txMsg); |
- | |
| 108 | 105 | } |
|
| 109 | uint16_t dummycnt = 1; |
- | |
| 110 | while (dummycnt > 0) { |
- | |
| 111 | dummycnt++; |
- | |
| 112 |
|
106 | if (i==9) { |
| 113 |
|
107 | timePauseStarted = Timebase_CurrentTime(); |
| 114 | } |
- | |
| 115 | } |
108 | } |
| 116 | - | ||
| 117 | uint8_t lastpacketcnt = datacnt&0x03; |
- | |
| 118 | if (lastpacketcnt > 0) { |
- | |
| 119 | txMsg.DataLength = lastpacketcnt<<1; |
- | |
| 120 | for (uint8_t i = 0; i < lastpacketcnt; i++) { |
- | |
| 121 | txMsg.Data.words[i] = getRawData((datacnt&0xfc)|i); |
- | |
| 122 | } |
109 | } |
| 123 | /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ |
- | |
| 124 | //while (BIOS_CanSend(&txMsg) != CAN_OK) {} |
- | |
| 125 | BIOS_CanSend(&txMsg); |
- | |
| 126 |
|
110 | |
| 127 | - | ||
| 128 |
|
111 | time = Timebase_CurrentTime(); |
| 129 | state = STATE_IR_REPEAT; |
- | |
| 130 |
|
112 | if (time - timePauseStarted >= proto.timeout) { |
| 131 |
|
113 | if (proto.protocol != IR_PROTO_UNKNOWN) { |
| 132 | /* debug, send the returned message on errors (except for IR_NO_DATA-errors) */ |
- | |
| 133 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DEBUG<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
- | |
| 134 |
|
114 | //skicka på can |
| 135 | txMsg.Data.bytes[0] = |
115 | txMsg.Data.bytes[0] = IR_BUTTON_UP; |
| 136 | BIOS_CanSend(&txMsg); |
116 | BIOS_CanSend(&txMsg); |
| 137 | } |
117 | } |
| 138 | - | ||
| 139 | } else if (state == STATE_IR_REPEAT) { |
- | |
| 140 | /* check for new ir-data, if no new data during the timout-time then set state to IDLE */ |
- | |
| 141 | if (time - irTimeoutTime >= ir_timeout) { |
- | |
| 142 | state = STATE_IDLE; |
118 | state = STATE_IDLE; |
| 143 | /* if the protocol was known then send a release-message */ |
- | |
| 144 | if (ir_protocol != IR_PROTO_UNKNOWN) { |
- | |
| 145 | txMsg.Data.bytes[0] = IR_BUTTON_UP; |
- | |
| 146 | BIOS_CanSend(&txMsg); |
- | |
| 147 | } |
- | |
| 148 | } |
119 | } |
| 149 | if (IrReceive_CheckIdle() == IR_OK) { |
- | |
| 150 | irTimeoutTime = Timebase_CurrentTime(); |
- | |
| 151 | } |
120 | } |
| 152 |
|
121 | |
| 153 | - | ||
| 154 | 122 | ||
| 155 | if (rxMsgFull) { |
123 | if (rxMsgFull) { |
| 156 | /* No message reception functionality implemented */ |
124 | /* No message reception functionality implemented */ |
| 157 | - | ||
| 158 | /* Flush the received message */ |
125 | /* Flush the received message */ |
| 159 | rxMsgFull = 0; // |
126 | rxMsgFull = 0; // |
| 160 | } |
127 | } |
| 161 | } |
128 | } |
| 162 | - | ||
| 163 | return 0; |
129 | return 0; |
| 164 | } |
130 | } |