Example Path: examples/RTOS/OSMultiTaskUserInput
RTOS Multi-Task User Input Example
Overview
This application demonstrates a multi-task Real-Time Operating System (RTOS) program that handles user input through inter-task communication using mailboxes. The program showcases task creation, mailbox communication, and graceful task termination in an embedded RTOS environment.
Application Name
MultiTask Input example**
Architecture
The application consists of two main tasks that communicate via a mailbox:
Task Structure
- UserMain Task (Lower Priority)
- Main application task that waits for user input messages
- Pends on a mailbox to receive processed user input
- Handles application logic and user commands
- Can terminate the input task when "exit" command is received
- UserInputTask (Higher Priority - MAIN_PRIO - 1)
- Dedicated input handling task
- Blocks on fgets() waiting for user input from serial port
- Processes input strings (removes trailing newlines/carriage returns)
- Posts processed input to mailbox for UserMain task consumption
- Runs until termination signal is received
Communication Mechanism
- Mailbox: UserInputMbox - OS_MBOX object for inter-task communication
- Control Variable: bExitUserInputTask - Boolean flag for task termination
Program Flow
- Initialization
- System initialization and network setup
- Mailbox initialization
- Task creation with priority settings
- Runtime Operation
- UserInputTask waits for serial input (blocking)
- User types input which is captured and processed
- Processed input is posted to mailbox
- UserMain task polls mailbox (non-blocking) and processes messages
- LED counting continues during input wait periods
- Termination
- User enters "exit" command
- UserMain sets termination flag
- UserInputTask exits its loop and terminates
- Program requires reset after task termination
Key Features
Non-Blocking Mailbox Operations
The application uses PendNoWait() to check for messages without blocking, allowing the main task to continue other operations (like LED counting) while waiting for user input.
Input Processing
- Accepts up to 80 characters of user input
- Automatically removes trailing \r and \n characters
- Reports the number of bytes received
Task Priority Management
- UserInputTask has higher priority to ensure responsive input handling
- UserMain task can perform background operations while waiting
Network Integration
- Waits for active network connection during startup
- Includes system diagnostics (recommended to disable in production)
Usage
- Startup: Program initializes and displays "Enter String: " prompt
- Input: Type any string and press Enter
- Processing: Input is echoed back with confirmation message
- Exit: Type "exit" to terminate the input task
- Reset: Manual reset required after task termination
Code Structure
main.cpp - Main application file containing:
- Global variables and mailbox declaration
- UserInputTask() - Input handling task function
- UserMain() - Main application task function
- Task creation and mailbox communication logic
Dependencies
- init.h - System initialization functions
- nbrtos.h - NetBurner RTOS API
- string.h - String manipulation functions
Technical Notes
Memory Management
- Static input buffer (1024 bytes) used in UserInputTask
- Mailbox passes pointers to string data (not copies)
Error Handling
- Mailbox operations include error status checking
- Input length validation and bounds checking
System Integration
- 5-second timeout for network activation
- System diagnostics enabled for development
Development Considerations
- Remove EnableSystemDiagnostics() for production builds
- Consider implementing proper cleanup for task termination
- Input buffer size (80 chars) may need adjustment based on requirements
- Manual reset requirement could be improved with automatic restart logic
Example Session
Enter String: hello world
Received 13 bytes
UserMain Received: "hello world"
Enter String: test message
Received 14 bytes
UserMain Received: "test message"
Enter String: exit
Received 6 bytes
UserMain Received: "exit"
UserInputTask() terminated. Please reset program ***