diff --git a/.gitignore b/.gitignore index 48a9424..4c1dd93 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.svn +/.vscode CMakeLists.txt.user* /release /build diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index ac817e5..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/**", - "${env:PICO_SDK_PATH}/**" - ], - "defines": [], - "compilerPath": "C:/workRaspberry_Pi/Pico_SDK_v1.5.1/gcc-arm-none-eabi/bin/arm-none-eabi-gcc.exe", - "cStandard": "gnu17", - "cppStandard": "gnu++14", - "intelliSenseMode": "linux-gcc-arm", - "configurationProvider" : "ms-vscode.cmake-tools" - } - ], - "version": 4 -} diff --git a/.vscode/extensions.json b/.vscode/extensions.json deleted file mode 100644 index 95226d5..0000000 --- a/.vscode/extensions.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "recommendations": [ - "marus25.cortex-debug", - "ms-vscode.cmake-tools", - "ms-vscode.cpptools" - ] -} diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 3776960..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Cortex Debug", - "cwd": "${workspaceRoot}", - "executable": "${command:cmake.launchTargetPath}", - "request": "launch", - "type": "cortex-debug", - "servertype": "openocd", - "gdbPath": "gdb-multiarch", - "serverArgs": [ - - ], - "device": "RP2040", - "configFiles": [ - "interface/picoprobe.cfg", - "target/rp2040.cfg" - ], - "svdFile": "${env:PICO_SDK_PATH}/src/rp2040/hardware_regs/rp2040.svd", - "runToEntryPoint": "main", - // Give restart the same functionality as runToEntryPoint - main - "postRestartCommands": [ - "break main", - "continue" - ] - } - ] -} diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index f9a0259..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,77 +0,0 @@ -{ - "cmake.configureOnOpen": false, - "cmake.statusbar.advanced": { - "debug": { - "visibility": "hidden" - }, - "launch": { - "visibility": "hidden" - }, - "build": { - "visibility": "hidden" - }, - "buildTarget": { - "visibility": "hidden" - }, - }, - "clang-format.executable": "C:/qt/qt-creator-8_win_x64/bin/clang-format.exe", - "clang-format.style": "file", - "editor.formatOnSave": true, - "[cpp]": { - "editor.defaultFormatter": "demiaochen.clang-format-indent-4", - }, - "files.associations": { - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "complex": "cpp", - "concepts": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "list": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "string": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "initializer_list": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "ostream": "cpp", - "ranges": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "cinttypes": "cpp", - "typeinfo": "cpp", - "chrono": "cpp", - "ctime": "cpp", - "ratio": "cpp" - }, -} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index f594e80..efa68cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,12 @@ project(ValveController C CXX ASM) # Initialise the Raspberry Pi Pico SDK pico_sdk_init() -add_executable(ValveController ValveController.cpp) +add_executable(ValveController) + +file(GLOB SRCS "*.cpp" "*.c") +file(GLOB HDRS "*.h") +message("SOURCES=${SRCS}") +target_sources(ValveController PRIVATE ${SRCS} ${HDRS}) pico_set_program_name(ValveController "ValveController") pico_set_program_version(ValveController "0.1") diff --git a/Valve.cpp b/Valve.cpp new file mode 100644 index 0000000..b0700bf --- /dev/null +++ b/Valve.cpp @@ -0,0 +1,59 @@ +#include "Valve.h" + +#include "gpio_common.h" + +using namespace std::chrono; + + +Valve::Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end) + : motor1(gpio_motor1) + , motor2(gpio_motor2) + , start_pin(gpio_start) + , end_pin(gpio_end) { + gpio_init_out(motor1); + gpio_init_out(motor2); + gpio_init_input(start_pin); + gpio_init_input(end_pin); + start_time = steady_clock::now(); +} + + +void Valve::open() { + if (state == State::Opening) { + return; + } + gpio_put(motor2, 0); + gpio_put(motor1, 1); + state = State::Opening; + start_time = steady_clock::now(); +} + + +void Valve::close() { + if (state == State::Closing) { + return; + } + gpio_put(motor1, 0); + gpio_put(motor2, 1); + state = State::Closing; + start_time = steady_clock::now(); +} + +Valve::State Valve::process() { + if ((state == State::Opening) && (gpio_get(start_pin) == 0)) { + gpio_put(motor1, 0); + state = State::Opened; + } + if ((state == State::Closing) && (gpio_get(end_pin) == 0)) { + gpio_put(motor2, 0); + state = State::Closed; + } + if (state == State::Opening || state == State::Closing) { + if (duration_cast(steady_clock::now() - start_time) > timeout) { + gpio_put(motor1, 0); + gpio_put(motor2, 0); + state = State::Unknown; + } + } + return state; +} diff --git a/Valve.h b/Valve.h new file mode 100644 index 0000000..f4be57d --- /dev/null +++ b/Valve.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + + +class Valve { +public: + enum class State { + Unknown, + Opening, + Opened, + Closing, + Closed + }; + + Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end); + + void open(); + void close(); + State process(); + State getState() const { return state; } + +private: + uint motor1, motor2; + uint start_pin, end_pin; + State state = State::Unknown; + std::chrono::seconds timeout = std::chrono::seconds(10); + decltype(std::chrono::steady_clock::now()) start_time; +}; diff --git a/ValveController.cpp b/ValveController.cpp index 38d07c9..706469b 100644 --- a/ValveController.cpp +++ b/ValveController.cpp @@ -1,89 +1,8 @@ -#include +#include "Valve.h" +#include "gpio_common.h" + #include -void gpio_init_out(uint gpio) { - gpio_set_dir(gpio, GPIO_OUT); - gpio_put(gpio, 0); - gpio_set_function(gpio, GPIO_FUNC_SIO); -} - -void gpio_init_input(uint gpio) { - gpio_set_dir(gpio, GPIO_IN); - gpio_pull_up(gpio); - gpio_set_function(gpio, GPIO_FUNC_SIO); -} - -class Valve { -public: - enum class State { - Unknown, - Opening, - Opened, - Closing, - Closed - }; - - Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end) - : motor1(gpio_motor1) - , motor2(gpio_motor2) - , start_pin(gpio_start) - , end_pin(gpio_end) { - gpio_init_out(motor1); - gpio_init_out(motor2); - gpio_init_input(start_pin); - gpio_init_input(end_pin); - start_time = std::chrono::steady_clock::now(); - } - - void open() { - if (state == State::Opening) { - return; - } - gpio_put(motor2, 0); - gpio_put(motor1, 1); - state = State::Opening; - start_time = std::chrono::steady_clock::now(); - } - - void close() { - if (state == State::Closing) { - return; - } - gpio_put(motor1, 0); - gpio_put(motor2, 1); - state = State::Closing; - start_time = std::chrono::steady_clock::now(); - } - - State process() { - if ((state == State::Opening) && (gpio_get(start_pin) == 0)) { - gpio_put(motor1, 0); - state = State::Opened; - } - if ((state == State::Closing) && (gpio_get(end_pin) == 0)) { - gpio_put(motor2, 0); - state = State::Closed; - } - if (state == State::Opening || state == State::Closing) { - using namespace std::chrono; - if (duration_cast(steady_clock::now() - start_time) > timeout) { - gpio_put(motor1, 0); - gpio_put(motor2, 0); - state = State::Unknown; - } - } - return state; - } - - State getState() const { return state; } - -private: - uint motor1, motor2; - uint start_pin, end_pin; - State state = State::Unknown; - std::chrono::seconds timeout = std::chrono::seconds(10); - decltype(std::chrono::steady_clock::now()) start_time; -}; int main() { Valve cold_valve{10, 11, 12, 13}; @@ -108,6 +27,11 @@ int main() { sleep_ms(tick_ms); const auto cst = cold_valve.process(); hot_valve.process(); + switch (cst) { + case Valve::State::Opened: + case Valve::State::Closed: gpio_put(PICO_DEFAULT_LED_PIN, 1); break; + default: break; + } if (cst == Valve::State::Opened || cst == Valve::State::Closed) { gpio_put(PICO_DEFAULT_LED_PIN, 1); } else { diff --git a/gpio_common.c b/gpio_common.c new file mode 100644 index 0000000..82a4cb3 --- /dev/null +++ b/gpio_common.c @@ -0,0 +1,26 @@ +#include "gpio_common.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +void gpio_init_out(uint gpio) { + gpio_set_dir(gpio, GPIO_OUT); + gpio_put(gpio, 0); + gpio_set_function(gpio, GPIO_FUNC_SIO); +} + + +void gpio_init_input(uint gpio) { + gpio_set_dir(gpio, GPIO_IN); + gpio_pull_up(gpio); + gpio_set_function(gpio, GPIO_FUNC_SIO); +} + + +#ifdef __cplusplus +} +#endif diff --git a/gpio_common.h b/gpio_common.h new file mode 100644 index 0000000..b41f948 --- /dev/null +++ b/gpio_common.h @@ -0,0 +1,17 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + + +void gpio_init_out(uint gpio); + +void gpio_init_input(uint gpio); + + +#ifdef __cplusplus +} +#endif