Three Methods to Implement State Machines in C Language

Three Methods to Implement State Machines in C Language

[Paid] STM32 Embedded Resource Package The implementation of a state machine essentially involves three elements: state, event, and response. This translates into three questions regarding specific behaviors. What happened? What state is the system currently in? In this state, given this event, what should the system do? There are mainly three methods to implement a … Read more

sml2: A Powerful C++20 State Machine Library

sml2: A Powerful C++20 State Machine Library

sml2: A Powerful C++20 State Machine Library In modern software development, state machines are a crucial design pattern that helps developers better manage and organize complex program logic. sml2 is a state machine library based on C++20 that offers powerful features and efficient performance while maintaining simplicity and ease of use. Features Based on UML-2.5 … Read more

Building an Embedded Modbus Protocol Stack with 2000 Lines of Code!

Building an Embedded Modbus Protocol Stack with 2000 Lines of Code!

Follow our official account to keep receiving embedded knowledge! Introduction In the development of industrial automation and IoT devices, the Modbus protocol has become the preferred choice for embedded system communication due to its simplicity and reliability. However, traditional Modbus protocol stacks are often large, with tens of thousands of lines of code, making them … Read more

Efficient Parsing of Variable-Length Data Frames in Embedded Communication Protocols

Efficient Parsing of Variable-Length Data Frames in Embedded Communication Protocols

I am Lao Wen, an embedded engineer who loves learning.Follow me to become even better together! In communication design, considering the flexibility of protocols, they are often designed to be “variable length”. An example is shown in the figure below: the communication protocol frame of the Ruimi LoRa terminal. If a system receives the above … Read more

How Engineers Design Debouncing for Microcontrollers

How Engineers Design Debouncing for Microcontrollers

The contact bounce of mechanical buttons (5-20ms high-frequency level fluctuations) is a common pain point in microcontroller input modules. If not addressed, it can lead to false triggering. This article focuses on a collaborative solution of hardware filtering and software algorithms, distilling the latest debouncing technology details from 2025, avoiding vague terms like ‘moderate selection’, … Read more

Why State Machine Architecture is Frequently Used in Embedded Development?

Why State Machine Architecture is Frequently Used in Embedded Development?

1. Improving CPU UtilizationWhenever I see a program filled with delay_ms(), I get a headache. Software delays of several tens of milliseconds are a huge waste of CPU resources, with precious CPU time wasted on NOP instructions.It also troubles me when the entire program halts just to wait for a pin level change or serial … Read more

Using State Machine Concepts in Embedded C Programming to Handle Complex Logic

Using State Machine Concepts in Embedded C Programming to Handle Complex Logic

The state machine pattern is a behavioral pattern that effectively implements different state transitions through polymorphism. Unfortunately, in embedded environments, one often has to write pure C code while also considering reentrancy and multitasking request transitions, making the implementation quite challenging. Recently, while reviewing an open-source system, I came across an implementation of a state … Read more

Understanding ‘Callback Hell’ in C Language

Understanding 'Callback Hell' in C Language

Content Hello everyone, I am Bug Jun~Recently, I came across a very interesting term “callback hell”, which refers to endless callback functions that ultimately lead to stack overflow and deadlock. The manifestation of this is multiple layers of nested function pointer callbacks, resulting in poor code readability and maintenance difficulties.Below is a simple example simulating … Read more

Five State Machine Design Patterns for Embedded Systems

Five State Machine Design Patterns for Embedded Systems

“ State machines are a key tool in embedded system development, allowing complex logic to be broken down into clear states and transition rules. This article introduces the core elements, five implementation patterns, and code examples, along with recommendations for pattern selection to help developers efficiently create reliable embedded systems.“ 01 — Introduction to State … Read more

CUTEHSM – An Efficient Hierarchical State Machine in C

CUTEHSM - An Efficient Hierarchical State Machine in C

Why Do We Need an Elegant Hierarchical State Machine? Traditional State Machine Implementation: // Traditional implementation: 4 function pointers + complex state transition logic typedef struct { void (*entry)(); void (*exit)(); void (*work)(); void (*event)(); // … more function pointers } TraditionalState; This design not only leads to code redundancy but also has a rigid … Read more