raspberry pi 1 model bで、node-red　その１７

#概要
raspberry pi 1 model bの、node-redで、ゲームしてみた。
仕掛けは、
　まず、jsdoでプロトタイピング。
　入力は、スイッチ１個、表示は、OLED。
　フロー内でスコープな変数、content.flow使ってる。
　exec モジュールで、pythonに引数を与えてる。

#開発環境
raspberry pi 1 model b
raspbian 2016_09_23 jessie lite

#写真
![MVC-013S.JPG](https://qiita-image-store.s3.amazonaws.com/0/18104/d05b1914-4486-5638-44fe-0db4780c3eb7.jpeg)


#動画
https://www.youtube.com/watch?v=YQItdvvzN2M

#プロトタイピング
http://jsdo.it/ohisama1/Qt7D

#サンプルコード
![pong.JPG](https://qiita-image-store.s3.amazonaws.com/0/18104/49ffe4ca-6c1f-1ef9-457b-6edb6de36c91.jpeg)


```
[{"id":"2f69734f.48286c","type":"exec","z":"4f4f703e.a2a9d","command":"sudo python /home/pi/py/pong.py","addpay":true,"append":"","useSpawn":"","timer":"","name":"pong","x":128,"y":1576.5,"wires":[["ec949010.86b94"],[],[]]},{"id":"11c22c0f.285334","type":"rpi-gpio in","z":"4f4f703e.a2a9d","name":"gpio17","pin":"11","intype":"up","debounce":"200","read":true,"x":67,"y":1183,"wires":[["7d016083.ab7d8"]]},{"id":"fa3ed4cc.c6ce88","type":"inject","z":"4f4f703e.a2a9d","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":103,"y":1298,"wires":[["4e61184f.06e028","d6368de3.d8471"]]},{"id":"4e61184f.06e028","type":"function","z":"4f4f703e.a2a9d","name":"setup","func":"context.flow.set('x', 64);\ncontext.flow.set('y', 0);\ncontext.flow.set('dx', -4);\ncontext.flow.set('dy', -4);\ncontext.flow.set('z', 54);\ncontext.flow.set('pressed', 1);\ncontext.flow.set('game', 1);\nreturn msg;","outputs":1,"noerr":0,"x":272,"y":1299,"wires":[["e60ff8b.ae56608"]]},{"id":"e60ff8b.ae56608","type":"switch","z":"4f4f703e.a2a9d","name":"owari","property":"game","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"neq","v":"1","vt":"num"}],"checkall":"true","outputs":2,"x":202,"y":1386,"wires":[["a38ac590.71af58"],["7fd95f20.43153","92a7c74a.5a0e68"]]},{"id":"a38ac590.71af58","type":"function","z":"4f4f703e.a2a9d","name":"press check","func":"var pressed = context.flow.get('pressed') || 0;\nvar x = context.flow.get('x') || 0;\nvar y = context.flow.get('y') || 0;\nvar dx = context.flow.get('dx') || 0;\nvar dy = context.flow.get('dy') || 0;\nvar z = context.flow.get('z') || 0;\n\nif (x + dx > 128 - 3 || x + dx < 3)\n{\n    dx = -dx;\n}\nif (y + dy < 3)\n{\n    dy = -dy;\n}\nelse if (y + dy > 64 - 3)\n{\n    if (x > z && x < z + 20)\n    {\n        dy = -dy;\n    }\n    else\n    {\n        context.flow.set('game', 0);\n        msg.payload = [\"stop\"];\n    }\n}\nif (pressed == '0' && z < 128 - 20) \n{\n    z += 6;\n}\nelse if (z > 0) \n{\n    z -= 6;\n}\nx += dx;\ny += dy;    \ncontext.flow.set('x', x);\ncontext.flow.set('y', y);\ncontext.flow.set('dx', dx);\ncontext.flow.set('dy', dy);\ncontext.flow.set('z', z);\nmsg.payload = [x, y, z];\nreturn msg;\n\n","outputs":1,"noerr":0,"x":133,"y":1491,"wires":[["2f69734f.48286c"]]},{"id":"7fd95f20.43153","type":"debug","z":"4f4f703e.a2a9d","name":"","active":true,"console":"false","complete":"false","x":401,"y":1406,"wires":[]},{"id":"7d016083.ab7d8","type":"function","z":"4f4f703e.a2a9d","name":"pressed","func":"var v = msg.payload;\ncontext.flow.set('pressed', v);\nreturn msg;","outputs":1,"noerr":0,"x":272,"y":1186,"wires":[["7fd95f20.43153"]]},{"id":"ec949010.86b94","type":"delay","z":"4f4f703e.a2a9d","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":334,"y":1556,"wires":[["e60ff8b.ae56608"]]},{"id":"d6368de3.d8471","type":"exec","z":"4f4f703e.a2a9d","command":"sudo aplay /home/pi/start.wav","addpay":true,"append":"","useSpawn":"","timer":"","name":"start","x":364,"y":1246.5,"wires":[[],[],[]]},{"id":"92a7c74a.5a0e68","type":"exec","z":"4f4f703e.a2a9d","command":"sudo aplay /home/pi/game0.wav","addpay":true,"append":"","useSpawn":"","timer":"","name":"over","x":393,"y":1358.5,"wires":[[],[],[]]}]

```

#サンプルコード

```
import smbus
import sys

args = sys.argv
list = [int(item) for item in args[1].split(",")]
x = list[0]
y = list[1]
z = list[2]
px = [0] * 128 * 64
px[x + 128 * y] = 1
px[x + 1 + 128 * y] = 1
px[x + 2 + 128 * y] = 1
px[x + 128 + 128 * y] = 1
px[x + 129 + 128 * y] = 1
px[x + 130 + 128 * y] = 1
px[x + 256 + 128 * y] = 1
px[x + 257 + 128 * y] = 1
px[x + 258 + 128 * y] = 1
for i in range(20):
    for j in range(3):
        px[z + i + 128 * (j + 61)] = 1
oled = smbus.SMBus(1)
list0 = [0x21, 0, 127, 0x22, 0, 7]
for k in list0:
    block = []
    block.append(k)
    oled.write_i2c_block_data(0x3c, 0x0, block)
step = 1024
buf = [0] * 8 * step
of = [0, 128, 256, 384, 512, 640, 768, 896]
w = 128
j = 0
for y in range(0, 8 * step, step):
    i = y + w - 1
    while i >= y:
        buf[j] = (px[i] & 0x01) | (px[i + of[1]] & 0x01) << 1 | (px[i + of[2]] & 0x01) << 2 | (px[i + of[3]] & 0x01) << 3 | (px[i + of[4]] & 0x01) << 4 | (px[i + of[5]] & 0x01) << 5 | (px[i + of[6]] & 0x01) << 6 | (px[i + of[7]] & 0x01) << 7
        i -= 1
        j += 1
for i in range(64):
    block = []
    for j in range(16):
        b = i * 16 + j
        block.append(buf[b])
    oled.write_i2c_block_data(0x3c, 0x40, block)
print("ok")

```