Skip to content

Commit

Permalink
Added Arduino library Manager needs
Browse files Browse the repository at this point in the history
  • Loading branch information
nhjschulz committed Oct 19, 2024
1 parent 0e35529 commit 36c2b6a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
82 changes: 82 additions & 0 deletions examples/CfsmBlink/CfsmBlink.ino
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
}
6 changes: 3 additions & 3 deletions examples/UnoBlink/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
#include "states/OnState.h"

/**
* @brief CFSM state machine instanc
* @brief CFSM state machine instance
*/
static cfsm_Ctx blinkFsm;

/**
* @brief BLink application data used by the blink FSM
* @brief Blink application data used by the blink FSM.
*
* Contains the PIN id used for led toggling.
*/
Expand All @@ -68,7 +68,7 @@ void setup()
*/
void loop()
{
cfsm_process(&blinkFsm); /* Do work in current CFSM state */
cfsm_process(&blinkFsm); /* Do work in current CFSM state. */

/* Turn LED on every 2 seconds by a CFSM event. */
if ((millis() - blinkData.turnOnTimeMillis) >= 2000ull)
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "CFSM",
"version": "0.2.1",
"version": "0.2.2",
"description": "A State Design Pattern Approach for C-Programs",
"keywords": "state design pattern, FSM, C-Language",
"repository": {
Expand Down
10 changes: 10 additions & 0 deletions library.properties
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
2 changes: 1 addition & 1 deletion src/c_fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern "C" {

#define CFSM_VER_MAJOR 0 /**< semantic versioning major X.x.x */
#define CFSM_VER_MINOR 2 /**< semantic versioning minor x.X.x */
#define CFSM_VER_PATCH 1 /**< semantic versioning patch x.x.X */
#define CFSM_VER_PATCH 2 /**< semantic versioning patch x.x.X */

/******************************************************************************
* Types and Classes
Expand Down

0 comments on commit 36c2b6a

Please sign in to comment.