1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 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 <unistd.h>
27 #include <stdlib.h>
28 #include <stdexcept>
29 
30 #include "max5487.h"
31 
32 using namespace upm;
33 
MAX5487(int csn)34 MAX5487::MAX5487 (int csn) : m_csnPinCtx(csn), m_spi(0) {
35     mraa::Result error = mraa::SUCCESS;
36     m_name = "MAX5487";
37 
38     if (csn == -1) {
39         throw std::invalid_argument(std::string(__FUNCTION__));
40     }
41 
42     error = m_csnPinCtx.dir (mraa::DIR_OUT);
43     if (error != mraa::SUCCESS) {
44         throw std::invalid_argument(std::string(__FUNCTION__) +
45                                     ": mraa_gpio_dir() failed");
46     }
47 
48     CSOff ();
49 }
50 
51 void
setWiperA(uint8_t wiper)52 MAX5487::setWiperA (uint8_t wiper) {
53     uint8_t data[2] = { 0x00, 0x00};
54 
55     CSOn ();
56 
57     data[0] = R_WR_WIPER_A;
58     data[1] = wiper;
59 
60     uint8_t* retData = m_spi.write(data, 2);
61 
62     CSOff ();
63 }
64 
65 void
setWiperB(uint8_t wiper)66 MAX5487::setWiperB (uint8_t wiper) {
67     uint8_t data[2] = { 0x00, 0x00};
68 
69     CSOn ();
70 
71     data[0] = R_WR_WIPER_B;
72     data[1] = wiper;
73 
74     uint8_t* retData = m_spi.write(data, 2);
75 
76     CSOff ();
77 }
78 
79 /*
80  * **************
81  *  private area
82  * **************
83  */
84 
85 mraa::Result
CSOn()86 MAX5487::CSOn () {
87     return m_csnPinCtx.write(LOW);
88 }
89 
90 mraa::Result
CSOff()91 MAX5487::CSOff () {
92     return m_csnPinCtx.write(HIGH);
93 }
94