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 #include <string>
27 #include <stdexcept>
28 
29 #include "ht9170.h"
30 
31 using namespace upm;
32 using namespace std;
33 
HT9170(int dr,int o1,int o2,int o3,int o4)34 HT9170::HT9170(int dr, int o1, int o2, int o3, int o4)
35 {
36   // init the gpio's we will need
37   if ( !(m_dr = mraa_gpio_init(dr)) )
38     {
39       throw std::invalid_argument(std::string(__FUNCTION__) +
40                                   ": mraa_gpio_init(dr) failed, invalid pin?");
41       return;
42     }
43   mraa_gpio_dir(m_dr, MRAA_GPIO_IN);
44 
45   if ( !(m_o1 = mraa_gpio_init(o1)) )
46     {
47       throw std::invalid_argument(std::string(__FUNCTION__) +
48                                   ": mraa_gpio_init(o1) failed, invalid pin?");
49       return;
50     }
51   mraa_gpio_dir(m_o1, MRAA_GPIO_IN);
52 
53   if ( !(m_o2 = mraa_gpio_init(o2)) )
54     {
55       throw std::invalid_argument(std::string(__FUNCTION__) +
56                                   ": mraa_gpio_init(o2) failed, invalid pin?");
57       return;
58     }
59   mraa_gpio_dir(m_o2, MRAA_GPIO_IN);
60 
61   if ( !(m_o3 = mraa_gpio_init(o3)) )
62     {
63       throw std::invalid_argument(std::string(__FUNCTION__) +
64                                   ": mraa_gpio_init(o3) failed, invalid pin?");
65       return;
66     }
67   mraa_gpio_dir(m_o3, MRAA_GPIO_IN);
68 
69   if ( !(m_o4 = mraa_gpio_init(o4)) )
70     {
71       throw std::invalid_argument(std::string(__FUNCTION__) +
72                                   ": mraa_gpio_init(o4) failed, invalid pin?");
73       return;
74     }
75   mraa_gpio_dir(m_o4, MRAA_GPIO_IN);
76 }
77 
~HT9170()78 HT9170::~HT9170()
79 {
80   mraa_gpio_close(m_dr);
81   mraa_gpio_close(m_o1);
82   mraa_gpio_close(m_o2);
83   mraa_gpio_close(m_o3);
84   mraa_gpio_close(m_o4);
85 }
86 
digitReady()87 bool HT9170::digitReady()
88 {
89   return (mraa_gpio_read(m_dr) ? true : false);
90 }
91 
decodeDigit()92 char HT9170::decodeDigit()
93 {
94   // This lookup table is derived from the decode table on page 7 of
95   // the HT9170B/HT9170D datasheet.
96 
97   static const char decode[16] = {
98     'D',     // 0
99     '1',     // 1
100     '2',     // 2
101     '3',     // 3
102     '4',     // 4
103     '5',     // 5
104     '6',     // 6
105     '7',     // 7
106     '8',     // 8
107     '9',     // 9
108     '0',     // 10
109     '*',     // 11
110     '#',     // 12
111     'A',     // 13
112     'B',     // 14
113     'C'      // 15
114   };
115 
116   uint8_t bits = 0;
117 
118   bits = mraa_gpio_read(m_o1);
119   bits |= (mraa_gpio_read(m_o2) << 1);
120   bits |= (mraa_gpio_read(m_o3) << 2);
121   bits |= (mraa_gpio_read(m_o4) << 3);
122 
123   return decode[bits];
124 }
125