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 
25 #include <iostream>
26 
27 #include "wheelencoder.h"
28 
29 using namespace upm;
30 using namespace std;
31 
WheelEncoder(int pin)32 WheelEncoder::WheelEncoder(int pin) :
33   m_gpio(pin)
34 {
35   m_gpio.dir(mraa::DIR_IN);
36 
37   initClock();
38   m_counter = 0;
39   m_isrInstalled = false;
40 }
41 
~WheelEncoder()42 WheelEncoder::~WheelEncoder()
43 {
44   stopCounter();
45 }
46 
initClock()47 void WheelEncoder::initClock()
48 {
49   gettimeofday(&m_startTime, NULL);
50 }
51 
getMillis()52 uint32_t WheelEncoder::getMillis()
53 {
54   struct timeval elapsed, now;
55   uint32_t elapse;
56 
57   // get current time
58   gettimeofday(&now, NULL);
59 
60   // compute the delta since m_startTime
61   if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 )
62     {
63       elapsed.tv_usec += 1000000;
64       elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
65     }
66   else
67     {
68       elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
69     }
70 
71   elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));
72 
73   // never return 0
74   if (elapse == 0)
75     elapse = 1;
76 
77   return elapse;
78 }
79 
startCounter()80 void WheelEncoder::startCounter()
81 {
82   initClock();
83   m_counter = 0;
84 
85   // install our interrupt handler
86   if (!m_isrInstalled)
87     m_gpio.isr(mraa::EDGE_RISING, &wheelISR, this);
88 
89   m_isrInstalled = true;
90 }
91 
stopCounter()92 void WheelEncoder::stopCounter()
93 {
94   // remove the interrupt handler
95   if (m_isrInstalled)
96     m_gpio.isrExit();
97 
98   m_isrInstalled = false;
99 }
100 
wheelISR(void * ctx)101 void WheelEncoder::wheelISR(void *ctx)
102 {
103   upm::WheelEncoder *This = (upm::WheelEncoder *)ctx;
104   This->m_counter++;
105 }
106 
107