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 "isd1820.h"
30 
31 using namespace upm;
32 using namespace std;
33 
ISD1820(int playPin,int recPin)34 ISD1820::ISD1820(int playPin, int recPin)
35 {
36   if ( !(m_gpioPlay = mraa_gpio_init(playPin)) )
37     {
38       throw std::invalid_argument(std::string(__FUNCTION__) +
39                                   ": mraa_gpio_init(playPin) failed, invalid pin?");
40       return;
41     }
42 
43   mraa_gpio_dir(m_gpioPlay, MRAA_GPIO_OUT);
44 
45   if ( !(m_gpioRec = mraa_gpio_init(recPin)) )
46     {
47       throw std::invalid_argument(std::string(__FUNCTION__) +
48                                   ": mraa_gpio_init(recPin) failed, invalid pin?");
49       return;
50     }
51 
52   mraa_gpio_dir(m_gpioRec, MRAA_GPIO_OUT);
53 
54   // make sure we are not recording or playing
55   play(false);
56   record(false);
57 }
58 
~ISD1820()59 ISD1820::~ISD1820()
60 {
61   mraa_gpio_close(m_gpioPlay);
62   mraa_gpio_close(m_gpioRec);
63 }
64 
play(bool enable)65 void ISD1820::play(bool enable)
66 {
67   if (enable)
68     {
69       // make sure we are not recording
70       record(false);
71       mraa_gpio_write(m_gpioPlay, 1);
72     }
73   else
74     {
75       mraa_gpio_write(m_gpioPlay, 0);
76     }
77 }
78 
record(bool enable)79 void ISD1820::record(bool enable)
80 {
81   if (enable)
82     {
83       // make sure we are not playing
84       play(false);
85       mraa_gpio_write(m_gpioRec, 1);
86     }
87   else
88     {
89       mraa_gpio_write(m_gpioRec, 0);
90     }
91 }
92