Skip to content

Commit dfb7e6d

Browse files
SoucheSouche0xjakob
authored andcommitted
feat: Unify usage of StrongType::get_value() and make this function a template function
get_value can now be used as a type converter if the type is specified. - remove all wrapper functions (e.g, get_num, get_level, etc...) from the associated strong type classes - unify the usage of the inherited get_value() method throughout the code - remove specific conversion functions and use template function StrongValue::get_value<>() instead
1 parent 664d3e3 commit dfb7e6d

File tree

13 files changed

+99
-184
lines changed

13 files changed

+99
-184
lines changed

examples/simple_spi_rw_example/main/simple_spi_rw_example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern "C" void app_main(void)
3232
MISO(26),
3333
SCLK(27));
3434

35-
shared_ptr<SPIDevice> spi_dev = master.create_dev(CS(NSS.get_num()), Frequency::MHz(1));
35+
shared_ptr<SPIDevice> spi_dev = master.create_dev(CS(NSS.get_value()), Frequency::MHz(1));
3636

3737
vector<uint8_t> write_data = {MPU9250_WHO_AM_I_REG_ADDR | READ_FLAG, 0x00};
3838
vector<uint8_t> result = spi_dev->transfer(write_data).get();

gpio_cxx.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ constexpr std::array<uint32_t, 0> INVALID_GPIOS = {};
3131
#error "No GPIOs defined for the current target"
3232
#endif
3333

34-
gpio_num_t gpio_to_driver_type(const GPIONum &gpio_num)
35-
{
36-
return static_cast<gpio_num_t>(gpio_num.get_num());
37-
}
38-
3934
}
4035

4136
GPIOException::GPIOException(esp_err_t error) : ESPException(error) { }
@@ -117,55 +112,55 @@ GPIODriveStrength GPIODriveStrength::STRONGEST()
117112

118113
GPIOBase::GPIOBase(GPIONum num) : gpio_num(num)
119114
{
120-
GPIO_CHECK_THROW(gpio_reset_pin(gpio_to_driver_type(gpio_num)));
115+
GPIO_CHECK_THROW(gpio_reset_pin(gpio_num.get_value<gpio_num_t>()));
121116
}
122117

123118
void GPIOBase::hold_en()
124119
{
125-
GPIO_CHECK_THROW(gpio_hold_en(gpio_to_driver_type(gpio_num)));
120+
GPIO_CHECK_THROW(gpio_hold_en(gpio_num.get_value<gpio_num_t>()));
126121
}
127122

128123
void GPIOBase::hold_dis()
129124
{
130-
GPIO_CHECK_THROW(gpio_hold_dis(gpio_to_driver_type(gpio_num)));
125+
GPIO_CHECK_THROW(gpio_hold_dis(gpio_num.get_value<gpio_num_t>()));
131126
}
132127

133128
void GPIOBase::set_drive_strength(GPIODriveStrength strength)
134129
{
135-
GPIO_CHECK_THROW(gpio_set_drive_capability(gpio_to_driver_type(gpio_num),
136-
static_cast<gpio_drive_cap_t>(strength.get_strength())));
130+
GPIO_CHECK_THROW(gpio_set_drive_capability(gpio_num.get_value<gpio_num_t>(),
131+
strength.get_value<gpio_drive_cap_t>()));
137132
}
138133

139134
GPIO_Output::GPIO_Output(GPIONum num) : GPIOBase(num)
140135
{
141-
GPIO_CHECK_THROW(gpio_set_direction(gpio_to_driver_type(gpio_num), GPIO_MODE_OUTPUT));
136+
GPIO_CHECK_THROW(gpio_set_direction(gpio_num.get_value<gpio_num_t>(), GPIO_MODE_OUTPUT));
142137
}
143138

144139
void GPIO_Output::set_high()
145140
{
146-
GPIO_CHECK_THROW(gpio_set_level(gpio_to_driver_type(gpio_num), 1));
141+
GPIO_CHECK_THROW(gpio_set_level(gpio_num.get_value<gpio_num_t>(), 1));
147142
}
148143

149144
void GPIO_Output::set_low()
150145
{
151-
GPIO_CHECK_THROW(gpio_set_level(gpio_to_driver_type(gpio_num), 0));
146+
GPIO_CHECK_THROW(gpio_set_level(gpio_num.get_value<gpio_num_t>(), 0));
152147
}
153148

