DMXkingのLeDMX4MAXとNode-REDを使ってSPI-LEDの制御をする方法 その1

Case1:指定した明るさで指定した数量のLEDを点灯させるフロー

Node-RED Dashboardにて、LEDの数量(SPIのアドレス数)と明るさを入力し点灯ボタンを押すと点灯します。

ただし、これではRGBやRGBWのLEDは全て白色でしか点灯しません。

[{"id":"5a49eb99c904a118","type":"tab","label":"ROOM","disabled":false,"info":"","env":[]},{"id":"9dc32a814eaecf05","type":"sACN","z":"5a49eb99c904a118","server":"32222a9fe48c7238","universe":"","channel":"","transition":"instant","transitionRate":50,"transitionTime":1000,"name":"","x":540,"y":740,"wires":[]},{"id":"f3e94c6f62638dc0","type":"inject","z":"5a49eb99c904a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":120,"wires":[["3c349ebef5583d7a"]]},{"id":"115caffe4e619b59","type":"function","z":"5a49eb99c904a118","name":"function 20","func":"const ran=flow.get(\"number\");\nvar light = 0;\n\nfor(let i=0;i<200;i++){\n    light=light+1.2\n    ran.push(light);\n    flow.set(\"number\", ran)\n}\n\nnode.send({\n    topic: \"1/1\",\n    payload: ran,\n})","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":120,"wires":[["473a71fff830083e"]]},{"id":"a2d84c73c392cec7","type":"inject","z":"5a49eb99c904a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":40,"wires":[["10420cba55caf821"]]},{"id":"10420cba55caf821","type":"switch","z":"5a49eb99c904a118","name":"number","property":"number","propertyType":"flow","rules":[{"t":"nnull"},{"t":"null"}],"checkall":"true","repair":false,"outputs":2,"x":320,"y":40,"wires":[["02ab4331b2385952"],["02ab4331b2385952"]]},{"id":"02ab4331b2385952","type":"change","z":"5a49eb99c904a118","name":"","rules":[{"t":"set","p":"number","pt":"flow","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":40,"wires":[[]]},{"id":"458f584b18d7fad9","type":"function","z":"5a49eb99c904a118","name":"function 22","func":"const ran = flow.get(\"number\");\nvar number = 0;\n\nfor(let i=0;i<200;i++){\n    ran.push(number);\n}\n\nflow.set(\"number\",ran);\n\nnode.send({\n    topic: \"1/1\",\n    payload: ran,\n})","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":180,"wires":[["473a71fff830083e"]]},{"id":"5306758f472a77ff","type":"inject","z":"5a49eb99c904a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":180,"wires":[["9b834eb398ad8048"]]},{"id":"3c349ebef5583d7a","type":"change","z":"5a49eb99c904a118","name":"","rules":[{"t":"set","p":"number","pt":"flow","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":120,"wires":[["115caffe4e619b59"]]},{"id":"9b834eb398ad8048","type":"change","z":"5a49eb99c904a118","name":"","rules":[{"t":"set","p":"number","pt":"flow","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":180,"wires":[["458f584b18d7fad9"]]},{"id":"473a71fff830083e","type":"debug","z":"5a49eb99c904a118","name":"debug 69","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":100,"wires":[]},{"id":"cfb3d2729460448f","type":"debug","z":"5a49eb99c904a118","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":660,"wires":[]},{"id":"dbc11c92160043e4","type":"ui_text_input","z":"5a49eb99c904a118","name":"","label":"LED数","tooltip":"","group":"71c614f549b6503f","order":1,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":210,"y":520,"wires":[["d79a98d02e9fb05e"]]},{"id":"d79a98d02e9fb05e","type":"change","z":"5a49eb99c904a118","name":"global","rules":[{"t":"set","p":"ledNubmers","pt":"global","to":"payload ~> $number","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":520,"wires":[[]]},{"id":"cc47a7e6977fb154","type":"ui_text_input","z":"5a49eb99c904a118","name":"","label":"明るさ","tooltip":"","group":"71c614f549b6503f","order":1,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":210,"y":460,"wires":[["af70b8e04ca2013f"]]},{"id":"af70b8e04ca2013f","type":"change","z":"5a49eb99c904a118","name":"global","rules":[{"t":"set","p":"dimLevel","pt":"global","to":"payload ~> $number","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":460,"wires":[[]]},{"id":"6e36eb1bdca884a2","type":"ui_button","z":"5a49eb99c904a118","name":"","group":"71c614f549b6503f","order":2,"width":0,"height":0,"passthru":false,"label":"点灯","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":210,"y":600,"wires":[["f08592be1ba0010a"]]},{"id":"f08592be1ba0010a","type":"function","z":"5a49eb99c904a118","name":"データ生成","func":"var dimLevel = global.get(\"dimLevel\");\nvar ledNubmers = global.get(\"ledNubmers\");\n\nmsg.payload = Array(ledNubmers).fill(dimLevel);\n\nmsg.topic= \"1/1\";\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":660,"wires":[["9dc32a814eaecf05","cfb3d2729460448f"]]},{"id":"32222a9fe48c7238","type":"sacn-config","hostOrUniverse":"192.168.1.83","port":"5568"},{"id":"71c614f549b6503f","type":"ui_group","name":"Case1","tab":"534a5ff078a4f4f4","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"534a5ff078a4f4f4","type":"ui_tab","name":"SPI-Control","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

CASE2: 指定したRGBの値で指定したRGB LED素子のLEDを点灯させるフロー

このフローはRGBのLED素子の数量を最初に入力し、次にRed,Green,Blueの値を入れ点灯ボタンを押すと、RGBLEDの素子数のLEDが点灯するフローです。

[{"id":"9330d723b5bea9db","type":"sACN","z":"5a49eb99c904a118","server":"32222a9fe48c7238","universe":"","channel":"","transition":"instant","transitionRate":50,"transitionTime":1000,"name":"","x":1300,"y":560,"wires":[]},{"id":"b36e8c3057d30413","type":"debug","z":"5a49eb99c904a118","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1370,"y":480,"wires":[]},{"id":"45b56d2b5c7ce180","type":"ui_text_input","z":"5a49eb99c904a118","name":"","label":"LED数(RGB)","tooltip":"","group":"71c614f549b6503f","order":6,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":890,"y":320,"wires":[["01e1d7528ac9ddd4"]]},{"id":"01e1d7528ac9ddd4","type":"change","z":"5a49eb99c904a118","name":"global","rules":[{"t":"set","p":"ledNubmers","pt":"global","to":"payload ~> $number","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1050,"y":320,"wires":[[]]},{"id":"51715b4281c54cff","type":"change","z":"5a49eb99c904a118","name":"global","rules":[{"t":"set","p":"dimLevelR","pt":"global","to":"payload.dimLevelR","tot":"jsonata"},{"t":"set","p":"dimLevelG","pt":"global","to":"payload.dimLevelG","tot":"jsonata"},{"t":"set","p":"dimLevelB","pt":"global","to":"payload.dimLevelB","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":400,"wires":[["953080e9c8e5ee22","4c0a937a991f8e82"]]},{"id":"953080e9c8e5ee22","type":"function","z":"5a49eb99c904a118","name":"データ生成","func":"// LEDの数を取得\nconst count = global.get(\"ledNubmers\");\n\n// グローバル変数からRGB値を取得\nconst r = global.get('dimLevelR');\nconst g = global.get('dimLevelG');\nconst b = global.get('dimLevelB');\n\n// 各色の値を配列に展開\nlet rgbArray = [];\nfor (let i = 0; i < count; i++) {\n    rgbArray.push(r, g, b);\n}\n\nmsg.topic= \"1/1\";\nmsg.payload = rgbArray;\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1130,"y":480,"wires":[["9330d723b5bea9db","b36e8c3057d30413"]]},{"id":"4c0a937a991f8e82","type":"ui_form","z":"5a49eb99c904a118","name":"","label":"","group":"71c614f549b6503f","order":7,"width":0,"height":0,"options":[{"label":"Red","value":"dimLevelR","type":"number","required":true,"rows":null},{"label":"Green","value":"dimLevelG","type":"number","required":true,"rows":null},{"label":"Blue","value":"dimLevelB","type":"number","required":true,"rows":null}],"formValue":{"dimLevelR":"","dimLevelG":"","dimLevelB":""},"payload":"","submit":"点灯","cancel":"","topic":"topic","topicType":"msg","splitLayout":"","className":"","x":870,"y":400,"wires":[["51715b4281c54cff"]]},{"id":"f0d074b341e92445","type":"ui_text","z":"5a49eb99c904a118","group":"71c614f549b6503f","order":5,"width":0,"height":0,"name":"","label":"CASE2","format":"{{msg.payload}}","layout":"row-spread","className":"","style":true,"font":"Arial Black,Arial Black,Gadget,sans-serif","fontSize":16,"color":"#000000","x":880,"y":260,"wires":[]},{"id":"32222a9fe48c7238","type":"sacn-config","hostOrUniverse":"192.168.1.83","port":"5568"},{"id":"71c614f549b6503f","type":"ui_group","name":"Case1","tab":"534a5ff078a4f4f4","order":1,"disp":true,"width":6,"collapse":false,"className":""},{"id":"534a5ff078a4f4f4","type":"ui_tab","name":"SPI-Control","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

CASE3: RGB-LEDで明るさが段階的に明るくなるフロー

RGBのLEDがN個あり、1番目の明るさが0、N番目の明るさが指定された値で、1番からN番目まで徐々に指定された値で点灯するフローです。

[{"id":"08f0cf91cae7cfbc","type":"ui_text","z":"5a49eb99c904a118","group":"71c614f549b6503f","order":8,"width":0,"height":0,"name":"","label":"CASE3","format":"{{msg.payload}}","layout":"row-spread","className":"","style":true,"font":"Arial Black,Arial Black,Gadget,sans-serif","fontSize":16,"color":"#000000","x":1620,"y":340,"wires":[]},{"id":"92d30ccdebb7aff8","type":"sACN","z":"5a49eb99c904a118","server":"32222a9fe48c7238","universe":"","channel":"","transition":"instant","transitionRate":50,"transitionTime":1000,"name":"","x":2040,"y":640,"wires":[]},{"id":"3cc65b645db769a0","type":"debug","z":"5a49eb99c904a118","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2110,"y":560,"wires":[]},{"id":"7b8505856443099c","type":"ui_text_input","z":"5a49eb99c904a118","name":"CASE3 LED数","label":"LED数(RGB)","tooltip":"","group":"71c614f549b6503f","order":9,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":1640,"y":400,"wires":[["ecbcdc08b403cd32"]]},{"id":"ecbcdc08b403cd32","type":"change","z":"5a49eb99c904a118","name":"global","rules":[{"t":"set","p":"LEDCount","pt":"global","to":"payload ~> $number","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1790,"y":400,"wires":[[]]},{"id":"163186183a66b3e5","type":"change","z":"5a49eb99c904a118","name":"global","rules":[{"t":"set","p":"dimLevelR","pt":"global","to":"payload.dimLevelR","tot":"jsonata"},{"t":"set","p":"dimLevelG","pt":"global","to":"payload.dimLevelG","tot":"jsonata"},{"t":"set","p":"dimLevelB","pt":"global","to":"payload.dimLevelB","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1770,"y":480,"wires":[["5301560b8b4a52af","25f6ecbdbcc5551a"]]},{"id":"5301560b8b4a52af","type":"function","z":"5a49eb99c904a118","name":"データ生成","func":"// LEDの数と各色の目標値を取得\nconst count = global.get(\"LEDCount\");\n\nconst targetR = global.get('dimLevelR');\nconst targetG = global.get('dimLevelG');\nconst targetB = global.get('dimLevelB');\n\n// 各色の値を配列に展開\nlet rgbArray = [];\nfor (let i = 0; i < count; i++) {\n    // 線形に増加する計算\n    let r = Math.round(targetR * i / (count - 1));\n    let g = Math.round(targetG * i / (count - 1));\n    let b = Math.round(targetB * i / (count - 1));\n    rgbArray.push(r, g, b);\n}\n\nmsg.topic = \"1/1\";\nmsg.payload = rgbArray;\nreturn msg;\n\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1870,"y":560,"wires":[["92d30ccdebb7aff8","3cc65b645db769a0"]]},{"id":"25f6ecbdbcc5551a","type":"ui_form","z":"5a49eb99c904a118","name":"CASE3","label":"","group":"71c614f549b6503f","order":10,"width":0,"height":0,"options":[{"label":"Red","value":"dimLevelR","type":"number","required":true,"rows":null},{"label":"Green","value":"dimLevelG","type":"number","required":true,"rows":null},{"label":"Blue","value":"dimLevelB","type":"number","required":true,"rows":null}],"formValue":{"dimLevelR":"","dimLevelG":"","dimLevelB":""},"payload":"","submit":"点灯","cancel":"","topic":"topic","topicType":"msg","splitLayout":"","className":"","x":1620,"y":480,"wires":[["163186183a66b3e5"]]},{"id":"71c614f549b6503f","type":"ui_group","name":"Case1","tab":"534a5ff078a4f4f4","order":1,"disp":true,"width":6,"collapse":false,"className":""},{"id":"32222a9fe48c7238","type":"sacn-config","hostOrUniverse":"192.168.1.83","port":"5568"},{"id":"534a5ff078a4f4f4","type":"ui_tab","name":"SPI-Control","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

CASE4: RGB-LEDで明るさが段階的に暗くなるフロー

上記の反対バージョン。

RGBのLEDがN個あり、1番目の明るさが指定された値で、N番目の明るさが0になり、1番からN番目まで徐々に暗くなるフローです。

[{"id":"08f0cf91cae7cfbc","type":"ui_text","z":"5a49eb99c904a118","group":"71c614f549b6503f","order":9,"width":0,"height":0,"name":"","label":"CASE4","format":"{{msg.payload}}","layout":"row-spread","className":"","style":true,"font":"Arial Black,Arial Black,Gadget,sans-serif","fontSize":16,"color":"#000000","x":1620,"y":340,"wires":[]},{"id":"92d30ccdebb7aff8","type":"sACN","z":"5a49eb99c904a118","server":"32222a9fe48c7238","universe":"","channel":"","transition":"instant","transitionRate":50,"transitionTime":1000,"name":"","x":2040,"y":640,"wires":[]},{"id":"3cc65b645db769a0","type":"debug","z":"5a49eb99c904a118","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2110,"y":560,"wires":[]},{"id":"7b8505856443099c","type":"ui_text_input","z":"5a49eb99c904a118","name":"CASE4 LED数","label":"LED数(RGB)","tooltip":"","group":"71c614f549b6503f","order":8,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":1640,"y":400,"wires":[["ecbcdc08b403cd32"]]},{"id":"ecbcdc08b403cd32","type":"change","z":"5a49eb99c904a118","name":"global","rules":[{"t":"set","p":"LEDCount","pt":"global","to":"payload ~> $number","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1790,"y":400,"wires":[[]]},{"id":"163186183a66b3e5","type":"change","z":"5a49eb99c904a118","name":"global","rules":[{"t":"set","p":"dimLevelR","pt":"global","to":"payload.dimLevelR","tot":"jsonata"},{"t":"set","p":"dimLevelG","pt":"global","to":"payload.dimLevelG","tot":"jsonata"},{"t":"set","p":"dimLevelB","pt":"global","to":"payload.dimLevelB","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1770,"y":480,"wires":[["25f6ecbdbcc5551a","1498c525b0f32268"]]},{"id":"25f6ecbdbcc5551a","type":"ui_form","z":"5a49eb99c904a118","name":"CASE4 Target","label":"","group":"71c614f549b6503f","order":10,"width":0,"height":0,"options":[{"label":"Red","value":"dimLevelR","type":"number","required":true,"rows":null},{"label":"Green","value":"dimLevelG","type":"number","required":true,"rows":null},{"label":"Blue","value":"dimLevelB","type":"number","required":true,"rows":null}],"formValue":{"dimLevelR":"","dimLevelG":"","dimLevelB":""},"payload":"","submit":"点灯","cancel":"","topic":"topic","topicType":"msg","splitLayout":"","className":"","x":1640,"y":480,"wires":[["163186183a66b3e5"]]},{"id":"1498c525b0f32268","type":"function","z":"5a49eb99c904a118","name":"データ生成","func":"// LEDの数と各色の目標値を取得\nconst count = global.get(\"LEDCount\");\nconst targetR = global.get('dimLevelR');\nconst targetG = global.get('dimLevelG');\nconst targetB = global.get('dimLevelB');\n\n// 各色の値を配列に展開(LED制御用)\nlet rgbArray = [];\nfor (let i = 0; i < count; i++) {\n    // 逆線形に減少する計算\n    let r = Math.round(targetR * (count - 1 - i) / (count - 1));\n    let g = Math.round(targetG * (count - 1 - i) / (count - 1));\n    let b = Math.round(targetB * (count - 1 - i) / (count - 1));\n    rgbArray.push(r, g, b);  // RGBをサブ配列として格納\n}\n\nmsg.topic = \"1/1\";\nmsg.payload = rgbArray;\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1870,"y":560,"wires":[["92d30ccdebb7aff8","3cc65b645db769a0"]]},{"id":"6e54706ed42dc589","type":"function","z":"5a49eb99c904a118","name":"データ生成","func":"// LEDの数と各色の目標値を取得\nconst count = global.get(\"LEDCount\");\n\nconst targetR = global.get('dimLevelR');\nconst targetG = global.get('dimLevelG');\nconst targetB = global.get('dimLevelB');\n\n// 各色の値を配列に展開\nlet rgbArray = [];\nfor (let i = 0; i < count; i++) {\n    // 線形に増加する計算\n    let r = Math.round(targetR * i / (count - 1));\n    let g = Math.round(targetG * i / (count - 1));\n    let b = Math.round(targetB * i / (count - 1));\n    rgbArray.push(r, g, b);\n}\n\nmsg.topic = \"1/1\";\nmsg.payload = rgbArray;\nreturn msg;\n\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1970,"y":480,"wires":[[]]},{"id":"71c614f549b6503f","type":"ui_group","name":"Case1","tab":"534a5ff078a4f4f4","order":1,"disp":true,"width":6,"collapse":false,"className":""},{"id":"32222a9fe48c7238","type":"sacn-config","hostOrUniverse":"192.168.1.83","port":"5568"},{"id":"534a5ff078a4f4f4","type":"ui_tab","name":"SPI-Control","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

まとめ

以上、DMXkingのLeDMX4MAXとNode-REDを使ってSPI-LEDの制御をする方法について、静的なケースについて紹介しました。

次は動的なケースも紹介します。