Skip to content

hemonserrat/IoT-uFSM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

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 Status Quality Gate Status License: GPL v3 C++11 Platform

πŸ“š Documentation β€’ πŸ› Report Bug β€’ ✨ Request Feature


πŸ“‹ Table of Contents


πŸš€ Features

IoT-uFSM is a micro finite state machine library designed specifically for IoT devices, embedded systems, and automation 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
#define IDLE_STATE     0x0001
#define ACTIVE_STATE   0x0002
#define ERROR_STATE    0xFFFF

// Define your events
#define START_EVENT    0x0001
#define STOP_EVENT     0x0002
#define ERROR_EVENT    0x0003

// Create your event handler
class MyEventHandler : public uEventHandler {
public:
    MyEventHandler() : uEventHandler(3) {
        FillHandlersArray();
    }

private:
    void FillHandlersArray() override {
        functions_[0] = (TransitionFunc)&MyEventHandler::handleStart;
        functions_[1] = (TransitionFunc)&MyEventHandler::handleStop;
        functions_[2] = (TransitionFunc)&MyEventHandler::handleError;
    }
    
    bool handleStart(void* params) {
        std::cout << "Starting device..." << std::endl;
        return true;
    }
    
    bool handleStop(void* params) {
        std::cout << "Stopping device..." << std::endl;
        return true;
    }
    
    bool handleError(void* params) {
        std::cout << "Error occurred!" << std::endl;
        return false;
    }
};

int main() {
    // Create event handler and FSM
    MyEventHandler handler;
    uFsm fsm(&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 -> IDLE
    
    return 0;
}

πŸ“¦ 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 repository
git clone https://github.com/hemonserrat/IoT-uFSM.git
cd IoT-uFSM

# Create build directory
mkdir -p build

# Configure the project
cmake -S . -B build

# Build the project
cmake --build build

# Run tests (optional)
ctest --test-dir build

Linux (Ubuntu/Debian)

# Install dependencies
sudo apt-get update
sudo apt-get install build-essential cmake git

# Clone and build
git clone https://github.com/hemonserrat/IoT-uFSM.git
cd IoT-uFSM
mkdir -p build
cmake -S . -B build
cmake --build build

# Run tests
ctest --test-dir build

Windows (Visual Studio)

# Clone the repository
git clone https://github.com/hemonserrat/IoT-uFSM.git
cd IoT-uFSM

# Create build directory
mkdir build

# Configure with Visual Studio
cmake -S . -B build -G "Visual Studio 16 2019"

# Build the project
cmake --build build --config Release

# Run tests
ctest --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_target PRIVATE IoT-uFSM)

Manual Integration

Simply copy the inc/ and src/ directories to your project and include them in your build system.


πŸ’‘ Usage Examples

IoT Sensor State Machine

// IoT temperature sensor with different operating modes
class TemperatureSensor : public uEventHandler {
    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_ = new uFsm(this, 10, SLEEP);
        setupTransitions();
        FillHandlersArray();
    }

private:
    void setupTransitions() {
        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);
    }
    
    void FillHandlersArray() override {
        functions_[0] = (TransitionFunc)&TemperatureSensor::startMeasurement;
        functions_[1] = (TransitionFunc)&TemperatureSensor::startTransmission;
        functions_[2] = (TransitionFunc)&TemperatureSensor::enterSleepMode;
        functions_[3] = (TransitionFunc)&TemperatureSensor::handleSensorError;
    }
    
    bool startMeasurement(void* params) {
        // Initialize sensor and start measurement
        return true;
    }
    
    bool startTransmission(void* params) {
        // Send data to IoT platform
        return true;
    }
    
    bool enterSleepMode(void* params) {
        // Enter low-power mode
        return true;
    }
    
    bool handleSensorError(void* params) {
        // Handle sensor malfunction
        return false;
    }
    
    uFsm* fsm_;
};

Home Automation Controller

// Smart home device controller
class SmartDevice : public uEventHandler {
    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 tests
ctest --test-dir build

# Run with verbose output
ctest --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 the GNU General Public License v3.0 - see the LICENSE 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, please contact 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