Skip to content

Commit 93eb1c6

Browse files
committed
clang-tidy libs
1 parent a3dd796 commit 93eb1c6

4 files changed

Lines changed: 51 additions & 39 deletions

File tree

libs/cap1188/src/Cap1188.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "Cap1188.h"
22

3+
#include <algorithm>
4+
#include <array>
5+
36
Cap1188::Cap1188(uint8_t address, i2c_inst *i2c, uint8_t threshold, Sensitivity sensitivity, Gain gain)
47
: m_i2c(i2c), m_address(address) {
58

@@ -50,7 +53,7 @@ bool Cap1188::getTouched(uint8_t input) {
5053
return false;
5154
}
5255

53-
return getTouched() & (1 << input);
56+
return (getTouched() & (1 << input)) != 0;
5457
}
5558

5659
void Cap1188::setGain(Gain gain) {
@@ -70,19 +73,17 @@ void Cap1188::setSensitivity(Cap1188::Sensitivity sensitivity) {
7073

7174
void Cap1188::setThreshold(uint8_t threshold) {
7275
// By default writing SENSOR_INPUT_1_THRESHOLD will set all inputs
73-
if (threshold > 127) {
74-
threshold = 127;
75-
}
76+
threshold = std::min(threshold, (uint8_t)127);
7677

7778
writeRegister(Register::SENSOR_INPUT_1_THRESHOLD, threshold);
7879
}
7980

8081
int8_t Cap1188::getDeltaCount(uint8_t input) {
8182
if (input > 7) {
82-
return false;
83+
return 0;
8384
}
8485

85-
return readRegister(Register::SENSOR_INPUT_1_DELTA_COUNT, input);
86+
return static_cast<int8_t>(readRegister(Register::SENSOR_INPUT_1_DELTA_COUNT, input));
8687
}
8788

8889
void Cap1188::clearInterrupt() {
@@ -92,7 +93,7 @@ void Cap1188::clearInterrupt() {
9293
}
9394

9495
uint8_t Cap1188::readRegister(Cap1188::Register reg, uint8_t offset) {
95-
uint8_t result;
96+
uint8_t result = 0;
9697
const uint8_t reg_addr = static_cast<uint8_t>(reg) + offset;
9798

9899
i2c_write_blocking(m_i2c, m_address, &reg_addr, 1, true);
@@ -103,7 +104,7 @@ uint8_t Cap1188::readRegister(Cap1188::Register reg, uint8_t offset) {
103104

104105
void Cap1188::writeRegister(Cap1188::Register reg, uint8_t value, uint8_t offset) {
105106
const uint8_t reg_addr = static_cast<uint8_t>(reg) + offset;
106-
const uint8_t data[] = {reg_addr, value};
107+
const std::array data = {reg_addr, value};
107108

108-
i2c_write_blocking(m_i2c, m_address, data, 2, false);
109+
i2c_write_blocking(m_i2c, m_address, data.data(), 2, false);
109110
}

libs/is31se5117a/include/is31se5117a/Is31se5117a.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class Is31se5117a {
300300
i2c_inst *m_i2c;
301301
uint8_t m_address;
302302

303-
RegisterPage m_current_page;
303+
RegisterPage m_current_page{RegisterPage::Page0};
304304

305305
public:
306306
Is31se5117a(uint8_t address, i2c_inst *i2c, uint8_t threshold, uint8_t hysteresis);
@@ -327,6 +327,8 @@ class Is31se5117a {
327327
uint8_t readRegister8(Register reg, uint8_t offset = 0);
328328
uint16_t readRegister16(Register reg, uint8_t offset = 0);
329329
void writeRegister(Register reg, uint8_t value, uint8_t offset = 0);
330+
331+
void doWriteRegister(uint8_t addr, uint8_t value);
330332
};
331333

332334
#endif // IS31SE5117A_IS31SE5117A_H_

libs/is31se5117a/src/is31se5117a.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
#include "pico/time.h"
44

5+
#include <array>
6+
57
namespace {
6-
static constexpr uint8_t KEY_COUNT = 16;
8+
constexpr uint8_t KEY_COUNT = 16;
79
} // namespace
810

911
Is31se5117a::Is31se5117a(uint8_t address, i2c_inst *i2c, uint8_t threshold, uint8_t hysteresis)
10-
: m_i2c(i2c), m_address(address), m_current_page(RegisterPage::Page0) {
12+
: m_i2c(i2c), m_address(address) {
1113

1214
// Reset
1315
writeRegister(Register::MAIN_CONTROL, 0x80);
@@ -82,7 +84,7 @@ Is31se5117a::Is31se5117a(uint8_t address, i2c_inst *i2c, uint8_t threshold, uint
8284
}
8385

8486
uint16_t Is31se5117a::getTouched() {
85-
uint16_t touched = readRegister16(Register::KEY_STATUS_1);
87+
const uint16_t touched = readRegister16(Register::KEY_STATUS_1);
8688

8789
return touched;
8890
}
@@ -91,7 +93,7 @@ bool Is31se5117a::getTouched(uint8_t input) {
9193
return false;
9294
}
9395

94-
return getTouched() & (1 << input);
96+
return (getTouched() & (1 << input)) != 0;
9597
}
9698

9799
void Is31se5117a::setFingerThresholds(uint8_t threshold) {
@@ -163,21 +165,22 @@ uint16_t Is31se5117a::getBaseline(uint8_t input) {
163165
void Is31se5117a::setRegisterPage(uint16_t address) {
164166
if (address > 0x0009) {
165167
if (address > 0x00FF && m_current_page != RegisterPage::Page1) {
166-
writeRegister(Register::SWITCH_PAGE, 0x01);
168+
doWriteRegister(static_cast<uint8_t>(Register::SWITCH_PAGE), 0x01);
167169
m_current_page = RegisterPage::Page1;
168170
} else if (address < 0x0100 && m_current_page != RegisterPage::Page0) {
169-
writeRegister(Register::SWITCH_PAGE, 0x00);
171+
doWriteRegister(static_cast<uint8_t>(Register::SWITCH_PAGE), 0x00);
170172
m_current_page = RegisterPage::Page0;
171173
}
172174
}
173175
}
174176

175177
uint8_t Is31se5117a::readRegister8(Is31se5117a::Register reg, uint8_t offset) {
176-
uint8_t result;
177-
uint16_t offset_addr = static_cast<uint16_t>(reg) + offset;
178+
uint8_t result = 0;
179+
180+
const uint16_t offset_addr = static_cast<uint16_t>(reg) + offset;
181+
const auto reg_addr = static_cast<uint8_t>(offset_addr & 0x00FF);
178182

179183
setRegisterPage(offset_addr);
180-
uint8_t reg_addr = static_cast<uint8_t>(offset_addr & 0x00FF);
181184

182185
i2c_write_blocking(m_i2c, m_address, &reg_addr, 1, true);
183186
i2c_read_blocking(m_i2c, m_address, &result, 1, false);
@@ -186,24 +189,28 @@ uint8_t Is31se5117a::readRegister8(Is31se5117a::Register reg, uint8_t offset) {
186189
}
187190

188191
uint16_t Is31se5117a::readRegister16(Is31se5117a::Register reg, uint8_t offset) {
189-
uint8_t result[2];
190-
uint16_t offset_addr = static_cast<uint16_t>(reg) + offset;
192+
std::array<uint8_t, 2> result = {};
193+
194+
const uint16_t offset_addr = static_cast<uint16_t>(reg) + offset;
195+
const auto reg_addr = static_cast<uint8_t>(offset_addr & 0x00FF);
191196

192197
setRegisterPage(offset_addr);
193-
uint8_t reg_addr = static_cast<uint8_t>(offset_addr & 0x00FF);
194198

195199
i2c_write_blocking(m_i2c, m_address, &reg_addr, 1, true);
196-
i2c_read_blocking(m_i2c, m_address, result, 2, false);
200+
i2c_read_blocking(m_i2c, m_address, result.data(), 2, false);
197201

198202
// High byte comes first for 16bit values
199203
return static_cast<uint16_t>(result[0]) << 8 | static_cast<uint16_t>(result[1]);
200204
}
201205

202206
void Is31se5117a::writeRegister(Is31se5117a::Register reg, uint8_t value, uint8_t offset) {
203-
uint16_t offset_addr = static_cast<uint16_t>(reg) + offset;
207+
const uint16_t offset_addr = static_cast<uint16_t>(reg) + offset;
204208

205209
setRegisterPage(offset_addr);
210+
doWriteRegister(static_cast<uint8_t>(offset_addr & 0x00FF), value);
211+
}
206212

207-
uint8_t data[] = {static_cast<uint8_t>(offset_addr & 0x00FF), value};
208-
i2c_write_blocking(m_i2c, m_address, data, 2, false);
213+
void Is31se5117a::doWriteRegister(uint8_t addr, uint8_t value) {
214+
std::array data = {addr, value};
215+
i2c_write_blocking(m_i2c, m_address, data.data(), 2, false);
209216
}

libs/mpr121/src/Mpr121.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "pico/time.h"
44

5+
#include <array>
6+
57
Mpr121::Mpr121(uint8_t address, i2c_inst *i2c, uint8_t touch_threshold, uint8_t release_threshold, bool autoconfig)
68
: m_i2c(i2c), m_address(address) {
79

@@ -47,7 +49,7 @@ Mpr121::Mpr121(uint8_t address, i2c_inst *i2c, uint8_t touch_threshold, uint8_t
4749
}
4850

4951
uint16_t Mpr121::getTouched() {
50-
uint16_t touched = readRegister16(Register::TOUCHSTATUS_L);
52+
const uint16_t touched = readRegister16(Register::TOUCHSTATUS_L);
5153

5254
return touched & 0x0FFF;
5355
}
@@ -56,7 +58,7 @@ bool Mpr121::getTouched(uint8_t input) {
5658
return false;
5759
}
5860

59-
return getTouched() & (1 << input);
61+
return (getTouched() & (1 << input)) != 0;
6062
}
6163

6264
void Mpr121::setThresholds(uint8_t touch_threshold, uint8_t release_threshold) {
@@ -86,40 +88,40 @@ uint16_t Mpr121::getFilteredData(uint8_t input) {
8688
}
8789

8890
uint8_t Mpr121::readRegister8(Mpr121::Register reg, uint8_t offset) {
89-
uint8_t result;
91+
uint8_t result = 0;
9092

91-
uint8_t reg_addr = static_cast<uint8_t>(reg) + offset;
93+
const uint8_t reg_addr = static_cast<uint8_t>(reg) + offset;
9294
i2c_write_blocking(m_i2c, m_address, &reg_addr, 1, true);
9395
i2c_read_blocking(m_i2c, m_address, &result, 1, false);
9496

9597
return result;
9698
}
9799

98100
uint16_t Mpr121::readRegister16(Mpr121::Register reg, uint8_t offset) {
99-
uint8_t result[2];
101+
std::array<uint8_t, 2> result = {};
100102

101-
uint8_t reg_addr = static_cast<uint8_t>(reg) + offset;
103+
const uint8_t reg_addr = static_cast<uint8_t>(reg) + offset;
102104
i2c_write_blocking(m_i2c, m_address, &reg_addr, 1, true);
103-
i2c_read_blocking(m_i2c, m_address, result, 2, false);
105+
i2c_read_blocking(m_i2c, m_address, result.data(), 2, false);
104106

105107
return static_cast<uint16_t>(result[1]) << 8 | static_cast<uint16_t>(result[0]);
106108
}
107109

108110
void Mpr121::writeRegister(Mpr121::Register reg, uint8_t value, uint8_t offset) {
109111
// Only ECR and GPIO related registers can be written in 'Run' mode.
110-
bool need_stop = (reg != Register::ECR) && (reg != Register::GPIODIR) && (reg != Register::GPIOEN) &&
111-
(reg != Register::GPIOSET) && (reg != Register::GPIOCLR) && (reg != Register::GPIOTOGGLE);
112+
const bool need_stop = (reg != Register::ECR) && (reg != Register::GPIODIR) && (reg != Register::GPIOEN) &&
113+
(reg != Register::GPIOSET) && (reg != Register::GPIOCLR) && (reg != Register::GPIOTOGGLE);
112114

113115
auto do_write_register = [&](Mpr121::Register reg, uint8_t value, uint8_t offset = 0) {
114-
uint8_t reg_addr = static_cast<uint8_t>(reg) + offset;
115-
uint8_t data[] = {reg_addr, value};
116+
const uint8_t reg_addr = static_cast<uint8_t>(reg) + offset;
117+
std::array data = {reg_addr, value};
116118

117-
i2c_write_blocking(m_i2c, m_address, data, 2, false);
119+
i2c_write_blocking(m_i2c, m_address, data.data(), 2, false);
118120
};
119121

120122
if (need_stop) {
121123
// Backup ECR
122-
uint8_t ecr = readRegister8(Register::ECR);
124+
const uint8_t ecr = readRegister8(Register::ECR);
123125

124126
// Issue stop
125127
do_write_register(Register::ECR, 0x00);

0 commit comments

Comments
 (0)