Subversion Repositories HomeAutomation

Rev

Rev 1745 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1745 Rev 1752
Line 1... Line 1...
1
#include "sns_PIR.h"
1
#include "sns_PIR.h"
2
 
2
 
3
#define ABS(a) ((a) < 0 ? (-1*a) : (a))
3
#define ABS(a) ((a) < 0 ? (-a) : (a))
4
 
4
 
5
 
5
 
6
// a weighted moving avarage value is used to remove "DC offset"
6
// a weighted moving avarage value is used to remove "DC offset"
7
static uint32_t zeroOffset = 0;
7
static uint32_t zeroOffset = 0;
8
 
8
 
9
// a second avarage value is used to detect changes in avarage "energy"
9
// a second avarage value is used to detect changes in avarage "energy"
10
#define NR_AVG_VALUES   15
10
#define NR_AVG_VALUES   15
11
static int8_t avgValues[NR_AVG_VALUES];
11
static int16_t avgValues[NR_AVG_VALUES];
12
static uint8_t avgIndex = 0;
12
static uint8_t avgIndex = 0;
13
 
13
 
14
 
14
 
15
void sns_PIR_Init(void)
15
void sns_PIR_Init(void)
16
{
16
{
Line 21... Line 21...
21
}
21
}
22
 
22
 
23
 
23
 
24
void sns_PIR_Process(void)
24
void sns_PIR_Process(void)
25
{
25
{
26
    uint8_t motionDetect;
26
    static uint8_t motionDetect = 0;
27
    uint16_t brightness;
27
    uint16_t brightness;
28
    if (Timer_Expired(sns_PIR_SAMPLE_TIMER)) {
28
    if (Timer_Expired(sns_PIR_SAMPLE_TIMER)) {
29
        /**
29
        /**
30
         * Read PIR sensor (via external ADC)
30
         * Read PIR sensor (via external ADC)
31
         */
31
         */
Line 53... Line 53...
53
       
53
       
54
        /* Or to keep motiondetect==1 true for at least one CAN send period, so detections are never lost */
54
        /* Or to keep motiondetect==1 true for at least one CAN send period, so detections are never lost */
55
        motionDetect |= (avgValue >= 80);
55
        motionDetect |= (avgValue >= 80);
56
       
56
       
57
    }
57
    }
58
    else if (Timer_Expired(sns_PIR_SEND_TIMER)) {
58
    if (Timer_Expired(sns_PIR_SEND_TIMER)) {
59
        /**
59
        /**
60
         * Read brightness sensor (via internal ADC).
60
         * Read brightness sensor (via internal ADC).
61
         */
61
         */
62
        brightness = ADC_Get(sns_PIR_BRIGHTNESS_AD);
62
        brightness = ADC_Get(sns_PIR_BRIGHTNESS_AD);
63
 
63