Subversion Repositories HomeAutomation

Rev

Rev 1502 | Rev 1797 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "act_output.h"
  3. uint8_t chnValue[8];
  4. #ifdef act_output_USEEEPROM
  5. #include "act_output_eeprom.h"
  6. struct eeprom_act_output EEMEM eeprom_act_output =
  7. {
  8.     {
  9.         ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
  10. #ifdef  act_output_CH0
  11.         0x00,
  12. #endif
  13. #ifdef  act_output_CH1
  14.         0x00,
  15. #endif
  16. #ifdef  act_output_CH2
  17.         0x00,
  18. #endif
  19. #ifdef  act_output_CH3
  20.         0x00,
  21. #endif
  22. #ifdef  act_output_CH4
  23.         0x00,
  24. #endif
  25. #ifdef  act_output_CH5
  26.         0x00,
  27. #endif
  28. #ifdef  act_output_CH6
  29.         0x00,
  30. #endif
  31. #ifdef  act_output_CH7
  32.         0x00,
  33. #endif
  34.     },
  35.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  36. };
  37. #endif
  38.  
  39. void act_output_Init(void)
  40. {
  41. #ifdef act_output_USEEEPROM
  42.     if (EEDATA_OK)
  43.     {
  44.       ///TODO: Use stored data to set initial values for the module
  45.       uint8_t index = 0;
  46. #ifdef  act_output_CH0
  47.         chnValue[index] = eeprom_read_byte(EEDATA.ch0);
  48.         index++;
  49. #endif
  50. #ifdef  act_output_CH1
  51.         chnValue[index] = eeprom_read_byte(EEDATA.ch1);
  52.         index++;
  53. #endif
  54. #ifdef  act_output_CH2
  55.         chnValue[index] = eeprom_read_byte(EEDATA.ch2);
  56.         index++;
  57. #endif
  58. #ifdef  act_output_CH3
  59.         chnValue[index] = eeprom_read_byte(EEDATA.ch3);
  60.         index++;
  61. #endif
  62. #ifdef  act_output_CH4
  63.         chnValue[index] = eeprom_read_byte(EEDATA.ch4);
  64.         index++;
  65. #endif
  66. #ifdef  act_output_CH5
  67.         chnValue[index] = eeprom_read_byte(EEDATA.ch5);
  68.         index++;
  69. #endif
  70. #ifdef  act_output_CH6
  71.         chnValue[index] = eeprom_read_byte(EEDATA.ch6);
  72.         index++;
  73. #endif
  74. #ifdef  act_output_CH7
  75.         chnValue[index] = eeprom_read_byte(EEDATA.ch7);
  76.         index++;
  77. #endif   
  78.     } else
  79.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  80. #ifdef  act_output_CH0
  81.         eeprom_write_byte_crc(EEDATA.ch0, 0x00, WITHOUT_CRC);
  82. #endif
  83. #ifdef  act_output_CH1
  84.         eeprom_write_byte_crc(EEDATA.ch1, 0x00, WITHOUT_CRC);
  85. #endif
  86. #ifdef  act_output_CH2
  87.         eeprom_write_byte_crc(EEDATA.ch2, 0x00, WITHOUT_CRC);
  88. #endif
  89. #ifdef  act_output_CH3
  90.         eeprom_write_byte_crc(EEDATA.ch3, 0x00, WITHOUT_CRC);
  91. #endif
  92. #ifdef  act_output_CH4
  93.         eeprom_write_byte_crc(EEDATA.ch4, 0x00, WITHOUT_CRC);
  94. #endif
  95. #ifdef  act_output_CH5
  96.         eeprom_write_byte_crc(EEDATA.ch5, 0x00, WITHOUT_CRC);
  97. #endif
  98. #ifdef  act_output_CH6
  99.         eeprom_write_byte_crc(EEDATA.ch6, 0x00, WITHOUT_CRC);
  100. #endif
  101. #ifdef  act_output_CH7
  102.         eeprom_write_byte_crc(EEDATA.ch7, 0x00, WITHOUT_CRC);
  103. #endif   
  104.         EEDATA_UPDATE_CRC;
  105.     }
  106. #endif  
  107.     ///Initialize hardware
  108.     uint8_t index = 0;
  109.  
  110. #if act_output_CH0PCA95xxIO==1 |act_output_CH1PCA95xxIO==1 | act_output_CH2PCA95xxIO==1 | act_output_CH3PCA95xxIO==1 | act_output_CH4PCA95xxIO==1 | act_output_CH5PCA95xxIO==1 | act_output_CH6PCA95xxIO==1 | act_output_CH7PCA95xxIO==1
  111.     Pca95xx_Init(0);
  112. #endif
  113.    
  114. #ifdef  act_output_CH0
  115. #if act_output_CH0PCA95xxIO==0
  116.     gpio_set_statement(chnValue[index],act_output_CH0);
  117.     gpio_set_out(act_output_CH0);
  118. #else
  119.     Pca95xx_set_statement(chnValue[index],act_output_CH0);
  120.     Pca95xx_set_out(act_output_CH0);
  121. #endif
  122.     index++;
  123. #endif
  124. #ifdef  act_output_CH1
  125. #if act_output_CH1PCA95xxIO==0
  126.     gpio_set_statement(chnValue[index],act_output_CH1);
  127.     gpio_set_out(act_output_CH1);
  128. #else
  129.     Pca95xx_set_statement(chnValue[index],act_output_CH1);
  130.     Pca95xx_set_out(act_output_CH1);
  131. #endif
  132.     index++;
  133. #endif
  134. #ifdef  act_output_CH2
  135. #if act_output_CH2PCA95xxIO==0
  136.     gpio_set_statement(chnValue[index],act_output_CH2);
  137.     gpio_set_out(act_output_CH2);
  138. #else
  139.     Pca95xx_set_statement(chnValue[index],act_output_CH2);
  140.     Pca95xx_set_out(act_output_CH2);
  141. #endif
  142.     index++;
  143. #endif
  144. #ifdef  act_output_CH3
  145. #if act_output_CH3PCA95xxIO==0
  146.     gpio_set_statement(chnValue[index],act_output_CH3);
  147.     gpio_set_out(act_output_CH3);
  148. #else
  149.     Pca95xx_set_statement(chnValue[index],act_output_CH3);
  150.     Pca95xx_set_out(act_output_CH3);
  151. #endif
  152.     index++;
  153. #endif
  154. #ifdef  act_output_CH4
  155. #if act_output_CH4PCA95xxIO==0
  156.     gpio_set_statement(chnValue[index],act_output_CH4);
  157.     gpio_set_out(act_output_CH4);
  158. #else
  159.     Pca95xx_set_statement(chnValue[index],act_output_CH4);
  160.     Pca95xx_set_out(act_output_CH4);
  161. #endif
  162.     index++;
  163. #endif
  164. #ifdef  act_output_CH5
  165. #if act_output_CH5PCA95xxIO==0
  166.     gpio_set_statement(chnValue[index],act_output_CH5);
  167.     gpio_set_out(act_output_CH5);
  168. #else
  169.     Pca95xx_set_statement(chnValue[index],act_output_CH5);
  170.     Pca95xx_set_out(act_output_CH5);
  171. #endif
  172.     index++;
  173. #endif
  174. #ifdef  act_output_CH6
  175. #if act_output_CH6PCA95xxIO==0
  176.     gpio_set_statement(chnValue[index],act_output_CH6);
  177.     gpio_set_out(act_output_CH6);
  178. #else
  179.     Pca95xx_set_statement(chnValue[index],act_output_CH6);
  180.     Pca95xx_set_out(act_output_CH6);
  181. #endif
  182.     index++;
  183. #endif
  184. #ifdef  act_output_CH7
  185. #if act_output_CH7PCA95xxIO==0
  186.     gpio_set_statement(chnValue[index],act_output_CH7);
  187.     gpio_set_out(act_output_CH7);
  188. #else
  189.     Pca95xx_set_statement(chnValue[index],act_output_CH7);
  190.     Pca95xx_set_out(act_output_CH7);
  191. #endif
  192.     index++;
  193. #endif
  194.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  195.     // Pcint_SetCallbackPin(act_output_PCINT, EXP_C , &act_output_pcint_callback);
  196.  
  197. }
  198.  
  199. void act_output_Process(void)
  200. {
  201.     if (Timer_Expired(act_output_STORE_VALUE_TIMEOUT))
  202.     {
  203.         uint8_t index = 0;
  204. #ifdef  act_output_CH0
  205.         eeprom_write_byte_crc(EEDATA.ch0, chnValue[index], WITHOUT_CRC);
  206.         index++;
  207. #endif
  208. #ifdef  act_output_CH1
  209.         eeprom_write_byte_crc(EEDATA.ch1, chnValue[index], WITHOUT_CRC);
  210.         index++;
  211. #endif
  212. #ifdef  act_output_CH2
  213.         eeprom_write_byte_crc(EEDATA.ch2, chnValue[index], WITHOUT_CRC);
  214.         index++;
  215. #endif
  216. #ifdef  act_output_CH3
  217.         eeprom_write_byte_crc(EEDATA.ch3, chnValue[index], WITHOUT_CRC);
  218.         index++;
  219. #endif
  220. #ifdef  act_output_CH4
  221.         eeprom_write_byte_crc(EEDATA.ch4, chnValue[index], WITHOUT_CRC);
  222.         index++;
  223. #endif
  224. #ifdef  act_output_CH5
  225.         eeprom_write_byte_crc(EEDATA.ch5, chnValue[index], WITHOUT_CRC);
  226.         index++;
  227. #endif
  228. #ifdef  act_output_CH6
  229.         eeprom_write_byte_crc(EEDATA.ch6, chnValue[index], WITHOUT_CRC);
  230.         index++;
  231. #endif
  232. #ifdef  act_output_CH7
  233.         eeprom_write_byte_crc(EEDATA.ch7, chnValue[index], WITHOUT_CRC);
  234.         index++;
  235. #endif   
  236.         EEDATA_UPDATE_CRC;
  237.     }
  238. }
  239.  
  240. void act_output_HandleMessage(StdCan_Msg_t *rxMsg)
  241. {
  242.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  243.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  244.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_OUTPUT &&
  245.         rxMsg->Header.ModuleId == act_output_ID)
  246.     {
  247.         uint8_t index = 0;
  248.         uint8_t value = 0;
  249.         switch (rxMsg->Header.Command)
  250.         {
  251.         case CAN_MODULE_CMD_PHYSICAL_SETPIN:
  252.            
  253.             index = 0;
  254.             if (rxMsg->Length == 2) {
  255.     #ifdef  act_output_CH0
  256.                 if (rxMsg->Data[0] == 0) {
  257.                     chnValue[index] = rxMsg->Data[1];
  258.     #if act_output_CH0PCA95xxIO==0
  259.                     gpio_set_statement(chnValue[index],act_output_CH0);
  260.     #else
  261.                     Pca95xx_set_statement(chnValue[index],act_output_CH0);
  262.     #endif
  263.                 }
  264.                 index++;
  265.     #endif
  266.     #ifdef  act_output_CH1
  267.                 if (rxMsg->Data[0] == 1) {
  268.                     chnValue[index] = rxMsg->Data[1];
  269.     #if act_output_CH1PCA95xxIO==0
  270.                     gpio_set_statement(chnValue[index],act_output_CH1);
  271.     #else
  272.                     Pca95xx_set_statement(chnValue[index],act_output_CH1);
  273.     #endif
  274.                 }
  275.                 index++;
  276.     #endif
  277.     #ifdef  act_output_CH2
  278.                 if (rxMsg->Data[0] == 2) {
  279.                     chnValue[index] = rxMsg->Data[1];
  280.     #if act_output_CH2PCA95xxIO==0
  281.                     gpio_set_statement(chnValue[index],act_output_CH2);
  282.     #else
  283.                     Pca95xx_set_statement(chnValue[index],act_output_CH2);
  284.     #endif
  285.                 }
  286.                 index++;
  287.     #endif
  288.     #ifdef  act_output_CH3
  289.                 if (rxMsg->Data[0] == 3) {
  290.                     chnValue[index] = rxMsg->Data[1];
  291.     #if act_output_CH3PCA95xxIO==0
  292.                     gpio_set_statement(chnValue[index],act_output_CH3);
  293.     #else
  294.                     Pca95xx_set_statement(chnValue[index],act_output_CH3);
  295.     #endif
  296.                 }
  297.                 index++;
  298.     #endif
  299.     #ifdef  act_output_CH4
  300.                 if (rxMsg->Data[0] == 4) {
  301.                     chnValue[index] = rxMsg->Data[1];
  302.     #if act_output_CH4PCA95xxIO==0
  303.                     gpio_set_statement(chnValue[index],act_output_CH4);
  304.     #else
  305.                     Pca95xx_set_statement(chnValue[index],act_output_CH4);
  306.     #endif
  307.                 }
  308.                 index++;
  309.     #endif
  310.     #ifdef  act_output_CH5
  311.                 if (rxMsg->Data[0] == 5) {
  312.                     chnValue[index] = rxMsg->Data[1];
  313.     #if act_output_CH5PCA95xxIO==0
  314.                     gpio_set_statement(chnValue[index],act_output_CH5);
  315.     #else
  316.                     Pca95xx_set_statement(chnValue[index],act_output_CH5);
  317.     #endif
  318.                 }
  319.                 index++;
  320.     #endif
  321.     #ifdef  act_output_CH6
  322.                 if (rxMsg->Data[0] == 6) {
  323.                     chnValue[index] = rxMsg->Data[1];
  324.     #if act_output_CH6PCA95xxIO==0
  325.                     gpio_set_statement(chnValue[index],act_output_CH6);
  326.     #else
  327.                     Pca95xx_set_statement(chnValue[index],act_output_CH6);
  328.     #endif
  329.                 }
  330.                 index++;
  331.     #endif
  332.     #ifdef  act_output_CH7
  333.                 if (rxMsg->Data[0] == 7) {
  334.                     chnValue[index] = rxMsg->Data[1];
  335.     #if act_output_CH7PCA95xxIO==0
  336.                     gpio_set_statement(chnValue[index],act_output_CH7);
  337.     #else
  338.                     Pca95xx_set_statement(chnValue[index],act_output_CH7);
  339.     #endif
  340.                 }
  341.                 index++;
  342.     #endif   
  343.                 Timer_SetTimeout(act_output_STORE_VALUE_TIMEOUT, act_output_STORE_VALUE_TIMEOUT_TIME_S*1000, TimerTypeOneShot, 0);
  344.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  345.                 rxMsg->Length = 2;
  346.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  347.             } else {
  348.                 index = 0;
  349.         #ifdef  act_output_CH0
  350.                 if (rxMsg->Data[0] == 0) {value = chnValue[index];}
  351.                 index++;
  352.         #endif
  353.         #ifdef  act_output_CH1
  354.                 if (rxMsg->Data[0] == 1) {value = chnValue[index];}
  355.                 index++;
  356.         #endif
  357.         #ifdef  act_output_CH2
  358.                 if (rxMsg->Data[0] == 2) {value = chnValue[index];}
  359.                 index++;
  360.         #endif
  361.         #ifdef  act_output_CH3
  362.                 if (rxMsg->Data[0] == 3) {value = chnValue[index];}
  363.                 index++;
  364.         #endif
  365.         #ifdef  act_output_CH4
  366.                 if (rxMsg->Data[0] == 4) {value = chnValue[index];}
  367.                 index++;
  368.         #endif
  369.         #ifdef  act_output_CH5
  370.                 if (rxMsg->Data[0] == 5) {value = chnValue[index];}
  371.                 index++;
  372.         #endif
  373.         #ifdef  act_output_CH6
  374.                 if (rxMsg->Data[0] == 6) {value = chnValue[index];}
  375.                 index++;
  376.         #endif
  377.         #ifdef  act_output_CH7
  378.                 if (rxMsg->Data[0] == 7) {value = chnValue[index];}
  379.                 index++;
  380.         #endif     
  381.                 rxMsg->Data[1] = value;
  382.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  383.                 rxMsg->Length = 2;
  384.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  385.  
  386.             }
  387.         break;
  388.         }
  389.     }
  390. }
  391.  
  392. void act_output_List(uint8_t ModuleSequenceNumber)
  393. {
  394.     StdCan_Msg_t txMsg;
  395.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  396.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  397.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_OUTPUT;
  398.     txMsg.Header.ModuleId = act_output_ID;
  399.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  400.     txMsg.Length = 6;
  401.  
  402.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  403.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  404.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  405.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  406.    
  407.     txMsg.Data[4] = NUMBER_OF_MODULES;
  408.     txMsg.Data[5] = ModuleSequenceNumber;
  409.    
  410.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  411. }
  412.