Refactor UART handling and TUI dialogs: integrate UartThreadFlags structure, update uartListener to manage UART status, and add functions for person dialog operations.
This commit is contained in:
+12
-4
@@ -22,7 +22,6 @@ int main(int argc, char const **argv)
|
||||
}
|
||||
|
||||
int y_max, x_max;
|
||||
BYTE uart_status = FALSE;
|
||||
char uart_address[51];
|
||||
pthread_t uart_thread;
|
||||
|
||||
@@ -79,17 +78,24 @@ int main(int argc, char const **argv)
|
||||
|
||||
uartDialog(uart_address, x_max, y_max);
|
||||
|
||||
UartThreadFlags flags;
|
||||
flags.end_from_main_flag = FALSE;
|
||||
flags.wait_for_UART_flag = FALSE;
|
||||
|
||||
UartThreadArgs *args = malloc(sizeof(UartThreadArgs));
|
||||
args->uart_address = uart_address;
|
||||
args->person_list_head = &person_list;
|
||||
args->uart_status = &uart_status;
|
||||
args->flags = &flags;
|
||||
|
||||
pthread_create(&uart_thread, NULL, uartListener, args);
|
||||
|
||||
usleep(1000);
|
||||
while (flags.wait_for_UART_flag)
|
||||
;
|
||||
|
||||
usleep(50000);
|
||||
|
||||
attron(A_BOLD);
|
||||
if (uart_status == TRUE)
|
||||
if (flags.uart_status == TRUE)
|
||||
{
|
||||
mvprintw(y_max - 1, 0, "RFID reader status: ");
|
||||
attron(COLOR_PAIR(3));
|
||||
@@ -108,6 +114,8 @@ int main(int argc, char const **argv)
|
||||
|
||||
mainMenu(&person_list, argv[1], x_max, y_max);
|
||||
|
||||
flags.end_from_main_flag = TRUE;
|
||||
|
||||
pthread_join(uart_thread, NULL);
|
||||
free(args);
|
||||
|
||||
|
||||
+28
-7
@@ -17,17 +17,18 @@ void *uartListener(void *arg)
|
||||
|
||||
char *uart_address = thread_args->uart_address;
|
||||
list_t *person_list = thread_args->person_list_head;
|
||||
BYTE *uart_status = thread_args->uart_status;
|
||||
UartThreadFlags *flags = thread_args->flags;
|
||||
|
||||
flags->wait_for_UART_flag = TRUE;
|
||||
|
||||
int uart = open(uart_address, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (uart == -1)
|
||||
{
|
||||
//perror("Failed to open UART");
|
||||
*uart_status = FALSE;
|
||||
// perror("Failed to open UART");
|
||||
flags->wait_for_UART_flag = FALSE;
|
||||
flags->uart_status = FALSE;
|
||||
return NULL;
|
||||
}
|
||||
*uart_status = TRUE;
|
||||
|
||||
struct termios options;
|
||||
tcgetattr(uart, &options);
|
||||
|
||||
@@ -49,10 +50,14 @@ void *uartListener(void *arg)
|
||||
|
||||
if (tcsetattr(uart, TCSANOW, &options) != 0)
|
||||
{
|
||||
perror("tcsetattr");
|
||||
exit(EXIT_FAILURE);
|
||||
flags->wait_for_UART_flag = FALSE;
|
||||
flags->uart_status = FALSE;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
flags->uart_status = TRUE;
|
||||
flags->wait_for_UART_flag = FALSE;
|
||||
|
||||
enum
|
||||
{
|
||||
SYNC0,
|
||||
@@ -64,13 +69,29 @@ void *uartListener(void *arg)
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (flags->end_from_main_flag)
|
||||
{
|
||||
close(uart);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t byte;
|
||||
ssize_t n = read(uart, &byte, 1);
|
||||
if (n == -1)
|
||||
{
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
{
|
||||
usleep(1000);
|
||||
continue;
|
||||
}
|
||||
perror("read");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (n == 0)
|
||||
{
|
||||
usleep(1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
|
||||
@@ -686,4 +686,14 @@ int exportDialog(list_t *_list, int terminal_x, int terminal_y)
|
||||
|
||||
void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y)
|
||||
{
|
||||
}
|
||||
|
||||
void addPersonDialog(list_t *_person_list, int terminal_x, int terminal_y)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void deletePersonDialog(list_t *_person_list, int terminal_x, int terminal_y)
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user