忘備録-備忘録

技術的な備忘録

広告

※このエリアは、60日間投稿が無い場合に表示されます。記事を投稿すると、表示されなくなります。

node-redでLCD表示 その3

2017-12-12 20:08:00 | raspberry ...

前回のnode-redでLCD表示その2を少し変更しループなしで表示できるようにしました。LCDモジュールの初期化だけは時間をかける必要があるので別のフローにしてあります。

image

[{"id":"6a0bb275.50092c","type":"function","z":"e50639a8.db4118","name":"LCD表示","func":"// context.set('変数名',値); グローバル変数の設定\n// var val = context.get('変数名')||0;グローバル変数の読み込み\n// ||0; の部分は初期化指定 \n// 変数が設定されていない場合内は0に初期化\n\n//プロパティの追加\n//静的変数のように使用できる\n//注意: 外部からの参照も可能\nif (typeof this.times === 'undefined') {\n    this.times = 0;\n}\n\nmsg.address = 0x3e;\n//msg.address = 59;\nswitch(times) {\n    case 0:\n        msg.command = 0x00;\n        msg.payload = 0x38;\n        break;\n    case 1:\n        msg.command = 0x00;\n        msg.payload = 0x39;\n        break;\n    case 2:\n        msg.command = 0x00;\n        msg.payload = 0x14;\n        break;\n    case 3:\n        msg.command = 0x00;\n        msg.payload = 0x73;\n        break;\n    case 4:\n        msg.command = 0x00;\n        msg.payload = 0x56;\n        break;\n    case 5:\n        msg.command = 0x00;\n        msg.payload = 0x6c;\n        break;\n    case 6:\n        msg.command = 0x00;\n        msg.payload = 0x38;\n        break;\n    case 7:\n        msg.command = 0x00;\n        msg.payload = 0x01;\n        break;\n    case 8:\n        msg.command = 0x00;\n        msg.payload = 0x0c;\n        break;\n    case 9:\n        msg.command = 0x00;\n        msg.payload = 0xc0;\n        break;\n    default:\n        return ;    \n}\nthis.times++;\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":460,"wires":[["3f3e776a.313c68","59fdff7a.0ccb1"]]},{"id":"efa65d49.0b2b5","type":"inject","z":"e50639a8.db4118","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"","x":237,"y":460,"wires":[["6a0bb275.50092c"]]},{"id":"3f3e776a.313c68","type":"delay","z":"e50639a8.db4118","name":"","pauseType":"delay","timeout":"400","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":449,"y":372,"wires":[["6a0bb275.50092c"]]},{"id":"59fdff7a.0ccb1","type":"i2c out","z":"e50639a8.db4118","name":"","address":"","command":"","payload":"payload","payloadType":"msg","count":"1","x":680,"y":460,"wires":[]},{"id":"d41c3836.8f9b58","type":"function","z":"e50639a8.db4118","name":"LCD表示","func":"\n let lcdData = [\n//    [0x00,0x38], 初期化データ\n//    [0x00,0x39],\n//    [0x00,0x14],\n//    [0x00,0x73],\n//    [0x00,0x56],\n//    [0x00,0x6c],\n//    [0x00,0x38],\n//    [0x00,0x01],\n//    [0x00,0x0c],\n//    [0x00,0xc0],\n    ];\n\nlet outputMsgs = [];\n\nconst LcdI2Caddr = 0x3e;\nlet i;\n\nfor(i=0;i
コメント

node-redでLCD表示 その2

2017-12-11 20:36:14 | raspberry ...

前回のnode-redでLCD表示を少し変更し任意の文字列を表示できるようにしました。

image

[{"id":"38826655.6c18fa","type":"function","z":"3cd97708.f90178","name":"LCD表示","func":"if (typeof this.times === 'undefined') {\n    this.times = 0;\n}\n\nif( typeof this.lcdData === 'undefined' ) {\n    this.lcdData = [\n    [0x00,0x38],\n    [0x00,0x39],\n    [0x00,0x14],\n    [0x00,0x73],\n    [0x00,0x56],\n    [0x00,0x6c],\n    [0x00,0x38],\n    [0x00,0x01],\n    [0x00,0x0c],\n    [0x00,0xc0],\n    ];\n}\n\nconst initLcdData = 10;\nconst LcdI2Caddr = 0x3e;\n\nif(this.times===0) {\n    for(i=0;i<msg.payload.length;i++) {\n        this.lcdData.push( [0x40, msg.payload[i] ] );\n    }\n}\n\nif(this.times<this.lcdData.length) {\n    msg.address = LcdI2Caddr;\n    msg.command = this.lcdData[times][0];\n    msg.payload = this.lcdData[times][1];\n    this.times++;\n    return msg;    \n} else {\n    return;\n}\nreturn msg;","outputs":1,"noerr":0,"x":535,"y":813,"wires":[["9871647a.f198f8","d10bebd2.0b5d38","9655fadd.ec6138"]]},{"id":"42b20597.19dd7c","type":"inject","z":"3cd97708.f90178","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":175,"y":813,"wires":[["b50affff.3e98"]]},{"id":"9655fadd.ec6138","type":"debug","z":"3cd97708.f90178","name":"","active":true,"console":"false","complete":"false","x":699,"y":857,"wires":[]},{"id":"9871647a.f198f8","type":"i2c out","z":"3cd97708.f90178","name":"","address":"","command":"","payload":"payload","payloadType":"msg","count":"1","x":699,"y":813,"wires":[]},{"id":"d10bebd2.0b5d38","type":"delay","z":"3cd97708.f90178","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":532,"y":729,"wires":[["38826655.6c18fa"]]},{"id":"b50affff.3e98","type":"template","z":"3cd97708.f90178","name":"メッセージ","field":"payload","fieldType":"msg","format":"text","syntax":"plain","template":"Hello world!!","output":"str","x":354,"y":812,"wires":[["38826655.6c18fa"]]}]
コメント

node-redでLCD表示

2017-12-09 18:20:00 | raspberry ...

raspberry piとnode-red上で秋月電子通商製のI2C接続小型キャラクタLCDモジュール に文字を出力するflowを作成しました。最初に[パレットの管理]で[node-red-contrib-i2c]を追加します。

image

設定は

msg.address : I2C機器のアドレス

msg.command : 機器に送るコマンド(最初の1byte)

msg.payload : 機器に送るデータ(2byte目以降 最大32byte)

となります。

LCDに表示するflowは次のようになります。

image

複数回コマンドを出力する必要があったため、[delay]を使いループを組みました。[LCD表示]ではreturnを工夫して無限ループを終了しています。

ループは”return msg;”で値返し回ります。”return;”のみだとメッセージがないのでループを回らなくなります。

全体を次に置いておきます。

[{"id":"1c3ab761.76a7e9","type":"function","z":"3cd97708.f90178","name":"LCD表示","func":"// context.set('変数名',値); グローバル変数の設定\n// var val = context.get('変数名')||0;グローバル変数の読み込み\n// ||0; の部分は初期化指定 \n// 変数が設定されていない場合内は0に初期化\n\n//プロパティの追加\n//静的変数のように使用できる\n//注意: 外部からの参照も可能\nif (typeof this.times === 'undefined') {\n    this.times = 0;\n}\n\nvar lcdData = [\n    [0x00,0x38],\n    [0x00,0x39],\n    [0x00,0x14],\n    [0x00,0x73],\n    [0x00,0x56],\n    [0x00,0x6c],\n    [0x00,0x38],\n    [0x00,0x01],\n    [0x00,0x0c],\n    [0x00,0xc0],\n    [0x40,'H'],\n    [0x40,'e'],\n    [0x40,'l'],\n    [0x40,'l'],\n    [0x40,'o'],\n    [0x40,' '],\n    [0x40,'w'],\n    [0x40,'o'],\n    [0x40,'r'],\n    [0x40,'l'],\n    [0x40,'d'],\n    ];\n\nif(times<lcdData.length) {\n    msg.address = 0x3e;\n    msg.command = lcdData[times][0];\n    msg.payload = lcdData[times][1];\n    this.times++;\n    return msg;    \n} else {\n    return;\n}\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":567,"wires":[["b6f9485b.c4d978","a4a8e74f.f796a8","39ea46fa.df3bfa"]]},{"id":"d6d6c364.b56ee","type":"inject","z":"3cd97708.f90178","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":254,"y":567,"wires":[["1c3ab761.76a7e9"]]},{"id":"39ea46fa.df3bfa","type":"debug","z":"3cd97708.f90178","name":"","active":true,"console":"false","complete":"false","x":584,"y":611,"wires":[]},{"id":"b6f9485b.c4d978","type":"i2c out","z":"3cd97708.f90178","name":"","address":"","command":"","payload":"payload","payloadType":"msg","count":"1","x":584,"y":567,"wires":[]},{"id":"a4a8e74f.f796a8","type":"delay","z":"3cd97708.f90178","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":417,"y":483,"wires":[["1c3ab761.76a7e9"]]}]
コメント