Subversion Repositories HomeAutomation

Rev

Rev 1516 | Rev 1524 | 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];
1515 arune 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
 
1421 linlun 14
void sns_input_pcint_callback(uint8_t id, uint8_t status)
15
{
16
#ifdef  sns_input_CH0
1514 arune 17
#if sns_input_CH0PCA95xxIO==0
1421 linlun 18
    if (id == sns_input_PCINT_CH0) {
1515 arune 19
      if (IO_LOW != status) {
1421 linlun 20
        pinStatus[0] = HIGH;
21
      } else {
22
        pinStatus[0] = LOW;
23
      }
24
    }
25
#endif
1514 arune 26
#endif
1421 linlun 27
#ifdef  sns_input_CH1
1514 arune 28
#if sns_input_CH1PCA95xxIO==0
1421 linlun 29
    if (id == sns_input_PCINT_CH1) {
1515 arune 30
      if (IO_LOW != status) {
1421 linlun 31
        pinStatus[1] = HIGH;
32
      } else {
33
        pinStatus[1] = LOW;
34
      }
35
    }
36
#endif
1514 arune 37
#endif
1421 linlun 38
#ifdef  sns_input_CH2
1514 arune 39
#if sns_input_CH2PCA95xxIO==0
1421 linlun 40
    if (id == sns_input_PCINT_CH2) {
1515 arune 41
      if (IO_LOW != status) {
1421 linlun 42
        pinStatus[2] = HIGH;
43
      } else {
44
        pinStatus[2] = LOW;
45
      }
46
    }
47
#endif
1514 arune 48
#endif
1421 linlun 49
#ifdef  sns_input_CH3
1514 arune 50
#if sns_input_CH3PCA95xxIO==0
1421 linlun 51
    if (id == sns_input_PCINT_CH3) {
1515 arune 52
      if (IO_LOW != status) {
1421 linlun 53
        pinStatus[3] = HIGH;
54
      } else {
55
        pinStatus[3] = LOW;
56
      }
57
    }
58
#endif
1514 arune 59
#endif
1421 linlun 60
#ifdef  sns_input_CH4
1514 arune 61
#if sns_input_CH4PCA95xxIO==0
1421 linlun 62
    if (id == sns_input_PCINT_CH4) {
1515 arune 63
      if (IO_LOW != status) {
1421 linlun 64
        pinStatus[4] = HIGH;
65
      } else {
66
        pinStatus[4] = LOW;
67
      }
68
    }
69
#endif
1514 arune 70
#endif
1421 linlun 71
#ifdef  sns_input_CH5
1514 arune 72
#if sns_input_CH5PCA95xxIO==0
1421 linlun 73
    if (id == sns_input_PCINT_CH5) {
1515 arune 74
      if (IO_LOW != status) {
1421 linlun 75
        pinStatus[5] = HIGH;
76
      } else {
77
        pinStatus[5] = LOW;
78
      }
79
    }
80
#endif
1514 arune 81
#endif
1421 linlun 82
#ifdef  sns_input_CH6
1514 arune 83
#if sns_input_CH6PCA95xxIO==0
1421 linlun 84
    if (id == sns_input_PCINT_CH6) {
1515 arune 85
      if (IO_LOW != status) {
1421 linlun 86
        pinStatus[6] = HIGH;
87
      } else {
88
        pinStatus[6] = LOW;
89
      }
90
    }
91
#endif
1514 arune 92
#endif
1421 linlun 93
#ifdef  sns_input_CH7
1514 arune 94
#if sns_input_CH7PCA95xxIO==0
1421 linlun 95
    if (id == sns_input_PCINT_CH7) {
1515 arune 96
      if (IO_LOW != status) {
1421 linlun 97
        pinStatus[7] = HIGH;
98
      } else {
99
        pinStatus[7] = LOW;
100
      }
101
    }
102
#endif
1514 arune 103
#endif
1421 linlun 104
}
105
 
1514 arune 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
 
1515 arune 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
 
