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.
ExioMcp23s17RotaryEncoder.cpp
- Committer:
- ryood
- Date:
- 2016-11-02
- Revision:
- 5:b82675c50720
- Child:
- 6:1b3511093630
File content as of revision 5:b82675c50720:
/**
* =============================================================================
* Rotary Encoder class (Version 0.0.1)
* =============================================================================
* Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* =============================================================================
*/
/*
* 2016.11.02 Customized for MCP23S17 by rood
*/
#include "ExioMcp23s17RotaryEncoder.h"
/**
* Create rotary encoder.
*
* @param pin1
* @param pin2
* @param min Minimum value.
* @param max Maximum value.
* @param val Default value.
*/
ExioMcp23s17RotaryEncoder::ExioMcp23s17RotaryEncoder(ExioMcp23s17DigitalIn& _pin1, ExioMcp23s17DigitalIn& _pin2, int min, int max, int val)
: pin1(_pin1), pin2(_pin2), min(min), max(max), val(val) {
pin1.mode(PullUp);
pin2.mode(PullUp);
ticker.attach_us(this, &ExioMcp23s17RotaryEncoder::func_ticker, 500);
}
/**
* Dispose.
*/
ExioMcp23s17RotaryEncoder::~ExioMcp23s17RotaryEncoder() {
}
/**
* Internal tick function.
*/
void ExioMcp23s17RotaryEncoder::func_ticker() {
//static uint8_t code;
code = (code << 2) + (((pin1.read() << 1) | (pin2.read() << 0)) & 3);
code &= 15;
switch (code) {
case 0x7:
if (min < val) {
val--;
} else {
val = min;
}
break;
case 0xd:
if (val < max) {
val++;
} else {
val = max;
}
break;
}
}