Subversion Repositories HomeAutomation

Rev

Rev 1516 | Rev 1524 | 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. #define HIGH_NOCH   3
  8. #define LOW_NOCH    4
  9.  
  10. #define IO_HIGH     1
  11. #define IO_LOW      0
  12.  
  13.  
  14. void sns_input_pcint_callback(uint8_t id, uint8_t status)
  15. {
  16. #ifdef  sns_input_CH0
  17. #if sns_input_CH0PCA95xxIO==0
  18.     if (id == sns_input_PCINT_CH0) {
  19.       if (IO_LOW != status) {
  20.         pinStatus[0] = HIGH;
  21.       } else {
  22.         pinStatus[0] = LOW;
  23.       }
  24.     }
  25. #endif
  26. #endif
  27. #ifdef  sns_input_CH1
  28. #if sns_input_CH1PCA95xxIO==0
  29.     if (id == sns_input_PCINT_CH1) {
  30.       if (IO_LOW != status) {
  31.         pinStatus[1] = HIGH;
  32.       } else {
  33.         pinStatus[1] = LOW;
  34.       }
  35.     }
  36. #endif
  37. #endif
  38. #ifdef  sns_input_CH2
  39. #if sns_input_CH2PCA95xxIO==0
  40.     if (id == sns_input_PCINT_CH2) {
  41.       if (IO_LOW != status) {
  42.         pinStatus[2] = HIGH;
  43.       } else {
  44.         pinStatus[2] = LOW;
  45.       }
  46.     }
  47. #endif
  48. #endif
  49. #ifdef  sns_input_CH3
  50. #if sns_input_CH3PCA95xxIO==0
  51.     if (id == sns_input_PCINT_CH3) {
  52.       if (IO_LOW != status) {
  53.         pinStatus[3] = HIGH;
  54.       } else {
  55.         pinStatus[3] = LOW;
  56.       }
  57.     }
  58. #endif
  59. #endif
  60. #ifdef  sns_input_CH4
  61. #if sns_input_CH4PCA95xxIO==0
  62.     if (id == sns_input_PCINT_CH4) {
  63.       if (IO_LOW != status) {
  64.         pinStatus[4] = HIGH;
  65.       } else {
  66.         pinStatus[4] = LOW;
  67.       }
  68.     }
  69. #endif
  70. #endif
  71. #ifdef  sns_input_CH5
  72. #if sns_input_CH5PCA95xxIO==0
  73.     if (id == sns_input_PCINT_CH5) {
  74.       if (IO_LOW != status) {
  75.         pinStatus[5] = HIGH;
  76.       } else {
  77.         pinStatus[5] = LOW;
  78.       }
  79.     }
  80. #endif
  81. #endif
  82. #ifdef  sns_input_CH6
  83. #if sns_input_CH6PCA95xxIO==0
  84.     if (id == sns_input_PCINT_CH6) {
  85.       if (IO_LOW != status) {
  86.         pinStatus[6] = HIGH;
  87.       } else {
  88.         pinStatus[6] = LOW;
  89.       }
  90.     }
  91. #endif
  92. #endif
  93. #ifdef  sns_input_CH7
  94. #if sns_input_CH7PCA95xxIO==0
  95.     if (id == sns_input_PCINT_CH7) {
  96.       if (IO_LOW != status) {
  97.         pinStatus[7] = HIGH;
  98.       } else {
  99.         pinStatus[7] = LOW;
  100.       }
  101.     }
  102. #endif
  103. #endif
  104. }
  105.  
  106. #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
  107. /*void sns_input_PCA95xx_callback(uint16_t status)
  108. {
  109.     printf("0x%x\n",status);
  110. }*/
  111. #endif
  112.  
  113.  
  114. #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
  115. void sns_input_setPCA95xxPinStatus(void)
  116. {
  117.     uint16_t status = Pca95xx_GetInputs();
  118. #ifdef  sns_input_CH0
  119. #if sns_input_CH0PCA95xxIO==1
  120.     if (((status>>sns_input_CH0)&0x1) != IO_LOW) {
  121.         pinStatus[0] = HIGH_NOCH;
  122.     } else {
  123.         pinStatus[0] = LOW_NOCH;
  124.     }
  125. #endif
  126. #endif
  127. #ifdef  sns_input_CH1
  128. #if sns_input_CH1PCA95xxIO==1
  129.     if (((status>>sns_input_CH1)&0x1) != IO_LOW) {
  130.         pinStatus[1] = HIGH_NOCH;
  131.     } else {
  132.         pinStatus[1] = LOW_NOCH;
  133.     }
  134. #endif
  135. #endif
  136. #ifdef  sns_input_CH2
  137. #if sns_input_CH2PCA95xxIO==1
  138.     if (((status>>sns_input_CH2)&0x1) != IO_LOW) {
  139.         pinStatus[2] = HIGH_NOCH;
  140.     } else {
  141.         pinStatus[2] = LOW_NOCH;
  142.     }
  143. #endif
  144. #endif
  145. #ifdef  sns_input_CH3
  146. #if sns_input_CH3PCA95xxIO==1
  147.     if (((status>>sns_input_CH3)&0x1) != IO_LOW) {
  148.         pinStatus[3] = HIGH_NOCH;
  149.     } else {
  150.         pinStatus[3] = LOW_NOCH;
  151.     }
  152. #endif
  153. #endif
  154. #ifdef  sns_input_CH4
  155. #if sns_input_CH4PCA95xxIO==1
  156.     if (((status>>sns_input_CH4)&0x1) != IO_LOW) {
  157.         pinStatus[4] = HIGH_NOCH;
  158.     } else {
  159.         pinStatus[4] = LOW_NOCH;
  160.     }
  161. #endif
  162. #endif
  163. #ifdef  sns_input_CH5
  164. #if sns_input_CH5PCA95xxIO==1
  165.     if (((status>>sns_input_CH5)&0x1) != IO_LOW) {
  166.         pinStatus[5] = HIGH_NOCH;
  167.     } else {
  168.         pinStatus[5] = LOW_NOCH;
  169.     }
  170. #endif
  171. #endif
  172. #ifdef  sns_input_CH6
  173. #if sns_input_CH6PCA95xxIO==1
  174.     if (((status>>sns_input_CH6)&0x1) != IO_LOW) {
  175.         pinStatus[6] = HIGH_NOCH;
  176.     } else {
  177.         pinStatus[6] = LOW_NOCH;
  178.     }
  179. #endif
  180. #endif
  181. #ifdef  sns_input_CH7
  182. #if sns_input_CH7PCA95xxIO==1
  183.     if (((status>>sns_input_CH7)&0x1) != IO_LOW) {
  184.         pinStatus[7] = HIGH_NOCH;
  185.     } else {
  186.         pinStatus[7] = LOW_NOCH;
  187.     }
  188. #endif
  189. #endif
  190. }
  191. #endif
  192.  
  193. void sns_input_Init(void)
  194. {
  195.     pinStatus[0] = NOCHANGE;
  196.     pinStatus[1] = NOCHANGE;
  197.     pinStatus[2] = NOCHANGE;
  198.     pinStatus[3] = NOCHANGE;
  199.     pinStatus[4] = NOCHANGE;
  200.     pinStatus[5] = NOCHANGE;
  201.     pinStatus[6] = NOCHANGE;
  202.     pinStatus[7] = NOCHANGE;
  203.     uint8_t index = 0;
  204.  
  205. #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
  206.     Pca95xx_Init(0);
  207. #endif
  208.  
  209. #ifdef  sns_input_CH0
  210. #if sns_input_CH0PCA95xxIO==0
  211.     #if (sns_input_CH0_pullup == 1)
  212.         gpio_set_pullup(sns_input_CH0);
  213.     #else
  214.         gpio_clr_pullup(sns_input_CH0);
  215.     #endif
  216.         gpio_set_in(sns_input_CH0);
  217.         Pcint_SetCallbackPin(sns_input_PCINT_CH0, sns_input_CH0, &sns_input_pcint_callback);
  218. #else
  219.         Pca95xx_set_in(sns_input_CH0);
  220. #endif
  221.         index++;
  222. #endif
  223. #ifdef  sns_input_CH1
  224. #if sns_input_CH1PCA95xxIO==0
  225.     #if (sns_input_CH1_pullup == 1)
  226.         gpio_set_pullup(sns_input_CH1);
  227.     #else
  228.         gpio_clr_pullup(sns_input_CH1);
  229.     #endif     
  230.         gpio_set_in(sns_input_CH1);
  231.         Pcint_SetCallbackPin(sns_input_PCINT_CH1, sns_input_CH1, &sns_input_pcint_callback);
  232. #else
  233.         Pca95xx_set_in(sns_input_CH1);
  234. #endif
  235.         index++;
  236. #endif
  237. #ifdef  sns_input_CH2
  238. #if sns_input_CH2PCA95xxIO==0
  239.     #if (sns_input_CH2_pullup == 1)
  240.         gpio_set_pullup(sns_input_CH2);
  241.     #else
  242.         gpio_clr_pullup(sns_input_CH2);
  243.     #endif     
  244.         gpio_set_in(sns_input_CH2);
  245.         Pcint_SetCallbackPin(sns_input_PCINT_CH2, sns_input_CH2, &sns_input_pcint_callback);
  246. #else
  247.         Pca95xx_set_in(sns_input_CH2);
  248. #endif
  249.         index++;
  250. #endif
  251. #ifdef  sns_input_CH3
  252. #if sns_input_CH3PCA95xxIO==0
  253.     #if (sns_input_CH3_pullup == 1)
  254.         gpio_set_pullup(sns_input_CH3);
  255.     #else
  256.         gpio_clr_pullup(sns_input_CH3);
  257.     #endif     
  258.         gpio_set_in(sns_input_CH3);
  259.         Pcint_SetCallbackPin(sns_input_PCINT_CH3, sns_input_CH3, &sns_input_pcint_callback);
  260. #else
  261.         Pca95xx_set_in(sns_input_CH3);
  262. #endif
  263.         index++;
  264. #endif
  265. #ifdef  sns_input_CH4
  266. #if sns_input_CH4PCA95xxIO==0
  267.     #if (sns_input_CH4_pullup == 1)
  268.         gpio_set_pullup(sns_input_CH4);
  269.     #else
  270.         gpio_clr_pullup(sns_input_CH4);
  271.     #endif     
  272.         gpio_set_in(sns_input_CH4);
  273.         Pcint_SetCallbackPin(sns_input_PCINT_CH4, sns_input_CH4, &sns_input_pcint_callback);
  274. #else
  275.         Pca95xx_set_in(sns_input_CH4);
  276. #endif
  277.         index++;
  278. #endif
  279. #ifdef  sns_input_CH5
  280. #if sns_input_CH5PCA95xxIO==0
  281.     #if (sns_input_CH5_pullup == 1)
  282.         gpio_set_pullup(sns_input_CH5);
  283.     #else
  284.         gpio_clr_pullup(sns_input_CH5);
  285.     #endif     
  286.         gpio_set_in(sns_input_CH5);
  287.         Pcint_SetCallbackPin(sns_input_PCINT_CH5, sns_input_CH5, &sns_input_pcint_callback);
  288. #else
  289.         Pca95xx_set_in(sns_input_CH5);
  290. #endif
  291.         index++;
  292. #endif
  293. #ifdef  sns_input_CH6
  294. #if sns_input_CH6PCA95xxIO==0
  295.     #if (sns_input_CH6_pullup == 1)
  296.         gpio_set_pullup(sns_input_CH6);
  297.     #else
  298.         gpio_clr_pullup(sns_input_CH6);
  299.     #endif         
  300.         gpio_set_in(sns_input_CH6);
  301.         Pcint_SetCallbackPin(sns_input_PCINT_CH6, sns_input_CH6, &sns_input_pcint_callback);
  302. #else
  303.         Pca95xx_set_in(sns_input_CH6);
  304. #endif
  305.         index++;
  306. #endif
  307. #ifdef  sns_input_CH7
  308. #if sns_input_CH7PCA95xxIO==0
  309.     #if (sns_input_CH7_pullup == 1)
  310.         gpio_set_pullup(sns_input_CH7);
  311.     #else
  312.         gpio_clr_pullup(sns_input_CH7);
  313.     #endif     
  314.         gpio_set_in(sns_input_CH7);
  315.         Pcint_SetCallbackPin(sns_input_PCINT_CH7, sns_input_CH7, &sns_input_pcint_callback);
  316. #else
  317.         Pca95xx_set_in(sns_input_CH7);
  318. #endif
  319.         index++;
  320. #endif
  321.  
  322. #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
  323.     sns_input_setPCA95xxPinStatus();
  324.     /* Currently only polling of ports is implemented */
  325.     //Pca95xx_SetCallback(sns_input_PCA95XX_CALLBACK_INDEX, &sns_input_PCA95xx_callback);
  326.     Timer_SetTimeout(sns_input_PCA95XX_POLL_TIMER, sns_input_PCA95XX_POLL_TIME_MS, TimerTypeFreeRunning, 0);
  327. #endif
  328.  
  329. }
  330.  
  331. void sns_input_Process(void)
  332. {
  333.     ///TODO: IMPLEMENT DEBOUNCE
  334.     uint8_t index = 0;
  335.     for (index = 0; index < 8; index++) {
  336.         if (pinStatus[index] == LOW || pinStatus[index] == HIGH) {
  337.             StdCan_Msg_t txMsg;
  338.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  339.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  340.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_INPUT;
  341.             txMsg.Header.ModuleId = sns_input_ID;
  342.             txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PINSTATUS;
  343.             txMsg.Length = 2;
  344.             txMsg.Data[0] = index;
  345.             if (pinStatus[index] == HIGH) {
  346.                 txMsg.Data[1] = CAN_MODULE_ENUM_PHYSICAL_PINSTATUS_STATUS_HIGH;
  347.                 pinStatus[index] = HIGH_NOCH;
  348.             } else {
  349.                 txMsg.Data[1] = CAN_MODULE_ENUM_PHYSICAL_PINSTATUS_STATUS_LOW;
  350.                 pinStatus[index] = LOW_NOCH;
  351.             }
  352.             while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  353.         }
  354.     }
  355.  
  356. #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
  357.     /* Currently only polling of PCAxx-inputs is implemented */
  358.     if (Timer_Expired(sns_input_PCA95XX_POLL_TIMER))
  359.     {
  360.         uint16_t status = Pca95xx_GetInputs();
  361. #ifdef  sns_input_CH0
  362. #if sns_input_CH0PCA95xxIO==1
  363.         if (pinStatus[0] == LOW_NOCH && ((status>>sns_input_CH0)&0x1) == IO_HIGH) {
  364.             pinStatus[0] = HIGH;
  365.         } else if (pinStatus[0] == HIGH_NOCH && ((status>>sns_input_CH0)&0x1) == IO_LOW) {
  366.             pinStatus[0] = LOW;
  367.         }
  368. #endif
  369. #endif
  370. #ifdef  sns_input_CH1
  371. #if sns_input_CH1PCA95xxIO==1
  372.         if (pinStatus[1] == LOW_NOCH && ((status>>sns_input_CH1)&0x1) == IO_HIGH) {
  373.             pinStatus[1] = HIGH;
  374.         } else if (pinStatus[1] == HIGH_NOCH && ((status>>sns_input_CH1)&0x1) == IO_LOW) {
  375.             pinStatus[1] = LOW;
  376.         }
  377. #endif
  378. #endif
  379. #ifdef  sns_input_CH2
  380. #if sns_input_CH2PCA95xxIO==1
  381.         if (pinStatus[2] == LOW_NOCH && ((status>>sns_input_CH2)&0x1) == IO_HIGH) {
  382.             pinStatus[2] = HIGH;
  383.         } else if (pinStatus[2] == HIGH_NOCH && ((status>>sns_input_CH2)&0x1) == IO_LOW) {
  384.             pinStatus[2] = LOW;
  385.         }
  386. #endif
  387. #endif
  388. #ifdef  sns_input_CH3
  389. #if sns_input_CH3PCA95xxIO==1
  390.         if (pinStatus[3] == LOW_NOCH && ((status>>sns_input_CH3)&0x1) == IO_HIGH) {
  391.             pinStatus[3] = HIGH;
  392.         } else if (pinStatus[3] == HIGH_NOCH && ((status>>sns_input_CH3)&0x1) == IO_LOW) {
  393.             pinStatus[3] = LOW;
  394.         }
  395. #endif
  396. #endif
  397. #ifdef  sns_input_CH4
  398. #if sns_input_CH4PCA95xxIO==1
  399.         if (pinStatus[4] == LOW_NOCH && ((status>>sns_input_CH4)&0x1) == IO_HIGH) {
  400.             pinStatus[4] = HIGH;
  401.         } else if (pinStatus[4] == HIGH_NOCH && ((status>>sns_input_CH4)&0x1) == IO_LOW) {
  402.             pinStatus[4] = LOW;
  403.         }
  404. #endif
  405. #endif
  406. #ifdef  sns_input_CH5
  407. #if sns_input_CH5PCA95xxIO==1
  408.         if (pinStatus[5] == LOW_NOCH && ((status>>sns_input_CH5)&0x1) == IO_HIGH) {
  409.             pinStatus[5] = HIGH;
  410.         } else if (pinStatus[5] == HIGH_NOCH && ((status>>sns_input_CH5)&0x1) == IO_LOW) {
  411.             pinStatus[5] = LOW;
  412.         }
  413. #endif
  414. #endif
  415. #ifdef  sns_input_CH6
  416. #if sns_input_CH0PCA95xxIO==1
  417.         if (pinStatus[6] == LOW_NOCH && ((status>>sns_input_CH6)&0x1) == IO_HIGH) {
  418.             pinStatus[6] = HIGH;
  419.         } else if (pinStatus[6] == HIGH_NOCH && ((status>>sns_input_CH6)&0x1) == IO_LOW) {
  420.             pinStatus[6] = LOW;
  421.         }
  422. #endif
  423. #endif
  424. #ifdef  sns_input_CH7
  425. #if sns_input_CH7PCA95xxIO==1
  426.         if (pinStatus[7] == LOW_NOCH && ((status>>sns_input_CH7)&0x1) == IO_HIGH) {
  427.             pinStatus[7] = HIGH;
  428.         } else if (pinStatus[7] == HIGH_NOCH && ((status>>sns_input_CH7)&0x1) == IO_LOW) {
  429.             pinStatus[7] = LOW;
  430.         }
  431. #endif
  432. #endif
  433.  
  434.     }
  435. #endif
  436. }
  437.  
  438. void sns_input_HandleMessage(StdCan_Msg_t *rxMsg)
  439. {
  440.    
  441. }
  442.  
  443. void sns_input_List(uint8_t ModuleSequenceNumber)
  444. {
  445.     StdCan_Msg_t txMsg;
  446.    
  447.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  448.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  449.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_INPUT;
  450.     txMsg.Header.ModuleId = sns_input_ID;
  451.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  452.     txMsg.Length = 6;
  453.  
  454.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  455.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  456.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  457.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  458.    
  459.     txMsg.Data[4] = NUMBER_OF_MODULES;
  460.     txMsg.Data[5] = ModuleSequenceNumber;
  461.    
  462.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  463. }
  464.