This is a very simple way to count DigitalIn rising by using Ticker Interrupts.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Pswitch.cpp Source File

Pswitch.cpp

00001 
00002 
00003 #include "Pswitch.h"
00004  #include "mbed.h"
00005  
00006  /*
00007   * Constructor
00008   */
00009  Pswitch::Pswitch(PinName in) 
00010      : _in(in) {    
00011          
00012      // reset all the flags and counters    
00013      _samples = 0;
00014      _output = 0;
00015      _output_last = 0;
00016      _rising_flag = 0;
00017      
00018      // Attach ticker
00019      _ticker.attach(this, &Pswitch::_sample, 0.005);     
00020  }
00021    
00022  void Pswitch::_sample() {
00023  
00024 
00025     _output_last = _output;
00026     _output = _in;
00027     if (!_output && _output_last)
00028     {
00029         _rising_flag++;
00030     }
00031 
00032 }
00033 
00034  
00035  
00036  
00037  // return number of rising edges
00038  int Pswitch::count(void) {
00039      int return_value = _rising_flag; 
00040      _rising_flag = 0;
00041      return(return_value);
00042  }
00043  
00044  // return the debounced status
00045  int Pswitch::read(void) {
00046      return(_in);
00047  }
00048  
00049  // shorthand for read()
00050  Pswitch::operator int() {
00051      return read();
00052  }