refactoring

This commit is contained in:
2024-02-05 18:56:51 +03:00
parent b1007e815d
commit 194095da7d
11 changed files with 147 additions and 219 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
/.svn
/.vscode
CMakeLists.txt.user*
/release
/build

View File

@@ -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
}

View File

@@ -1,7 +0,0 @@
{
"recommendations": [
"marus25.cortex-debug",
"ms-vscode.cmake-tools",
"ms-vscode.cpptools"
]
}

32
.vscode/launch.json vendored
View File

@@ -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"
]
}
]
}

77
.vscode/settings.json vendored
View File

@@ -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"
},
}

View File

@@ -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")

59
Valve.cpp Normal file
View File

@@ -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<seconds>(steady_clock::now() - start_time) > timeout) {
gpio_put(motor1, 0);
gpio_put(motor2, 0);
state = State::Unknown;
}
}
return state;
}

30
Valve.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <chrono>
#include <pico/stdlib.h>
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;
};

View File

@@ -1,89 +1,8 @@
#include <chrono>
#include "Valve.h"
#include "gpio_common.h"
#include <pico/stdlib.h>
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<seconds>(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 {

26
gpio_common.c Normal file
View File

@@ -0,0 +1,26 @@
#include "gpio_common.h"
#include <pico/stdlib.h>
#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

17
gpio_common.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <pico/types.h>
void gpio_init_out(uint gpio);
void gpio_init_input(uint gpio);
#ifdef __cplusplus
}
#endif