Skip to content

Fade in out

cflurin edited this page Jan 30, 2019 · 5 revisions

Input (msg.payload):

{
    "start": 0,
    "end": 1,
    "duration": 3000,
    "step": 100
}
  • start: start value
  • end: end value
  • duration: time in Milliseconds
  • step: interval time in Milliseconds

Output (msg.payload):

Increasing or decreasing value depending on the start and end values.

fadeinout

Configuration

{
    "currentState": "stopped",
    
    "states": {
        "stopped": {
            "start": "started",
            "stop": "stopped"
        },
        "started": {
            "fade_in": "fade_in",
            "fade_out": "fade_out",
            "stop": "stopped"
        },
        "fade_in": {
            "fade_in": "fade_in",
            "stop": "stopped"
        },
        "fade_out": {
            "fade_out": "fade_out",
            "stop": "stopped"
        }
    },
    "data": {
      "start": 0,
      "end": 1,
      "duration": 3000,
      "step": 100
    },
    "methods": {
        "init": [
            "sm.tick = function(tran, msg) {",
            "   timeout.id = setTimeout(function() {",
            "       sm.text = sm.data.duration + ' - ' + sm.data.actual;",
            "       msg.payload = sm.data.actual;",
            "       node.send(msg);",
            "       resume(tran, msg);",
            "   }, sm.data.step);",
            "};",
            "sta.fill = 'grey';"
        ],
        "onBeforeTransition": [
            "if (typeof msg.payload.start !== 'undefined') sm.data.start = msg.payload.start;",
            "if (typeof msg.payload.end !== 'undefined') sm.data.end = msg.payload.end;",
            "if (typeof msg.payload.duration !== 'undefined') sm.data.duration = msg.payload.duration;",
            "if (typeof msg.payload.step !== 'undefined') sm.data.step = msg.payload.step;"
        ],
        "start": [
            "clearTimeout(timeout.id);",
            "sm.data.delta = (sm.data.step / sm.data.duration) * Math.abs(sm.data.start - sm.data.end);",
            "sm.data.actual = sm.data.start;",
            
            "if (sm.data.end > sm.data.start) {",
            "   sm.text = 'fade_in';",
            "   resume('fade_in', msg);",
            "} else {",
            "   sm.text = 'fade_out';",
            "   resume('fade_out', msg);",
            "};"
        ],
        "fade_in": [
            "if (sm.data.actual < sm.data.end) {",
            "   sm.data.actual = Math.round((sm.data.actual + sm.data.delta) * 100) / 100;",
            "   if (sm.data.actual > sm.data.end) {sm.data.actual = sm.data.end;};",
            "   sm.tick('fade_in', msg);",
            "} else {",
            "   resume('stop', msg);",
            "};"
        ],
        "fade_out": [
            "if (sm.data.actual > sm.data.end) {",
            "   sm.data.actual = Math.round((sm.data.actual - sm.data.delta) * 100) / 100;",
            "   if (sm.data.actual < sm.data.end) {sm.data.actual = sm.data.end;};",
            "   sm.tick('fade_out', msg);",
            "} else {",
            "   resume('stop', msg);",
            "};"
        ],
        "stop": [
            "sm.text = sm.currentState;",
            "clearTimeout(timeout.id);"
        ],
        "onAfterTransition": "output = false;",
        "status": {
            "fill": {
                "get": "sm.currentState === 'stopped' ? 'grey': 'green';"
            },
            "shape": "dot",
            "text": {
                "get": "sm.text || sm.currentState;"
            }
        }
    }
}

Flow

