C Runtime for Lingua Franca
No Matches
sensor_simulator.c File Reference

Simple terminal-based user interface based on ncurses. See sensor_simulator.h. More...

#include <pthread.h>
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include "sensor_simulator.h"
#include "include/api/api.h"
#include "util.h"
#include "platform.h"

Data Structures

struct  _lf_sensor_message_t


#define WGETCHR_TIMEOUT   1000


typedef struct _lf_sensor_message_t _lf_sensor_message_t


enum  _lf_sensor_message_type { _lf_sensor_message , _lf_sensor_tick , _lf_sensor_close_windows }


void _lf_show_message (const char *message_lines[], int number_of_lines)
void _lf_start_tick_window (int width)
void _lf_start_print_window (int above, int right)
void _lf_sensor_post_message (enum _lf_sensor_message_type type, char *body)
void _lf_print_message_function (const char *format, va_list args)
void_lf_sensor_read_input (void *ignored)
void_lf_sensor_simulator_thread (void *ignored)
void end_sensor_simulator ()
int start_sensor_simulator (const char *message_lines[], int number_of_lines, int tick_window_width, char *log_file, int log_level)
void show_tick (const char *character)
int register_sensor_key (char key, void *action)


trigger_t_lf_sensor_trigger_table [LF_SENSOR_TRIGGER_TABLE_SIZE]
trigger_t_lf_sensor_sensor_newline_trigger = NULL
trigger_t_lf_sensor_any_key_trigger = NULL
lf_mutex_t _lf_sensor_mutex
lf_cond_t _lf_sensor_simulator_cond_var
struct { 
   lf_thread_t   input_thread_id 
   lf_thread_t   output_thread_id 
   int   thread_created 
   WINDOW *   default_window 
   WINDOW *   tick_window 
   int   tick_cursor_x 
   int   tick_cursor_y 
   int   print_cursor_x 
   int   print_cursor_y 
   WINDOW *   print_window 
   int   print_window_height 
   FILE *   log_file 
   const char **   welcome_message 
   int   welcome_message_length 
   struct _lf_sensor_message_t *   message_q 
   struct _lf_sensor_message_t *   message_recycle_q 
   int   tick_window_width 

Detailed Description

Simple terminal-based user interface based on ncurses. See sensor_simulator.h.

Edward A. Lee

Macro Definition Documentation




#define WGETCHR_TIMEOUT   1000

Typedef Documentation

◆ _lf_sensor_message_t

Enumeration Type Documentation

◆ _lf_sensor_message_type


Function Documentation

◆ _lf_print_message_function()

void _lf_print_message_function ( const char format,
va_list  args 

Function to register to handle printing of messages in util.h/c. This acquires the mutex lock.

◆ _lf_sensor_post_message()

void _lf_sensor_post_message ( enum _lf_sensor_message_type  type,
char body 

Post a message to be displayed. This acquires the mutex lock.

typeThe message type, one of _lf_sensor_message, _lf_sensor_tick, or _lf_sensor_close_window.
bodyThe message, or NULL for exit type.

◆ _lf_sensor_read_input()

void * _lf_sensor_read_input ( void ignored)

Thread to read input characters until an EOF is received. For each character received, if there is a registered trigger for that character, schedule that trigger with a payload equal to the character that was typed. Otherwise, the character is ignored.

◆ _lf_sensor_simulator_thread()

void * _lf_sensor_simulator_thread ( void ignored)

Thread to display strings in either the tick window or the message window.

◆ _lf_show_message()

void _lf_show_message ( const char message_lines[],
int  number_of_lines 

Put a persistent message in the upper left of the terminal window. The message will be left justified, with each string in the specified array on a new line. This should not be called directly by the user. It assumes the mutex lock is held.

message_linesThe message lines.
number_of_linesThe number of lines.

◆ _lf_start_print_window()

void _lf_start_print_window ( int  above,
int  right 

Start a window on the bottom left of the terminal window for printed messages from the application. This should not be called directly by the user. It assumes the mutex lock is held.

aboveSpace to leave above the window.
rightSpace to leave to the right of the window.

◆ _lf_start_tick_window()

void _lf_start_tick_window ( int  width)

Start a tick window on the right of the terminal window. This should not be called directly by the user. It assumes the mutex lock is held.

widthThe width of the window.

◆ end_sensor_simulator()

void end_sensor_simulator ( )

End ncurses control of the terminal.

◆ register_sensor_key()

int register_sensor_key ( char  key,
void action 

Register a keyboard key to trigger the specified action. Printable ASCII characters (codes 32 to 127) are supported plus '
' and '\0', where the latter registers a trigger to invoked when any key is pressed. If a specific key is registered and any key ('\0') is also registered, the any key trigger will be scheduled after the specific key is scheduled. If these triggers belong to different reactors, they could be invoked in parallel. This will fail if the specified key has already been registered (error code 1), or the key is not a supported key or a newline ‘
’ or any key '\0' (error code 2) or if the trigger is NULL (error code 3).

keyThe key to register.
actionThe action to trigger when the key is pressed (a pointer to a trigger_t struct).
0 for success, error code for failure.

◆ show_tick()

void show_tick ( const char character)

Place a tick (usually a single character) in the tick window.

characterThe tick character.

◆ start_sensor_simulator()

int start_sensor_simulator ( const char message_lines[],
int  number_of_lines,
int  tick_window_width,
char log_file,
int  log_level 

Start the sensor simulator if it has not been already started. This must be called at least once before any call to register_sensor_key. The specified message is an initial message to display at the upper left, typically a set of instructions, that remains displayed throughout the lifetime of the window. Please ensure that the message_lines array and its contained strings are not on the stack because they will be used later in a separate thread.

message_linesThe message lines.
number_of_linesThe number of lines.
tick_window_widthThe width of the tick window or 0 for none.
log_fileIf non-NULL, the name of a file to which to write logging messages.
log_levelThe level of log messages to redirect to the file. The level should be one of LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, LOG_LEVEL_INFO, LOG_LEVEL_LOG, LOG_LEVEL_DEBUG, or LOG_LEVEL_ALL.
0 for success, error code for failure.

Variable Documentation

◆ [struct]

struct { ... } _lf_sensor

◆ _lf_sensor_any_key_trigger

trigger_t* _lf_sensor_any_key_trigger = NULL

Trigger for any key.

◆ _lf_sensor_mutex

lf_mutex_t _lf_sensor_mutex

◆ _lf_sensor_sensor_newline_trigger

trigger_t* _lf_sensor_sensor_newline_trigger = NULL

Trigger for the newline character '
', which is platform dependent.

◆ _lf_sensor_simulator_cond_var

lf_cond_t _lf_sensor_simulator_cond_var

◆ _lf_sensor_trigger_table

trigger_t* _lf_sensor_trigger_table[LF_SENSOR_TRIGGER_TABLE_SIZE]

Table of Lingua Franca trigger objects to schedule in response to keypresses.

◆ default_window

WINDOW* default_window

Default window from which to get input characters. If show_welcome_message() is called, this will be the welcome message window. Otherwise, it will be stdscr, the default curses window.

◆ input_thread_id

lf_thread_t input_thread_id

◆ log_file

FILE* log_file

File to which to write log data, or NULL to send to window.

◆ message_q

◆ message_recycle_q

struct _lf_sensor_message_t* message_recycle_q

◆ output_thread_id

lf_thread_t output_thread_id

◆ print_cursor_x

int print_cursor_x

◆ print_cursor_y

int print_cursor_y

◆ print_window

WINDOW* print_window

Print window.

◆ print_window_height

int print_window_height

The print window height.

◆ thread_created

int thread_created

◆ tick_cursor_x

int tick_cursor_x

Keep track of the tick cursor position directly so it doesn't get as messed up by printf() calls.

◆ tick_cursor_y

int tick_cursor_y

◆ tick_window

WINDOW* tick_window

Tick window.

◆ tick_window_width

int tick_window_width

The width of the tick window.

◆ welcome_message

const char** welcome_message

The welcome message.

◆ welcome_message_length

int welcome_message_length

The length of the welcome message.