Niksa Zupcic / RotaryEncoder
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RotaryEncoder.h Source File

RotaryEncoder.h

Go to the documentation of this file.
00001 /**
00002  * @file    RotaryEncoder.h
00003  * @brief   Rotary encoder with one interrupt and one digital input
00004  * @author  Nikša Zupčić
00005  * @version 1.0
00006  *
00007  * Copyright (c) 2022
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #ifndef ROTARY_ENCODER_H
00023 #define ROTARY_ENCODER_H
00024 
00025 #include "mbed.h"
00026 
00027 /** Incremental rotary encoder with a push button
00028  *
00029  * Can be used as only an incremental rotary encoder, with/without a pushbutton
00030  * and the PinMode can be set if needed.
00031  *
00032  * Example:
00033  * @code
00034  * #include "RotaryEncoder.h"
00035  *
00036  * RotaryEncoder re(p5, p6);                   // Default
00037  * //RotaryEncoder re(p5, p6, PullDown);       // With pull mode specified
00038  * //RotaryEncoder re(p5, p6, p7);             // With Switch
00039  * //RotaryEncoder re(p5, p6, p7, PullDown);   // Switch + pull mode specified
00040  *
00041  * Serial pc(USBTX, USBRX);                    // Serial PC connection
00042  *
00043  * int main(){
00044  *  double value = re.Value;
00045  *  pc.printf("\nEncoder program started!");
00046  *  while(1){
00047  *      if(value!=re.Value){
00048  *          pc.printf("\nEncoder value is: %2.f", re.Value);
00049  *          value = re.Value;
00050  *      }
00051  *      wait_ms(1000);
00052  *  }
00053  * }
00054  * @endcode
00055  */
00056 
00057 /**
00058  *  @class RotaryEncoder
00059  *  @brief Rotary encoder with one interrupt and one digital input
00060  */  
00061  
00062 class RotaryEncoder{
00063     public:
00064     /** Create an Incremental Rotary Encoder, connected to the specified DT and CLK pins
00065      *  @param a  - Data transmition (DT) pin
00066      *  @param b  - Clock (CLK) pin
00067      *  @param sw - Switch (SW) pin
00068      *  @note PullMode can be added on the end of the constructor, 
00069      */
00070         RotaryEncoder(PinName a, PinName b, PinName sw, PinMode pullMode);
00071         RotaryEncoder(PinName a, PinName b, PinMode pullMode);
00072         RotaryEncoder(PinName a, PinName b, PinName sw);
00073         RotaryEncoder(PinName a, PinName b);
00074         
00075         /** This global class variable stores the current value  
00076          */
00077         double      Value;
00078         
00079         /** This global class variable is used to set the resolution with which 
00080          *  the value is incremented, e.g., Value+=Resolution 
00081          */
00082         float       Resolution;
00083         
00084         /** Sets the range in which the global variable Value is limited
00085          *  @param min - minimal value
00086          *  @param max - maximal value   
00087          */
00088         void        SetRange(double min, double max);
00089     private:
00090         void        init(PinMode tPullMode);
00091         void        count();
00092         int         bLastState;
00093         InterruptIn pinA;
00094         DigitalIn   pinB;
00095         DigitalIn   pinSw;
00096         Timer       debounce;
00097         double      Min, Max;
00098 };
00099 
00100 #endif