Subversion Repositories HomeAutomation

Rev

Rev 1515 | Rev 1518 | 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
{
1514 arune 195
 
196
#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
197
    Pca95xx_Init(0);
1515 arune 198
    /* Currently only polling of ports is implemented */
1514 arune 199
    //Pca95xx_SetCallback(sns_input_PCA95XX_CALLBACK_INDEX, &sns_input_PCA95xx_callback);
1516 arune 200
    Timer_SetTimeout(sns_input_PCA95XX_POLL_TIMER, sns_input_PCA95XX_POLL_TIME_MS, TimerTypeFreeRunning, 0);
1514 arune 201
#endif 
202
 
1421 linlun 203
    pinStatus[0] = NOCHANGE;
204
    pinStatus[1] = NOCHANGE;
205
    pinStatus[2] = NOCHANGE;
206
    pinStatus[3] = NOCHANGE;
207
    pinStatus[4] = NOCHANGE;
208
    pinStatus[5] = NOCHANGE;
209
    pinStatus[6] = NOCHANGE;
210
    pinStatus[7] = NOCHANGE;
211
    uint8_t index = 0;
212
#ifdef  sns_input_CH0
1514 arune 213
#if sns_input_CH0PCA95xxIO==0
1421 linlun 214
    #if (sns_input_CH0_pullup == 1)
215
        gpio_set_pullup(sns_input_CH0);
216
    #else
217
        gpio_clr_pullup(sns_input_CH0);
218
    #endif
219
        gpio_set_in(sns_input_CH0);
220
        Pcint_SetCallbackPin(sns_input_PCINT_CH0, sns_input_CH0, &sns_input_pcint_callback);
1514 arune 221
#else
222
        Pca95xx_set_in(sns_input_CH0);
223
#endif
1421 linlun 224
        index++;
225
#endif
226
#ifdef  sns_input_CH1
1514 arune 227
#if sns_input_CH1PCA95xxIO==0
1421 linlun 228
    #if (sns_input_CH1_pullup == 1)
229
        gpio_set_pullup(sns_input_CH1);
230
    #else
231
        gpio_clr_pullup(sns_input_CH1);
232
    #endif      
233
        gpio_set_in(sns_input_CH1);
234
        Pcint_SetCallbackPin(sns_input_PCINT_CH1, sns_input_CH1, &sns_input_pcint_callback);
1514 arune 235
#else
236
        Pca95xx_set_in(sns_input_CH1);
237
#endif
1421 linlun 238
        index++;
239
#endif
240
#ifdef  sns_input_CH2
1514 arune 241
#if sns_input_CH2PCA95xxIO==0
1421 linlun 242
    #if (sns_input_CH2_pullup == 1)
243
        gpio_set_pullup(sns_input_CH2);
244
    #else
245
        gpio_clr_pullup(sns_input_CH2);
246
    #endif      
247
        gpio_set_in(sns_input_CH2);
248
        Pcint_SetCallbackPin(sns_input_PCINT_CH2, sns_input_CH2, &sns_input_pcint_callback);
1514 arune 249
#else
250
        Pca95xx_set_in(sns_input_CH2);
251
#endif
1421 linlun 252
        index++;
253
#endif
254
#ifdef  sns_input_CH3
1514 arune 255
#if sns_input_CH3PCA95xxIO==0
1421 linlun 256
    #if (sns_input_CH3_pullup == 1)
257
        gpio_set_pullup(sns_input_CH3);
258
    #else
259
        gpio_clr_pullup(sns_input_CH3);
260
    #endif      
261
        gpio_set_in(sns_input_CH3);
262
        Pcint_SetCallbackPin(sns_input_PCINT_CH3, sns_input_CH3, &sns_input_pcint_callback);
1514 arune 263
#else
264
        Pca95xx_set_in(sns_input_CH3);
265
#endif
1421 linlun 266
        index++;
267
#endif
268
#ifdef  sns_input_CH4
1514 arune 269
#if sns_input_CH4PCA95xxIO==0
1421 linlun 270
    #if (sns_input_CH4_pullup == 1)
271
        gpio_set_pullup(sns_input_CH4);
272
    #else
273
        gpio_clr_pullup(sns_input_CH4);
274
    #endif      
275
        gpio_set_in(sns_input_CH4);
276
        Pcint_SetCallbackPin(sns_input_PCINT_CH4, sns_input_CH4, &sns_input_pcint_callback);
1514 arune 277
#else
278
        Pca95xx_set_in(sns_input_CH4);
279
#endif
1421 linlun 280
        index++;
281
#endif
282
#ifdef  sns_input_CH5
1514 arune 283
#if sns_input_CH5PCA95xxIO==0
1421 linlun 284
    #if (sns_input_CH5_pullup == 1)
285
        gpio_set_pullup(sns_input_CH5);
286
    #else
287
        gpio_clr_pullup(sns_input_CH5);
288
    #endif      
289
        gpio_set_in(sns_input_CH5);
290
        Pcint_SetCallbackPin(sns_input_PCINT_CH5, sns_input_CH5, &sns_input_pcint_callback);
1514 arune 291
#else
292
        Pca95xx_set_in(sns_input_CH5);
293
#endif
1421 linlun 294
        index++;
295
#endif
296
#ifdef  sns_input_CH6
1514 arune 297
#if sns_input_CH6PCA95xxIO==0
1421 linlun 298
    #if (sns_input_CH6_pullup == 1)
299
        gpio_set_pullup(sns_input_CH6);
300
    #else
301
        gpio_clr_pullup(sns_input_CH6);
302
    #endif          
303
        gpio_set_in(sns_input_CH6);
304
        Pcint_SetCallbackPin(sns_input_PCINT_CH6, sns_input_CH6, &sns_input_pcint_callback);
1514 arune 305
#else
306
        Pca95xx_set_in(sns_input_CH6);
307
#endif
1421 linlun 308
        index++;
309
#endif
310
#ifdef  sns_input_CH7
1514 arune 311
#if sns_input_CH7PCA95xxIO==0
1421 linlun 312
    #if (sns_input_CH7_pullup == 1)
313
        gpio_set_pullup(sns_input_CH7);
314
    #else
315
        gpio_clr_pullup(sns_input_CH7);
316
    #endif      
317
        gpio_set_in(sns_input_CH7);
318
        Pcint_SetCallbackPin(sns_input_PCINT_CH7, sns_input_CH7, &sns_input_pcint_callback);
1514 arune 319
#else
320
        Pca95xx_set_in(sns_input_CH7);
321
#endif
1421 linlun 322
        index++;
323
#endif
324
 
325
}
326
 
