summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/at42qt1070/at42qt1070.cxx
blob: fed9f382dec72ddb021e0ff94c4ab02a52b9fe27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <unistd.h>
#include <math.h>
#include <iostream>
#include <string>
#include <stdexcept>

#include "at42qt1070.hpp"

using namespace upm;
using namespace std;


AT42QT1070::AT42QT1070(int bus, uint8_t address)
{
    m_addr = address;

    // setup our i2c link
    if (!(m_i2c = mraa_i2c_init(bus))) {
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_i2c_init() failed");
        return;
    }

    mraa_result_t rv;

    if ((rv = mraa_i2c_address(m_i2c, m_addr)) != MRAA_SUCCESS) {
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_i2c_address() failed");
        return;
    }

    if (readChipID() != 0x2E) {
        throw std::runtime_error("Chip ID does not match the expected value (2Eh)");
    }

    m_buttonStates = 0;
    m_calibrating = false;
    m_overflow = false;
}

AT42QT1070::~AT42QT1070()
{
    mraa_i2c_stop(m_i2c);
}

bool
AT42QT1070::writeByte(uint8_t reg, uint8_t byte)
{
    mraa_result_t rv = mraa_i2c_write_byte_data(m_i2c, byte, reg);

    if (rv != MRAA_SUCCESS) {
        throw std::runtime_error(std::string(__FUNCTION__) +
                                 ": mraa_i2c_write_byte() failed");
        return false;
    }

    return true;
}

bool
AT42QT1070::writeWord(uint8_t reg, uint16_t word)
{
    mraa_result_t rv = mraa_i2c_write_word_data(m_i2c, word, reg);

    if (rv != MRAA_SUCCESS) {
        throw std::runtime_error(std::string(__FUNCTION__) +
                                 ": mraa_i2c_write_word() failed");
        return false;
    }

    return true;
}

uint8_t
AT42QT1070::readByte(uint8_t reg)
{
    int x = mraa_i2c_read_byte_data(m_i2c, reg);
    if (x != -1) {
        return (uint8_t) x;
    }
    return 0;
}

uint16_t
AT42QT1070::readWord(uint8_t reg)
{
    int x = mraa_i2c_read_word_data(m_i2c, reg);
    if (x != -1) {
        return (uint16_t) x;
    }
    return 0;
}

uint8_t
AT42QT1070::readChipID(void)
{
    return readByte(REG_CHIPID);
}

void
AT42QT1070::updateState()
{
    uint8_t stat = readByte(REG_DETSTATUS);

    // if we are calibrating, don't change anything
    if (stat & DET_CALIBRATE) {
        m_calibrating = true;
        return;
    } else {
        m_calibrating = false;
    }

    if (stat & DET_OVERFLOW)
        m_overflow = true;
    else
        m_overflow = false;

    // if a touch is occurring, read the button states
    if (stat & DET_TOUCH) {
        uint8_t keys = readByte(REG_KEYSTATUS);
        // high bit is reserved, filter it out
        m_buttonStates = keys & ~0x80;
    } else {
        m_buttonStates = 0;
    }
}

uint8_t
AT42QT1070::getLPMode(void)
{
    return readByte(REG_LP);
}

uint8_t
AT42QT1070::setLPMode(uint8_t mode)
{
    writeByte(REG_LP, mode);

    return getLPMode();
}

uint8_t
AT42QT1070::getAVE(uint8_t key)
{
    uint8_t value, ave;

    if (key > 6) {
        throw std::invalid_argument("Only keys 0-6 are allowed");
    }

    value = readByte(REG_AVE0 + key);
    ave = (value & 0xFC) >> 2;

    return ave;
}

uint8_t
AT42QT1070::setAVE(uint8_t key, uint8_t ave)
{
    uint8_t value;

    if (key > 6) {
        throw std::invalid_argument("Only keys 0-6 are allowed");
    }

    switch (ave) {
        case 1:
        case 2:
        case 4:
        case 8:
        case 16:
        case 32:
            break;

        default:
            throw std::invalid_argument("Invalid averaging factor");
    }

    value = readByte(REG_AVE0 + key);
    value = value & 0x03;
    value = value | (ave << 2);
    writeByte(REG_AVE0 + key, value);

    return getAVE(key);
}

uint8_t
AT42QT1070::getAKSGroup(uint8_t key)
{
    uint8_t value, aks;

    if (key > 6) {
        throw std::invalid_argument("Only keys 0-6 are allowed");
    }

    value = readByte(REG_AVE0 + key);
    aks = value & 0x03;

    return aks;
}

uint8_t
AT42QT1070::setAKSGroup(uint8_t key, uint8_t group)
{
    uint8_t value;

    if (key > 6) {
        throw std::invalid_argument("Only keys 0-6 are allowed");
    }

    if (group > 3) {
        throw std::invalid_argument("Only groups 0-3 are allowed");
    }

    value = readByte(REG_AVE0 + key);
    value = value & 0xFC;
    value = value | group;

    writeByte(REG_AVE0 + key, value);

    return getAKSGroup(key);
}

bool
AT42QT1070::reset()
{
    // write a non-zero value to the reset register
    return writeByte(REG_RESET, 0xff);
}

bool
AT42QT1070::calibrate()
{
    // write a non-zero value to the calibrate register
    return writeByte(REG_CALIBRATE, 0xff);
}