1421 linlun 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;
1518 arune 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
 
1421 linlun 209
#ifdef  sns_input_CH0
1514 arune 210
#if sns_input_CH0PCA95xxIO==0
1421 linlun 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);
1514 arune 218
#else
219
        Pca95xx_set_in(sns_input_CH0);
220
#endif
1421 linlun 221
        index++;
222
#endif
223
#ifdef  sns_input_CH1
1514 arune 224
#if sns_input_CH1PCA95xxIO==0
1421 linlun 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);
1514 arune 232
#else
233
        Pca95xx_set_in(sns_input_CH1);
234
#endif
1421 linlun 235
        index++;
236
#endif
237
#ifdef  sns_input_CH2
1514 arune 238
#if sns_input_CH2PCA95xxIO==0
1421 linlun 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);
1514 arune 246
#else
247
        Pca95xx_set_in(sns_input_CH2);
248
#endif
1421 linlun 249
        index++;
250
#endif
251
#ifdef  sns_input_CH3
1514 arune 252
#if sns_input_CH3PCA95xxIO==0
1421 linlun 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);
1514 arune 260
#else
261
        Pca95xx_set_in(sns_input_CH3);
262
#endif
1421 linlun 263
        index++;
264
#endif
265
#ifdef  sns_input_CH4
1514 arune 266
#if sns_input_CH4PCA95xxIO==0
1421 linlun 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);
1514 arune 274
#else
275
        Pca95xx_set_in(sns_input_CH4);
276
#endif
1421 linlun 277
        index++;
278
#endif
279
#ifdef  sns_input_CH5
1514 arune 280
#if sns_input_CH5PCA95xxIO==0
1421 linlun 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);
1514 arune 288
#else
289
        Pca95xx_set_in(sns_input_CH5);
290
#endif
1421 linlun 291
        index++;
292
#endif
293
#ifdef  sns_input_CH6
1514 arune 294
#if sns_input_CH6PCA95xxIO==0
1421 linlun 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);
1514 arune 302
#else
303
        Pca95xx_set_in(sns_input_CH6);
304
#endif
1421 linlun 305
        index++;
306
#endif
307
#ifdef  sns_input_CH7
1514 arune 308
#if sns_input_CH7PCA95xxIO==0
1421 linlun 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);
1514 arune 316
#else
317
        Pca95xx_set_in(sns_input_CH7);
318
#endif
1421 linlun 319
        index++;
320
#endif
321
 
1518 arune 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
 
1421 linlun 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++) {
1515 arune 336
        if (pinStatus[index] == LOW || pinStatus[index] == HIGH) {
1421 linlun 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;
1515 arune 347
                pinStatus[index] = HIGH_NOCH;
1421 linlun 348
            } else {
349
                txMsg.Data[1] = CAN_MODULE_ENUM_PHYSICAL_PINSTATUS_STATUS_LOW;
1515 arune 350
                pinStatus[index] = LOW_NOCH;
1421 linlun 351
            }
352
            while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
353
        }
354
    }
1514 arune 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
1515 arune 357
    /* Currently only polling of PCAxx-inputs is implemented */
1514 arune 358
    if (Timer_Expired(sns_input_PCA95XX_POLL_TIMER))
