From 46cf67ee91eedc0d1c7e77fea021c625218e6f2a Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Tue, 29 Aug 2023 23:57:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BC=D1=91=D0=BD=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.sh | 4 +-- gui/app.js | 6 +++-- src/config.h | 3 +++ src/device.c | 34 ------------------------- src/device.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/main.c | 19 +++++++------- src/ui.c | 28 --------------------- src/ui.h | 36 ++++++++++++++++++++------- 8 files changed, 112 insertions(+), 88 deletions(-) delete mode 100644 src/device.c delete mode 100644 src/ui.c diff --git a/build.sh b/build.sh index bafd894..c4a6ffa 100755 --- a/build.sh +++ b/build.sh @@ -10,11 +10,11 @@ rm brakeconf &> /dev/null echo "[ 2/$STEPS] Build GUI..." python3 ./build_gui.py gui/index.html dist/index.html &> /dev/null -xxd -i -n html_document dist/index.html > src/html.h +xxd -i -n app_html dist/index.html > src/html.h # sed -i 's/unsigned char/const unsigned char/g' src/html.h echo "[ 3/$STEPS] Compile..." -gcc -I./webui -o brakeconf src/device.c src/main.c src/ui.c webui/libwebui-2-static.a -lserialport -ljansson +gcc -I./webui -o brakeconf src/main.c webui/libwebui-2-static.a -lserialport -ljansson echo "[ 4/$STEPS] Running..." ./brakeconf icanthink diff --git a/gui/app.js b/gui/app.js index 234146b..ecb9f18 100644 --- a/gui/app.js +++ b/gui/app.js @@ -125,7 +125,7 @@ function refresh_ports() { } /** - * Подключается к устройству + * Подключается к выбранному устройству */ function connect_to_device() { const port_selector = document.getElementById('port_selector'); @@ -137,7 +137,9 @@ function connect_to_device() { return; } - webui.call('webui_connect_device', port_selector.value); + webui.call('webui_connect_device', port_selector.value).then((response) => { + alert(`!: ${response}`); + }); if (btn_device_connect.innerText == 'Подключиться') { port_selector.disabled = true; diff --git a/src/config.h b/src/config.h index f2da001..939b318 100644 --- a/src/config.h +++ b/src/config.h @@ -8,4 +8,7 @@ #define DEBUG +#include "html.h" +struct sp_port *serial_port; + #endif diff --git a/src/device.c b/src/device.c deleted file mode 100644 index f53ed95..0000000 --- a/src/device.c +++ /dev/null @@ -1,34 +0,0 @@ -/* - * AUTHOR: Alexander Popov - * DESC: ... - */ - -#include "device.h" - -json_t *get_serial_ports() { - struct sp_port **port_list; - enum sp_return result = sp_list_ports(&port_list); - - json_t *ports_data = NULL; - json_t *ports_array = NULL; - - ports_array = json_array(); - - if (result == SP_OK) { - /* Get the name of the port. */ - int i; - for (i = 0; port_list[i] != NULL; i++) { - struct sp_port *port = port_list[i]; - char *port_name = sp_get_port_name(port); - - json_array_append(ports_array, json_string(port_name)); - } - - sp_free_port_list(port_list); - } - - ports_data = json_object(); - json_object_set_new(ports_data, "ports", ports_array); - - return ports_data; -} diff --git a/src/device.h b/src/device.h index bebd9fc..cc4dbf8 100644 --- a/src/device.h +++ b/src/device.h @@ -6,9 +6,73 @@ #ifndef DEVICE_H_ #define DEVICE_H_ -#include -#include +int check(enum sp_return result) { + char *error_message; -json_t *get_serial_ports(); + switch (result) { + case SP_ERR_ARG: + puts("Error: Invalid argument."); + abort(); + case SP_ERR_FAIL: + error_message = sp_last_error_message(); + printf("Error: Failed: %s\n", error_message); + sp_free_error_message(error_message); + abort(); + case SP_ERR_SUPP: + puts("Error: Not supported."); + abort(); + case SP_ERR_MEM: + puts("Error: Couldn't allocate memory."); + abort(); + case SP_OK: + default: + return result; + } +} + +json_t *device_get_available() { + struct sp_port **port_list; + enum sp_return result = sp_list_ports(&port_list); + + json_t *ports_data = NULL; + json_t *ports_array = NULL; + + ports_array = json_array(); + + if (result == SP_OK) { + /* Get the name of the port. */ + int i; + for (i = 0; port_list[i] != NULL; i++) { + struct sp_port *port = port_list[i]; + char *port_name = sp_get_port_name(port); + + json_array_append(ports_array, json_string(port_name)); + } + + sp_free_port_list(port_list); + } + + ports_data = json_object(); + json_object_set_new(ports_data, "ports", ports_array); + + return ports_data; +} + +int device_connect(const char *port_name) { + check(sp_get_port_by_name(port_name, &serial_port)); + check(sp_open(serial_port, SP_MODE_READ_WRITE)); + + check(sp_set_baudrate(serial_port, 9600)); + check(sp_set_bits(serial_port, 8)); + check(sp_set_parity(serial_port, SP_PARITY_NONE)); + check(sp_set_stopbits(serial_port, 1)); + check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE)); + + return 0; +} + +int device_disconnect() { + sp_close(serial_port); +} #endif diff --git a/src/main.c b/src/main.c index 9978039..d2d2682 100644 --- a/src/main.c +++ b/src/main.c @@ -1,31 +1,30 @@ #include +#include #include - +#include +#include #include "webui.h" #include "config.h" -#include "html.h" -#include "ui.h" #include "device.h" - -struct sp_port *serial_port; +#include "ui.h" int main(int argc, char const *argv[]) { int app_window = webui_new_window(); - html_document[html_document_len] = '\0'; + app_html[app_html_len] = '\0'; #ifndef DEBUG webui_set_kiosk(app_window, true); #endif - webui_bind(app_window, "close_app", close_app); - webui_bind(app_window, "webui_refresh_ports", refresh_devices); - webui_bind(app_window, "webui_connect_device", connect_device); + webui_bind(app_window, "close_app", app_close); + webui_bind(app_window, "webui_refresh_ports", app_refresh_devices); + webui_bind(app_window, "webui_connect_device", app_connect_device); printf("Enjoy :)\n"); - webui_show(app_window, html_document); + webui_show(app_window, app_html); webui_wait(); return 0; diff --git a/src/ui.c b/src/ui.c deleted file mode 100644 index 599b597..0000000 --- a/src/ui.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - * AUTHOR: Alexander Popov - * DESC: ... - */ - -#include "ui.h" - -void close_app(webui_event_t* e) { - printf("Bye!\n"); - // webui_destroy(); - webui_exit(); -} - -void refresh_devices(webui_event_t* e) { - const char *available_ports = json_dumps(get_serial_ports(), 0); - -#ifdef DEBUG - printf("Доступные порты:\n%s\n", available_ports); -#endif - - webui_return_string(e, available_ports); -} - -void connect_device(webui_event_t* e) { - const char* str = webui_get_string(e); - - webui_return_bool(e, true); -} diff --git a/src/ui.h b/src/ui.h index fd37599..1dadd49 100644 --- a/src/ui.h +++ b/src/ui.h @@ -6,16 +6,34 @@ #ifndef UI_H_ #define UI_H_ -#include -#include -#include -#include "webui.h" +void app_close(webui_event_t* e) { + printf("Bye!\n"); + // webui_destroy(); + webui_exit(); +} -#include "config.h" -#include "device.h" +void app_refresh_devices(webui_event_t* e) { + const char *available_ports = json_dumps(device_get_available(), 0); -void close_app(webui_event_t* e); -void connect_device(webui_event_t* e); -void refresh_devices(webui_event_t* e); +#ifdef DEBUG + printf("Доступные порты:\n%s\n", available_ports); +#endif + + webui_return_string(e, available_ports); +} + +void app_connect_device(webui_event_t* e) { + const char* port = webui_get_string(e); + int result; + +#ifdef DEBUG + printf("Подключение к порту: %s\n", port); +#endif + + // TODO: Необходимо проверить подключение + result = device_connect(port); + + webui_return_bool(e, (bool)result); +} #endif