树莓派3b+指南(二十)LCD128128显示二维码

main.py

import RPi.GPIO as GPIO
from qr import *
import time

#写命令
def transfer_command(cmd):
    GPIO.output(csl  ,GPIO.LOW)
    GPIO.output(rs   ,GPIO.LOW)
    for i in range(8):
        GPIO.output(sclk ,GPIO.LOW)
        if(cmd&0x80):
            GPIO.output(sid  ,GPIO.HIGH)
        else:
            GPIO.output(sid  ,GPIO.LOW)
        GPIO.output(sclk ,GPIO.HIGH)
        cmd <<= 1
    GPIO.output(csl  ,GPIO.HIGH)
    
#写数据
def transfer_data(data):
    GPIO.output(csl  ,GPIO.LOW)
    GPIO.output(rs   ,GPIO.HIGH)
    for i in range(8):
        GPIO.output(sclk ,GPIO.LOW)
        if (data&0x80):
            GPIO.output(sid  ,GPIO.HIGH)
        else:
            GPIO.output(sid  ,GPIO.LOW)
        GPIO.output(sclk ,GPIO.HIGH)
        data <<= 1    
    GPIO.output(csl  ,GPIO.HIGH)

#初始化lcd
def initial_lcd():
    
    GPIO.output(reset,GPIO.LOW)
    GPIO.output(reset,GPIO.HIGH)

    transfer_command(0x2c)
    transfer_command(0x2e)
    transfer_command(0x2f)
    
    transfer_command(0xae)
    transfer_command(0x38)
    transfer_command(0xb8)
    transfer_command(0xc8)
    transfer_command(0xa0)

    transfer_command(0x44)
    transfer_command(0x00)
    transfer_command(0x40)
    transfer_command(0x00)

    transfer_command(0xab)
    transfer_command(0x67)
    transfer_command(0x27)
    transfer_command(0x81)
    transfer_command(0x28)

    transfer_command(0x56)
    transfer_command(0xf3)
    transfer_command(0x04)
    transfer_command(0x93)
    transfer_command(0xaf)

#设定写数据位置(1-16,0-127)    
def lcd_address(page, column):
    GPIO.output(csl  ,GPIO.LOW)
    column = int(column)
    page = int(page)-1
    transfer_command(0xb0+page)
    transfer_command(((column>>4)&0x0f)+0x10)
    transfer_command(column&0x0f)

#清屏
def clear_screen():
    for j in range(16):
        lcd_address(j+1,0)
        for i in range(128):
            transfer_data(0x00)
            transfer_data(0x00)

#显示二维码,信息为string
def display_qrcode(string):
    qr = make_qrcode(string)
    lcd_address(1,0)
    for i in range (16*128):
        transfer_data(255-qr[i])
        transfer_data(255-qr[i])
    
if __name__ == '__main__':

    #GPIO install
    GPIO.setmode(GPIO.BOARD)
    sclk  = 37
    sid   = 35
    csl   = 33
    reset = 31
    rs    = 29

    GPIO.setup(sclk ,GPIO.OUT)
    GPIO.setup(sid  ,GPIO.OUT)
    GPIO.setup(csl  ,GPIO.OUT)
    GPIO.setup(reset,GPIO.OUT)
    GPIO.setup(rs   ,GPIO.OUT)

    #install lcd
    initial_lcd()
    clear_screen()

    #display qrcode
    display_qrcode("HELLO WORLD")  #数据不能太长,格式会乱,或尝试修改qr.py
    time.sleep(10)
    
    #clean lcd and release GPIO
    clear_screen()
    GPIO.cleanup()

qr.py

import qrcode #pip install qrcode pil
import numpy

#生成二维码
def make_qrcode(string): 
    qr = qrcode.QRCode(version=1,
                       error_correction=qrcode.constants.ERROR_CORRECT_H,
                       box_size=6,
                       border=0)

    # 设置二维码数据
    data = string
    qr.add_data(data=data)
    img = qr.make_image()

	# 将数据bool类型转成int类型,并将ndarray转成list
    img = numpy.int0(numpy.asarray(img))
    img = list(img)
    for i in range(126):
        img[i]=list(img[i])
	
	#因为生成的是126126,lcd是128128,所以需要在外圈添加一圈1以便于后面显示
	#在最上和最下增加一行
    one = []
    for i in range(126):
        one.append(1)
    img.insert(0,one)
    img.append(one)
    
	#在最左和最右增加一行
    for i in range(128):
        img[i].insert(0,1)
        img[i].append(1)

	#因为lcd显示是每8个像素为一个数(0x00-0xff),所以将每8个数加在一起求和,生成新的list。
    qr = []
    for l in range(16):
        for j in range(128):
            k=0
            for i in range(8):
                k+=2**i*img[i+l*8][j]
            qr.append(k)

    return qr

qrcode详情看这里

你可能感兴趣的:(树莓派)