1 /* 2 * Author: Jon Trulson <jtrulson@ics.com> 3 * Copyright (c) 2015 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 #pragma once 25 26 #include <string> 27 #include <stdint.h> 28 #include <sys/time.h> 29 #include <mraa/gpio.h> 30 31 namespace upm { 32 33 /** 34 * @brief Grove Rotary Encoder library 35 * @defgroup rotaryencoder libupm-rotaryencoder 36 * @ingroup seeed gpio other hak 37 */ 38 /** 39 * @library rotaryencoder 40 * @sensor rotaryencoder 41 * @comname Grove Rotary Encoder 42 * @type other 43 * @man seeed 44 * @web http://www.seeedstudio.com/wiki/Grove_-_Encoder 45 * @con gpio 46 * @kit hak 47 * 48 * @brief API for the Grove Rotary Encoder 49 * 50 * UPM module for the Grove rotary encoder. This rotary encoder 51 * encodes a rotation signal into electronic pulses that can be used 52 * to measure rotation and direction. It is useful in cases where a 53 * rotary knob is required, but using a potentiometer is not 54 * desirable. A rotary encoder can turn a full 360 degrees 55 * without a stop and does not place a resistive load on the 56 * circuit, as is the case with a potentiometer. 57 * 58 * This module maintains a position that is incremented or 59 * decremented according to the rotation on the encoder. 60 * 61 * @image html rotaryencoder.jpg 62 * @snippet rotaryencoder.cxx Interesting 63 */ 64 class RotaryEncoder { 65 public: 66 /** 67 * RotaryEncoder constructor 68 * 69 * @param pinA Digital pin to use for signal A 70 * @param pinB Digital pin to use for signal B 71 */ 72 RotaryEncoder(int pinA, int pinB); 73 /** 74 * RotaryEncoder destructor 75 */ 76 ~RotaryEncoder(); 77 78 /** 79 * Resets the position to a given number; default is 0. 80 * 81 * @param count Integer to initialize the position to 82 */ 83 void initPosition(int count=0); 84 85 /** 86 * Gets the position value 87 * 88 */ 89 int position(); 90 91 private: 92 /** 93 * Interrupt service routine (ISR) for signal A 94 * 95 * @param ctx User context for the ISR (*this pointer) 96 */ 97 static void signalAISR(void *ctx); 98 99 volatile int m_position; 100 mraa_gpio_context m_gpioA; 101 mraa_gpio_context m_gpioB; 102 }; 103 } 104 105 106