Subversion Repositories HomeAutomation

Rev

Rev 1419 | Rev 1502 | 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.         gpio_set_statement(chnValue[index],act_output_CH0);
  111.         gpio_set_out(act_output_CH0);
  112.         index++;
  113. #endif
  114. #ifdef  act_output_CH1
  115.         gpio_set_statement(chnValue[index],act_output_CH1);
  116.         gpio_set_out(act_output_CH1);
  117.         index++;
  118. #endif
  119. #ifdef  act_output_CH2
  120.         gpio_set_statement(chnValue[index],act_output_CH2);
  121.         gpio_set_out(act_output_CH2);
  122.         index++;
  123. #endif
  124. #ifdef  act_output_CH3
  125.         gpio_set_statement(chnValue[index],act_output_CH3);
  126.         gpio_set_out(act_output_CH3);
  127.         index++;
  128. #endif
  129. #ifdef  act_output_CH4
  130.         gpio_set_statement(chnValue[index],act_output_CH4);
  131.         gpio_set_out(act_output_CH4);
  132.         index++;
  133. #endif
  134. #ifdef  act_output_CH5
  135.         gpio_set_statement(chnValue[index],act_output_CH5);
  136.         gpio_set_out(act_output_CH5);
  137.         index++;
  138. #endif
  139. #ifdef  act_output_CH6
  140.         gpio_set_statement(chnValue[index],act_output_CH6);
  141.         gpio_set_out(act_output_CH6);
  142.         index++;
  143. #endif
  144. #ifdef  act_output_CH7
  145.         gpio_set_statement(chnValue[index],act_output_CH7);
  146.         gpio_set_out(act_output_CH7);
  147.         index++;
  148. #endif
  149.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  150.     // Pcint_SetCallbackPin(act_output_PCINT, EXP_C , &act_output_pcint_callback);
  151.  
  152. }
  153.  
  154. void act_output_Process(void)
  155. {
  156.     if (Timer_Expired(act_output_STORE_VALUE_TIMEOUT))
  157.     {
  158.         uint8_t index = 0;
  159. #ifdef  act_output_CH0
  160.         eeprom_write_byte_crc(EEDATA.ch0, chnValue[index], WITHOUT_CRC);
  161.         index++;
  162. #endif
  163. #ifdef  act_output_CH1
  164.         eeprom_write_byte_crc(EEDATA.ch1, chnValue[index], WITHOUT_CRC);
  165.         index++;
  166. #endif
  167. #ifdef  act_output_CH2
  168.         eeprom_write_byte_crc(EEDATA.ch2, chnValue[index], WITHOUT_CRC);
  169.         index++;
  170. #endif
  171. #ifdef  act_output_CH3
  172.         eeprom_write_byte_crc(EEDATA.ch3, chnValue[index], WITHOUT_CRC);
  173.         index++;
  174. #endif
  175. #ifdef  act_output_CH4
  176.         eeprom_write_byte_crc(EEDATA.ch4, chnValue[index], WITHOUT_CRC);
  177.         index++;
  178. #endif
  179. #ifdef  act_output_CH5
  180.         eeprom_write_byte_crc(EEDATA.ch5, chnValue[index], WITHOUT_CRC);
  181.         index++;
  182. #endif
  183. #ifdef  act_output_CH6
  184.         eeprom_write_byte_crc(EEDATA.ch6, chnValue[index], WITHOUT_CRC);
  185.         index++;
  186. #endif
  187. #ifdef  act_output_CH7
  188.         eeprom_write_byte_crc(EEDATA.ch7, chnValue[index], WITHOUT_CRC);
  189.         index++;
  190. #endif   
  191.         EEDATA_UPDATE_CRC;
  192.     }
  193. }
  194.  
  195. void act_output_HandleMessage(StdCan_Msg_t *rxMsg)
  196. {
  197.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  198.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  199.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_OUTPUT &&
  200.         rxMsg->Header.ModuleId == act_output_ID)
  201.     {
  202.         uint8_t index = 0;
  203.         uint8_t value = 0;
  204.         switch (rxMsg->Header.Command)
  205.         {
  206.         case CAN_MODULE_CMD_PHYSICAL_SETPIN:
  207.            
  208.             index = 0;
  209.             if (rxMsg->Length == 2) {
  210.     #ifdef  act_output_CH0
  211.                 if (rxMsg->Data[0] == 0) {
  212.                     chnValue[index] = rxMsg->Data[1];
  213.                     gpio_set_statement(chnValue[index],act_output_CH0);
  214.                 }              
  215.                 index++;
  216.     #endif
  217.     #ifdef  act_output_CH1
  218.                 if (rxMsg->Data[0] == 1) {
  219.                     chnValue[index] = rxMsg->Data[1];
  220.                     gpio_set_statement(chnValue[index],act_output_CH1);
  221.                 }              
  222.                 index++;
  223.     #endif
  224.     #ifdef  act_output_CH2
  225.                 if (rxMsg->Data[0] == 2) {
  226.                     chnValue[index] = rxMsg->Data[1];
  227.                     gpio_set_statement(chnValue[index],act_output_CH2);
  228.                 }              
  229.                 index++;
  230.     #endif
  231.     #ifdef  act_output_CH3
  232.                 if (rxMsg->Data[0] == 3) {
  233.                     chnValue[index] = rxMsg->Data[1];
  234.                     gpio_set_statement(chnValue[index],act_output_CH3);
  235.                 }              
  236.                 index++;
  237.     #endif
  238.     #ifdef  act_output_CH4
  239.                 if (rxMsg->Data[0] == 4) {
  240.                     chnValue[index] = rxMsg->Data[1];
  241.                     gpio_set_statement(chnValue[index],act_output_CH4);
  242.                 }              
  243.                 index++;
  244.     #endif
  245.     #ifdef  act_output_CH5
  246.                 if (rxMsg->Data[0] == 5) {
  247.                     chnValue[index] = rxMsg->Data[1];
  248.                     gpio_set_statement(chnValue[index],act_output_CH5);
  249.                 }              
  250.                 index++;
  251.     #endif
  252.     #ifdef  act_output_CH6
  253.                 if (rxMsg->Data[0] == 6) {
  254.                     chnValue[index] = rxMsg->Data[1];
  255.                     gpio_set_statement(chnValue[index],act_output_CH6);
  256.                 }              
  257.                 index++;
  258.     #endif
  259.     #ifdef  act_output_CH7
  260.                 if (rxMsg->Data[0] == 7) {
  261.                     chnValue[index] = rxMsg->Data[1];
  262.                     gpio_set_statement(chnValue[index],act_output_CH7);
  263.                 }              
  264.                 index++;
  265.     #endif   
  266.                 Timer_SetTimeout(act_output_STORE_VALUE_TIMEOUT, act_output_STORE_VALUE_TIMEOUT_TIME_S*1000, TimerTypeOneShot, 0);
  267.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  268.                 rxMsg->Length = 2;
  269.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  270.             } else {
  271.                 index = 0;
  272.         #ifdef  act_output_CH0
  273.                 if (rxMsg->Data[0] == 0) {value = chnValue[index];}
  274.                 index++;
  275.         #endif
  276.         #ifdef  act_output_CH1
  277.                 if (rxMsg->Data[0] == 1) {value = chnValue[index];}
  278.                 index++;
  279.         #endif
  280.         #ifdef  act_output_CH2
  281.                 if (rxMsg->Data[0] == 2) {value = chnValue[index];}
  282.                 index++;
  283.         #endif
  284.         #ifdef  act_output_CH3
  285.                 if (rxMsg->Data[0] == 3) {value = chnValue[index];}
  286.                 index++;
  287.         #endif
  288.         #ifdef  act_output_CH4
  289.                 if (rxMsg->Data[0] == 4) {value = chnValue[index];}
  290.                 index++;
  291.         #endif
  292.         #ifdef  act_output_CH5
  293.                 if (rxMsg->Data[0] == 5) {value = chnValue[index];}
  294.                 index++;
  295.         #endif
  296.         #ifdef  act_output_CH6
  297.                 if (rxMsg->Data[0] == 6) {value = chnValue[index];}
  298.                 index++;
  299.         #endif
  300.         #ifdef  act_output_CH7
  301.                 if (rxMsg->Data[0] == 7) {value = chnValue[index];}
  302.                 index++;
  303.         #endif     
  304.                 rxMsg->Data[1] = value;
  305.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  306.                 rxMsg->Length = 2;
  307.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  308.  
  309.             }
  310.         break;
  311.         }
  312.     }
  313. }
  314.  
  315. void act_output_List(uint8_t ModuleSequenceNumber)
  316. {
  317.     StdCan_Msg_t txMsg;
  318.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  319.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  320.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_OUTPUT;
  321.     txMsg.Header.ModuleId = act_output_ID;
  322.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  323.     txMsg.Length = 6;
  324.  
  325.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  326.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  327.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  328.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  329.    
  330.     txMsg.Data[4] = NUMBER_OF_MODULES;
  331.     txMsg.Data[5] = ModuleSequenceNumber;
  332.    
  333.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  334. }
  335.