From ff83e4ad619bb21306f88203458ca290c6c2ba68 Mon Sep 17 00:00:00 2001 From: Veton Saliu Date: Tue, 13 Jul 2021 16:13:38 -0400 Subject: [PATCH 1/2] Add analog_read capability --- src/commandconstants.hpp | 5 +++++ src/main.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/commandconstants.hpp b/src/commandconstants.hpp index 5cbf1b8..162da02 100644 --- a/src/commandconstants.hpp +++ b/src/commandconstants.hpp @@ -30,6 +30,9 @@ int analog_write(CommandRouter *cmd, int argc, const char **argv); int analog_write_frequency(CommandRouter *cmd, int argc, const char **argv); int analog_write_resolution(CommandRouter *cmd, int argc, const char **argv); +// Analog IO +int analog_read(CommandRouter *cmd, int argc, const char **argv); + // SPI support int spi_begin(CommandRouter *cmd, int argc, const char **argv); int spi_end(CommandRouter *cmd, int argc, const char **argv); @@ -98,6 +101,8 @@ command_item_t command_list[] = { "analog_write_frequency pin frequency", analog_write_frequency}, {"analog_write_resolution", "Write the resolution of the PWM timer", "analog_write_resolution bitdepth", analog_write_resolution}, + {"analog_read", "Read the value of an analog pin, 0-255", + "analog_read pin", analog_read}, {"spi_begin", "SPI Begin", "spi_begin", spi_begin}, {"spi_end", "SPI End", "spi_end", spi_begin}, {"spi_set_mosi", "SPI set MOSI", "spi_set_mosi mosi", spi_set_mosi}, diff --git a/src/main.cpp b/src/main.cpp index 3d1d3a5..aa6f74b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -332,6 +332,17 @@ int analog_write(CommandRouter *cmd, int argc, const char **argv) { return 0; } +int analog_read(CommandRouter *cmd, int argc, const char **argv) { + if (argc != 2) + return EINVAL; + + uint8_t pin = strtol(argv[1], nullptr, 0); + uint8_t value = analogRead(pin); + + snprintf(cmd->buffer, cmd->buffer_size, "%d", value); + return 0; +} + int analog_write_frequency(CommandRouter *cmd, int argc, const char **argv) { int pin; int frequency; From 77a11ef3632b8c374506f9bb9d17af27226b190b Mon Sep 17 00:00:00 2001 From: Veton Saliu Date: Wed, 14 Jul 2021 09:16:17 -0400 Subject: [PATCH 2/2] Increment the version --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75cb239..1f3276a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 0.0.15 + +* Add the ability to do analog reads. + ### 0.0.14 * Enable reading and writing a contiguous payload of bytes