Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / AT42QT1050 / v1 / alld_AT42QT1050_v1.c @ b6364b51

History | View | Annotate | Download (7.534 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    alld_AT42QT1050_v1.c
21
 * @brief   Touch sensor function implementations.
22
 *
23
 * @addtogroup lld_touch
24
 * @{
25
 */
26

    
27
#include <alld_AT42QT1050.h>
28

    
29
#if (defined(AMIROLLD_CFG_AT42QT1050) && (AMIROLLD_CFG_AT42QT1050 == 1)) || defined(__DOXYGEN__)
30

    
31
#include <math.h>
32

    
33
/******************************************************************************/
34
/* LOCAL DEFINITIONS                                                          */
35
/******************************************************************************/
36

    
37
/******************************************************************************/
38
/* EXPORTED VARIABLES                                                         */
39
/******************************************************************************/
40

    
41
/******************************************************************************/
42
/* LOCAL TYPES                                                                */
43
/******************************************************************************/
44

    
45
/******************************************************************************/
46
/* LOCAL VARIABLES                                                            */
47
/******************************************************************************/
48

    
49
/******************************************************************************/
50
/* LOCAL FUNCTIONS                                                            */
51
/******************************************************************************/
52

    
53
/******************************************************************************/
54
/* EXPORTED FUNCTIONS                                                         */
55
/******************************************************************************/
56

    
57
/**
58
 * @brief   Read 8bit data from any register.
59
 *
60
 * @param[in]   at42qt1050d   The AT42QT1050 driver to use.
61
 * @param[in]   reg           Register address to read from.
62
 * @param[out]  data          Pointer to store the register data to.
63
 * @param[in]   timeout       Timeout for the function to return (in microseconds).
64
 *
65
 * @return    Indicator whether the function call was successful or a timeout occurred.
66
 */
67
apalExitStatus_t at42qt1050_lld_read_reg(const AT42QT1050Driver* at42qt1050d, const at42qt1050_lld_register_t reg, uint8_t* const data, const apalTime_t timeout)
68
{
69
  apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL);
70
  apalDbgAssert(data != NULL);
71

    
72
  const uint8_t txbuf = (uint8_t)reg;
73
  return apalI2CMasterTransmit(at42qt1050d->i2cd, at42qt1050d->addr, &txbuf, 1, data, 1, timeout);
74
}
75

    
76
/**
77
 * @brief   Write 8bit data to any (writable) register.
78
 *
79
 * @param[in]   at42qt1050d   The AT42QT1050 driver to use.
80
 * @param[in]   reg           Register address to write to.
81
 * @param[in]   data          Data to transmit.
82
 * @param[in]   timeout       Timeout for the function to return (in microseconds).
83
 *
84
 * @return    Indicator whether the function call was successful or a timeout occurred.
85
 */
86
apalExitStatus_t at42qt1050_lld_write_reg(const AT42QT1050Driver* at42qt1050d, const at42qt1050_lld_register_t reg, const uint8_t data, const apalTime_t timeout)
87
{
88
  apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL);
89

    
90
  const uint8_t txbuf[2] = { (uint8_t)reg, data };
91
  return apalI2CMasterTransmit(at42qt1050d->i2cd, at42qt1050d->addr, txbuf, 2, NULL, 0, timeout);
92
}
93

    
94
/**
95
 * @brief   Read signal information of a key.
96
 *
97
 * @param[in]   at42qt1050d   The AT42QT1050 driver to use.
98
 * @param[in]   key           Key to read the signal information of.
99
 * @param[out]  signal        Pointer to store the data to.
100
 * @param[in]   timeout       Timeout for the function to return (in microseconds).
101
 *
102
 * @return    Indicator whether the function call was successful or a timeout occurred.
103
 */
104
apalExitStatus_t at42qt1050_lld_read_keyssignal(const AT42QT1050Driver* at42qt1050d, const uint8_t key, uint16_t* signal, const apalTime_t timeout)
105
{
106
  apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL);
107
  apalDbgAssert(key < AT42QT1050_LLD_NUM_KEYS);
