Rotary encoder library.

Dependents:   RotaryEncoder_TestProgram ST7735_Pong accuBlast_display

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RotaryEncoder.cpp Source File

RotaryEncoder.cpp

00001 /**
00002  * =============================================================================
00003  * Rotary Encoder class (Version 0.0.1)
00004  * =============================================================================
00005  * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, including without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  * =============================================================================
00025  */
00026 
00027 #include "RotaryEncoder.h"
00028 
00029 /**
00030  * Create rotary encoder.
00031  *
00032  * @param pin1_name
00033  * @param pin2_name
00034  * @param min Minimum value.
00035  * @param max Maximum value.
00036  * @param val Default value.
00037  */
00038 RotaryEncoder::RotaryEncoder(PinName pin1_name, PinName pin2_name, int min, int max, int val)
00039         : pin1(pin1_name), pin2(pin2_name), min(min), max(max), val(val) {
00040     pin1.mode(PullUp);
00041     pin2.mode(PullUp);
00042     ticker.attach_us(this, &RotaryEncoder::func_ticker, 500);
00043 }
00044 
00045 /**
00046  * Dispose.
00047  */
00048 RotaryEncoder::~RotaryEncoder() {
00049 }
00050 
00051 /**
00052  * Internal tick function.
00053  */
00054 void RotaryEncoder::func_ticker() {
00055     static uint8_t code;
00056 
00057     code = (code << 2) + (((pin1.read() << 1) | (pin2.read() << 0)) & 3);
00058     code &= 15;
00059     switch (code) {
00060         case 0x7:
00061             if (min < val) {
00062                 val--;
00063             } else {
00064                 val = min;
00065             }
00066             break;
00067         case 0xd:
00068             if (val < max) {
00069                 val++;
00070             } else {
00071                 val = max;
00072             }
00073             break;
00074     }
00075 }