Subversion Repositories HomeAutomation

Rev

Rev 1420 | Rev 1519 | 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. #ifdef  act_output_CH0
  110. #if act_output_CH0PCA95xxIO==0
  111.         gpio_set_statement(chnValue[index],act_output_CH0);
  112.         gpio_set_out(act_output_CH0);
  113. #else
  114.         Pca95xx_set_statement(chnValue[index],act_output_CH0);
  115.         Pca95xx_set_out(act_output_CH0);
  116. #endif
  117.         index++;
  118. #endif
  119. #ifdef  act_output_CH1
  120. #if act_output_CH1PCA95xxIO==0
  121.         gpio_set_statement(chnValue[index],act_output_CH1);
  122.         gpio_set_out(act_output_CH1);
  123. #else
  124.         Pca95xx_set_statement(chnValue[index],act_output_CH1);
  125.         Pca95xx_set_out(act_output_CH1);
  126. #endif
  127.         index++;
  128. #endif
  129. #ifdef  act_output_CH2
  130. #if act_output_CH2PCA95xxIO==0
  131.         gpio_set_statement(chnValue[index],act_output_CH2);
  132.         gpio_set_out(act_output_CH2);
  133. #else
  134.         Pca95xx_set_statement(chnValue[index],act_output_CH2);
  135.         Pca95xx_set_out(act_output_CH2);
  136. #endif
  137.         index++;
  138. #endif
  139. #ifdef  act_output_CH3
  140. #if act_output_CH3PCA95xxIO==0
  141.         gpio_set_statement(chnValue[index],act_output_CH3);
  142.         gpio_set_out(act_output_CH3);
  143. #else
  144.         Pca95xx_set_statement(chnValue[index],act_output_CH3);
  145.         Pca95xx_set_out(act_output_CH3);
  146. #endif
  147.         index++;
  148. #endif
  149. #ifdef  act_output_CH4
  150. #if act_output_CH4PCA95xxIO==0
  151.         gpio_set_statement(chnValue[index],act_output_CH4);
  152.         gpio_set_out(act_output_CH4);
  153. #else
  154.         Pca95xx_set_statement(chnValue[index],act_output_CH4);
  155.         Pca95xx_set_out(act_output_CH4);
  156. #endif
  157.         index++;
  158. #endif
  159. #ifdef  act_output_CH5
  160. #if act_output_CH5PCA95xxIO==0
  161.         gpio_set_statement(chnValue[index],act_output_CH5);
  162.         gpio_set_out(act_output_CH5);
  163. #else
  164.         Pca95xx_set_statement(chnValue[index],act_output_CH5);
  165.         Pca95xx_set_out(act_output_CH5);
  166. #endif
  167.         index++;
  168. #endif
  169. #ifdef  act_output_CH6
  170. #if act_output_CH6PCA95xxIO==0
  171.         gpio_set_statement(chnValue[index],act_output_CH6);
  172.         gpio_set_out(act_output_CH6);
  173. #else
  174.         Pca95xx_set_statement(chnValue[index],act_output_CH6);
  175.         Pca95xx_set_out(act_output_CH6);
  176. #endif
  177.         index++;
  178. #endif
  179. #ifdef  act_output_CH7
  180. #if act_output_CH7PCA95xxIO==0
  181.         gpio_set_statement(chnValue[index],act_output_CH7);
  182.         gpio_set_out(act_output_CH7);
  183. #else
  184.         Pca95xx_set_statement(chnValue[index],act_output_CH7);
  185.         Pca95xx_set_out(act_output_CH7);
  186. #endif
  187.         index++;
  188. #endif
  189.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  190.     // Pcint_SetCallbackPin(act_output_PCINT, EXP_C , &act_output_pcint_callback);
  191.  
  192. }
  193.  
  194. void act_output_Process(void)
  195. {
  196.     if (Timer_Expired(act_output_STORE_VALUE_TIMEOUT))
  197.     {
  198.         uint8_t index = 0;
  199. #ifdef  act_output_CH0
  200.         eeprom_write_byte_crc(EEDATA.ch0, chnValue[index], WITHOUT_CRC);
  201.         index++;
  202. #endif
  203. #ifdef  act_output_CH1
  204.         eeprom_write_byte_crc(EEDATA.ch1, chnValue[index], WITHOUT_CRC);
  205.         index++;
  206. #endif
  207. #ifdef  act_output_CH2
  208.         eeprom_write_byte_crc(EEDATA.ch2, chnValue[index], WITHOUT_CRC);
  209.         index++;
  210. #endif
  211. #ifdef  act_output_CH3
  212.         eeprom_write_byte_crc(EEDATA.ch3, chnValue[index], WITHOUT_CRC);
  213.         index++;
  214. #endif
  215. #ifdef  act_output_CH4
  216.         eeprom_write_byte_crc(EEDATA.ch4, chnValue[index], WITHOUT_CRC);
  217.         index++;
  218. #endif
  219. #ifdef  act_output_CH5
  220.         eeprom_write_byte_crc(EEDATA.ch5, chnValue[index], WITHOUT_CRC);
  221.         index++;
  222. #endif
  223. #ifdef  act_output_CH6
  224.         eeprom_write_byte_crc(EEDATA.ch6, chnValue[index], WITHOUT_CRC);
  225.         index++;
  226. #endif
  227. #ifdef  act_output_CH7
  228.         eeprom_write_byte_crc(EEDATA.ch7, chnValue[index], WITHOUT_CRC);
  229.         index++;
  230. #endif   
  231.         EEDATA_UPDATE_CRC;
  232.     }
  233. }
  234.  
  235. void act_output_HandleMessage(StdCan_Msg_t *rxMsg)
  236. {
  237.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  238.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  239.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_OUTPUT &&
  240.         rxMsg->Header.ModuleId == act_output_ID)
  241.     {
  242.         uint8_t index = 0;
  243.         uint8_t value = 0;
  244.         switch (rxMsg->Header.Command)
  245.         {
  246.         case CAN_MODULE_CMD_PHYSICAL_SETPIN:
  247.            
  248.             index = 0;
  249.             if (rxMsg->Length == 2) {
  250.     #ifdef  act_output_CH0
  251.                 if (rxMsg->Data[0] == 0) {
  252.                     chnValue[index] = rxMsg->Data[1];
  253.     #if act_output_CH0PCA95xxIO==0
  254.                     gpio_set_statement(chnValue[index],act_output_CH0);
  255.     #else
  256.                     Pca95xx_set_statement(chnValue[index],act_output_CH0);
  257.     #endif
  258.                 }
  259.                 index++;
  260.     #endif
  261.     #ifdef  act_output_CH1
  262.                 if (rxMsg->Data[0] == 1) {
  263.                     chnValue[index] = rxMsg->Data[1];
  264.     #if act_output_CH1PCA95xxIO==0
  265.                     gpio_set_statement(chnValue[index],act_output_CH1);
  266.     #else
  267.                     Pca95xx_set_statement(chnValue[index],act_output_CH1);
  268.     #endif
  269.                 }
  270.                 index++;
  271.     #endif
  272.     #ifdef  act_output_CH2
  273.                 if (rxMsg->Data[0] == 2) {
  274.                     chnValue[index] = rxMsg->Data[1];
  275.     #if act_output_CH2PCA95xxIO==0
  276.                     gpio_set_statement(chnValue[index],act_output_CH2);
  277.     #else
  278.                     Pca95xx_set_statement(chnValue[index],act_output_CH2);
  279.     #endif
  280.                 }
  281.                 index++;
  282.     #endif
  283.     #ifdef  act_output_CH3
  284.                 if (rxMsg->Data[0] == 3) {
  285.                     chnValue[index] = rxMsg->Data[1];
  286.     #if act_output_CH3PCA95xxIO==0
  287.                     gpio_set_statement(chnValue[index],act_output_CH3);
  288.     #else
  289.                     Pca95xx_set_statement(chnValue[index],act_output_CH3);
  290.     #endif
  291.                 }
  292.                 index++;
  293.     #endif
  294.     #ifdef  act_output_CH4
  295.                 if (rxMsg->Data[0] == 4) {
  296.                     chnValue[index] = rxMsg->Data[1];
  297.     #if act_output_CH4PCA95xxIO==0
  298.                     gpio_set_statement(chnValue[index],act_output_CH4);
  299.     #else
  300.                     Pca95xx_set_statement(chnValue[index],act_output_CH4);
  301.     #endif
  302.                 }
  303.                 index++;
  304.     #endif
  305.     #ifdef  act_output_CH5
  306.                 if (rxMsg->Data[0] == 5) {
  307.                     chnValue[index] = rxMsg->Data[1];
  308.     #if act_output_CH5PCA95xxIO==0
  309.                     gpio_set_statement(chnValue[index],act_output_CH5);
  310.     #else
  311.                     Pca95xx_set_statement(chnValue[index],act_output_CH5);
  312.     #endif
  313.                 }
  314.                 index++;
  315.     #endif
  316.     #ifdef  act_output_CH6
  317.                 if (rxMsg->Data[0] == 6) {
  318.                     chnValue[index] = rxMsg->Data[1];
  319.     #if act_output_CH6PCA95xxIO==0
  320.                     gpio_set_statement(chnValue[index],act_output_CH6);
  321.     #else
  322.                     Pca95xx_set_statement(chnValue[index],act_output_CH6);
  323.     #endif
  324.                 }
  325.                 index++;
  326.     #endif
  327.     #ifdef  act_output_CH7
  328.                 if (rxMsg->Data[0] == 7) {
  329.                     chnValue[index] = rxMsg->Data[1];
  330.     #if act_output_CH7PCA95xxIO==0
  331.                     gpio_set_statement(chnValue[index],act_output_CH7);
  332.     #else
  333.                     Pca95xx_set_statement(chnValue[index],act_output_CH7);
  334.     #endif
  335.                 }
  336.                 index++;
  337.     #endif   
  338.                 Timer_SetTimeout(act_output_STORE_VALUE_TIMEOUT, act_output_STORE_VALUE_TIMEOUT_TIME_S*1000, TimerTypeOneShot, 0);
  339.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  340.                 rxMsg->Length = 2;
  341.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  342.             } else {
  343.                 index = 0;
  344.         #ifdef  act_output_CH0
  345.                 if (rxMsg->Data[0] == 0) {value = chnValue[index];}
  346.                 index++;
  347.         #endif
  348.         #ifdef  act_output_CH1
  349.                 if (rxMsg->Data[0] == 1) {value = chnValue[index];}
  350.                 index++;
  351.         #endif
  352.         #ifdef  act_output_CH2
  353.                 if (rxMsg->Data[0] == 2) {value = chnValue[index];}
  354.                 index++;
  355.         #endif
  356.         #ifdef  act_output_CH3
  357.                 if (rxMsg->Data[0] == 3) {value = chnValue[index];}
  358.                 index++;
  359.         #endif
  360.         #ifdef  act_output_CH4
  361.                 if (rxMsg->Data[0] == 4) {value = chnValue[index];}
  362.                 index++;
  363.         #endif
  364.         #ifdef  act_output_CH5
  365.                 if (rxMsg->Data[0] == 5) {value = chnValue[index];}
  366.                 index++;
  367.         #endif
  368.         #ifdef  act_output_CH6
  369.                 if (rxMsg->Data[0] == 6) {value = chnValue[index];}
  370.                 index++;
  371.         #endif
  372.         #ifdef  act_output_CH7
  373.                 if (rxMsg->Data[0] == 7) {value = chnValue[index];}
  374.                 index++;
  375.         #endif     
  376.                 rxMsg->Data[1] = value;
  377.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  378.                 rxMsg->Length = 2;
  379.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  380.  
  381.             }
  382.         break;
  383.         }
  384.     }
  385. }
  386.  
  387. void act_output_List(uint8_t ModuleSequenceNumber)
  388. {
  389.     StdCan_Msg_t txMsg;
  390.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  391.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  392.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_OUTPUT;
  393.     txMsg.Header.ModuleId = act_output_ID;
  394.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  395.     txMsg.Length = 6;
  396.  
  397.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  398.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  399.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  400.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  401.    
  402.     txMsg.Data[4] = NUMBER_OF_MODULES;
  403.     txMsg.Data[5] = ModuleSequenceNumber;
  404.    
  405.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  406. }
  407.