Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Ultra-lightweight, event-driven C++11 finite state machine library for IoT, embedded, and automation projects.

License

NotificationsYou must be signed in to change notification settings

hemonserrat/IoT-uFSM

Repository files navigation

IoT-uFSM Logo

IoT-uFSM

Ultra-lightweight, event-driven C++11 finite state machine library for IoT, embedded, and automation projects

Build StatusQuality Gate StatusLicense: GPL v3C++11Platform

📚 Documentation🐛 Report Bug✨ Request Feature


📋 Table of Contents


🚀 Features

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.

✨ Key Benefits

  • 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

🎯 Perfect For

  • 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

⚡ Quick Start

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;}

📦 Installation

Prerequisites

  • C++11 compatible compiler (GCC 4.8+, Clang 3.3+, MSVC 2015+)
  • CMake 3.10 or higher

Build from Source

macOS

# 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

Linux (Ubuntu/Debian)

# 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

Windows (Visual Studio)

# 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

Integration with Your Project

Using CMake FetchContent

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)

Manual Integration

Simply copy theinc/ andsrc/ directories to your project and include them in your build system.


💡 Usage Examples

IoT Sensor State Machine

// 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_;};

Home Automation Controller

// 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...};

🏗️ Architecture

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] --> G
Loading

Core Components

  • uFsm: The main finite state machine class that manages states and transitions
  • uEventHandler: Abstract base class for implementing event handling logic
  • uFsmEvent: Event class that encapsulates event data and parameters
  • Transition Table: Efficient hash-based lookup for state transitions

Key Design Principles

  • 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

📖 Documentation

Key Classes


🧪 Testing

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

Test Coverage

  • ✅ State transition validation
  • ✅ Event handling and parameter passing
  • ✅ Error condition handling
  • ✅ Memory management and cleanup
  • ✅ Edge cases and boundary conditions

🤝 Contributing

We welcome contributions from the community! Here's how you can help:

Ways to Contribute

  • 🐛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

Development Setup

  1. Fork the repository
  2. Create a feature branch:git checkout -b feature/amazing-feature
  3. Make your changes and add tests
  4. Ensure all tests pass:ctest --test-dir build
  5. Commit your changes:git commit -m 'Add amazing feature'
  6. Push to the branch:git push origin feature/amazing-feature
  7. Open a Pull Request

Code Style

  • Follow existing C++11 coding conventions
  • Include Doxygen documentation for public APIs
  • Add unit tests for new functionality
  • Ensure cross-platform compatibility

📄 License

This project is licensed under theGNU General Public License v3.0 - see theLICENSE file for details.

What this means:

  • 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.


🔗 Links

Project Resources

Community

  • Discussions: Community Q&A and ideas
  • Wiki: Additional documentation and tutorials

Related Projects

  • Catch2: Testing framework used by IoT-uFSM
  • CMake: Build system

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp