优化约束

This commit is contained in:
JIe 2024-10-10 15:59:53 +08:00
parent e0094441c2
commit 517c4699d4
2 changed files with 104 additions and 38 deletions

View File

@ -22,6 +22,64 @@ namespace ranges = std::ranges;
namespace views = std::views; namespace views = std::views;
namespace serial { namespace serial {
template <typename... Types> struct _StrongType {};
template <typename T> struct _StrongType<T> {
using Type = T;
};
template <typename Ta, typename Tb> struct _StrongType<Ta, Tb> {
using Type = decltype(true ? std::declval<Ta>() : std::declval<Tb>());
};
template <typename T, typename... Types> struct _StrongType<T, Types...> {
using Type =
typename _StrongType<T, typename _StrongType<Types...>::Type>::Type;
};
template <typename... Types>
using _strongType_t = typename _StrongType<Types...>::Type;
template <typename... Args> struct _IsCastable {};
template <typename T> struct _IsCastable<T> {
static constexpr bool value = true;
using Type = T;
};
template <typename T, typename U> struct _IsCastable<T, U> {
using __TRUE = char;
using __FALSE = struct {
char _[2];
};
static consteval __TRUE __TEST(U);
static consteval __FALSE __TEST(...);
static constexpr bool value =
sizeof(__TEST(std::declval<T>())) == sizeof(__TRUE);
using Type = std::conditional_t<value, U, void>;
};
template <typename T, typename... Args> struct _IsCastable<T, Args...> {
static constexpr bool value =
_IsCastable<T, typename _IsCastable<Args...>::Type>::value;
using Type =
std::conditional_t<value, typename _IsCastable<Args...>::Type, void>;
};
template <typename T>
concept _SupportString = requires {
_IsCastable<T, const char *, char *, std::string, std::string_view>::value;
};
template <_SupportString T> std::string _to_string(T &&str) {
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
return str;
} else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>) {
return std::move(std::string(str.data()));
} else if constexpr (std::is_same_v<std::decay_t<T>, const char*>) {
return std::string(str);
}
}
static std::vector<std::string> GetUsbPorts() { static std::vector<std::string> GetUsbPorts() {
std::vector<std::string> portArray; std::vector<std::string> portArray;
@ -43,12 +101,6 @@ static std::vector<std::string> GetUsbPorts() {
return portArray; return portArray;
} }
template <typename T>
concept SupportString = requires {
std::is_same_v<T, const char *>;
std::is_same_v<T, std::string>;
};
enum class [[maybe_unused]] SerialErrorCode { enum class [[maybe_unused]] SerialErrorCode {
SUCCESS, SUCCESS,
TIMEOUT, TIMEOUT,
@ -73,6 +125,8 @@ class Serial {
Serial &operator=(Serial &&other) = delete; Serial &operator=(Serial &&other) = delete;
auto SetRemoveEcho(bool remove) { removeEcho = remove; } auto SetRemoveEcho(bool remove) { removeEcho = remove; }
auto SetDtr(bool flag) { return flag ? ser.setDTR() : ser.clearDTR(); }
auto SetRts(bool flag) { return flag ? ser.setRTS() : ser.clearRTS(); }
static std::string GetTimeNow() { static std::string GetTimeNow() {
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
@ -84,7 +138,7 @@ class Serial {
bool IsOpen() { return ser.isDeviceOpen(); } bool IsOpen() { return ser.isDeviceOpen(); }
template <SupportString T> template <_SupportString T>
bool OpenDevice(T portName, unsigned int bauds = 115200, bool OpenDevice(T portName, unsigned int bauds = 115200,
int delayTime = 0) { int delayTime = 0) {
std::string reallyPortName; std::string reallyPortName;
@ -118,7 +172,7 @@ class Serial {
ser.closeDevice(); ser.closeDevice();
} }
template <SupportString T> template <_SupportString T>
std::expected<std::string, SerialErrorCode> std::expected<std::string, SerialErrorCode>
DelayGetResponse(int delayTime, T command, int timeout = 50) { DelayGetResponse(int delayTime, T command, int timeout = 50) {
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime)); std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
@ -126,7 +180,7 @@ class Serial {
} }
template <int repeatTime = 5, int delayTime = 200, int timeout = 200, template <int repeatTime = 5, int delayTime = 200, int timeout = 200,
SupportString T, SupportString... Args> _SupportString T, _SupportString... Args>
bool GetAtUntilRepeat(T command, Args... args) { bool GetAtUntilRepeat(T command, Args... args) {
std::stringstream ss; std::stringstream ss;
int i = 0; int i = 0;
@ -141,16 +195,11 @@ class Serial {
return false; return false;
} }
template <SupportString T> template <_SupportString T>
std::expected<std::string, SerialErrorCode> std::expected<std::string, SerialErrorCode>
GetAtResponse(T command, int timeout = 50) { GetAtResponse(T command, int timeout = 50) {
ser.flushReceiver(); ser.flushReceiver();
std::string reallyCommand; std::string reallyCommand = std::string(command) + endChar;
if constexpr (std::is_same_v<T, std::string>) {
reallyCommand = command + endChar;
} else {
reallyCommand = std::string(command) + endChar;
}
ser.writeString(reallyCommand.c_str()); ser.writeString(reallyCommand.c_str());
Log("Send: " + reallyCommand); Log("Send: " + reallyCommand);
std::this_thread::sleep_for(10ms); std::this_thread::sleep_for(10ms);
@ -161,7 +210,7 @@ class Serial {
if (size > 0) { if (size > 0) {
buffer[size] = '\0'; buffer[size] = '\0';
std::string response = std::string(buffer); std::string response = std::string(buffer.get());
Log("Receive: " + response); Log("Receive: " + response);
if (removeEcho) if (removeEcho)
response.replace(0, reallyCommand.length(), ""); response.replace(0, reallyCommand.length(), "");
@ -170,7 +219,7 @@ class Serial {
return std::unexpected(SerialErrorCode::TIMEOUT); return std::unexpected(SerialErrorCode::TIMEOUT);
} }
template <SupportString T> template <_SupportString T>
auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1) auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1)
-> void { -> void {
for (int i = 0; i <= repeatTime; i++) { for (int i = 0; i <= repeatTime; i++) {
@ -178,17 +227,12 @@ class Serial {
} }
} }
template <int timeout = 200, SupportString T, SupportString... Args> template <int timeout = 200, _SupportString T, _SupportString... Args>
bool GetAtUntil(T command, Args... expect) { bool GetAtUntil(T command, Args... expect) {
auto endTime = std::chrono::system_clock::now() + auto endTime = std::chrono::system_clock::now() +
std::chrono::milliseconds(timeout); std::chrono::milliseconds(timeout);
ser.flushReceiver(); ser.flushReceiver();
std::string reallyCommand; std::string reallyCommand = std::string(command) + endChar;
if constexpr (std::is_same_v<T, std::string>) {
reallyCommand = command + endChar;
} else {
reallyCommand = std::string(command) + endChar;
}
ser.writeString(reallyCommand.c_str()); ser.writeString(reallyCommand.c_str());
Log("Send : " + reallyCommand); Log("Send : " + reallyCommand);
while (std::chrono::system_clock::now() < endTime) { while (std::chrono::system_clock::now() < endTime) {

48
main.cc
View File

@ -1,19 +1,40 @@
#include "include/serial.h"
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <cstdio>
#include <iostream> #include <iostream>
#include <ranges>
#include <limits> #include <limits>
#include <ranges>
#include <string_view> #include <string_view>
#include "include/serial.h"
#undef max #undef max
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
using namespace std::literals::string_view_literals;
using namespace std::literals::string_literals;
using namespace serial; using namespace serial;
namespace ranges = std::ranges; namespace ranges = std::ranges;
namespace views = std::views; namespace views = std::views;
void PrintLog(const std::string &msg) { std::cout << msg << std::endl; } void PrintLog(const std::string &msg) { std::cout << msg << std::endl; }
template <typename T>
requires std::is_same_v<T, std::string> ||
std::is_same_v<T, std::string_view>
std::vector<T> split(T str, T d) {
auto v = views::split(str, d) | views::transform([](auto word) {
return T(word.begin(), word.end());
});
return std::vector<T>(v.begin(), v.end());
}
int main() {
Serial ser;
ser.OpenDevice("COM11", 115200);
ser.SetLogCallBack(PrintLog);
ser.GetAtUntilRepeat<5>("AT", "OK");
return 0;
}
/*
int main(int argc, char **const argv) { int main(int argc, char **const argv) {
Serial serial; Serial serial;
auto ports = serial::GetUsbPorts(); auto ports = serial::GetUsbPorts();
@ -30,20 +51,20 @@ int main(int argc, char **const argv) {
std::getline(std::cin, command); std::getline(std::cin, command);
system("cls"); system("cls");
auto startTime = std::chrono::system_clock::now(); auto startTime = std::chrono::system_clock::now();
if (command.find(":") != std::string::npos) { if (command.at(0) == ':') {
command.erase(std::remove_if(command.begin(), command.end(), command = command.substr(1, command.length() - 1);
[](char c) { return c == ':'; }), auto sp = split(command, " "s);
command.end()); auto reallyCommand = sp[0];
auto reallyCommand = command.substr(0, command.find_first_of(' ')); auto expect = sp[1];
auto expect = command.substr(command.find_first_of(' ') + 1, auto res = serial.GetAtUntil(reallyCommand, 2000, expect);
command.length());
auto res = serial.GetAtUntil(reallyCommand, expect, 2000);
auto endTime = std::chrono::system_clock::now(); auto endTime = std::chrono::system_clock::now();
std::cout << "dura: " res ? std::cout
<< "dura: "
<< std::chrono::duration_cast<std::chrono::milliseconds>( << std::chrono::duration_cast<std::chrono::milliseconds>(
endTime - startTime) endTime - startTime)
.count() .count()
<< std::endl; << std::endl
: std::cout << "Recive Error";
} else { } else {
auto resp = serial.GetAtResponse(command); auto resp = serial.GetAtResponse(command);
auto endTime = std::chrono::system_clock::now(); auto endTime = std::chrono::system_clock::now();
@ -57,3 +78,4 @@ int main(int argc, char **const argv) {
} }
return 0; return 0;
} }
*/