Blinking - cflurin/node-red-contrib-dsm GitHub Wiki

A dsm for blinking a LED (Raspberry Pi pin). Feature: the next start input is only triggered after the sequence is finished or after a stop input.

Settings are provided for on/off time and the number of blinks. Set the sm.data.blinks to continuing if you don't want to limit the number of blinks. Optionally the output (default 1/0) can be set to true/false, "on"/"off" or whatever you need.

blinking

Configuration

{
    "currentState": "idle",
    "states": {
        "idle": {
            "start": "on",
            "stop": "idle"
        },
        "on": {
            "turn_off": "off",
            "stop": "idle"
        },
        "off": {
            "turn_on": "on",
            "stop": "idle"
    }
    },
    "data": {
        "on": 1,
        "off": 0,
        "on_time": 1000,
        "off_time": 1000,
        "blinks": 3,
        "count": 0
    },
    "methods": {
        "init": [
            "sm.schedule = function(tran, delay, msg) {",
            "   timeout.id = setTimeout(function() {",
            "       resume(tran, msg)",
            "   },delay);",
            "};",
            "sta.fill = 'grey';"
        ],
        "onBeforeTransition": [
            "if (!msg.topic) msg.topic = 'start';",
            "if (msg.blinks) sm.data.blinks = msg.blinks;",
            "if (msg.on_time) sm.data.on_time = msg.on_time;",
            "if (msg.off_time) sm.data.off_time = msg.off_time;"
        ],
        "start": [
            "sm.data.count = sm.data.blinks;",
            "resume('turn_on', msg);",
            "output = false;"
        ],
        "turn_on": [
            "if (sm.data.count) {",
            "   sm.schedule('turn_off', sm.data.on_time, msg);",
            "   if(sm.data.blinks !== 'continuing') sm.data.count--;",
            "   sm.fill = 'yellow';",
            "   msg.payload = sm.data.on;",
            "   output = true;",
            "} else {",
            "   sm.currentState = 'idle';",
            "   sm.fill = 'grey';",
            "   output = false;",
            "}"
        ],
        "turn_off": [
            "sm.schedule('turn_on', sm.data.off_time, msg);",
            "sm.fill = 'blue';",
            "msg.payload = sm.data.off;"
        ],
        "stop": [
            "clearTimeout(timeout.id);",
            "sm.fill = 'grey';",
            "msg.payload = sm.data.off;"
        ],
        "status": {
            "fill": {
                "get": "sm.fill;"
            },
            "shape": "dot",
            "text": {
                "get": "sm.currentState;"
            }
        }
    }
}

Flow

[{"id":"93729166.52a7e","type":"debug","z":"6ff0723.8c6b78c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":900,"wires":[]},{"id":"e1a5ebe3.6c6df8","type":"inject","z":"6ff0723.8c6b78c","name":"","topic":"start","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":880,"wires":["ee084f39.d0118"](/cflurin/node-red-contrib-dsm/wiki/"ee084f39.d0118")},{"id":"ee084f39.d0118","type":"dsm","z":"6ff0723.8c6b78c","name":"blinking","sm_config":"{\n    \"currentState\": \"idle\",\n    \"states\": {\n        \"idle\": {\n            \"start\": \"on\",\n            \"stop\": \"idle\"\n        },\n        \"on\": {\n            \"turn_off\": \"off\",\n            \"stop\": \"idle\"\n        },\n        \"off\": {\n            \"turn_on\": \"on\",\n            \"stop\": \"idle\"\n    }\n    },\n    \"data\": {\n        \"on\": 1,\n        \"off\": 0,\n        \"on_time\": 1000,\n        \"off_time\": 1000,\n        \"blinks\": 3,\n        \"count\": 0\n    },\n    \"methods\": {\n        \"init\": [\n            \"sm.schedule = function(tran, delay, msg) {\",\n            \"   timeout.id = setTimeout(function() {\",\n            \"       resume(tran, msg)\",\n            \"   },delay);\",\n            \"};\",\n            \"sta.fill = 'grey';\"\n        ],\n        \"onBeforeTransition\": [\n            \"if (!msg.topic) msg.topic = 'start';\",\n            \"if (msg.blinks) sm.data.blinks = msg.blinks;\",\n            \"if (msg.on_time) sm.data.on_time = msg.on_time;\",\n            \"if (msg.off_time) sm.data.off_time = msg.off_time;\"\n        ],\n        \"start\": [\n            \"sm.data.count = sm.data.blinks;\",\n            \"resume('turn_on', msg);\",\n            \"output = false;\"\n        ],\n        \"turn_on\": [\n            \"if (sm.data.count) {\",\n            \"   sm.schedule('turn_off', sm.data.on_time, msg);\",\n            \"   if(sm.data.blinks !== 'continuing') sm.data.count--;\",\n            \"   sm.fill = 'yellow';\",\n            \"   msg.payload = sm.data.on;\",\n            \"   output = true;\",\n            \"} else {\",\n            \"   sm.currentState = 'idle';\",\n            \"   sm.fill = 'grey';\",\n            \"   output = false;\",\n            \"}\"\n        ],\n        \"turn_off\": [\n            \"sm.schedule('turn_on', sm.data.off_time, msg);\",\n            \"sm.fill = 'blue';\",\n            \"msg.payload = sm.data.off;\"\n        ],\n        \"stop\": [\n            \"clearTimeout(timeout.id);\",\n            \"sm.fill = 'grey';\",\n            \"msg.payload = sm.data.off;\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.fill;\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.currentState;\"\n            }\n        }\n    }\n}","x":270,"y":900,"wires":["93729166.52a7e"](/cflurin/node-red-contrib-dsm/wiki/"93729166.52a7e")},{"id":"1ccae227.b1b73e","type":"inject","z":"6ff0723.8c6b78c","name":"","topic":"stop","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":920,"wires":["ee084f39.d0118"](/cflurin/node-red-contrib-dsm/wiki/"ee084f39.d0118")}]