Driver for a binary on/off presence device
Presence.cpp
- Committer:
- erigow01
- Date:
- 2015-02-09
- Revision:
- 5:1d5ca464ac89
- Parent:
- 0:21320a151be0
- Child:
- 6:b1c665320f0c
File content as of revision 5:1d5ca464ac89:
/* Presence.cpp - presence sensor library Developed by Andrea Corrado & Eric Gowland Connect to a hardware device that is boolean present/not present. Such as PIR or Rangefinder with appropriate signalling. Also drives LED as presence indicator. */ #include "mbed.h" #include "Presence.h" Presence::Presence(PinName pin, bool true_on_rise, int debounce_time_ms):_myint(pin), _led1(LED1) { debounce_ms = debounce_time_ms; _true_on_rise = true_on_rise; // if(true_on_rise) { // _myint.rise(this, &Presence::presence_interrupt_off); // _myint.fall(this, &Presence::presence_interrupt_on); // } else { // _myint.rise(this, &Presence::presence_interrupt_on); // _myint.fall(this, &Presence::presence_interrupt_off); // } _detection=false; debounce_timer.start(); _led1=1; } void Presence::presence_interrupt_off(){ if(debounce_timer.read_ms() > debounce_ms) { _detection=false; _led1=1; } } void Presence::presence_interrupt_on() //Detection of motion. { //Always trigger detection.. _detection=true; _led1=0; debounce_timer.reset(); // Reset counter to 0... } bool Presence::isPresent(){ bool new_detection = (_true_on_rise && _myint == 1) || (!_true_on_rise && _myint == 0); if(new_detection)debounce_timer.reset(); //Poll the pin and update value... if(!_detection || debounce_timer.read_ms() > debounce_ms) { _detection = new_detection; if(_detection) _led1 = 0; else _led1 = 1; } return _detection; }