Обновление имён функций

This commit is contained in:
Alexander Popov 2023-08-29 23:57:06 +03:00
parent 74aeca7e7d
commit 46cf67ee91
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
8 changed files with 112 additions and 88 deletions

View File

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

View File

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

View File

@ -8,4 +8,7 @@
#define DEBUG
#include "html.h"
struct sp_port *serial_port;
#endif

View File

@ -1,34 +0,0 @@
/*
* AUTHOR: Alexander Popov <iiiypuk {at} fastmail.fm>
* 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;
}

View File

@ -6,9 +6,73 @@
#ifndef DEVICE_H_
#define DEVICE_H_
#include <libserialport.h>
#include <jansson.h>
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

View File

@ -1,31 +1,30 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <libserialport.h>
#include <jansson.h>
#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;

View File

@ -1,28 +0,0 @@
/*
* AUTHOR: Alexander Popov <iiiypuk {at} fastmail.fm>
* 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);
}

View File

@ -6,16 +6,34 @@
#ifndef UI_H_
#define UI_H_
#include <stdio.h>
#include <libserialport.h>
#include <jansson.h>
#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