/** * * SWITCH.cpp * Jan Dvorak z Vozerovic - dvorkaman@centrum.cz - dvorkaman.php5.cz */ #ifndef switch_cpp #define switch_cpp #include "WProgram.h" #include "switch.h" /*************************************************** /** * Constructor * @param int input pin; minimal press time = 10ms */ Switch::Switch(int pin) { inputPin=pin; minTime=10; init(); } /*************************************************** /** * Constructor * @param {int,int} input pin, minimal switch press time [ms] */ Switch::Switch(int pin,int minPressTime) { inputPin=pin; if(minPressTime>=0)minTime=minPressTime; init(); } /*************************************************** /** * Inicialize input pin, implicit values * PRIVATE */ inline void Switch::init() { pinMode(inputPin,INPUT); pressed1=false; pressed2=false; pressTime=-1; //not pressed yet firstPress=false; } /*************************************************** /** * All necessery calculations * PRIVATE */ void Switch::makeCalculations() { state=digitalRead(inputPin); //button is pressed if( state==1 ) { //was button pressed before?? if( firstPress==false ) { pressTime=millis(); //actual system time firstPress=true; } //evaluation of pressing if( pressed1==false ) //NOT ELSE PART, becouse minTime can be to small { long m=millis(); if( (m-pressTime)>=minTime ) { //button pressed for minimal amount of time pressed1=true; pressed2=true; //first cycle of momentary switch => ON } }else if( pressed2==true )pressed2=false; //pressed1=true; => 2.+ cycle => momentary switch OFF //switch is OFF => clear registers }else{ //clear if neccessery if( pressed1==true ) { pressTime=-1; // pressed1=false; pressed2=false; firstPress=false; } } } /*************************************************** /** * Switch as momentary one * @returns boolean is momentary pressed - true for one processor cycle */ boolean Switch::isMomentaryPressed() { makeCalculations(); return pressed2; } /*************************************************** /** * Switch as ordinary one * @returns boolean is pressed - true while pressed */ boolean Switch::isPressed() { makeCalculations(); return pressed1; } #endif