Skip to content

Instantly share code, notes, and snippets.

@Shinichi-Ohki
Last active January 3, 2018 23:02
Show Gist options
  • Save Shinichi-Ohki/b18480244bb09fd65f86dd0c60710b0a to your computer and use it in GitHub Desktop.
Save Shinichi-Ohki/b18480244bb09fd65f86dd0c60710b0a to your computer and use it in GitHub Desktop.
Nekonium QRコードをプリンタシールドから出力します。
# -*- coding: utf-8 -*-
"""
このスクリプトはNekonium walletのQRコードをキャプチャして認識し、QRコードを生成してAS-289R2 プリンタシールドで印刷します
"""
import os
import sys
import subprocess
import json
import time
import PIL
import qrcode
import serial
import wiringpi as wp
argvs = sys.argv
QR = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=6,
border=4,
)
QR.add_data(argvs[1])
QR.make(fit=True)
image = QR.make_image(fill_color="white", back_color="black")
image.save('/work/qr.bmp')
check = subprocess.call(['convert', '/work/qr.bmp', '-colorspace', 'Gray',
'-geometry', '225', '-threshold', '35000', '-colors', '2', '/work/qrcode.bmp'])
check = subprocess.call(['composite', '-gravity', 'center', '-compose',
'over', '/work/qrcode.bmp', 'nekonium_mono.bmp', '/work/neko.bmp'])
check = subprocess.call(
['convert', '-append', '/work/neko.bmp', 'nekonium_logo.bmp', '/work/nekoqr.bmp'])
check = subprocess.call(['convert', '/work/nekoqr.bmp', '-colorspace', 'Gray',
'-geometry', '384', '-threshold', '35000', '-colors', '2', '/work/nekoqrcode.bmp'])
# AS-289R2 Initialize
ser = serial.Serial("/dev/ttyS0", baudrate=38400, timeout=2)
# CMD DC2 F
ser.write(bytes([0x12]))
ser.write(bytes([0x46]))
ser.write(bytes([0x36]))
# Get BMP(384px 384px 1bpp)
file = open('/work/nekoqrcode.bmp', 'rb')
file.seek(10)
offset = ord(file.read(1))
file.seek(22)
height = ord(file.read(1))
height += ord(file.read(1)) * 256
print("Offset:", offset)
print("Height:", height)
# Output CMD
ser.write(b"Wallet address:")
ser.write(argvs[1].encode('utf-8'))
ser.write(b"\r")
ser.write(bytes([0x1C]))
ser.write(bytes([0x2A]))
ser.write(bytes([0x65]))
n1 = height / 256
n2 = height % 256
print(n1, n2)
ser.write(bytes([int(n1)]))
ser.write(bytes([int(n2)]))
for num in range(height):
line = height * 48 + offset - ((num + 1) * 48)
file.seek(line)
for subnum in range(48):
x = ord(file.read(1)) ^ 0xFF
ser.write(bytes([x]))
for feed in range(6):
ser.write(bytes([0x0D]))
file.close()
ser.write(b"Merry Christmas!!\r")
ser.write(b"2017/12/25\r")
ser.write(b"by Candy Mechatronics\r")
for feed in range(20):
ser.write(bytes([0x0D]))

用意する物

  • Raspberry Pi 3(他のでも動くと思う)
  • AS-289R2 プリンタシールド
  • 配線用ジャンパケーブル少々
  • Python3パッケージ
    • qrcode (QRコード生成)
    • serial (シリアル通信)
    • WiringPi (GPIO制御など)
  • imagemagick

nekonium_logo.bmp と nekonium_mono.bmp はneko_qr_printer.py と同じディレクトリに置いておいてください。

使い方

シェルから起動します。引数としてウォレットアドレスが必要です。

sudo python3 neko_qr_printer.py 0xf25d84ae5430ad5107801af46f2408dd6b0b43b0

注意点

  • プリンタシールドは38400bps、3.3Vに設定しといてください。前者の設定にははんだ付けが必要です。
  • /work/に印刷用bmpファイルを保存します。microSDカードへの書き込みを減らしたいのでtmpfsを使うとよいでしょう。/etc/fstab を編集して/work/としてtmpfsを作っておいてください。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment