Ryo Od / ExioBufferdController
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ExioBufferedRotaryEncoder.cpp Source File

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