[{"id":"9baf77b8.1762e8","type":"dsm","z":"66646ca8.e97654","name":"fade in/out","sm_config":"{\n    \"currentState\": \"stopped\",\n    \n    \"states\": {\n        \"stopped\": {\n            \"start\": \"started\",\n            \"stop\": \"stopped\"\n        },\n        \"started\": {\n            \"fade_in\": \"fade_in\",\n            \"fade_out\": \"fade_out\",\n            \"stop\": \"stopped\"\n        },\n        \"fade_in\": {\n            \"fade_in\": \"fade_in\",\n            \"stop\": \"stopped\"\n        },\n        \"fade_out\": {\n            \"fade_out\": \"fade_out\",\n            \"stop\": \"stopped\"\n        }\n    },\n    \"data\": {\n      \"start\": 0,\n      \"end\": 1,\n      \"duration\": 3000,\n      \"step\": 100\n    },\n    \"methods\": {\n        \"init\": [\n            \"sm.tick = function(tran, msg) {\",\n            \"   timeout.id = setTimeout(function() {\",\n            \"       sm.text = sm.data.duration + ' - ' + sm.data.actual;\",\n            \"       msg.payload = sm.data.actual;\",\n            \"       node.send(msg);\",\n            \"       resume(tran, msg);\",\n            \"   }, sm.data.step);\",\n            \"};\",\n            \"sta.fill = 'grey';\"\n        ],\n        \"onBeforeTransition\": [\n            \"if (typeof msg.payload.start !== 'undefined') sm.data.start = msg.payload.start;\",\n            \"if (typeof msg.payload.end !== 'undefined') sm.data.end = msg.payload.end;\",\n            \"if (typeof msg.payload.duration !== 'undefined') sm.data.duration = msg.payload.duration;\",\n            \"if (typeof msg.payload.step !== 'undefined') sm.data.step = msg.payload.step;\"\n        ],\n        \"start\": [\n            \"clearTimeout(timeout.id);\",\n            \"sm.data.delta = (sm.data.step / sm.data.duration) * Math.abs(sm.data.start - sm.data.end);\",\n            \"sm.data.actual = sm.data.start;\",\n            \n            \"if (sm.data.end > sm.data.start) {\",\n            \"   sm.text = 'fade_in';\",\n            \"   resume('fade_in', msg);\",\n            \"} else {\",\n            \"   sm.text = 'fade_out';\",\n            \"   resume('fade_out', msg);\",\n            \"};\"\n        ],\n        \"fade_in\": [\n            \"if (sm.data.actual < sm.data.end) {\",\n            \"   sm.data.actual = Math.round((sm.data.actual + sm.data.delta) * 100) / 100;\",\n            \"   if (sm.data.actual > sm.data.end) {sm.data.actual = sm.data.end;};\",\n            \"   sm.tick('fade_in', msg);\",\n            \"} else {\",\n            \"   resume('stop', msg);\",\n            \"};\"\n        ],\n        \"fade_out\": [\n            \"if (sm.data.actual > sm.data.end) {\",\n            \"   sm.data.actual = Math.round((sm.data.actual - sm.data.delta) * 100) / 100;\",\n            \"   if (sm.data.actual < sm.data.end) {sm.data.actual = sm.data.end;};\",\n            \"   sm.tick('fade_out', msg);\",\n            \"} else {\",\n            \"   resume('stop', msg);\",\n            \"};\"\n        ],\n        \"stop\": [\n            \"sm.text = sm.currentState;\",\n            \"clearTimeout(timeout.id);\"\n        ],\n        \"onAfterTransition\": \"output = false;\",\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'stopped' ? 'grey': 'green';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.text || sm.currentState;\"\n            }\n        }\n    }\n}","x":350,"y":3440,"wires":[["b0b0af27.c2d3d"]]},{"id":"6c0e281f.b26388","type":"inject","z":"66646ca8.e97654","name":"fade in","topic":"start","payload":"{\"start\":0,\"end\":1,\"duration\":3000,\"step\":500}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":3440,"wires":[["9baf77b8.1762e8"]]},{"id":"d4bc8846.669bb8","type":"inject","z":"66646ca8.e97654","name":"fade out","topic":"start","payload":"{\"start\":1,\"end\":0,\"duration\":5000,\"step\":500}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":3480,"wires":[["9baf77b8.1762e8"]]},{"id":"f7b5f49e.165b58","type":"inject","z":"66646ca8.e97654","name":"","topic":"stop","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":3520,"wires":[["9baf77b8.1762e8"]]},{"id":"b0b0af27.c2d3d","type":"debug","z":"66646ca8.e97654","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":510,"y":3440,"wires":[]}]
Clone this wiki locally