327
void sns_input_Process(void)
328
{
329
    ///TODO: IMPLEMENT DEBOUNCE
330
    uint8_t index = 0;
331
    for (index = 0; index < 8; index++) {
1515 arune 332
        if (pinStatus[index] == LOW || pinStatus[index] == HIGH) {
1421 linlun 333
            StdCan_Msg_t txMsg;
334
            StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
335
            StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
336
            txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_INPUT;
337
            txMsg.Header.ModuleId = sns_input_ID;
338
            txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PINSTATUS;
339
            txMsg.Length = 2;
340
            txMsg.Data[0] = index;
341
            if (pinStatus[index] == HIGH) {
342
                txMsg.Data[1] = CAN_MODULE_ENUM_PHYSICAL_PINSTATUS_STATUS_HIGH;
1515 arune 343
                pinStatus[index] = HIGH_NOCH;
1421 linlun 344
            } else {
345
                txMsg.Data[1] = CAN_MODULE_ENUM_PHYSICAL_PINSTATUS_STATUS_LOW;
1515 arune 346
                pinStatus[index] = LOW_NOCH;
1421 linlun 347
            }
348
            while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
349
        }
350
    }
1514 arune 351
 
352
#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 353
    /* Currently only polling of PCAxx-inputs is implemented */
1514 arune 354
    if (Timer_Expired(sns_input_PCA95XX_POLL_TIMER))
355
    {
1515 arune 356
        uint16_t status = Pca95xx_GetInputs();
1514 arune 357
#ifdef  sns_input_CH0
358
#if sns_input_CH0PCA95xxIO==1
1515 arune 359
        if (pinStatus[0] == LOW_NOCH && ((status>>sns_input_CH0)&0x1) == HIGH) {
360
            pinStatus[0] = HIGH;
361
        } else if (pinStatus[0] == HIGH_NOCH && ((status>>sns_input_CH0)&0x1) == LOW) {
1514 arune 362
            pinStatus[0] = LOW;
363
        }
364
#endif
365
#endif
1515 arune 366
#ifdef  sns_input_CH1
367
#if sns_input_CH1PCA95xxIO==1
368
        if (pinStatus[1] == LOW_NOCH && ((status>>sns_input_CH1)&0x1) == HIGH) {
369
            pinStatus[1] = HIGH;
370
        } else if (pinStatus[1] == HIGH_NOCH && ((status>>sns_input_CH1)&0x1) == LOW) {
371
            pinStatus[1] = LOW;
372
        }
373
#endif
374
#endif
375
#ifdef  sns_input_CH2
376
#if sns_input_CH2PCA95xxIO==1
377
        if (pinStatus[2] == LOW_NOCH && ((status>>sns_input_CH2)&0x1) == HIGH) {
378
            pinStatus[2] = HIGH;
379
        } else if (pinStatus[2] == HIGH_NOCH && ((status>>sns_input_CH2)&0x1) == LOW) {
380
            pinStatus[2] = LOW;
381
        }
382
#endif
383
#endif
384
#ifdef  sns_input_CH3
385
#if sns_input_CH3PCA95xxIO==1
386
        if (pinStatus[3] == LOW_NOCH && ((status>>sns_input_CH3)&0x1) == HIGH) {
387
            pinStatus[3] = HIGH;
388
        } else if (pinStatus[3] == HIGH_NOCH && ((status>>sns_input_CH3)&0x1) == LOW) {
389
            pinStatus[3] = LOW;
390
        }
391
#endif
392
#endif
393
#ifdef  sns_input_CH4
394
#if sns_input_CH4PCA95xxIO==1
395
        if (pinStatus[4] == LOW_NOCH && ((status>>sns_input_CH4)&0x1) == HIGH) {
396
            pinStatus[4] = HIGH;
397
        } else if (pinStatus[4] == HIGH_NOCH && ((status>>sns_input_CH4)&0x1) == LOW) {
398
            pinStatus[4] = LOW;
399
        }
400
#endif
401
#endif
402
#ifdef  sns_input_CH5
403
#if sns_input_CH5PCA95xxIO==1
404
        if (pinStatus[5] == LOW_NOCH && ((status>>sns_input_CH5)&0x1) == HIGH) {
405
            pinStatus[5] = HIGH;
406
        } else if (pinStatus[5] == HIGH_NOCH && ((status>>sns_input_CH5)&0x1) == LOW) {
407
            pinStatus[5] = LOW;
408
        }
409
#endif
410
#endif
411
#ifdef  sns_input_CH6
412
#if sns_input_CH0PCA95xxIO==1
413
        if (pinStatus[6] == LOW_NOCH && ((status>>sns_input_CH6)&0x1) == HIGH) {
414
            pinStatus[6] = HIGH;
415
        } else if (pinStatus[6] == HIGH_NOCH && ((status>>sns_input_CH6)&0x1) == LOW) {
416
            pinStatus[6] = LOW;
417
        }
418
#endif
419
#endif
420
#ifdef  sns_input_CH7
421
#if sns_input_CH7PCA95xxIO==1
422
        if (pinStatus[7] == LOW_NOCH && ((status>>sns_input_CH7)&0x1) == HIGH) {
423
            pinStatus[7] = HIGH;
424
        } else if (pinStatus[7] == HIGH_NOCH && ((status>>sns_input_CH7)&0x1) == LOW) {
425
            pinStatus[7] = LOW;
426
        }
427
#endif
428
#endif
429
 
1514 arune 430
    }
431
#endif 
1421 linlun 432
}
433
 
434
void sns_input_HandleMessage(StdCan_Msg_t *rxMsg)
435
{
436
 
437
}
438
 
439
void sns_input_List(uint8_t ModuleSequenceNumber)
440
{
441
    StdCan_Msg_t txMsg;
442
 
443
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
444
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
445
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_INPUT;
446
    txMsg.Header.ModuleId = sns_input_ID;
447
    txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
448
    txMsg.Length = 6;
449
 
450
    txMsg.Data[0] = NODE_HW_ID_BYTE0;
451
    txMsg.Data[1] = NODE_HW_ID_BYTE1;
452
    txMsg.Data[2] = NODE_HW_ID_BYTE2;
453
    txMsg.Data[3] = NODE_HW_ID_BYTE3;
454
 
455
    txMsg.Data[4] = NUMBER_OF_MODULES;
456
    txMsg.Data[5] = ModuleSequenceNumber;
457
 
458
    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
459
}