Subversion Repositories HomeAutomation

Rev

Rev 543 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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/transmitter/irtransmitter.h>
  10.  
  11. #define APP_TYPE    CAN_APPTYPES_IRTRANSMITTER
  12. #define APP_VERSION 0x0001
  13.  
  14. #define STATE_IDLE          0
  15. #define STATE_REPEAT        1
  16. #define STATE_STOPPING      2
  17.  
  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();
  39.     IrTransmit_Init();
  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);
  53.  
  54.     //txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
  55.    
  56.     //unsigned long time = Timebase_CurrentTime();
  57.     //unsigned long irTimeoutTime = 100;
  58.    
  59.     //uint8_t ir_protocol, ir_address, ir_command;
  60.     //uint16_t ir_timeout;
  61.     uint8_t state = STATE_IDLE;
  62.  
  63.     while (1) {
  64.         //time = Timebase_CurrentTime();
  65.         if (state == STATE_IDLE) {
  66.         } else if (state == STATE_REPEAT) {
  67.             IrTransmit_Poll();
  68.         } else if (state == STATE_STOPPING) {
  69.             IrTransmit_Poll();
  70.             if (IrTransmit_Stop() == IR_OK) {
  71.                 state = STATE_IDLE;
  72.             }
  73.         }
  74.  
  75.        
  76.         if (rxMsgFull) {
  77.             if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT) {
  78.                 uint16_t acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE);
  79.                 uint8_t actid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
  80.                
  81.                 if (rxMsg.DataLength==4) {
  82.                     if (acttype == ACT_TYPE_IR) {
  83.                         if (state == STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_DOWN) {
  84.                             if (IrTransmit_Start(rxMsg.Data.bytes[1], rxMsg.Data.bytes[2], rxMsg.Data.bytes[3]) == IR_OK) {
  85.                                 state = STATE_REPEAT;
  86.                             }
  87.                         } else if (state == STATE_REPEAT && rxMsg.Data.bytes[0] == IR_BUTTON_UP) {
  88.                             state = STATE_STOPPING;
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.            
  94.             // Flush the received message
  95.             rxMsgFull = 0; //  
  96.         }
  97.     }
  98.    
  99.     return 0;
  100. }
  101.