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 <string>
27 #include <stdexcept>
28 #include <unistd.h>
29 #include <stdlib.h>
30
31 #include "max44000.h"
32
33 using namespace upm;
34
MAX44000(int bus,int devAddr)35 MAX44000::MAX44000 (int bus, int devAddr) : m_i2cMaxControlCtx(bus) {
36 m_name = "MAX44000";
37
38 m_maxControlAddr = devAddr;
39 m_bus = bus;
40
41 mraa::Result ret = m_i2cMaxControlCtx.address(m_maxControlAddr);
42 if (ret != mraa::SUCCESS) {
43 throw std::invalid_argument(std::string(__FUNCTION__) +
44 ": mraa_i2c_address() failed");
45 }
46
47 // i2cWriteReg (MCR, 0x2C);
48 // i2cWriteReg (TCR, 0x6);
49 }
50
51 uint16_t
getProximity()52 MAX44000::getProximity () {
53 uint16_t data = 0;
54
55 data = (i2cReadReg_8 (ALSDATA_HIGH) & 0x7F) << 8;
56 data = data | i2cReadReg_8 (ALSDATA_LOW);
57
58 return data;
59 }
60
61 uint16_t
getAmbient()62 MAX44000::getAmbient () {
63 uint16_t data = 0;
64
65 data = (i2cReadReg_8 (ALSDATA_HIGH) & 0x7F) << 8;
66 data = data | i2cReadReg_8 (ALSDATA_LOW);
67
68 return data;
69 }
70
71 /*
72 * **************
73 * private area
74 * **************
75 */
76 uint8_t
i2cReadReg_8(int reg)77 MAX44000::i2cReadReg_8 (int reg) {
78 uint8_t data;
79
80 m_i2cMaxControlCtx.address(m_maxControlAddr);
81 m_i2cMaxControlCtx.writeByte(reg);
82
83 m_i2cMaxControlCtx.address(m_maxControlAddr);
84 m_i2cMaxControlCtx.read(&data, 0x1);
85
86 return data;
87 }
88
89 uint16_t
i2cReadReg_16(int reg)90 MAX44000::i2cReadReg_16 (int reg) {
91 uint16_t data;
92
93 m_i2cMaxControlCtx.address(m_maxControlAddr);
94 m_i2cMaxControlCtx.writeByte(reg);
95
96 m_i2cMaxControlCtx.address(m_maxControlAddr);
97 m_i2cMaxControlCtx.read((uint8_t *)&data, 0x2);
98
99 return data;
100 }
101
102 mraa::Result
i2cWriteReg(uint8_t reg,uint8_t value)103 MAX44000::i2cWriteReg (uint8_t reg, uint8_t value) {
104 mraa::Result error = mraa::SUCCESS;
105
106 uint8_t data[2] = { reg, value };
107 error = m_i2cMaxControlCtx.address (m_maxControlAddr);
108 error = m_i2cMaxControlCtx.write (data, 2);
109
110 return error;
111 }
112