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-04
- Revision:
- 12:33706f4ace5c
- Parent:
- 9:b0e9ec45a720
File content as of revision 12:33706f4ace5c:
/**
 * =============================================================================
 * 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 _pDevice
 * @param port
 * @param pin1
 * @param pin2
 * @param min Minimum value.
 * @param max Maximum value.
 * @param val Default value.
 */
ExioMcp23s17RotaryEncoder::ExioMcp23s17RotaryEncoder(
        ExioMcp23s17* pDevice, ExioPort port, int pin1_n, int pin2_n,
        int min, int max, int val) 
    : min(min), max(max), val(val) 
{
    in1 = new ExioMcp23s17DigitalIn( pDevice, port, pin1_n);
    in2 = new ExioMcp23s17DigitalIn( pDevice, port, pin2_n);
    in1->mode(PullUp);
    in2->mode(PullUp);
    //ticker.attach_us(this, &ExioMcp23s17RotaryEncoder::func_ticker, 500);
}
/**
 * Dispose.
 */
ExioMcp23s17RotaryEncoder::~ExioMcp23s17RotaryEncoder() {
    delete in1;
    delete in2;
}
/**
 * Internal tick function.
 */
void ExioMcp23s17RotaryEncoder::func_ticker() {
    //static uint8_t code;
    code = (code << 2) + (((in1->read() << 1) | (in2->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;
    }
}