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.hpp> 30 31 namespace upm { 32 33 /** 34 * @brief DFRobot Wheel Encoder library 35 * @defgroup wheelencoder libupm-wheelencoder 36 * @ingroup dfrobot gpio other 37 */ 38 39 /** 40 * @library wheelencoder 41 * @sensor wheelencoder 42 * @comname DFRobot Wheel Encoder 43 * @type other 44 * @man dfrobot 45 * @web http://www.dfrobot.com/index.php?route=product/product&product_id=98 46 * @con gpio 47 48 * @brief API for the DFRobot Wheel Encoder 49 * 50 * This sensor was developed for the DFRobot Wheel Encoder, though 51 * it could be used for any counting time-based task. 52 * 53 * When you instantiate a class of this type, the gpio pin specified 54 * is connected to an interrupt. Whenever a low to high transition 55 * occurs on the gpio pin, the internal counter is incremented by 56 * one. 57 * 58 * This class also includes a millisecond counter, so that you can 59 * correlate the number of counts to a time period for calculating 60 * an RPM or other value as needed. 61 * 62 * @snippet wheelencoder.cxx Interesting 63 */ 64 class WheelEncoder { 65 public: 66 67 /** 68 * DFRobot Wheel Encoder sensor constructor 69 * 70 * @param pin Digital pin to use 71 */ 72 WheelEncoder(int pin); 73 74 /** 75 * WheelEncoder destructor 76 */ 77 ~WheelEncoder(); 78 79 /** 80 * Returns the number of milliseconds elapsed since initClock() 81 * was last called. 82 * 83 * @return Elapsed milliseconds 84 */ 85 uint32_t getMillis(); 86 87 /** 88 * Resets the clock 89 * 90 */ 91 void initClock(); 92 93 /** 94 * Resets the counter to 0. The counter should be 95 * stopped via stopCounter() prior to calling this function. 96 * 97 */ clearCounter()98 void clearCounter() { m_counter = 0; }; 99 100 /** 101 * Starts the counter. This function will also clear the current 102 * count and reset the clock. 103 * 104 */ 105 void startCounter(); 106 107 /** 108 * Stops the counter 109 * 110 */ 111 void stopCounter(); 112 113 /** 114 * Gets the current counter value 115 * 116 * @return counter value 117 */ counter()118 uint32_t counter() { return m_counter; }; 119 120 protected: 121 mraa::Gpio m_gpio; 122 static void wheelISR(void *ctx); 123 124 private: 125 volatile uint32_t m_counter; 126 struct timeval m_startTime; 127 bool m_isrInstalled; 128 }; 129 } 130 131 132