[PYTHON] raspberry pi 1 model b, node-red part 17

Overview

I played a game with node-red of raspberry pi 1 model b. The mechanism is First, prototyping with jsdo. Input is one switch, display is OLED. I'm using a scoped variable, content.flow, in the flow. The exec module gives python arguments.

Development environment

raspberry pi 1 model b raspbian 2016_09_23 jessie lite

Photo

MVC-013S.JPG

Video

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

Prototyping

http://jsdo.it/ohisama1/Qt7D

Sample code

pong.JPG

[{"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":[[],[],[]]}]

Sample code

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")

Recommended Posts

raspberry pi 1 model b, node-red part 17
raspberry pi 1 model b, python
USB boot on Raspberry Pi 4 Model B
Raspberry Pi 4B initial setting
USB boot with Raspberry Pi 4 Model B (3) LVM edition
Port FreeRTOS to Raspberry Pi 4B
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
"Honwaka Notification Lamp" on Raspberry Pi Part 2
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Programming normally with Node-RED programming on Raspberry Pi 3
"Honwaka Notification Lamp" on Raspberry Pi Part 3
Build OpenCV-Python environment on Raspberry Pi B +
Infer Custom Vision model with Raspberry Pi
Matrix multiplication on Raspberry Pi GPU (Part 2)
Raspberry Pi backup
Easy Raspberry Pi GUI App Development Beginner Part 1
Why detectMultiScale () is slow on Raspberry Pi B +
Easy Raspberry Pi GUI App Development Beginner Part 2
Introduced Ceph on Kubernetes on Raspberry Pi 4B (ARM64)
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
I tried running Flask on Raspberry Pi 3 Model B + using Nginx and uWSGI
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
What is Raspberry Pi?
GPGPU with Raspberry Pi
Try to visualize the room with Raspberry Pi, part 1
pigpio on Raspberry pi
Raspberry Pi video camera
Procedure for installing Xenomai on RaspberryPi 3 model B + Part 1
Raspberry Pi Bad Knowledge
Let's do Raspberry Pi?
DigitalSignage with Raspberry Pi
Raspberry Pi 4 setup memo
Cython on Raspberry Pi
Raspberry Pi system monitoring
Play with the Raspberry Pi Zero WH camera module Part 1
[Note] Installing vmware ESXi on Arm Fling on Raspberry Pi 4B
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
Introduced python3-OpenCV3 to Raspberry Pi
Indoor monitoring using Raspberry Pi
Mutter plants with Raspberry Pi
Install Raspberry Pi OS (Raspbian)
I talked to Raspberry Pi
Introducing PyMySQL to raspberry pi3
Raspberry Pi + Python + OpenGL memo
Raspbian initial settings (Raspberry Pi 4)
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Programd automatic start at startup with Raspberry Pi 3B + systemd Summary
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
Let's make an IoT shirt with Lambda, Kinesis, Raspberry Pi [Part 1]
Building a distributed environment with the Raspberry PI series (Part 1: Summary of availability of diskless clients by model)