Skip to content

Commit 70d90b7

Browse files
committed
[app] Add command to write to a gpio
1 parent fe07de8 commit 70d90b7

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

app/commands.hh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include "flash/flash.hh"
8+
#include "embeddedpp/gpio.hh"
89
#include "visuals/progressbar.hh"
910
#include "visuals/throughput.hh"
1011
#include <fstream>
@@ -262,4 +263,23 @@ struct LoadFile : public Commands<T> {
262263
}
263264
};
264265

266+
template <typename T>
267+
requires embeddedpp::Gpio<T>
268+
struct GpioWrite {
269+
T& gpio;
270+
uint8_t pin;
271+
bool value;
272+
273+
GpioWrite(T& gpio, uint8_t pin, bool value) : gpio(gpio), pin(pin), value(value) {}
274+
275+
int run() {
276+
if (is_error(gpio.set_pin(pin, value))) {
277+
std::println("GPIO write failed");
278+
return 1;
279+
}
280+
std::println("GPIO{} = {}", pin, value ? 1 : 0);
281+
return 0;
282+
}
283+
};
284+
265285
} // namespace commands

app/main.cc

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "ftdi/ftdi.hh"
1414
#include "ftdi/spi_host.hh"
15+
#include "ftdi/gpio.hh"
1516
#include "flash/flash.hh"
1617
#include "commands.hh"
1718
#include "ftdi/log.hh"
@@ -41,17 +42,22 @@ static std::vector<ftdi::DeviceInfo> scan() {
4142
}
4243

4344
static std::unique_ptr<argparse::ArgumentParser>
44-
new_flash_command(const std::string& name, const std::string& desc) {
45+
new_command(const std::string& name, const std::string& desc) {
4546
auto cmd = std::make_unique<argparse::ArgumentParser>(name);
4647
cmd->add_description(desc);
47-
cmd->add_argument("--interface", "-i").help("One of [spi, i2c, gpio]");
4848
cmd->add_argument("--traces", "-t")
4949
.help("Enable ftdi traces")
5050
.default_value(false)
5151
.implicit_value(true);
5252
cmd->add_argument("--ftdi")
5353
.default_value(std::string("FT4222"))
5454
.help("Filter ftdi chips connected to USB");
55+
return cmd;
56+
}
57+
58+
static std::unique_ptr<argparse::ArgumentParser>
59+
new_flash_command(const std::string& name, const std::string& desc) {
60+
auto cmd = new_command(name, desc);
5561
cmd->add_argument("--clock")
5662
.default_value(std::size_t{15000000})
5763
.help("The spi clock speed in Hz.")
@@ -79,6 +85,22 @@ handle_flash_command(std::unique_ptr<argparse::ArgumentParser>& cmd) {
7985
exit(0);
8086
}
8187

88+
static std::optional<ftdi::Gpio>
89+
handle_gpio_command(std::unique_ptr<argparse::ArgumentParser>& cmd) {
90+
auto ftdi_filter = cmd->get<std::string>("--ftdi");
91+
auto devices = scan();
92+
auto filtered = ftdi::DeviceInfo::filter_by_description(devices, ftdi_filter);
93+
if (filtered.empty()) {
94+
std::print("No {} ftdi found.\n", ftdi_filter);
95+
exit(0);
96+
}
97+
if (auto opt = ftdi::Gpio::from_device_info(filtered[0])) {
98+
return opt;
99+
}
100+
std::println("Can't open GPIO.");
101+
exit(0);
102+
}
103+
82104
int main(int argc, char* argv[]) {
83105
std::map<std::string, Action> commands;
84106

@@ -204,6 +226,19 @@ int main(int argc, char* argv[]) {
204226
return 0;
205227
};
206228

229+
auto gpio_write_cmd = new_command("gpio-write", "Write a value to an FTDI GPIO pin.");
230+
gpio_write_cmd->add_argument("pin").help("GPIO pin number (0-3)").required().scan<'d', int>();
231+
gpio_write_cmd->add_argument("value").help("Value to write (0 or 1)").required().scan<'d', int>();
232+
program.add_subparser(*gpio_write_cmd);
233+
commands["gpio-write"] = [&]() -> int {
234+
auto pin = static_cast<uint8_t>(gpio_write_cmd->get<int>("pin"));
235+
auto value = gpio_write_cmd->get<int>("value") != 0;
236+
auto gpio = handle_gpio_command(gpio_write_cmd);
237+
commands::GpioWrite(*gpio, pin, value).run();
238+
gpio->close();
239+
return 0;
240+
};
241+
207242
if (argc == 1) {
208243
std::cout << program; // This prints the auto-generated help
209244
return 0;

0 commit comments

Comments
 (0)