From 9b7f2309628f3429d37f962f4852b9efee2051c0 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sun, 9 Jun 2024 16:18:51 +0800 Subject: [PATCH] update --- CMakeLists.txt | 3 ++ src/basic/format_memory.cpp | 50 +++++++++++++++++++++++ src/basic/format_memory.hpp | 6 +++ src/mainwindow.cpp | 3 ++ src/tab/id_card_read_tab.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++ src/tab/id_card_read_tab.hpp | 43 ++++++++++++++++++++ test/test.cpp | 60 +++++++++++++++++++++++++++ 7 files changed, 261 insertions(+) create mode 100644 src/basic/format_memory.cpp create mode 100644 src/basic/format_memory.hpp create mode 100644 src/tab/id_card_read_tab.cpp create mode 100644 src/tab/id_card_read_tab.hpp create mode 100644 test/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fc3924..0b8b84f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,9 @@ set(PROJECT_SOURCES src/tab/xyrobot_tab.cpp + src/tab/id_card_read_tab.cpp + src/basic/format_memory.cpp + app.rc ) diff --git a/src/basic/format_memory.cpp b/src/basic/format_memory.cpp new file mode 100644 index 0000000..f2c4083 --- /dev/null +++ b/src/basic/format_memory.cpp @@ -0,0 +1,50 @@ +#include "format_memory.hpp" +// 函数:格式化内存并存储到 string 中 +std::string format_memory(const void* ptr, size_t size) { + std::ostringstream oss; + const unsigned char* buffer = static_cast(ptr); + const size_t bytes_per_line = 16; + + for (size_t i = 0; i < size; i += bytes_per_line) { + // 格式化地址 + oss << std::hex << std::setw(8) << std::setfill('0') << i << ": "; + + // 格式化每一行的内容 + for (size_t j = 0; j < bytes_per_line; ++j) { + if (i + j < size) { + // 格式化字节的十六进制值 + oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(buffer[i + j]) << " "; + } else { + // 对于填充空白处 + oss << " "; + } + + // 格式化可打印字符,非打印字符用'.'表示 + if (j == (bytes_per_line / 2) - 1) { + oss << " "; + } + } + + oss << " "; + + // 格式化每一行的可打印字符 + for (size_t j = 0; j < bytes_per_line; ++j) { + if (i + j < size) { + char c = buffer[i + j]; + // 判断是否为可打印字符 + if (c >= 32 && c <= 126) { + oss << c; + } else { + oss << "."; + } + } else { + // 对于填充空白处 + oss << " "; + } + } + + oss << std::endl; + } + + return oss.str(); +} \ No newline at end of file diff --git a/src/basic/format_memory.hpp b/src/basic/format_memory.hpp new file mode 100644 index 0000000..bc89011 --- /dev/null +++ b/src/basic/format_memory.hpp @@ -0,0 +1,6 @@ +#pragma once +#include +#include +#include + +std::string format_memory(const void* ptr, size_t size); \ No newline at end of file diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a33d81f..7e7783a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -24,6 +24,7 @@ #include "tab/board_ext_tab.hpp" #include "tab/code_scaner_tab.hpp" #include "tab/fan_ctrl_tab.hpp" +#include "tab/id_card_read_tab.hpp" #include "tab/mini_servo_tab.hpp" #include "tab/module_opera_tab.hpp" #include "tab/pipette_ctrl_tab.hpp" @@ -304,6 +305,8 @@ void MainWindow::constructAppUI() { WaterCoolingTemperatureControlerTab::inst()->construct(ui->buttonTabWidget); FanCtrlTab::inst()->construct(ui->buttonTabWidget); XYRobotTab::inst()->construct(ui->buttonTabWidget); + + IDCardReaderTab::inst()->construct(ui->buttonTabWidget); } void MainWindow::on_PublicState_ConfirmKey_clicked() {} diff --git a/src/tab/id_card_read_tab.cpp b/src/tab/id_card_read_tab.cpp new file mode 100644 index 0000000..7f45bae --- /dev/null +++ b/src/tab/id_card_read_tab.cpp @@ -0,0 +1,96 @@ +#include "id_card_read_tab.hpp" + +#include "iflytop_canbus/iflytop_canbus_master.hpp" +#include "logger.hpp" +#include "qt_serial_datachannel.hpp" +#include "zexception.hpp" +#include "zui/z_function_list_box.hpp" +#include "zui/z_reg_table_list_box.hpp" +#include "zui/zq_vtab_page.hpp" +// +#include "../basic/format_memory.hpp" +#include "../basic/stm32_pin.hpp" +#include "./mainwindow.h" +#include "zui\zqui.hpp" +// +using namespace iflytop; +using namespace std; + +extern Ui::MainWindow *main_ui; + +extern int getDeviceId(); + +IDCardReaderTab *IDCardReaderTab::inst() { + static IDCardReaderTab *ins = new IDCardReaderTab(); + return ins; +} + +static inline const char *hex2binstr(int32_t hex) { + static char buf[32]; + sprintf(buf, "%d%d%d%d_%d%d%d%d", (hex >> 7) & 1, (hex >> 6) & 1, (hex >> 5) & 1, (hex >> 4) & 1, (hex >> 3) & 1, (hex >> 2) & 1, (hex >> 1) & 1, (hex >> 0) & 1); + return buf; +} + +/** + * @brief + * format_memery + * + * 00 01 02 03 04 05 06 07 08 09 A B C D E F + * 12 34 57 90 AA XX XX XX XX XX XX XX XX XX ...dsfa.. + * 12 34 57 90 AA XX XX XX XX XX XX XX XX XX ...dsfa.. + * 12 34 57 90 AA XX XX XX XX XX XX XX XX XX ...dsfa.. + * 12 34 57 90 AA XX XX XX XX XX XX XX XX XX ...dsfa.. + * + */ +string format_memery(uint8_t *mem, int len) { + string result = ""; + char buf[128]; +} + +void IDCardReaderTab::construct(QTabWidget *fathertab) { + ZQVTabPage *tab = new ZQVTabPage(fathertab, "A8K读卡器"); + /*********************************************************************************************************************** + * 模块操作 * + ***********************************************************************************************************************/ + { + ZQFunctionListBox *box = new ZQFunctionListBox(tab, "方法", 4); + + box->newFunc("读卡", {"sector_index"}, [this](int argn, const char **args) { + int32_t sector_index = atoi(args[0]); + ICM->callcmd1(getDeviceId(), ka8000_idcard_reader_read_raw, sector_index); + + uint8_t *resultbuf = ICM->getAckBuf(); + int32_t resultlen = ICM->getAckBufLen(); + + if (resultlen > 0) { + QString result = QString(format_memory(resultbuf, resultlen).c_str()); + ZQUI::ins()->ishow("读卡结果: %s", result.toStdString().c_str()); + } else { + ZQUI::ins()->ishow("读卡失败"); + } + }); + } + + /*********************************************************************************************************************** + * 寄存器操作 * + ***********************************************************************************************************************/ + { + ZRegTableList *tableBox = new ZRegTableList(tab, "寄存器操作"); + tableBox->initializeRegOperation( + [this](int32_t add, int32_t val) { // + ICM->writereg(getDeviceId(), add, val); + return true; + }, + [this](int32_t add, int32_t *val) { // + ICM->readreg(getDeviceId(), add, val); + return true; + }); + tableBox->addReg("reg_id_card_reader_raw_sector_size", kreg_id_card_reader_raw_sector_size, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("reg_id_card_reader_raw_sector_num", kreg_id_card_reader_raw_sector_num, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("reg_id_card_reader_is_online", kreg_id_card_reader_is_online, ZRegItem::krw | ZRegItem::kdec); + + tableBox->addSpacer(); + } + + tab->addSpacer(); +} diff --git a/src/tab/id_card_read_tab.hpp b/src/tab/id_card_read_tab.hpp new file mode 100644 index 0000000..39bcefd --- /dev/null +++ b/src/tab/id_card_read_tab.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// +#include + +namespace iflytop { +using namespace std; + +class IDCardReaderTab { + public: + static IDCardReaderTab *inst(); + + void construct(QTabWidget *fathertab); +}; +} // namespace iflytop diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..cf483a0 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +// 函数:格式化内存并存储到 string 中 +std::string format_memory(const void* ptr, size_t size) { + std::ostringstream oss; + const unsigned char* buffer = static_cast(ptr); + const size_t bytes_per_line = 16; + + for (size_t i = 0; i < size; i += bytes_per_line) { + // 格式化地址 + oss << std::hex << std::setw(8) << std::setfill('0') << i << ": "; + + // 格式化每一行的内容 + for (size_t j = 0; j < bytes_per_line; ++j) { + if (i + j < size) { + // 格式化字节的十六进制值 + oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(buffer[i + j]) << " "; + } else { + // 对于填充空白处 + oss << " "; + } + + // 格式化可打印字符,非打印字符用'.'表示 + if (j == (bytes_per_line / 2) - 1) { + oss << " "; + } + } + + oss << " "; + + // 格式化每一行的可打印字符 + for (size_t j = 0; j < bytes_per_line; ++j) { + if (i + j < size) { + char c = buffer[i + j]; + // 判断是否为可打印字符 + if (c >= 32 && c <= 126) { + oss << c; + } else { + oss << "."; + } + } else { + // 对于填充空白处 + oss << " "; + } + } + + oss << std::endl; + } + + return oss.str(); +} + +int main() { + int array[] = {1, 2, 3, 4, 5}; + std::string formatted_memory = format_memory(array, sizeof(array)); + std::cout << formatted_memory << std::endl; + return 0; +}