Subversion Repositories HomeAutomation

Rev

Rev 1910 | 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. #if act_output_USEEEPROM==1
  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. #if act_output_USEEEPROM==1
  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 act_output_USEEEPROM==1
  242.     if (Timer_Expired(act_output_STORE_VALUE_TIMEOUT))
  243.     {
  244.         uint8_t index = 0;
  245. #ifdef  act_output_CH0
  246.         eeprom_write_byte_crc(EEDATA.ch0, chnValue[index], WITHOUT_CRC);
  247.         index++;
  248. #endif
  249. #ifdef  act_output_CH1
  250.         eeprom_write_byte_crc(EEDATA.ch1, chnValue[index], WITHOUT_CRC);
  251.         index++;
  252. #endif
  253. #ifdef  act_output_CH2
  254.         eeprom_write_byte_crc(EEDATA.ch2, chnValue[index], WITHOUT_CRC);
  255.         index++;
  256. #endif
  257. #ifdef  act_output_CH3
  258.         eeprom_write_byte_crc(EEDATA.ch3, chnValue[index], WITHOUT_CRC);
  259.         index++;
  260. #endif
  261. #ifdef  act_output_CH4
  262.         eeprom_write_byte_crc(EEDATA.ch4, chnValue[index], WITHOUT_CRC);
  263.         index++;
  264. #endif
  265. #ifdef  act_output_CH5
  266.         eeprom_write_byte_crc(EEDATA.ch5, chnValue[index], WITHOUT_CRC);
  267.         index++;
  268. #endif
  269. #ifdef  act_output_CH6
  270.         eeprom_write_byte_crc(EEDATA.ch6, chnValue[index], WITHOUT_CRC);
  271.         index++;
  272. #endif
  273. #ifdef  act_output_CH7
  274.         eeprom_write_byte_crc(EEDATA.ch7, chnValue[index], WITHOUT_CRC);
  275.         index++;
  276. #endif   
  277. #ifdef  act_output_CH8
  278.         eeprom_write_byte_crc(EEDATA.ch8, chnValue[index], WITHOUT_CRC);
  279.         index++;
  280. #endif   
  281. #ifdef  act_output_CH9
  282.         eeprom_write_byte_crc(EEDATA.ch9, chnValue[index], WITHOUT_CRC);
  283.         index++;
  284. #endif   
  285.         EEDATA_UPDATE_CRC;
  286.     }
  287. #endif
  288.  
  289. }
  290.  
  291. void act_output_HandleMessage(StdCan_Msg_t *rxMsg)
  292. {
  293.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  294.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  295.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_OUTPUT &&
  296.         rxMsg->Header.ModuleId == act_output_ID)
  297.     {
  298.         uint8_t index = 0;
  299.         uint8_t value = 0;
  300.         switch (rxMsg->Header.Command)
  301.         {
  302.         case CAN_MODULE_CMD_PHYSICAL_SETPIN:
  303.            
  304.             index = 0;
  305.             if (rxMsg->Length == 2) {
  306.     #ifdef  act_output_CH0
  307.                 if (rxMsg->Data[0] == 0) {
  308.                     chnValue[index] = rxMsg->Data[1];
  309.     #if act_output_CH0PCA95xxIO==0
  310.                     gpio_set_statement(chnValue[index],act_output_CH0);
  311.     #else
  312.                     Pca95xx_set_statement(chnValue[index],act_output_CH0);
  313.     #endif
  314.                 }
  315.                 index++;
  316.     #endif
  317.     #ifdef  act_output_CH1
  318.                 if (rxMsg->Data[0] == 1) {
  319.                     chnValue[index] = rxMsg->Data[1];
  320.     #if act_output_CH1PCA95xxIO==0
  321.                     gpio_set_statement(chnValue[index],act_output_CH1);
  322.     #else
  323.                     Pca95xx_set_statement(chnValue[index],act_output_CH1);
  324.     #endif
  325.                 }
  326.                 index++;
  327.     #endif
  328.     #ifdef  act_output_CH2
  329.                 if (rxMsg->Data[0] == 2) {
  330.                     chnValue[index] = rxMsg->Data[1];
  331.     #if act_output_CH2PCA95xxIO==0
  332.                     gpio_set_statement(chnValue[index],act_output_CH2);
  333.     #else
  334.                     Pca95xx_set_statement(chnValue[index],act_output_CH2);
  335.     #endif
  336.                 }
  337.                 index++;
  338.     #endif
  339.     #ifdef  act_output_CH3
  340.                 if (rxMsg->Data[0] == 3) {
  341.                     chnValue[index] = rxMsg->Data[1];
  342.     #if act_output_CH3PCA95xxIO==0
  343.                     gpio_set_statement(chnValue[index],act_output_CH3);
  344.     #else
  345.                     Pca95xx_set_statement(chnValue[index],act_output_CH3);
  346.     #endif
  347.                 }
  348.                 index++;
  349.     #endif
  350.     #ifdef  act_output_CH4
  351.                 if (rxMsg->Data[0] == 4) {
  352.                     chnValue[index] = rxMsg->Data[1];
  353.     #if act_output_CH4PCA95xxIO==0
  354.                     gpio_set_statement(chnValue[index],act_output_CH4);
  355.     #else
  356.  
  357.                     Pca95xx_set_statement(chnValue[index],act_output_CH4);
  358.     #endif
  359.                 }
  360.                 index++;
  361.     #endif
  362.     #ifdef  act_output_CH5
  363.                 if (rxMsg->Data[0] == 5) {
  364.                     chnValue[index] = rxMsg->Data[1];
  365.     #if act_output_CH5PCA95xxIO==0
  366.                     gpio_set_statement(chnValue[index],act_output_CH5);
  367.     #else
  368.                     Pca95xx_set_statement(chnValue[index],act_output_CH5);
  369.     #endif
  370.                 }
  371.                 index++;
  372.     #endif
  373.     #ifdef  act_output_CH6
  374.                 if (rxMsg->Data[0] == 6) {
  375.                     chnValue[index] = rxMsg->Data[1];
  376.     #if act_output_CH6PCA95xxIO==0
  377.                     gpio_set_statement(chnValue[index],act_output_CH6);
  378.     #else
  379.                     Pca95xx_set_statement(chnValue[index],act_output_CH6);
  380.     #endif
  381.                 }
  382.                 index++;
  383.     #endif
  384.     #ifdef  act_output_CH7
  385.                 if (rxMsg->Data[0] == 7) {
  386.                     chnValue[index] = rxMsg->Data[1];
  387.     #if act_output_CH7PCA95xxIO==0
  388.                     gpio_set_statement(chnValue[index],act_output_CH7);
  389.     #else
  390.                     Pca95xx_set_statement(chnValue[index],act_output_CH7);
  391.     #endif
  392.                 }
  393.                 index++;
  394.     #endif   
  395.     #ifdef  act_output_CH8
  396.                 if (rxMsg->Data[0] == 8) {
  397.                     chnValue[index] = rxMsg->Data[1];
  398.     #if act_output_CH8PCA95xxIO==0
  399.                     gpio_set_statement(chnValue[index],act_output_CH8);
  400.     #else
  401.                     Pca95xx_set_statement(chnValue[index],act_output_CH8);
  402.     #endif
  403.                 }
  404.                 index++;
  405.     #endif   
  406.     #ifdef  act_output_CH9
  407.                 if (rxMsg->Data[0] == 9) {
  408.                     chnValue[index] = rxMsg->Data[1];
  409.     #if act_output_CH9PCA95xxIO==0
  410.                     gpio_set_statement(chnValue[index],act_output_CH9);
  411.     #else
  412.                     Pca95xx_set_statement(chnValue[index],act_output_CH9);
  413.     #endif
  414.                 }
  415.                 index++;
  416.     #endif   
  417.                 Timer_SetTimeout(act_output_STORE_VALUE_TIMEOUT, act_output_STORE_VALUE_TIMEOUT_TIME_S*1000, TimerTypeOneShot, 0);
  418.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  419.                 rxMsg->Length = 2;
  420.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  421.             } else {
  422.                 index = 0;
  423.         #ifdef  act_output_CH0
  424.                 if (rxMsg->Data[0] == 0) {value = chnValue[index];}
  425.                 index++;
  426.         #endif
  427.         #ifdef  act_output_CH1
  428.                 if (rxMsg->Data[0] == 1) {value = chnValue[index];}
  429.                 index++;
  430.         #endif
  431.         #ifdef  act_output_CH2
  432.                 if (rxMsg->Data[0] == 2) {value = chnValue[index];}
  433.                 index++;
  434.         #endif
  435.         #ifdef  act_output_CH3
  436.                 if (rxMsg->Data[0] == 3) {value = chnValue[index];}
  437.                 index++;
  438.         #endif
  439.         #ifdef  act_output_CH4
  440.                 if (rxMsg->Data[0] == 4) {value = chnValue[index];}
  441.                 index++;
  442.         #endif
  443.         #ifdef  act_output_CH5
  444.                 if (rxMsg->Data[0] == 5) {value = chnValue[index];}
  445.                 index++;
  446.         #endif
  447.         #ifdef  act_output_CH6
  448.                 if (rxMsg->Data[0] == 6) {value = chnValue[index];}
  449.                 index++;
  450.         #endif
  451.         #ifdef  act_output_CH7
  452.                 if (rxMsg->Data[0] == 7) {value = chnValue[index];}
  453.                 index++;
  454.         #endif     
  455.         #ifdef  act_output_CH8
  456.                 if (rxMsg->Data[0] == 8) {value = chnValue[index];}
  457.                 index++;
  458.         #endif     
  459.         #ifdef  act_output_CH9
  460.                 if (rxMsg->Data[0] == 9) {value = chnValue[index];}
  461.                 index++;
  462.         #endif     
  463.                 rxMsg->Data[1] = value;
  464.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  465.                 rxMsg->Length = 2;
  466.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  467.  
  468.             }
  469.         break;
  470.         }
  471.     }
  472. }
  473.  
  474. void act_output_List(uint8_t ModuleSequenceNumber)
  475. {
  476.     StdCan_Msg_t txMsg;
  477.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  478.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  479.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_OUTPUT;
  480.     txMsg.Header.ModuleId = act_output_ID;
  481.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  482.     txMsg.Length = 6;
  483.  
  484.     uint32_t HwId=BIOS_GetHwId();
  485.     txMsg.Data[0] = HwId&0xff;
  486.     txMsg.Data[1] = (HwId>>8)&0xff;
  487.     txMsg.Data[2] = (HwId>>16)&0xff;
  488.     txMsg.Data[3] = (HwId>>24)&0xff;
  489.    
  490.     txMsg.Data[4] = NUMBER_OF_MODULES;
  491.     txMsg.Data[5] = ModuleSequenceNumber;
  492.    
  493.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  494. }
  495.