Im Simu-Mode Bedienung über Tastatur

This commit is contained in:
Bernhard
2026-01-24 18:56:30 +01:00
parent 4bde0cc888
commit 450e93982e
8 changed files with 167 additions and 42 deletions

92
main.c
View File

@@ -4,12 +4,17 @@
#include <can/can_client.h>
#include <io/io.h>
#include <settings/settings.h>
#include <termios.h>
// Period info of the realtime task
struct period_info pinfo;
int iThreadControl = 0; // 0: thread is running, <0: thread shall exit, >0 thread has exited
int iLogToConsole = 1;
atomic_short asThreadControl = ATOMIC_VAR_INIT(0); // 0: thread is running, <0: thread shall exit, >0 thread has exited
// values for simulation
atomic_bool abKeyPlus = ATOMIC_VAR_INIT(false);
atomic_bool abKeyMinus = ATOMIC_VAR_INIT(false);
atomic_bool abKeyStop = ATOMIC_VAR_INIT(false);
/// @brief send a log message
/// @param prio
@@ -150,7 +155,7 @@ void *thread_func(void *data)
}
// Connect to mqtt broker
while (MqttClient_Connect() && (iThreadControl == 0))
while (MqttClient_Connect() && atomic_load(&asThreadControl) == 0)
{
mylog(LOG_ERR, "MqttClient_Connect() failed!");
sleep(10);
@@ -163,7 +168,7 @@ void *thread_func(void *data)
WriteOutputPin(GPIO_OUT_PWRON, HIGH);
// cyclic call of do_cyclic_1ms()
while (iThreadControl == 0)
while (atomic_load(&asThreadControl) == 0)
{
pinfo.cyclecounter++;
if (pinfo.cyclecounter > CYCLE_COUNTER_MAX)
@@ -185,11 +190,22 @@ void *thread_func(void *data)
Can_CloseInterface(0);
// signal thread has finnished
iThreadControl = 1;
atomic_store(&asThreadControl, 1);
return NULL;
}
// Funktion, um das Terminal in den "Raw Mode" zu versetzen
void set_conio_terminal_mode()
{
struct termios new_termios;
tcgetattr(0, &new_termios);
new_termios.c_lflag &= ~ICANON; // Deaktiviert den zeilenweisen Modus
new_termios.c_lflag &= ~ECHO; // Verhindert, dass die Taste angezeigt wird
tcsetattr(0, TCSANOW, &new_termios);
}
/// @brief catch signals and set flag to terminate for the realtime thread
/// @param signo
void sig_handler(int signo)
@@ -197,7 +213,7 @@ void sig_handler(int signo)
if ((signo == SIGINT) || (signo == SIGTERM))
{
mylog(LOG_INFO, "Received signal %d", signo);
iThreadControl = -1; // signal realtime thread to exit
atomic_store(&asThreadControl, -1); // signal realtime thread to exit
}
}
@@ -222,16 +238,24 @@ int main(int argc, char* argv[])
// Read the settings file after opening the log
Settings_ReadConfFile();
// catch signals
if (signal(SIGTERM, sig_handler) == SIG_ERR)
if (settings.iCanSimu)
{
mylog(LOG_ERR, "Can't catch SIGTERM");
exit(-1);
// in simulation mode switch terminal to raw mode
set_conio_terminal_mode();
}
if (signal(SIGINT, sig_handler) == SIG_ERR)
else
{
mylog(LOG_ERR, "Can't catch SIGINT");
exit(-2);
// catch signals
if (signal(SIGTERM, sig_handler) == SIG_ERR)
{
mylog(LOG_ERR, "Can't catch SIGTERM");
exit(-1);
}
if (signal(SIGINT, sig_handler) == SIG_ERR)
{
mylog(LOG_ERR, "Can't catch SIGINT");
exit(-2);
}
}
/* Lock memory */
@@ -281,7 +305,7 @@ int main(int argc, char* argv[])
/* Create a pthread with specified attributes */
iThreadControl = 0;
atomic_store(&asThreadControl, 0);
ret = pthread_create(&thread, &attr, thread_func, NULL);
if (ret)
{
@@ -289,6 +313,48 @@ int main(int argc, char* argv[])
goto out;
}
if (settings.iCanSimu)
{
// in simulation mode we use keys to control the motors
mylog(LOG_INFO, "*** CanRtDriver in Simu-Mode ***");
while (1)
{
char ch = getchar();
mylog(LOG_INFO, "SIMU: Taste gedrückt: %c", ch);
if (ch == '+')
{
atomic_store(&abKeyPlus, true);
}
else if (ch == '-')
{
atomic_store(&abKeyMinus, true);
}
else if (ch == 's')
{
atomic_store(&abKeyStop, true);
}
else if (ch == 'p')
{
if (iPowerSupplyOn)
{
MqttClient_SwitchPowerSupply(0);
iPowerSupplyOn = 0;
}
else
{
MqttClient_SwitchPowerSupply(1);
iPowerSupplyOn = 1;
}
}
else if ( ch == 'q')
{
atomic_store(&asThreadControl, -1); // signal realtime thread to exit
break;
}
}
}
// join the thread and wait for it to exit
ret = pthread_join(thread, NULL);
if (ret)