Subversion Repositories HomeAutomation

Rev

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

Rev 1477 Rev 1481
Line 12... Line 12...
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
{
-
 
17
    ADC_Init();
17
    ltc2450_init();
18
    ltc2450_init();
-
 
19
    Timer_SetTimeout(sns_PIR_SAMPLE_TIMER,  sns_PIR_SAMPLE_PERIOD,  TimerTypeFreeRunning, 0);
18
    Timer_SetTimeout(sns_PIR_BRIGHT_TIMER, 40, TimerTypeFreeRunning, 0);
20
    Timer_SetTimeout(sns_PIR_SEND_TIMER,    sns_PIR_SEND_PERIOD,    TimerTypeFreeRunning, 0);
19
}
21
}
20
 
22
 
21
 
23
 
22
void sns_PIR_Process(void)
24
void sns_PIR_Process(void)
23
{
25
{
24
    static uint8_t printCounter = 0;
26
    uint8_t motionDetect;
-
 
27
    uint16_t brightness;
25
    if (Timer_Expired(sns_PIR_BRIGHT_TIMER)) {
28
    if (Timer_Expired(sns_PIR_SAMPLE_TIMER)) {
-
 
29
        /**
-
 
30
         * Read PIR sensor (via external ADC)
-
 
31
         */
26
        uint16_t pirValue;
32
        uint16_t pirValue;
27
        do {
33
        do {
28
            // if ADC is busy, returned value is 0xFFFF
34
            // if ADC is busy, returned value is 0xFFFF
29
            pirValue = ltc2450_read();
35
            pirValue = ltc2450_read();
30
        } while (pirValue >= 0xFFFF);
36
        } while (pirValue >= 0xFFFF);
31
       
-
 
32
        // first time?
37
        // first time?
33
        if (zeroOffset == 0) {
38
        if (zeroOffset == 0) {
34
            zeroOffset = pirValue;
39
            zeroOffset = pirValue;
35
        }
40
        }
36
        else {
41
        else {
37
            zeroOffset = (zeroOffset*4 + (uint32_t)pirValue)/5;
42
            zeroOffset = (zeroOffset*4 + (uint32_t)pirValue)/5;
38
        }
43
        }
39
       
-
 
40
        int8_t test = (int8_t)((int32_t)pirValue - (int32_t)zeroOffset);
44
        int8_t test = (int8_t)((int32_t)pirValue - (int32_t)zeroOffset);
41
        avgValues[avgIndex++] = test;
45
        avgValues[avgIndex++] = test;
42
        if (avgIndex >= NR_AVG_VALUES) {
46
        if (avgIndex >= NR_AVG_VALUES) {
43
            avgIndex = 0;
47
            avgIndex = 0;
44
        }
48
        }
45
        uint16_t avgValue = 0;
49
        uint16_t avgValue = 0;
46
        for (uint8_t i=0; i<NR_AVG_VALUES; i++) {
50
        for (uint8_t i=0; i<NR_AVG_VALUES; i++) {
47
            avgValue += ABS(avgValues[i]);
51
            avgValue += ABS(avgValues[i]);
48
        }
52
        }
-
 
53
        // TODO: keep motiondetect==1 true for at least one CAN send period, so detections are never lost
49
        uint8_t motionDetect = (avgValue >= 80);
54
        motionDetect = (avgValue >= 80);
50
       
55
       
51
               
56
        /**
52
        if (printCounter++ >= 200/40) {
57
         * Read brightness sensor (via internal ADC).
53
            printCounter = 0;
58
         */
54
            //printf("AD:%u\n", pirValue);
59
        brightness = ADC_Get(sns_PIR_BRIGHTNESS_AD);
-
 
60
    }
-
 
61
    else if (Timer_Expired(sns_PIR_SEND_TIMER)) {
-
 
62
        /**
55
            printf("Motion: %u\n", motionDetect);
63
         * Time to send CAN message.
56
           
64
         */
57
            /*StdCan_Msg_t txMsg;
65
        StdCan_Msg_t txMsg;
58
            StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
66
        StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
59
            StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
67
        StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
60
            txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_PIR;
68
        txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_PIR;
61
            txMsg.Header.ModuleId = sns_PIR_ID;
69
        txMsg.Header.ModuleId = sns_PIR_ID;
62
            txMsg.Header.Command = CAN_MODULE_CMD_PIR_MOTION;
70
        txMsg.Header.Command = CAN_MODULE_CMD_PIR_MOTION;
63
            txMsg.Length = 3;
71
        txMsg.Length = 4;
64
            txMsg.Data[0] = 0;
72
        txMsg.Data[0] = 0;
65
            txMsg.Data[1] = (uint8_t)((pirValue & 0xFF00) >> 2);
73
        txMsg.Data[1] = (uint8_t)((brightness&0xFF00) >> 8);
66
            txMsg.Data[2] = (uint8_t)((pirValue & 0x00FF) >> 0);
74
        txMsg.Data[2] = (uint8_t)((brightness&0x00FF) >> 0);
-
 
75
        txMsg.Data[3] = motionDetect;
67
            StdCan_Put(&txMsg);*/
76
        StdCan_Put(&txMsg);
68
        }
77
        // also print data to debug
-
 
78
        printf("M:%u,B:%d\n", motionDetect, brightness);
69
    }
79
    }
70
}
80
}
71
 
81
 
72
void sns_PIR_HandleMessage(StdCan_Msg_t *rxMsg)
82
void sns_PIR_HandleMessage(StdCan_Msg_t *rxMsg)
73
{
83
{