Ryo Od / ExioController
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ExioMcp23s17RotaryEncoder.cpp Source File

ExioMcp23s17RotaryEncoder.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 /*
00028  * 2016.11.02 Customized for MCP23S17 by rood
00029  */
00030 #include "ExioMcp23s17RotaryEncoder.h"
00031 
00032 /**
00033  * Create rotary encoder.
00034  * @param _pDevice
00035  * @param port
00036  * @param pin1
00037  * @param pin2
00038  * @param min Minimum value.
00039  * @param max Maximum value.
00040  * @param val Default value.
00041  */
00042 ExioMcp23s17RotaryEncoder::ExioMcp23s17RotaryEncoder(
00043         ExioMcp23s17* pDevice, ExioPort port, int pin1_n, int pin2_n,
00044         int min, int max, int val) 
00045     : min(min), max(max), val(val) 
00046 {
00047     in1 = new ExioMcp23s17DigitalIn( pDevice, port, pin1_n);
00048     in2 = new ExioMcp23s17DigitalIn( pDevice, port, pin2_n);
00049     in1->mode(PullUp);
00050     in2->mode(PullUp);
00051     //ticker.attach_us(this, &ExioMcp23s17RotaryEncoder::func_ticker, 500);
00052 }
00053 
00054 /**
00055  * Dispose.
00056  */
00057 ExioMcp23s17RotaryEncoder::~ExioMcp23s17RotaryEncoder() {
00058     delete in1;
00059     delete in2;
00060 }
00061 
00062 /**
00063  * Internal tick function.
00064  */
00065 void ExioMcp23s17RotaryEncoder::func_ticker() {
00066     //static uint8_t code;
00067 
00068     code = (code << 2) + (((in1->read() << 1) | (in2->read() << 0)) & 3);
00069     code &= 15;
00070     switch (code) {
00071         case 0x7:
00072             if (min < val) {
00073                 val--;
00074             } else {
00075                 val = min;
00076             }
00077             break;
00078         case 0xd:
00079             if (val < max) {
00080                 val++;
00081             } else {
00082                 val = max;
00083             }
00084             break;
00085     }
00086 }