Demo Team / Presence

Dependents:   mbed-IBooth-ETH

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Presence.cpp Source File

Presence.cpp

00001 /*
00002     presence.cpp -  presence sensor library
00003     Developed by Andrea Corrado & Eric Gowland
00004     
00005     Connect to a hardware device that is boolean present/not present. Such as PIR or Rangefinder with appropriate signalling.
00006     Also drives LED as presence indicator.
00007 */
00008 
00009 
00010 #include "mbed.h"
00011 #include "Presence.h"
00012 
00013 
00014 
00015 presence::presence(PinName pin, bool true_on_rise, int debounce_time_ms):_myint(pin), _led1(LED1)
00016 {
00017     debounce_ms = debounce_time_ms;
00018     _true_on_rise = true_on_rise;
00019 //    if(true_on_rise) {
00020 //        _myint.rise(this, &presence::presence_interrupt_off);
00021 //        _myint.fall(this, &presence::presence_interrupt_on);
00022 //    } else {
00023 //        _myint.rise(this, &presence::presence_interrupt_on);
00024 //        _myint.fall(this, &presence::presence_interrupt_off);
00025 //    }
00026     _detection=false;
00027     debounce_timer.start();
00028     _led1=1;
00029 
00030 }
00031 
00032 void presence::presence_interrupt_off(){
00033     if(debounce_timer.read_ms() > debounce_ms) {
00034         _detection=false;
00035         _led1=1;
00036     }
00037 }
00038 
00039 void presence::presence_interrupt_on() //Detection of motion.
00040 {
00041     //Always trigger detection..
00042     _detection=true;
00043     _led1=0;
00044     debounce_timer.reset(); // Reset counter to 0...
00045 }
00046 
00047 
00048 bool presence::isPresent(){
00049     bool new_detection = (_true_on_rise && _myint == 1) || (!_true_on_rise && _myint == 0);
00050     if(new_detection)debounce_timer.reset();
00051     //Poll the pin and update value...
00052     if(!_detection || debounce_timer.read_ms() > debounce_ms) {
00053         _detection = new_detection;
00054         if(_detection) _led1 = 0; else _led1 = 1;
00055     }
00056     return _detection;
00057 }