Subversion Repositories HomeAutomation

Rev

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