{"id":16237552,"url":"https://github.com/lexus2k/sm_engine","last_synced_at":"2025-04-08T08:46:15.315Z","repository":{"id":108162823,"uuid":"269841569","full_name":"lexus2k/sm_engine","owner":"lexus2k","description":"State machine engine for small projects","archived":false,"fork":false,"pushed_at":"2020-06-15T05:37:36.000Z","size":67,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-14T05:43:08.474Z","etag":null,"topics":["arduino-library","esp32","state-machine","statemachine"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lexus2k.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-06T02:21:30.000Z","updated_at":"2024-09-03T13:39:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"afd9bd85-0838-46b5-a74e-9f7e3b5a52de","html_url":"https://github.com/lexus2k/sm_engine","commit_stats":{"total_commits":13,"total_committers":1,"mean_commits":13.0,"dds":0.0,"last_synced_commit":"d2fd92fd39922267d59ce4a3a5c6e8479948ebbb"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lexus2k%2Fsm_engine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lexus2k%2Fsm_engine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lexus2k%2Fsm_engine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lexus2k%2Fsm_engine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lexus2k","download_url":"https://codeload.github.com/lexus2k/sm_engine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247808852,"owners_count":20999798,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino-library","esp32","state-machine","statemachine"],"created_at":"2024-10-10T13:36:07.175Z","updated_at":"2025-04-08T08:46:15.292Z","avatar_url":"https://github.com/lexus2k.png","language":"C++","readme":"# sm_engine\n\nState machine engine for small projects\n\n## Arduino users\n\nArduino users, please copy all files from include directory to src, and use it as a standard arduino library\n\n## Simple C++ API\n\n```.cpp\n#include \"sme/engine.h\"\n#include \"sme/generic_state.h\"\n#include \"sme/generic_state_engine.h\"\n\n#include \u003cstdio.h\u003e\n#include \u003cthread\u003e\n#include \u003cchrono\u003e\n\n// Here are our events\nenum\n{\n    EVENT_BUTTON_PRESS,\n};\n\n// This is state unique ids\nenum\n{\n    STATE_ON,\n    STATE_OFF,\n};\n\n// Transition table for off state\nstatic C_TRANSITION_TBL(stateOffTable)\n{\n    TRANSITION_SWITCH(EVENT_BUTTON_PRESS, SM_EVENT_ARG_ANY, sme::NO_FUNC, STATE_ON)\n    TRANSITION_TBL_END\n}\n\n// Transition table for on state\nstatic C_TRANSITION_TBL(stateOnTable)\n{\n    TRANSITION_SWITCH(EVENT_BUTTON_PRESS, SM_EVENT_ARG_ANY, sme::NO_FUNC, STATE_OFF)\n    TRANSITION_TBL_END\n}\n\nvoid enterOnState(SEventData *event)\n{\n    fprintf( stderr, \"Switch is turned On\\n\" );\n}\n\nvoid enterOffState(SEventData *event)\n{\n    fprintf( stderr, \"Switch is turned Off\\n\" );\n}\n\n\nextern \"C\" void app_main()\n{\n    // Create switch states\n    GenericState\u003centerOffState, sme::NO_UPDATE, sme::NO_EXIT, stateOffTable\u003e stateOff(STATE_OFF);\n    GenericState\u003centerOnState,  sme::NO_UPDATE, sme::NO_EXIT, stateOnTable\u003e stateOn(STATE_ON);\n    SmStateInfo statesList[] =\n    {\n        STATE_LIST_ITEM(stateOff),\n        STATE_LIST_ITEM(stateOn),\n        STATE_LIST_END,\n    };\n    GenericStateEngine\u003csme::NO_TABLE\u003e switchSm(statesList);\n\n    std::thread timer([\u0026]() {\n        // Send 10 times EVENT_BUTTON_PRESS and then stop state machine\n        for (int i=0; i\u003c10; i++) {\n            std::this_thread::sleep_for(std::chrono::milliseconds(1000));\n            switchSm.sendEvent( { EVENT_BUTTON_PRESS, 0 } );\n        }\n        switchSm.stop();\n    });\n\n    // This will call begin method for all registered states\n    // and will call enter() function for state OFF\n    switchSm.begin(STATE_OFF);\n    // run() method will run infinite loop until state machine is stopped\n    // in this infinite loop, it will call update() method of active state every 100 milliseconds\n    switchSm.loop( 100 );\n    // This will call end methods for all registered states\n    switchSm.end();\n    timer.join();\n}\n\n```\n\n\n## C API\n\n## C++ API\n\n```.cpp\n\n#include \"sme/engine.h\"\n\n#include \u003cthread\u003e\n#include \u003cchrono\u003e\n#include \u003cstdio.h\u003e\n\n// Here are our events\nenum\n{\n    EVENT_BUTTON_PRESS,\n};\n\n// This is state unique ids\nenum\n{\n    STATE_ON,\n    STATE_OFF,\n};\n\nclass StateOff: public SmState\n{\npublic:\n    StateOff(): SmState(\"off\") {}\n\n    void enter(SEventData *event) override\n    {\n        fprintf( stderr, \"Switch is turned Off\\n\" );\n    }\n\n    // Transition table for off state\n    STransitionData onEvent(SEventData event) override\n    {\n        TRANSITION_SWITCH(EVENT_BUTTON_PRESS, SM_EVENT_ARG_ANY, sme::NO_FUNC, STATE_ON)\n        TRANSITION_TBL_END\n    }\n};\n\nclass StateOn: public SmState\n{\npublic:\n    StateOn(): SmState(\"on\") {}\n\n    void enter(SEventData *event) override\n    {\n        fprintf( stderr, \"Switch is turned On\\n\" );\n    }\n\n    // Transition table for on state\n    STransitionData onEvent(SEventData event) override\n    {\n        TRANSITION_SWITCH(EVENT_BUTTON_PRESS, SM_EVENT_ARG_ANY, sme::NO_FUNC, STATE_OFF)\n        TRANSITION_TBL_END\n    }\n};\n\nclass SwitchFsm: public SmEngine\n{\npublic:\n     SwitchFsm(): SmEngine()\n     {\n         SM_STATE( StateOff, STATE_OFF );\n         SM_STATE( StateOn,  STATE_ON );\n     }\n};\n\nextern \"C\" void app_main()\n{\n    SwitchFsm switchSm;\n\n    std::thread timer([\u0026]() {\n        // Send 10 times EVENT_BUTTON_PRESS and then stop state machine\n        for (int i=0; i\u003c10; i++) {\n            std::this_thread::sleep_for(std::chrono::milliseconds(1000));\n            switchSm.sendEvent( { EVENT_BUTTON_PRESS, 0 } );\n        }\n        switchSm.stop();\n    });\n\n    // This will call begin method for all registered states\n    // and will call enter() function for state OFF\n    switchSm.begin(STATE_OFF);\n    // run() method will run infinite loop until state machine is stopped\n    // in this infinite loop, it will call update() method of active state every 100 milliseconds\n    switchSm.loop( 100 );\n    // This will call end methods for all registered states\n    switchSm.end();\n    timer.join();\n}\n\n```\n\n## License\n\nBSD 3-Clause License\n\nCopyright (c) 2020, Aleksei Dynda\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n1. Redistributions of source code must retain the above copyright notice, this\n   list of conditions and the following disclaimer.\n\n2. Redistributions in binary form must reproduce the above copyright notice,\n   this list of conditions and the following disclaimer in the documentation\n   and/or other materials provided with the distribution.\n\n3. Neither the name of the copyright holder nor the names of its\n   contributors may be used to endorse or promote products derived from\n   this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flexus2k%2Fsm_engine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flexus2k%2Fsm_engine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flexus2k%2Fsm_engine/lists"}