108
  apalDbgAssert(signal != NULL);
109

    
110
  const uint8_t txbuf = AT42QT1050_LLD_REG_KEYSIGNAL_0 + (2*key) + ((key > 1) ? 1 : 0);
111
  uint8_t rxbuf[2];
112
  const apalExitStatus_t status = apalI2CMasterTransmit(at42qt1050d->i2cd, at42qt1050d->addr, &txbuf, 1, rxbuf, 2, timeout);
113
  *signal = (rxbuf[0] << 8) | rxbuf[1];
114
  return status;
115
}
116

    
117
/**
118
 * @brief   Read reference data of a key.
119
 *
120
 * @param[in]   at42qt1050d   The AT42QT1050 driver to use.
121
 * @param[in]   key           Key to read the signal information of.
122
 * @param[out]  refdata       Pointer to store the data to.
123
 * @param[in]   timeout       Timeout for the function to return (in microseconds).
124
 *
125
 * @return    Indicator whether the function call was successful or a timeout occurred.
126
 */
127
apalExitStatus_t at42qt1050_lld_read_referencedata(const AT42QT1050Driver* at42qt1050d, const uint8_t key, uint16_t* refdata, const apalTime_t timeout)
128
{
129
  apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL);
130
  apalDbgAssert(key < AT42QT1050_LLD_NUM_KEYS);
131
  apalDbgAssert(refdata != NULL);
132

    
133
  const uint8_t txbuf = AT42QT1050_LLD_REG_REFERENCEDATA_0 + (2*key) + ((key > 1) ? 1 : 0);
134
  uint8_t rxbuf[2];
135
  const apalExitStatus_t status = apalI2CMasterTransmit(at42qt1050d->i2cd, at42qt1050d->addr, &txbuf, 1, rxbuf, 2, timeout);
136
  *refdata = (rxbuf[0] << 8) | rxbuf[1];
137
  return status;
138
}
139

    
140
/**
141
 * @brief   Convert a 4 bit pulse value to the representing number of samples.
142
 * @details Calculation: <#samples> = 2^(<pulse value>)
143
 *
144
 * @param[in]   pulse   Pulse value.
145
 *
146
 * @return    Resulting sample count.
147
 */
148
uint16_t at42qt1050_lld_pulse2samples(const uint8_t pulse)
149
{
150
  apalDbgAssert(pulse <= 0x0Fu);
151

    
152
  return (1 << pulse);
153
}
154

    
155
/**
156
 * @brief   Convert a desired number of samples to the according (theoretical) pulse value.
157
 * @details Calculation: <pulse value> = log2(<#samples>)
158
 *
159
 * @param[in]   samples   Desired number of samples.
160
 *
161
 * @return    The (theoretical) value to set to the pulse register.
162
 */
163
float at42qt1050_lld_samples2pulse(const uint16_t samples)
164
{
165
  return log2f(samples);
166
}
167

    
168
/**
169
 * @brief   Convert a 4 bit scale value to the accoring scaling factor.
170
 * @details Calculation: <scaling factor> = 2^(<scale value>)
171
 *
172
 * @param[in]   scale   Scale value.
173
 *
174
 * @return    Resulting scaling factor.
175
 */
176
uint16_t at42qt1050_lld_scale2scaling(const uint8_t scale)
177
{
178
  apalDbgAssert(scale <= 0x0Fu);
179

    
180
  return (1 << scale);
181
}
182

    
183
/**
184
 * @brief   Convert a desired scaling factor to the according (theoretical) scale value.
185
 * @details Calculation: <scale value> = log2(<scaling factor>
186
 * )
187
 * @param[in]   factor    Desired scaling factor.
188
 *
189
 * @return    The (theoretcial) value to set to the scale register.
190
 */
191
float at42qt1050_lld_scaling2scale(const uint16_t factor)
192
{
193
  return log2f(factor);
194
}
195

    
196
#endif /* defined(AMIROLLD_CFG_AT42QT1050) && (AMIROLLD_CFG_AT42QT1050 == 1) */
197

    
198
/** @} */