154149
GPIODriveStrength GPIOBase::get_drive_strength()
155150
{
156151
gpio_drive_cap_t strength;
157-
GPIO_CHECK_THROW(gpio_get_drive_capability(gpio_to_driver_type(gpio_num), &strength));
152+
GPIO_CHECK_THROW(gpio_get_drive_capability(gpio_num.get_value<gpio_num_t>(), &strength));
158153
return GPIODriveStrength(static_cast<uint32_t>(strength));
159154
}
160155

161156
GPIOInput::GPIOInput(GPIONum num) : GPIOBase(num)
162157
{
163-
GPIO_CHECK_THROW(gpio_set_direction(gpio_to_driver_type(gpio_num), GPIO_MODE_INPUT));
158+
GPIO_CHECK_THROW(gpio_set_direction(gpio_num.get_value<gpio_num_t>(), GPIO_MODE_INPUT));
164159
}
165160

166161
GPIOLevel GPIOInput::get_level() const noexcept
167162
{
168-
int level = gpio_get_level(gpio_to_driver_type(gpio_num));
163+
int level = gpio_get_level(gpio_num.get_value<gpio_num_t>());
169164
if (level) {
170165
return GPIOLevel::HIGH;
171166
} else {
@@ -175,34 +170,34 @@ GPIOLevel GPIOInput::get_level() const noexcept
175170

176171
void GPIOInput::set_pull_mode(GPIOPullMode mode)
177172
{
178-
GPIO_CHECK_THROW(gpio_set_pull_mode(gpio_to_driver_type(gpio_num),
179-
static_cast<gpio_pull_mode_t>(mode.get_pull_mode())));
173+
GPIO_CHECK_THROW(gpio_set_pull_mode(gpio_num.get_value<gpio_num_t>(),
174+
mode.get_value<gpio_pull_mode_t>()));
180175
}
181176

182177
void GPIOInput::wakeup_enable(GPIOWakeupIntrType interrupt_type)
183178
{
184-
GPIO_CHECK_THROW(gpio_wakeup_enable(gpio_to_driver_type(gpio_num),
185-
static_cast<gpio_int_type_t>(interrupt_type.get_level())));
179+
GPIO_CHECK_THROW(gpio_wakeup_enable(gpio_num.get_value<gpio_num_t>(),
180+
interrupt_type.get_value<gpio_int_type_t>()));
186181
}
187182

188183
void GPIOInput::wakeup_disable()
189184
{
190-
GPIO_CHECK_THROW(gpio_wakeup_disable(gpio_to_driver_type(gpio_num)));
185+
GPIO_CHECK_THROW(gpio_wakeup_disable(gpio_num.get_value<gpio_num_t>()));
191186
}
192187

193188
GPIO_OpenDrain::GPIO_OpenDrain(GPIONum num) : GPIOInput(num)
194189
{
195-
GPIO_CHECK_THROW(gpio_set_direction(gpio_to_driver_type(gpio_num), GPIO_MODE_INPUT_OUTPUT_OD));
190+
GPIO_CHECK_THROW(gpio_set_direction(gpio_num.get_value<gpio_num_t>(), GPIO_MODE_INPUT_OUTPUT_OD));
196191
}
197192

198193
void GPIO_OpenDrain::set_floating()
199194
{
200-
GPIO_CHECK_THROW(gpio_set_level(gpio_to_driver_type(gpio_num), 1));
195+
GPIO_CHECK_THROW(gpio_set_level(gpio_num.get_value<gpio_num_t>(), 1));
201196
}
202197

203198
void GPIO_OpenDrain::set_low()
204199
{
205-
GPIO_CHECK_THROW(gpio_set_level(gpio_to_driver_type(gpio_num), 0));
200+
GPIO_CHECK_THROW(gpio_set_level(gpio_num.get_value<gpio_num_t>(), 0));
206201
}
207202

208203
}

host_test/fixtures/test_fixtures.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ struct GPIOFixture : public CMockFixture {
8686
GPIOFixture(idf::GPIONum gpio_num = idf::GPIONum(18), gpio_mode_t mode = GPIO_MODE_OUTPUT)
8787
: CMockFixture(), num(gpio_num)
8888
{
89-
gpio_reset_pin_ExpectAndReturn(static_cast<gpio_num_t>(num.get_num()), ESP_OK);
90-
gpio_set_direction_ExpectAndReturn(static_cast<gpio_num_t>(num.get_num()), mode, ESP_OK);
89+
gpio_reset_pin_ExpectAndReturn(static_cast<gpio_num_t>(num.get_value()), ESP_OK);
90+
gpio_set_direction_ExpectAndReturn(static_cast<gpio_num_t>(num.get_value()), mode, ESP_OK);
9191
}
9292

9393
idf::GPIONum num;

0 commit comments

Comments
 (0)