Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
RotaryEncoder.cpp
00001 /** 00002 * @file RotaryEncoder.cpp 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 #include "RotaryEncoder.h" 00023 00024 //Default constructor 00025 RotaryEncoder::RotaryEncoder(PinName a, PinName b) : pinA(a), pinB(b),pinSw(NC){ 00026 RotaryEncoder::init(PullDown); 00027 } 00028 00029 //Pull mode constructor 00030 RotaryEncoder::RotaryEncoder(PinName a, PinName b, PinMode pullMode) 00031 : pinA(a), pinB(b), pinSw(NC){ 00032 RotaryEncoder::init(pullMode); 00033 } 00034 00035 //Encoder and switch constructor 00036 RotaryEncoder::RotaryEncoder(PinName a, PinName b, PinName sw) 00037 : pinA(a), pinB(b), pinSw(sw){ 00038 pinSw.mode(PullDown); 00039 RotaryEncoder::init(PullDown); 00040 } 00041 00042 //Encoder and switch constructor with pull mode 00043 RotaryEncoder::RotaryEncoder(PinName a, PinName b, PinName sw, 00044 PinMode pullMode): pinA(a), pinB(b), pinSw(sw){ 00045 pinSw.mode(pullMode); 00046 RotaryEncoder::init(pullMode); 00047 } 00048 00049 //Initialize routine 00050 void RotaryEncoder::init(PinMode tPullMode){ 00051 //Setup pin B pull mode 00052 pinB.mode(tPullMode); 00053 //Initialize start values 00054 Value = 0.0; 00055 Resolution = 1.0; 00056 SetRange(0.0, 100.0); //Range form 0-100 00057 bLastState = pinB.read(); 00058 //Setup hardware interrupt 00059 pinA.rise(this, &RotaryEncoder::count); 00060 pinA.fall(this, &RotaryEncoder::count); 00061 //Start debounce timer 00062 debounce.start(); 00063 } 00064 00065 //Count routine 00066 void RotaryEncoder::count(){ 00067 int bState = pinB.read(); 00068 if(RotaryEncoder::debounce.read_ms() > 10){ 00069 if(bState != bLastState && bState == 1){ 00070 (pinA.read()!=bState)? (Value -= Resolution) : (Value += Resolution); 00071 } 00072 00073 if(Value<Min) Value = Min; 00074 if(Value>Max) Value = Max; 00075 00076 RotaryEncoder::debounce.reset(); 00077 } 00078 bLastState = bState; 00079 } 00080 00081 void RotaryEncoder::SetRange(double min, double max){ 00082 Min = min; 00083 Max = max; 00084 }
Generated on Fri Jul 29 2022 10:03:48 by
1.7.2