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. |
||
| 7 | //#include <drivers/uart/serial.h> |
||
| 8 | #include <drivers/timer/timebase.h> |
||
| 9 | #include <drivers/ir/receiver/irreceiver.h> |
||
| 10 | |||
| 486 | arune | 11 | #define APP_TYPE CAN_APPTYPES_IRRECEIVER |
| 519 | arune | 12 | #define APP_VERSION 0x0002 |
| 403 | arune | 13 | |
| 14 | #define STATE_IDLE 0 |
||
| 15 | #define STATE_IR_REPEAT 1 |
||
| 16 | |||
| 439 | arune | 17 | |
| 403 | arune | 18 | // A simple message "queue", with space for one message only. |
| 19 | // These are declared volatile to tell the compiler not to optimize away accesses. |
||
| 20 | volatile Can_Message_t rxMsg; // Message storage |
||
| 21 | volatile uint8_t rxMsgFull; // Synchronization flag |
||
| 22 | |||
| 23 | // CAN message reception callback. |
||
| 24 | // This function runs with interrupts disabled, keep it as short as possible. |
||
| 25 | void can_receive(Can_Message_t *msg) { |
||
| 26 | if (!rxMsgFull) { |
||
| 27 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
||
| 28 | rxMsgFull = 1; |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | int main(void) |
||
| 33 | { |
||
| 34 | // Enable interrupts as early as possible |
||
| 35 | sei(); |
||
| 36 | |||
| 37 | Timebase_Init(); |
||
| 38 | //Serial_Init(); |
||
| 519 | arune | 39 | IrReceive_Init(); |
| 403 | arune | 40 | |
| 41 | Can_Message_t txMsg; |
||
| 42 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
||
| 43 | txMsg.DataLength = 4; |
||
| 44 | txMsg.RemoteFlag = 0; |
||
| 45 | txMsg.ExtendedFlag = 1; |
||
| 46 | txMsg.Data.words[0] = APP_TYPE; |
||
| 47 | txMsg.Data.words[1] = APP_VERSION; |
||
| 48 | |||
| 49 | // Set up callback for CAN reception, this is optional if only sending is required. |
||
| 50 | BIOS_CanCallback = &can_receive; |
||
| 51 | // Send CAN_NMT_APP_START |
||
| 52 | BIOS_CanSend(&txMsg); |
||
| 439 | arune | 53 | |
| 512 | arune | 54 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 519 | arune | 55 | |
| 403 | arune | 56 | unsigned long time = Timebase_CurrentTime(); |
| 519 | arune | 57 | unsigned long irTimeoutTime = 100; |
| 403 | arune | 58 | |
| 59 | uint8_t ir_protocol, ir_address, ir_command; |
||
| 519 | arune | 60 | uint16_t ir_timeout; |
| 403 | arune | 61 | uint8_t state = STATE_IDLE; |
| 62 | |||
| 63 | while (1) { |
||
| 64 | time = Timebase_CurrentTime(); |
||
| 65 | if (state == STATE_IDLE) { |
||
| 519 | arune | 66 | if (IrReceive_CheckIR(&ir_protocol, &ir_address, &ir_command, &ir_timeout) == IR_OK) { |
| 403 | arune | 67 | txMsg.Data.bytes[0] = 0x00; |
| 68 | txMsg.Data.bytes[1] = ir_protocol; |
||
| 69 | txMsg.Data.bytes[2] = ir_address; |
||
| 70 | txMsg.Data.bytes[3] = ir_command; |
||
| 71 | txMsg.DataLength = 4; |
||
| 72 | BIOS_CanSend(&txMsg); |
||
| 73 | irTimeoutTime = Timebase_CurrentTime(); |
||
| 74 | state = STATE_IR_REPEAT; |
||
| 75 | } |
||
| 76 | } else if (state == STATE_IR_REPEAT) { |
||
| 77 | //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE |
||
| 519 | arune | 78 | if (time - irTimeoutTime >= ir_timeout) { |
| 403 | arune | 79 | state = STATE_IDLE; |
| 80 | txMsg.Data.bytes[0] = 0x0f; |
||
| 81 | BIOS_CanSend(&txMsg); |
||
| 82 | } |
||
| 519 | arune | 83 | if (IrReceive_CheckIdle() == IR_OK) { |
| 403 | arune | 84 | irTimeoutTime = Timebase_CurrentTime(); |
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | if (rxMsgFull) { |
||
| 90 | // Flush the received message |
||
| 91 | rxMsgFull = 0; // |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return 0; |
||
| 96 | } |