- Notifications
You must be signed in to change notification settings - Fork0
Ultra-lightweight, event-driven C++11 finite state machine library for IoT, embedded, and automation projects.
License
hemonserrat/IoT-uFSM
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation

Ultra-lightweight, event-driven C++11 finite state machine library for IoT, embedded, and automation projects
- 🚀 Features
- ⚡ Quick Start
- 📦 Installation
- 💡 Usage Examples
- 🏗️ Architecture
- 📖 Documentation
- 🧪 Testing
- 🤝 Contributing
- 📄 License
- 🔗 Links
IoT-uFSM is amicro finite state machine library designed specifically forIoT devices,embedded systems, andautomation projects. Built with modern C++11 standards, it provides a robust foundation for state-driven applications.
- ⚡Ultra-lightweight: Minimal memory footprint, perfect for microcontrollers and resource-constrained devices
- 🔌C++11 Standard: Modern, portable, and easy to integrate with existing C++ projects
- 🔄Event-Driven Architecture: Clean separation between state logic and event handling
- 🌍Cross-Platform: Works seamlessly on macOS, Linux, Windows, and embedded targets
- 📦Zero Dependencies: No external libraries required beyond standard C++11
- 🎯IoT-Ready: Optimized for Internet of Things and embedded applications
- 🧪Well-Tested: Comprehensive test suite using Catch2 framework
- 📚Comprehensive Documentation: Full API documentation with Doxygen
- 🔧Easy Integration: Simple CMake build system with automatic dependency management
- IoT Devices: Smart sensors, actuators, and connected devices
- Embedded Systems: Microcontroller-based projects and real-time applications
- Automation: Industrial control systems and home automation
- Robotics: State-based robot behavior and control systems
- Protocol Implementations: Communication protocol state machines
Get up and running with IoT-uFSM in minutes:
#include"uFsm.hpp"#include"uEventHandler.hpp"// Define your states#defineIDLE_STATE0x0001#defineACTIVE_STATE0x0002#defineERROR_STATE0xFFFF// Define your events#defineSTART_EVENT0x0001#defineSTOP_EVENT0x0002#defineERROR_EVENT0x0003// Create your event handlerclassMyEventHandler :publicuEventHandler {public:MyEventHandler() : uEventHandler(3) {FillHandlersArray(); }private:voidFillHandlersArray()override { functions_[0] = (TransitionFunc)&MyEventHandler::handleStart; functions_[1] = (TransitionFunc)&MyEventHandler::handleStop; functions_[2] = (TransitionFunc)&MyEventHandler::handleError; }boolhandleStart(void* params) { std::cout <<"Starting device..." << std::endl;returntrue; }boolhandleStop(void* params) { std::cout <<"Stopping device..." << std::endl;returntrue; }boolhandleError(void* params) { std::cout <<"Error occurred!" << std::endl;returnfalse; }};intmain() {// Create event handler and FSM MyEventHandler handler; uFsmfsm(&handler,10, IDLE_STATE);// Define state transitions fsm.defineTransition(IDLE_STATE, ACTIVE_STATE, START_EVENT,0); fsm.defineTransition(ACTIVE_STATE, IDLE_STATE, STOP_EVENT,1); fsm.defineTransition(IDLE_STATE, ERROR_STATE, ERROR_EVENT,2);// Process events fsm.control(START_EVENT);// IDLE -> ACTIVE fsm.control(STOP_EVENT);// ACTIVE -> IDLEreturn0;}
- C++11 compatible compiler (GCC 4.8+, Clang 3.3+, MSVC 2015+)
- CMake 3.10 or higher
# Install CMake (if not already installed)brew install cmake# Clone the repositorygit clone https://github.com/hemonserrat/IoT-uFSM.gitcd IoT-uFSM# Create build directorymkdir -p build# Configure the projectcmake -S. -B build# Build the projectcmake --build build# Run tests (optional)ctest --test-dir build
# Install dependenciessudo apt-get updatesudo apt-get install build-essential cmake git# Clone and buildgit clone https://github.com/hemonserrat/IoT-uFSM.gitcd IoT-uFSMmkdir -p buildcmake -S. -B buildcmake --build build# Run testsctest --test-dir build
# Clone the repositorygit clone https://github.com/hemonserrat/IoT-uFSM.gitcd IoT-uFSM# Create build directorymkdir build# Configure with Visual Studiocmake -S . -B build -G"Visual Studio 16 2019"# Build the projectcmake --build build --config Release# Run testsctest --test-dir build --config Release
include(FetchContent)FetchContent_Declare( IoT-uFSM GIT_REPOSITORY https://github.com/hemonserrat/IoT-uFSM.git GIT_TAG master)FetchContent_MakeAvailable(IoT-uFSM)target_link_libraries(your_targetPRIVATE IoT-uFSM)
Simply copy theinc/ andsrc/ directories to your project and include them in your build system.
// IoT temperature sensor with different operating modesclassTemperatureSensor :publicuEventHandler {enum States { SLEEP =1, MEASURING =2, TRANSMITTING =3, ERROR =0xFF };enum Events { WAKE_UP =1, MEASURE_COMPLETE =2, TRANSMIT_COMPLETE =3, SENSOR_ERROR =4 };public:TemperatureSensor() : uEventHandler(4) { fsm_ =newuFsm(this,10, SLEEP);setupTransitions();FillHandlersArray(); }private:voidsetupTransitions() { fsm_->defineTransition(SLEEP, MEASURING, WAKE_UP,0); fsm_->defineTransition(MEASURING, TRANSMITTING, MEASURE_COMPLETE,1); fsm_->defineTransition(TRANSMITTING, SLEEP, TRANSMIT_COMPLETE,2); fsm_->defineTransition(MEASURING, ERROR, SENSOR_ERROR,3); }voidFillHandlersArray()override { functions_[0] = (TransitionFunc)&TemperatureSensor::startMeasurement; functions_[1] = (TransitionFunc)&TemperatureSensor::startTransmission; functions_[2] = (TransitionFunc)&TemperatureSensor::enterSleepMode; functions_[3] = (TransitionFunc)&TemperatureSensor::handleSensorError; }boolstartMeasurement(void* params) {// Initialize sensor and start measurementreturntrue; }boolstartTransmission(void* params) {// Send data to IoT platformreturntrue; }boolenterSleepMode(void* params) {// Enter low-power modereturntrue; }boolhandleSensorError(void* params) {// Handle sensor malfunctionreturnfalse; } uFsm* fsm_;};
// Smart home device controllerclassSmartDevice :publicuEventHandler {enum States { OFF =1, STANDBY =2, ACTIVE =3, MAINTENANCE =4 };enum Events { POWER_ON =1, ACTIVATE =2, DEACTIVATE =3, MAINTENANCE_MODE =4 };// Implementation details...};
IoT-uFSM follows a clean, event-driven architecture designed for embedded systems:
graph TD A[Event] --> B[uEventHandler] B --> C[uFsm] C --> D[State Transition] D --> E[Action Execution] E --> F[New State] F --> C G[Event Queue] --> A H[External Triggers] --> G I[Timer Events] --> G J[Sensor Data] --> GuFsm: The main finite state machine class that manages states and transitionsuEventHandler: Abstract base class for implementing event handling logicuFsmEvent: Event class that encapsulates event data and parameters- Transition Table: Efficient hash-based lookup for state transitions
- Memory Efficient: Static allocation with configurable limits
- Real-time Safe: Deterministic execution time for embedded systems
- Event-Driven: Asynchronous event processing with internal queue
- Extensible: Easy to extend with custom event handlers and states
- API Documentation: Complete Doxygen-generated API reference
- Getting Started Guide: Step-by-step tutorial for beginners
- Examples: Real-world usage examples and test cases
uFsm: Main FSM implementationuEventHandler: Event handler base classuFsmEvent: Event data container
IoT-uFSM includes a comprehensive test suite using the Catch2 framework:
# Run all testsctest --test-dir build# Run with verbose outputctest --test-dir build --verbose# Run specific test./build/test_fsm
- ✅ State transition validation
- ✅ Event handling and parameter passing
- ✅ Error condition handling
- ✅ Memory management and cleanup
- ✅ Edge cases and boundary conditions
We welcome contributions from the community! Here's how you can help:
- 🐛Report Bugs:Create an issue with detailed reproduction steps
- ✨Request Features:Suggest new features for IoT and embedded use cases
- 📝Improve Documentation: Help us make the docs clearer and more comprehensive
- 🔧Submit Code: Fork the repo and submit pull requests
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes and add tests
- Ensure all tests pass:
ctest --test-dir build - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
- Follow existing C++11 coding conventions
- Include Doxygen documentation for public APIs
- Add unit tests for new functionality
- Ensure cross-platform compatibility
This project is licensed under theGNU General Public License v3.0 - see theLICENSE file for details.
- ✅Free to use in open source projects
- ✅Modify and distribute under the same license
- ✅Commercial use allowed with GPL compliance
- ❌Cannot be used in proprietary software without GPL compliance
For commercial licensing options, pleasecontact the maintainer.
- GitHub Repository: Source code and issue tracking
- Documentation: Complete API reference
- Releases: Download stable versions
- Issues: Bug reports and feature requests
- Discussions: Community Q&A and ideas
- Wiki: Additional documentation and tutorials
Made with ❤️ for the IoT and embedded systems community
⭐Star this repo if you find it useful! ⭐
About
Ultra-lightweight, event-driven C++11 finite state machine library for IoT, embedded, and automation projects.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.