７セグディスプレイ「Nanapo」用のノードを作って、ささっと公開

ちょっとレトロな感じもするUSB接続の[７セグメントLEDのディスプレイ Nanapo](https://bit-trade-one.co.jp/ad7sgpr/)がビット・トレード・ワンから販売されていたので、使ってみました。
USB Type-CでPCと接続するとWebブラウザやプログラムから8桁の数値や7セグで表現された文字などを表示することができます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/e41977c6-fdf1-f927-b05b-2b3fc73b359c.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/154aa41b-da8b-a42c-3831-a82fe7b05afb.png)
（enebularと表示）

## ノードを作りました

オフィシャルの[サポートページ](https://bit-trade-one.github.io/AD7SGPR/)では、WebアプリやPythonコードなどが公開されているが、enebular（Node-RED）から簡単に制御できるようにノードを作ってみました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/3f7f961d-c731-1e09-c06d-4ea0b617f5f9.png)

Node-REDで公開しているので、`パレットの管理` ⇒ `ノードを追加`から「nanapo」で検索すれば出てきますので、`ノードを追加`すれば利用できます。
（エラー処理など手を抜いてる部分があるので、順次更新予定）

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/3a37f4d9-88aa-6cea-c158-8a2671ad8487.png)
（画像は、enebular editorを使ってますが、普通のNode-REDでも使用できます）

設定は、Nanapoが接続されているシリアルポート番号を指定するだけです。

Raspberry Piの例（Windowsの場合は、COM3などを書けばOKです）
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/62e9a1a3-df22-9a3e-3f90-41e87f04d55d.png)

## サンプルフロー

こんな感じのフローで、数値や文字の表示、輝度変更やアニメーション設定ができます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/44789897-fcea-1805-f9e8-9d6ae438e883.png)

フローのJSON（これをコピーして、読み込めば使えます）
```json
[{"id":"8c6e6c8b714184ea","type":"nanapo","z":"8ab8e8739a825a6f","name":"","port":"","x":460,"y":680,"wires":[["6203e616d07b4b6d"]]},{"id":"50060c04e5853d00","type":"inject","z":"8ab8e8739a825a6f","name":"enebularを表示","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"enebular","payloadType":"str","x":180,"y":680,"wires":[["8c6e6c8b714184ea"]]},{"id":"401d35f69c8881d2","type":"inject","z":"8ab8e8739a825a6f","name":"表示クリア","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"@CLR","payloadType":"str","x":160,"y":740,"wires":[["8c6e6c8b714184ea"]]},{"id":"6203e616d07b4b6d","type":"debug","z":"8ab8e8739a825a6f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":680,"wires":[]},{"id":"85689f3787169ce6","type":"inject","z":"8ab8e8739a825a6f","name":"輝度を2に設定","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"@BRI2","payloadType":"str","x":180,"y":800,"wires":[["8c6e6c8b714184ea"]]},{"id":"18780134ec1e6723","type":"inject","z":"8ab8e8739a825a6f","name":"輝度を7に設定","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"@BRI7","payloadType":"str","x":180,"y":860,"wires":[["8c6e6c8b714184ea"]]},{"id":"c6278896040371c1","type":"inject","z":"8ab8e8739a825a6f","name":"アニメーション設定","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"@ANI1","payloadType":"str","x":190,"y":920,"wires":[["8c6e6c8b714184ea"]]},{"id":"9675bfaff09aacab","type":"inject","z":"8ab8e8739a825a6f","name":"12345678を表示","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12345678","payloadType":"str","x":180,"y":620,"wires":[["8c6e6c8b714184ea"]]},{"id":"adee3f2054e2feda","type":"inject","z":"8ab8e8739a825a6f","name":"アニメーション解除","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"@ANI0","payloadType":"str","x":190,"y":980,"wires":[["8c6e6c8b714184ea"]]}]
```
Nanapoには、タクトスイッチが取り付けられています。作ったノードでは、このボタンを押したタイミングで、取得できるようになってます。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/92548e5c-c315-df78-ea2a-2e1f6e67480e.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/0c65f14c-8fe0-b3dd-ac69-339a40c3e45b.png)

