Rev 1482 | Rev 1515 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1421 | linlun | 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 |
||
| 1514 | arune | 10 | #if sns_input_CH0PCA95xxIO==0 |
| 1421 | linlun | 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 |
||
| 1514 | arune | 19 | #endif |
| 1421 | linlun | 20 | #ifdef sns_input_CH1 |
| 1514 | arune | 21 | #if sns_input_CH1PCA95xxIO==0 |
| 1421 | linlun | 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 |
||
| 1514 | arune | 30 | #endif |
| 1421 | linlun | 31 | #ifdef sns_input_CH2 |
| 1514 | arune | 32 | #if sns_input_CH2PCA95xxIO==0 |
| 1421 | linlun | 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 |
||
| 1514 | arune | 41 | #endif |
| 1421 | linlun | 42 | #ifdef sns_input_CH3 |
| 1514 | arune | 43 | #if sns_input_CH3PCA95xxIO==0 |
| 1421 | linlun | 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 |
||
| 1514 | arune | 52 | #endif |
| 1421 | linlun | 53 | #ifdef sns_input_CH4 |
| 1514 | arune | 54 | #if sns_input_CH4PCA95xxIO==0 |
| 1421 | linlun | 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 |
||
| 1514 | arune | 63 | #endif |
| 1421 | linlun | 64 | #ifdef sns_input_CH5 |
| 1514 | arune | 65 | #if sns_input_CH5PCA95xxIO==0 |
| 1421 | linlun | 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 |
||
| 1514 | arune | 74 | #endif |
| 1421 | linlun | 75 | #ifdef sns_input_CH6 |
| 1514 | arune | 76 | #if sns_input_CH6PCA95xxIO==0 |
| 1421 | linlun | 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 |
||
| 1514 | arune | 85 | #endif |
| 1421 | linlun | 86 | #ifdef sns_input_CH7 |
| 1514 | arune | 87 | #if sns_input_CH7PCA95xxIO==0 |
| 1421 | linlun | 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 |
||
| 1514 | arune | 96 | #endif |
| 1421 | linlun | 97 | } |
| 98 | |||
| 1514 | arune | 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 | |||
| 1421 | linlun | 107 | void sns_input_Init(void) |
| 108 | { |
||
| 1514 | arune | 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 | |||
| 1421 | linlun | 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 |
||
| 1514 | arune | 126 | #if sns_input_CH0PCA95xxIO==0 |
| 1421 | linlun | 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); |
||
| 1514 | arune | 134 | #else |
| 135 | Pca95xx_set_in(sns_input_CH0); |
||
| 136 | #endif |
||
| 1421 | linlun | 137 | index++; |
| 138 | #endif |
||
| 139 | #ifdef sns_input_CH1 |
||
| 1514 | arune | 140 | #if sns_input_CH1PCA95xxIO==0 |
| 1421 | linlun | 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); |
||
| 1514 | arune | 148 | #else |
| 149 | Pca95xx_set_in(sns_input_CH1); |
||
| 150 | #endif |
||
| 1421 | linlun | 151 | index++; |
| 152 | #endif |
||
| 153 | #ifdef sns_input_CH2 |
||
| 1514 | arune | 154 | #if sns_input_CH2PCA95xxIO==0 |
| 1421 | linlun | 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); |
||
| 1514 | arune | 162 | #else |
| 163 | Pca95xx_set_in(sns_input_CH2); |
||
| 164 | #endif |
||
| 1421 | linlun | 165 | index++; |
| 166 | #endif |
||
| 167 | #ifdef sns_input_CH3 |
||
| 1514 | arune | 168 | #if sns_input_CH3PCA95xxIO==0 |
| 1421 | linlun | 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); |
||
| 1514 | arune | 176 | #else |
| 177 | Pca95xx_set_in(sns_input_CH3); |
||
| 178 | #endif |
||
| 1421 | linlun | 179 | index++; |
| 180 | #endif |
||
| 181 | #ifdef sns_input_CH4 |
||
| 1514 | arune | 182 | #if sns_input_CH4PCA95xxIO==0 |
| 1421 | linlun | 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); |
||
| 1514 | arune | 190 | #else |
| 191 | Pca95xx_set_in(sns_input_CH4); |
||
| 192 | #endif |
||
| 1421 | linlun | 193 | index++; |
| 194 | #endif |
||
| 195 | #ifdef sns_input_CH5 |
||
| 1514 | arune | 196 | #if sns_input_CH5PCA95xxIO==0 |
| 1421 | linlun | 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); |
||
| 1514 | arune | 204 | #else |
| 205 | Pca95xx_set_in(sns_input_CH5); |
||
| 206 | #endif |
||
| 1421 | linlun | 207 | index++; |
| 208 | #endif |
||
| 209 | #ifdef sns_input_CH6 |
||
| 1514 | arune | 210 | #if sns_input_CH6PCA95xxIO==0 |
| 1421 | linlun | 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); |
||
| 1514 | arune | 218 | #else |
| 219 | Pca95xx_set_in(sns_input_CH6); |
||
| 220 | #endif |
||
| 1421 | linlun | 221 | index++; |
| 222 | #endif |
||
| 223 | #ifdef sns_input_CH7 |
||
| 1514 | arune | 224 | #if sns_input_CH7PCA95xxIO==0 |
| 1421 | linlun | 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); |
||
| 1514 | arune | 232 | #else |
| 233 | Pca95xx_set_in(sns_input_CH7); |
||
| 234 | #endif |
||
| 1421 | linlun | 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 | } |
||
| 1482 | linlun | 261 | pinStatus[index] = NOCHANGE; |
| 1421 | linlun | 262 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
| 263 | } |
||
| 264 | } |
||
| 1514 | arune | 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 |
||
| 1421 | linlun | 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 | } |