359
    {
1515 arune 360
        uint16_t status = Pca95xx_GetInputs();
1514 arune 361
#ifdef  sns_input_CH0
362
#if sns_input_CH0PCA95xxIO==1
1518 arune 363
        if (pinStatus[0] == LOW_NOCH && ((status>>sns_input_CH0)&0x1) == IO_HIGH) {
1515 arune 364
            pinStatus[0] = HIGH;
1518 arune 365
        } else if (pinStatus[0] == HIGH_NOCH && ((status>>sns_input_CH0)&0x1) == IO_LOW) {
1514 arune 366
            pinStatus[0] = LOW;
367
        }
368
#endif
369
#endif
1515 arune 370
#ifdef  sns_input_CH1
371
#if sns_input_CH1PCA95xxIO==1
1518 arune 372
        if (pinStatus[1] == LOW_NOCH && ((status>>sns_input_CH1)&0x1) == IO_HIGH) {
1515 arune 373
            pinStatus[1] = HIGH;
1518 arune 374
        } else if (pinStatus[1] == HIGH_NOCH && ((status>>sns_input_CH1)&0x1) == IO_LOW) {
1515 arune 375
            pinStatus[1] = LOW;
376
        }
377
#endif
378
#endif
379
#ifdef  sns_input_CH2
380
#if sns_input_CH2PCA95xxIO==1
1518 arune 381
        if (pinStatus[2] == LOW_NOCH && ((status>>sns_input_CH2)&0x1) == IO_HIGH) {
1515 arune 382
            pinStatus[2] = HIGH;
1518 arune 383
        } else if (pinStatus[2] == HIGH_NOCH && ((status>>sns_input_CH2)&0x1) == IO_LOW) {
1515 arune 384
            pinStatus[2] = LOW;
385
        }
386
#endif
387
#endif
388
#ifdef  sns_input_CH3
389
#if sns_input_CH3PCA95xxIO==1
1518 arune 390
        if (pinStatus[3] == LOW_NOCH && ((status>>sns_input_CH3)&0x1) == IO_HIGH) {
1515 arune 391
            pinStatus[3] = HIGH;
1518 arune 392
        } else if (pinStatus[3] == HIGH_NOCH && ((status>>sns_input_CH3)&0x1) == IO_LOW) {
1515 arune 393
            pinStatus[3] = LOW;
394
        }
395
#endif
396
#endif
397
#ifdef  sns_input_CH4
398
#if sns_input_CH4PCA95xxIO==1
1518 arune 399
        if (pinStatus[4] == LOW_NOCH && ((status>>sns_input_CH4)&0x1) == IO_HIGH) {
1515 arune 400
            pinStatus[4] = HIGH;
1518 arune 401
        } else if (pinStatus[4] == HIGH_NOCH && ((status>>sns_input_CH4)&0x1) == IO_LOW) {
1515 arune 402
            pinStatus[4] = LOW;
403
        }
404
#endif
405
#endif
406
#ifdef  sns_input_CH5
407
#if sns_input_CH5PCA95xxIO==1
1518 arune 408
        if (pinStatus[5] == LOW_NOCH && ((status>>sns_input_CH5)&0x1) == IO_HIGH) {
1515 arune 409
            pinStatus[5] = HIGH;
1518 arune 410
        } else if (pinStatus[5] == HIGH_NOCH && ((status>>sns_input_CH5)&0x1) == IO_LOW) {
1515 arune 411
            pinStatus[5] = LOW;
412
        }
413
#endif
414
#endif
415
#ifdef  sns_input_CH6
416
#if sns_input_CH0PCA95xxIO==1
1518 arune 417
        if (pinStatus[6] == LOW_NOCH && ((status>>sns_input_CH6)&0x1) == IO_HIGH) {
1515 arune 418
            pinStatus[6] = HIGH;
1518 arune 419
        } else if (pinStatus[6] == HIGH_NOCH && ((status>>sns_input_CH6)&0x1) == IO_LOW) {
1515 arune 420
            pinStatus[6] = LOW;
421
        }
422
#endif
423
#endif
424
#ifdef  sns_input_CH7
425
#if sns_input_CH7PCA95xxIO==1
1518 arune 426
        if (pinStatus[7] == LOW_NOCH && ((status>>sns_input_CH7)&0x1) == IO_HIGH) {
1515 arune 427
            pinStatus[7] = HIGH;
1518 arune 428
        } else if (pinStatus[7] == HIGH_NOCH && ((status>>sns_input_CH7)&0x1) == IO_LOW) {
1515 arune 429
            pinStatus[7] = LOW;
430
        }
431
#endif
432
#endif
433
 
1514 arune 434
    }
435
#endif 
1421 linlun 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
}