## 使用例
USB接続なので、Raspberry Piでも使用できます。数値の表示やボタン入力が手軽にできると思います。
Raspberry Piには、enebularの[エージェント実行環境](https://blog.enebular.com/function/practical-usage-of-the-agent-execution-environment)をインストールしてフローをデプロイしています。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/ac209440-fdf4-6ee5-ebe3-dedd15ead968.png)

オムロンの[環境センサ2JCIE-BU](https://components.omron.com/jp-ja/products/sensors/2JCIE-BU)を使用して、温度の表示とボタンを押すことで、湿度や気圧をNanapoで表示するフローを作ってみました。
※`@uhuru/node-red-contrib-omron-2jcie-bu` ノードをパレット管理から追加し、シリアルポートの設定が必要になります。(設定方法は、[こちら](https://blog.enebular.com/samples/ia-cloud-sample-blog03/)参照)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/ebbbe939-260a-287d-2513-f103160aa20e.png)

フローのJSON
```json
[{"id":"b93cc9e91521db1e","type":"nanapo","z":"8ab8e8739a825a6f","name":"","port":"/dev/ttyACM0","x":920,"y":180,"wires":[["d08e0b0e20dec4ba"]]},{"id":"e7ad568d16c77ccd","type":"2jcie-bu","z":"8ab8e8739a825a6f","name":"","port":"/dev/ttyUSB0","event_target":0,"event_type":"0x0001","event_upper":0,"event_lower":0,"event_count":0,"event2_target":0,"event2_type":"0x0001","event2_upper":0,"event2_lower":0,"event2_count":0,"event3_target":0,"event3_type":"0x0001","event3_upper":0,"event3_lower":0,"event3_count":0,"led_event_rule":"0x0001","led_event_red":0,"led_event_green":0,"led_event_blue":0,"led_normal_rule":0,"led_normal_red":0,"led_normal_green":0,"led_normal_blue":0,"x":420,"y":180,"wires":[["c9dfb68f37e5dbbd"],[]]},{"id":"6f6ede49050381df","type":"change","z":"8ab8e8739a825a6f","name":"温度","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.temperature","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":220,"wires":[["b93cc9e91521db1e"]]},{"id":"c9dfb68f37e5dbbd","type":"switch","z":"8ab8e8739a825a6f","name":"","property":"button","propertyType":"flow","rules":[{"t":"eq","v":"C","vt":"str"},{"t":"eq","v":"D","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":570,"y":180,"wires":[["16b8a7204db95de9"],["32369fcff0a953b1"],["6f6ede49050381df"]]},{"id":"d08e0b0e20dec4ba","type":"switch","z":"8ab8e8739a825a6f","name":"","property":"payload.cmd","propertyType":"msg","rules":[{"t":"eq","v":"push","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":380,"wires":[["05f5800a8ee54b65"]]},{"id":"05f5800a8ee54b65","type":"change","z":"8ab8e8739a825a6f","name":"","rules":[{"t":"set","p":"button","pt":"flow","to":"payload.data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":380,"wires":[["e7ad568d16c77ccd","c5cd2fc58172a692"]]},{"id":"16b8a7204db95de9","type":"change","z":"8ab8e8739a825a6f","name":"湿度","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.relativeHumidity","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":140,"wires":[["b93cc9e91521db1e"]]},{"id":"32369fcff0a953b1","type":"change","z":"8ab8e8739a825a6f","name":"気圧","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.barometricPressure","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":180,"wires":[["b93cc9e91521db1e"]]},{"id":"0c81f0c5852c2843","type":"change","z":"8ab8e8739a825a6f","name":"","rules":[{"t":"set","p":"button","pt":"flow","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1020,"y":380,"wires":[["e7ad568d16c77ccd"]]},{"id":"c5cd2fc58172a692","type":"delay","z":"8ab8e8739a825a6f","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":840,"y":380,"wires":[["0c81f0c5852c2843"]]},{"id":"ddbc6c471c54af16","type":"inject","z":"8ab8e8739a825a6f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"600","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":180,"wires":[["e7ad568d16c77ccd"]]}]
```

10分ごとにセンサから取得した温度を表示します。Cボタンを押すと湿度、Dボタンを押すと気圧を5秒表示し、温度に戻ります。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109992/296877b1-c3ea-b264-1122-29ea25b54bbb.png)

## Nanapoを使ってみて
Raspberry PiやPCから数値の表現をするのに良いですね。センサーやWebから取得できる数値と連動させても良いですし、アニメーションの表現ができるので、ちょっとしたゲームなどにも利用できるのではないでしょうか。
コマンド次第で、1セグごとに点灯を制御できるので、工夫次第でいろいろな表現ができますね。
もっと大きな７セグLEDを使ったものもあったら面白そう。