Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "sns_input.h"
  3. uint8_t pinStatus[8];
  4. #define HIGH    1
  5. #define LOW 2
  6. #define NOCHANGE 0 
  7. void sns_input_pcint_callback(uint8_t id, uint8_t status)
  8. {
  9. #ifdef  sns_input_CH0
  10. #if sns_input_CH0PCA95xxIO==0
  11.     if (id == sns_input_PCINT_CH0) {
  12.       if (0 != status) {
  13.         pinStatus[0] = HIGH;
  14.       } else {
  15.         pinStatus[0] = LOW;
  16.       }
  17.     }
  18. #endif
  19. #endif
  20. #ifdef  sns_input_CH1
  21. #if sns_input_CH1PCA95xxIO==0
  22.     if (id == sns_input_PCINT_CH1) {
  23.       if (0 != status) {
  24.         pinStatus[1] = HIGH;
  25.       } else {
  26.         pinStatus[1] = LOW;
  27.       }
  28.     }
  29. #endif
  30. #endif
  31. #ifdef  sns_input_CH2
  32. #if sns_input_CH2PCA95xxIO==0
  33.     if (id == sns_input_PCINT_CH2) {
  34.       if (0 != status) {
  35.         pinStatus[2] = HIGH;
  36.       } else {
  37.         pinStatus[2] = LOW;
  38.       }
  39.     }
  40. #endif
  41. #endif
  42. #ifdef  sns_input_CH3
  43. #if sns_input_CH3PCA95xxIO==0
  44.     if (id == sns_input_PCINT_CH3) {
  45.       if (0 != status) {
  46.         pinStatus[3] = HIGH;
  47.       } else {
  48.         pinStatus[3] = LOW;
  49.       }
  50.     }
  51. #endif
  52. #endif
  53. #ifdef  sns_input_CH4
  54. #if sns_input_CH4PCA95xxIO==0
  55.     if (id == sns_input_PCINT_CH4) {
  56.       if (0 != status) {
  57.         pinStatus[4] = HIGH;
  58.       } else {
  59.         pinStatus[4] = LOW;
  60.       }
  61.     }
  62. #endif
  63. #endif
  64. #ifdef  sns_input_CH5
  65. #if sns_input_CH5PCA95xxIO==0
  66.     if (id == sns_input_PCINT_CH5) {
  67.       if (0 != status) {
  68.         pinStatus[5] = HIGH;
  69.       } else {
  70.         pinStatus[5] = LOW;
  71.       }
  72.     }
  73. #endif
  74. #endif
  75. #ifdef  sns_input_CH6
  76. #if sns_input_CH6PCA95xxIO==0
  77.     if (id == sns_input_PCINT_CH6) {
  78.       if (0 != status) {
  79.         pinStatus[6] = HIGH;
  80.       } else {
  81.         pinStatus[6] = LOW;
  82.       }
  83.     }
  84. #endif
  85. #endif
  86. #ifdef  sns_input_CH7
  87. #if sns_input_CH7PCA95xxIO==0
  88.     if (id == sns_input_PCINT_CH7) {
  89.       if (0 != status) {
  90.         pinStatus[7] = HIGH;
  91.       } else {
  92.         pinStatus[7] = LOW;
  93.       }
  94.     }
  95. #endif
  96. #endif
  97. }
  98.  
  99. #if sns_input_CH0PCA95xxIO==1 | sns_input_CH1PCA95xxIO==1 | sns_input_CH2PCA95xxIO==1 | sns_input_CH3PCA95xxIO==1 | sns_input_CH4PCA95xxIO==1 | sns_input_CH5PCA95xxIO==1 | sns_input_CH6PCA95xxIO==1 | sns_input_CH7PCA95xxIO==1
  100. /*void sns_input_PCA95xx_callback(uint16_t status)
  101. {
  102.     printf("0x%x\n",status);
  103. }*/
  104. #endif
  105.  
  106.  
  107. void sns_input_Init(void)
  108. {
  109.  
  110. #if sns_input_CH0PCA95xxIO==1 |sns_input_CH1PCA95xxIO==1 | sns_input_CH2PCA95xxIO==1 | sns_input_CH3PCA95xxIO==1 | sns_input_CH4PCA95xxIO==1 | sns_input_CH5PCA95xxIO==1 | sns_input_CH6PCA95xxIO==1 | sns_input_CH7PCA95xxIO==1
  111.     Pca95xx_Init(0);
  112.     //Pca95xx_SetCallback(sns_input_PCA95XX_CALLBACK_INDEX, &sns_input_PCA95xx_callback);
  113.     Timer_SetTimeout(sns_input_PCA95XX_POLL_TIMER, sns_input_PCA95XX_POLL_TIME, TimerTypeFreeRunning, 0);
  114. #endif
  115.  
  116.     pinStatus[0] = NOCHANGE;
  117.     pinStatus[1] = NOCHANGE;
  118.     pinStatus[2] = NOCHANGE;
  119.     pinStatus[3] = NOCHANGE;
  120.     pinStatus[4] = NOCHANGE;
  121.     pinStatus[5] = NOCHANGE;
  122.     pinStatus[6] = NOCHANGE;
  123.     pinStatus[7] = NOCHANGE;
  124.     uint8_t index = 0;
  125. #ifdef  sns_input_CH0
  126. #if sns_input_CH0PCA95xxIO==0
  127.     #if (sns_input_CH0_pullup == 1)
  128.         gpio_set_pullup(sns_input_CH0);
  129.     #else
  130.         gpio_clr_pullup(sns_input_CH0);
  131.     #endif
  132.         gpio_set_in(sns_input_CH0);
  133.         Pcint_SetCallbackPin(sns_input_PCINT_CH0, sns_input_CH0, &sns_input_pcint_callback);
  134. #else
  135.         Pca95xx_set_in(sns_input_CH0);
  136. #endif
  137.         index++;
  138. #endif
  139. #ifdef  sns_input_CH1
  140. #if sns_input_CH1PCA95xxIO==0
  141.     #if (sns_input_CH1_pullup == 1)
  142.         gpio_set_pullup(sns_input_CH1);
  143.     #else
  144.         gpio_clr_pullup(sns_input_CH1);
  145.     #endif     
  146.         gpio_set_in(sns_input_CH1);
  147.         Pcint_SetCallbackPin(sns_input_PCINT_CH1, sns_input_CH1, &sns_input_pcint_callback);
  148. #else
  149.         Pca95xx_set_in(sns_input_CH1);
  150. #endif
  151.         index++;
  152. #endif
  153. #ifdef  sns_input_CH2
  154. #if sns_input_CH2PCA95xxIO==0
  155.     #if (sns_input_CH2_pullup == 1)
  156.         gpio_set_pullup(sns_input_CH2);
  157.     #else
  158.         gpio_clr_pullup(sns_input_CH2);
  159.     #endif     
  160.         gpio_set_in(sns_input_CH2);
  161.         Pcint_SetCallbackPin(sns_input_PCINT_CH2, sns_input_CH2, &sns_input_pcint_callback);
  162. #else
  163.         Pca95xx_set_in(sns_input_CH2);
  164. #endif
  165.         index++;
  166. #endif
  167. #ifdef  sns_input_CH3
  168. #if sns_input_CH3PCA95xxIO==0
  169.     #if (sns_input_CH3_pullup == 1)
  170.         gpio_set_pullup(sns_input_CH3);
  171.     #else
  172.         gpio_clr_pullup(sns_input_CH3);
  173.     #endif     
  174.         gpio_set_in(sns_input_CH3);
  175.         Pcint_SetCallbackPin(sns_input_PCINT_CH3, sns_input_CH3, &sns_input_pcint_callback);
  176. #else
  177.         Pca95xx_set_in(sns_input_CH3);
  178. #endif
  179.         index++;
  180. #endif
  181. #ifdef  sns_input_CH4
  182. #if sns_input_CH4PCA95xxIO==0
  183.     #if (sns_input_CH4_pullup == 1)
  184.         gpio_set_pullup(sns_input_CH4);
  185.     #else
  186.         gpio_clr_pullup(sns_input_CH4);
  187.     #endif     
  188.         gpio_set_in(sns_input_CH4);
  189.         Pcint_SetCallbackPin(sns_input_PCINT_CH4, sns_input_CH4, &sns_input_pcint_callback);
  190. #else
  191.         Pca95xx_set_in(sns_input_CH4);
  192. #endif
  193.         index++;
  194. #endif
  195. #ifdef  sns_input_CH5
  196. #if sns_input_CH5PCA95xxIO==0
  197.     #if (sns_input_CH5_pullup == 1)
  198.         gpio_set_pullup(sns_input_CH5);
  199.     #else
  200.         gpio_clr_pullup(sns_input_CH5);
  201.     #endif     
  202.         gpio_set_in(sns_input_CH5);
  203.         Pcint_SetCallbackPin(sns_input_PCINT_CH5, sns_input_CH5, &sns_input_pcint_callback);
  204. #else
  205.         Pca95xx_set_in(sns_input_CH5);
  206. #endif
  207.         index++;
  208. #endif
  209. #ifdef  sns_input_CH6
  210. #if sns_input_CH6PCA95xxIO==0
  211.     #if (sns_input_CH6_pullup == 1)
  212.         gpio_set_pullup(sns_input_CH6);
  213.     #else
  214.         gpio_clr_pullup(sns_input_CH6);
  215.     #endif         
  216.         gpio_set_in(sns_input_CH6);
  217.         Pcint_SetCallbackPin(sns_input_PCINT_CH6, sns_input_CH6, &sns_input_pcint_callback);
  218. #else
  219.         Pca95xx_set_in(sns_input_CH6);
  220. #endif
  221.         index++;
  222. #endif
  223. #ifdef  sns_input_CH7
  224. #if sns_input_CH7PCA95xxIO==0
  225.     #if (sns_input_CH7_pullup == 1)
  226.         gpio_set_pullup(sns_input_CH7);
  227.     #else
  228.         gpio_clr_pullup(sns_input_CH7);
  229.     #endif     
  230.         gpio_set_in(sns_input_CH7);
  231.         Pcint_SetCallbackPin(sns_input_PCINT_CH7, sns_input_CH7, &sns_input_pcint_callback);
  232. #else
  233.         Pca95xx_set_in(sns_input_CH7);
  234. #endif
  235.         index++;
  236. #endif
  237.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  238.     // Pcint_SetCallbackPin(sns_input_PCINT, EXP_C , &sns_input_pcint_callback);
  239.  
  240. }
  241.  
  242. void sns_input_Process(void)
  243. {
  244.     ///TODO: IMPLEMENT DEBOUNCE
  245.     uint8_t index = 0;
  246.     for (index = 0; index < 8; index++) {
  247.         if (pinStatus[index]!= NOCHANGE) {
  248.             StdCan_Msg_t txMsg;
  249.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  250.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  251.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_INPUT;
  252.             txMsg.Header.ModuleId = sns_input_ID;
  253.             txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PINSTATUS;
  254.             txMsg.Length = 2;
  255.             txMsg.Data[0] = index;
  256.             if (pinStatus[index] == HIGH) {
  257.                 txMsg.Data[1] = CAN_MODULE_ENUM_PHYSICAL_PINSTATUS_STATUS_HIGH;
  258.             } else {
  259.                 txMsg.Data[1] = CAN_MODULE_ENUM_PHYSICAL_PINSTATUS_STATUS_LOW;
  260.             }
  261.             pinStatus[index] = NOCHANGE;
  262.             while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  263.         }
  264.     }
  265.  
  266. #if sns_input_CH0PCA95xxIO==1 | sns_input_CH1PCA95xxIO==1 | sns_input_CH2PCA95xxIO==1 | sns_input_CH3PCA95xxIO==1 | sns_input_CH4PCA95xxIO==1 | sns_input_CH5PCA95xxIO==1 | sns_input_CH6PCA95xxIO==1 | sns_input_CH7PCA95xxIO==1
  267.     if (Timer_Expired(sns_input_PCA95XX_POLL_TIMER))
  268.     {
  269.     uint16_t status = Pca95xx_GetInputs();
  270. #ifdef  sns_input_CH0
  271. #if sns_input_CH0PCA95xxIO==1
  272.         /* TODO: går inte så bra med tanke på att modulen inte minns föregående pinstatus!!
  273.         if ((status>>sns_input_CH0)&0x1 != pinStatus[0]) {
  274.             pinStatus[0] = status>>sns_input_CH0)&0x1;
  275.         } else {
  276.             pinStatus[0] = LOW;
  277.         }
  278.         */
  279. #endif
  280. #endif
  281.        
  282.     }
  283. #endif
  284. }
  285.  
  286. void sns_input_HandleMessage(StdCan_Msg_t *rxMsg)
  287. {
  288.    
  289. }
  290.  
  291. void sns_input_List(uint8_t ModuleSequenceNumber)
  292. {
  293.     StdCan_Msg_t txMsg;
  294.    
  295.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  296.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  297.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_INPUT;
  298.     txMsg.Header.ModuleId = sns_input_ID;
  299.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  300.     txMsg.Length = 6;
  301.  
  302.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  303.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  304.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  305.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  306.    
  307.     txMsg.Data[4] = NUMBER_OF_MODULES;
  308.     txMsg.Data[5] = ModuleSequenceNumber;
  309.    
  310.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  311. }
  312.