-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Blink using a CFSM state machine | ||
This is a code example for CFSM state machine driven sketches. | ||
It implements a state pattern version of the simple "blink" sketch. | ||
Code uses 2 states to turn LED on or off every second. | ||
The circuit: | ||
* only a board with a builtin LED is required | ||
Created 20 10 2025 | ||
Haju | ||
See URL below for further information: | ||
https://github.com/nhjschulz/cfsm | ||
*/ | ||
|
||
#include <Arduino.h> | ||
#include <c_fsm.h> | ||
|
||
// Forward declaration for entering LED On state | ||
void LedOnState_enter(cfsm_Ctx * fsm); | ||
// Forward declaration for entering LED Off state | ||
void LedOffState_enter(cfsm_Ctx * fsm); | ||
|
||
cfsm_Ctx blinkFsm; // CFSM state machine date | ||
|
||
uint64_t ledUpdateTime = 0; // Hold time of last LED update. | ||
int LED_PIN = LED_BUILTIN; // Used LED Pin | ||
|
||
void setup() | ||
{ | ||
pinMode(LED_PIN, OUTPUT); | ||
|
||
// setup FSM and enter LED ON state | ||
cfsm_init(&blinkFsm, NULL); | ||
cfsm_transition(&blinkFsm, LedOnState_enter); | ||
} | ||
|
||
void loop() | ||
{ | ||
cfsm_process(&blinkFsm); // Do work in current CFSM state | ||
} | ||
|
||
// CFSM On state handler functions | ||
|
||
void LedOnState_process(cfsm_Ctx * fsm) | ||
{ | ||
if ((millis() - ledUpdateTime) >= 1000ull) | ||
{ | ||
// LED On time has expired, switch to off state | ||
cfsm_transition(fsm, LedOffState_enter); | ||
} | ||
} | ||
|
||
void LedOnState_enter(cfsm_Ctx * fsm) | ||
{ | ||
fsm->onProcess = LedOnState_process; // register state process handler | ||
|
||
digitalWrite(LED_PIN, HIGH); // turn the LED on | ||
ledUpdateTime = millis(); // store update time | ||
} | ||
|
||
// CFSM Off state handler functions | ||
|
||
void LedOffState_process(cfsm_Ctx * fsm) | ||
{ | ||
if ((millis() - ledUpdateTime) >= 1000ull) | ||
{ | ||
// LED Off time has expired, switch to on state | ||
cfsm_transition(fsm, LedOnState_enter); | ||
} | ||
} | ||
|
||
void LedOffState_enter(cfsm_Ctx * fsm) | ||
{ | ||
fsm->onProcess = LedOffState_process; // register state process handler | ||
|
||
digitalWrite(LED_PIN, LOW); // turn the LED off | ||
ledUpdateTime = millis(); // store update time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name=CFSM | ||
version=0.2.2 | ||
author=Haju Schulz <[email protected]> | ||
maintainer=Haju Schulz <[email protected]> | ||
sentence=A State Design Pattern Approach for C-Programs. | ||
paragraph=CFSM follows a simplistic approach for the C-Language to implement maintainable state machines according to the STATE design pattern. This differentiates it from other solutions that often rely on complex macros to construct state handlers. | ||
category=Other | ||
url=https://github.com/nhjschulz/cfsm | ||
architectures=* | ||
includes=c_fsm.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters