Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "act_multiSwitch.h"
  3.  
  4.  
  5. uint8_t activeSwitch = 0;
  6. #if act_multiSwitch_USEEEPROM==1
  7. #include "act_multiSwitch_eeprom.h"
  8. struct eeprom_act_multiSwitch EEMEM eeprom_act_multiSwitch =
  9. {
  10.   {
  11.     0x00,       // x
  12.   },
  13.   0 // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  14. };
  15. #endif
  16.  
  17. void act_multiSwitch_Init(void)
  18. {
  19.   #if act_multiSwitch_USEEEPROM==1
  20.   if (EEDATA_OK)
  21.   {
  22.     activeSwitch = eeprom_read_byte(EEDATA.activeSwitch);
  23.   } else
  24.   { //The CRC of the EEPROM is not correct, store default values and update CRC
  25.   eeprom_write_byte_crc(EEDATA.activeSwitch, 0x00, WITHOUT_CRC);
  26.   EEDATA_UPDATE_CRC;
  27.   }
  28.   #endif
  29.   ///TODO: Initialize hardware etc here
  30.  
  31.   // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  32.   // Pcint_SetCallbackPin(act_multiSwitch_PCINT, EXP_C , &act_multiSwitch_pcint_callback);
  33.   #ifdef    act_multiSwitch_Feedback_0
  34.   gpio_set_in(act_multiSwitch_Feedback_0);
  35.   #endif
  36.   #ifdef    act_multiSwitch_Feedback_1
  37.   gpio_set_in(act_multiSwitch_Feedback_1);
  38.   #endif
  39.   #ifdef    act_multiSwitch_Feedback_2
  40.   gpio_set_in(act_multiSwitch_Feedback_2);
  41.   #endif
  42.   #ifdef    act_multiSwitch_Feedback_3
  43.   gpio_set_in(act_multiSwitch_Feedback_3);
  44.   #endif
  45.   #ifdef    act_multiSwitch_Feedback_4
  46.   gpio_set_in(act_multiSwitch_Feedback_4);
  47.   #endif
  48.   #ifdef    act_multiSwitch_Feedback_5
  49.   gpio_set_in(act_multiSwitch_Feedback_5);
  50.   #endif
  51.   #ifdef    act_multiSwitch_Feedback_6
  52.   gpio_set_in(act_multiSwitch_Feedback_6);
  53.   #endif
  54.   #ifdef    act_multiSwitch_Feedback_7
  55.   gpio_set_in(act_multiSwitch_Feedback_7);
  56.   #endif
  57.   #ifdef OUTPUT_ACTIVE_LOW
  58.   gpio_set_pin(act_multiSwitch_switch);
  59.   #endif
  60.   #ifdef OUTPUT_ACTIVE_HIGH
  61.   gpio_clr_pin(act_multiSwitch_switch);
  62.   #endif
  63.  
  64.   gpio_set_out(act_multiSwitch_switch);
  65. }
  66.  
  67. #define STATE_IDLE  0
  68. #define STATE_PRESSED   1
  69. #define STATE_WAIT  2
  70.  
  71. void act_multiSwitch_Process(void)
  72. {
  73.   static uint8_t state = STATE_IDLE;
  74.   uint8_t position = 0;
  75.   // get current position
  76.   #ifdef    act_multiSwitch_Feedback_0
  77.   #ifdef INPUT_ACTIVE_LOW
  78.   if    (!gpio_get_state(act_multiSwitch_Feedback_0)) {  position = 0; }
  79.   #endif
  80.   #ifdef INPUT_ACTIVE_HIGH
  81.   if    (gpio_get_state(act_multiSwitch_Feedback_0)) {  position = 0; }
  82.   #endif
  83.  
  84.   #endif  
  85.   #ifdef    act_multiSwitch_Feedback_1
  86.   #ifdef INPUT_ACTIVE_LOW
  87.   if    (!gpio_get_state(act_multiSwitch_Feedback_1)) { position = 1; }
  88.   #endif
  89.   #ifdef INPUT_ACTIVE_HIGH
  90.   if    (gpio_get_state(act_multiSwitch_Feedback_1)) { position = 1; }
  91.   #endif
  92.  
  93.  
  94.   #endif
  95.   #ifdef    act_multiSwitch_Feedback_2
  96.   #ifdef INPUT_ACTIVE_LOW
  97.   if    (!gpio_get_state(act_multiSwitch_Feedback_2)) { position = 2; }
  98.   #endif
  99.   #ifdef INPUT_ACTIVE_HIGH
  100.   if    (gpio_get_state(act_multiSwitch_Feedback_2)) { position = 2; }
  101.   #endif
  102.  
  103.   #endif
  104.   #ifdef    act_multiSwitch_Feedback_3
  105.   #ifdef INPUT_ACTIVE_LOW
  106.   if    (!gpio_get_state(act_multiSwitch_Feedback_3)) {  position = 3; }
  107.   #endif
  108.   #ifdef INPUT_ACTIVE_HIGH
  109.   if    (gpio_get_state(act_multiSwitch_Feedback_3)) {  position = 3; }
  110.   #endif
  111.  
  112.  
  113.   #endif
  114.   #ifdef    act_multiSwitch_Feedback_4
  115.   #ifdef INPUT_ACTIVE_LOW
  116.   if    (!gpio_get_state(act_multiSwitch_Feedback_4)) { position = 4; }
  117.   #endif
  118.   #ifdef INPUT_ACTIVE_HIGH
  119.   if    (gpio_get_state(act_multiSwitch_Feedback_4)) { position = 4; }
  120.   #endif
  121.  
  122.  
  123.   #endif
  124.   #ifdef    act_multiSwitch_Feedback_5
  125.   #ifdef INPUT_ACTIVE_LOW
  126.   if    (!gpio_get_state(act_multiSwitch_Feedback_5)) { position = 5; }
  127.   #endif
  128.   #ifdef INPUT_ACTIVE_HIGH
  129.   if    (gpio_get_state(act_multiSwitch_Feedback_5)) { position = 5; }
  130.   #endif
  131.  
  132.  
  133.   #endif
  134.   #ifdef    act_multiSwitch_Feedback_6
  135.   #ifdef INPUT_ACTIVE_LOW
  136.   if    (!gpio_get_state(act_multiSwitch_Feedback_6)) {  position = 6; }
  137.   #endif
  138.   #ifdef INPUT_ACTIVE_HIGH
  139.   if    (gpio_get_state(act_multiSwitch_Feedback_6)) {  position = 6; }
  140.   #endif
  141.  
  142.   #endif
  143.   #ifdef    act_multiSwitch_Feedback_7
  144.   #ifdef INPUT_ACTIVE_LOW
  145.   if    (!gpio_get_state(act_multiSwitch_Feedback_7)) { position = 7; }
  146.   #endif
  147.   #ifdef INPUT_ACTIVE_HIGH
  148.   if    (gpio_get_state(act_multiSwitch_Feedback_7)) { position = 7; }
  149.   #endif
  150.   #endif
  151.  
  152.   if (state == STATE_IDLE) {
  153.     if (position != activeSwitch) {
  154.       //printf("1: %d = %d\n",position, activeSwitch);
  155.       #ifdef OUTPUT_ACTIVE_LOW
  156.       gpio_clr_pin(act_multiSwitch_switch);
  157.       #endif
  158.       #ifdef OUTPUT_ACTIVE_HIGH
  159.       gpio_set_pin(act_multiSwitch_switch);
  160.       #endif
  161.       state = STATE_PRESSED;
  162.       Timer_SetTimeout(act_multiSwitch_PRESS_TIMER, act_multiSwitch_PRESS_TIMEOUT_TIME_MS, TimerTypeOneShot, 0);
  163.     }
  164.   }
  165.  
  166.   if (state == STATE_PRESSED) {
  167.     if (Timer_Expired(act_multiSwitch_PRESS_TIMER))
  168.     {
  169.       //printf("2: \n");
  170.       #ifdef OUTPUT_ACTIVE_LOW
  171.       gpio_set_pin(act_multiSwitch_switch);
  172.       #endif
  173.       #ifdef OUTPUT_ACTIVE_HIGH
  174.       gpio_clr_pin(act_multiSwitch_switch);
  175.       #endif  
  176.       state = STATE_WAIT;
  177.       Timer_SetTimeout(act_multiSwitch_PRESS_TIMER, act_multiSwitch_WAIT_TIMEOUT_TIME_MS, TimerTypeOneShot, 0);
  178.     }
  179.   }
  180.   if (state == STATE_WAIT) {
  181.     if (Timer_Expired(act_multiSwitch_PRESS_TIMER))
  182.     {
  183.        //printf("3: \n");
  184.        state = STATE_IDLE;
  185.     }
  186.   }
  187.  
  188.   if (Timer_Expired(act_multiSwitch_STORE_VALUE_TIMER))
  189.   {
  190.     eeprom_write_byte_crc(EEDATA.activeSwitch, activeSwitch, WITH_CRC);
  191.   }
  192. }
  193.  
  194. void act_multiSwitch_HandleMessage(StdCan_Msg_t *rxMsg)
  195. {
  196.   if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  197.     StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  198.     rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_MULTISWITCH &&
  199.     rxMsg->Header.ModuleId == act_multiSwitch_ID)
  200.   {
  201.     switch (rxMsg->Header.Command)
  202.     {
  203.       case CAN_MODULE_CMD_PHYSICAL_SETPIN:
  204.     if (rxMsg->Length == 2) {
  205.       if (rxMsg->Data[1] == CAN_MODULE_ENUM_PHYSICAL_SETPIN_STATUS_HIGH) {
  206.         activeSwitch= rxMsg->Data[0];
  207.         Timer_SetTimeout(act_multiSwitch_STORE_VALUE_TIMER, act_multiSwitch_STORE_VALUE_TIMEOUT_TIME_S*1000, TimerTypeOneShot, 0);
  208.         StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  209.         rxMsg->Length = 2;
  210.         while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  211.       } else {
  212.         rxMsg->Data[0] = activeSwitch+10;
  213.         rxMsg->Data[1] = CAN_MODULE_ENUM_PHYSICAL_SETPIN_STATUS_HIGH;
  214.         StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  215.         rxMsg->Length = 2;
  216.         while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  217.       }
  218.      
  219.     }
  220.     break;
  221.     }
  222.   }
  223. }
  224.  
  225.   void act_multiSwitch_List(uint8_t ModuleSequenceNumber)
  226.   {
  227.     StdCan_Msg_t txMsg;
  228.    
  229.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  230.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  231.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_MULTISWITCH;
  232.     txMsg.Header.ModuleId = act_multiSwitch_ID;
  233.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  234.     txMsg.Length = 6;
  235.    
  236.     uint32_t HwId=BIOS_GetHwId();
  237.     txMsg.Data[0] = HwId&0xff;
  238.     txMsg.Data[1] = (HwId>>8)&0xff;
  239.     txMsg.Data[2] = (HwId>>16)&0xff;
  240.     txMsg.Data[3] = (HwId>>24)&0xff;
  241.    
  242.     txMsg.Data[4] = NUMBER_OF_MODULES;
  243.     txMsg.Data[5] = ModuleSequenceNumber;
  244.    
  245.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  246.   }
  247.  
  248.