Ryo Od / ExioController
Committer:
ryood
Date:
Wed Nov 02 04:16:46 2016 +0000
Revision:
5:b82675c50720
Child:
6:1b3511093630
Add: ExioMcp2317sRotaryEncoder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryood 5:b82675c50720 1 /**
ryood 5:b82675c50720 2 * =============================================================================
ryood 5:b82675c50720 3 * Rotary Encoder class (Version 0.0.1)
ryood 5:b82675c50720 4 * =============================================================================
ryood 5:b82675c50720 5 * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
ryood 5:b82675c50720 6 *
ryood 5:b82675c50720 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
ryood 5:b82675c50720 8 * of this software and associated documentation files (the "Software"), to deal
ryood 5:b82675c50720 9 * in the Software without restriction, including without limitation the rights
ryood 5:b82675c50720 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ryood 5:b82675c50720 11 * copies of the Software, and to permit persons to whom the Software is
ryood 5:b82675c50720 12 * furnished to do so, subject to the following conditions:
ryood 5:b82675c50720 13 *
ryood 5:b82675c50720 14 * The above copyright notice and this permission notice shall be included in
ryood 5:b82675c50720 15 * all copies or substantial portions of the Software.
ryood 5:b82675c50720 16 *
ryood 5:b82675c50720 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ryood 5:b82675c50720 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ryood 5:b82675c50720 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ryood 5:b82675c50720 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ryood 5:b82675c50720 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ryood 5:b82675c50720 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ryood 5:b82675c50720 23 * THE SOFTWARE.
ryood 5:b82675c50720 24 * =============================================================================
ryood 5:b82675c50720 25 */
ryood 5:b82675c50720 26
ryood 5:b82675c50720 27 /*
ryood 5:b82675c50720 28 * 2016.11.02 Customized for MCP23S17 by rood
ryood 5:b82675c50720 29 */
ryood 5:b82675c50720 30 #include "ExioMcp23s17RotaryEncoder.h"
ryood 5:b82675c50720 31
ryood 5:b82675c50720 32 /**
ryood 5:b82675c50720 33 * Create rotary encoder.
ryood 5:b82675c50720 34 *
ryood 5:b82675c50720 35 * @param pin1
ryood 5:b82675c50720 36 * @param pin2
ryood 5:b82675c50720 37 * @param min Minimum value.
ryood 5:b82675c50720 38 * @param max Maximum value.
ryood 5:b82675c50720 39 * @param val Default value.
ryood 5:b82675c50720 40 */
ryood 5:b82675c50720 41 ExioMcp23s17RotaryEncoder::ExioMcp23s17RotaryEncoder(ExioMcp23s17DigitalIn& _pin1, ExioMcp23s17DigitalIn& _pin2, int min, int max, int val)
ryood 5:b82675c50720 42 : pin1(_pin1), pin2(_pin2), min(min), max(max), val(val) {
ryood 5:b82675c50720 43 pin1.mode(PullUp);
ryood 5:b82675c50720 44 pin2.mode(PullUp);
ryood 5:b82675c50720 45 ticker.attach_us(this, &ExioMcp23s17RotaryEncoder::func_ticker, 500);
ryood 5:b82675c50720 46 }
ryood 5:b82675c50720 47
ryood 5:b82675c50720 48 /**
ryood 5:b82675c50720 49 * Dispose.
ryood 5:b82675c50720 50 */
ryood 5:b82675c50720 51 ExioMcp23s17RotaryEncoder::~ExioMcp23s17RotaryEncoder() {
ryood 5:b82675c50720 52 }
ryood 5:b82675c50720 53
ryood 5:b82675c50720 54 /**
ryood 5:b82675c50720 55 * Internal tick function.
ryood 5:b82675c50720 56 */
ryood 5:b82675c50720 57 void ExioMcp23s17RotaryEncoder::func_ticker() {
ryood 5:b82675c50720 58 //static uint8_t code;
ryood 5:b82675c50720 59
ryood 5:b82675c50720 60 code = (code << 2) + (((pin1.read() << 1) | (pin2.read() << 0)) & 3);
ryood 5:b82675c50720 61 code &= 15;
ryood 5:b82675c50720 62 switch (code) {
ryood 5:b82675c50720 63 case 0x7:
ryood 5:b82675c50720 64 if (min < val) {
ryood 5:b82675c50720 65 val--;
ryood 5:b82675c50720 66 } else {
ryood 5:b82675c50720 67 val = min;
ryood 5:b82675c50720 68 }
ryood 5:b82675c50720 69 break;
ryood 5:b82675c50720 70 case 0xd:
ryood 5:b82675c50720 71 if (val < max) {
ryood 5:b82675c50720 72 val++;
ryood 5:b82675c50720 73 } else {
ryood 5:b82675c50720 74 val = max;
ryood 5:b82675c50720 75 }
ryood 5:b82675c50720 76 break;
ryood 5:b82675c50720 77 }
ryood 5:b82675c50720 78 }