#!/usr/bin/python
#-*- coding: utf-8 -*-
import sys
import socket
import time
from getcup import *
try:
    import RPi.GPIO as GPIO
except:
    print('引入错误')
    sys.exit()

GPIO.setmode(GPIO.BCM)

RS=2 #RS=GPIO2
RW=3
E=4
D0=14
D1=15
D2=18
D3=23
D4=24
D5=25
D6=8
D7=7

GPIO_LIST=[RS,RW,E,D7,D6,D5,D4,D3,D2,D1,D0]
D_LIST=GPIO_LIST[3:]

#获取CPU温度
def getCPUtemp():
    res  = os.popen( 'vcgencmd measure_temp' ).readline()
    return res.replace( "temp=" ,"").replace("'C","")[:4]

#获取CPU使用率,抄别人的,感觉有问题
def getCPUuse():
    return(str(os.popen("top -b -n1 | grep 'Cpu(s)' | awk '{print $2 + $4}'").readline().strip()))

#读忙
def busy():
    GPIO.setup(D7,GPIO.IN,pull_up_down=GPIO.PUD_UP)
    GPIO.output(RS,GPIO.LOW)
    GPIO.output(RW,GPIO.HIGH)
    time.sleep(0.00000005)
    while True:
        GPIO.output(E,GPIO.HIGH)
        time.sleep(0.00000025)
        b=GPIO.input(D7)
        GPIO.output(E,GPIO.LOW)
        time.sleep(0.00000025)
        if not b:
            break
    GPIO.setup(D7,GPIO.OUT,initial=GPIO.LOW)

#获取本机ip
def get_ip():
    ss=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    ss.connect(('8.8.8.8',80))
    ip=ss.getsockname()[0]
    return ip

#高脉冲
def E_HIGH() -> None:
    time.sleep(0.00000025)
    GPIO.output(E,GPIO.HIGH)
    time.sleep(0.00000025)
    GPIO.output(E,GPIO.LOW)
    time.sleep(0.0005)

#写数据
def write_data(data):
    busy()
    GPIO.output(RS,GPIO.HIGH)
    GPIO.output(RW,GPIO.LOW)
    GPIO.output(E,GPIO.LOW)
    if isinstance(data,str):
        data=ord(data)
    data=bin(data)[2:].zfill(8)
    for i in range(8):
        if data[i] == '1':
            GPIO.output(D_LIST[i],GPIO.HIGH)
        else:
            GPIO.output(D_LIST[i],GPIO.LOW)
    E_HIGH()

#写命令
def write_cmd(cmd):
    busy()
    GPIO.output(RS,GPIO.LOW)
    GPIO.output(RW,GPIO.LOW)
    GPIO.output(E,GPIO.LOW)
    cmd=bin(cmd)[2:].zfill(8)
    for i in range(8):
        if cmd[i] == '1':
            GPIO.output(D_LIST[i],GPIO.HIGH)
        else:
            GPIO.output(D_LIST[i],GPIO.LOW)
    E_HIGH()

#初始化
def init():
    GPIO.setwarnings(False)
    for i in GPIO_LIST:
        GPIO.setup(i,GPIO.OUT)
    write_cmd(0x38)
    write_cmd(0x0c)
    write_cmd(0x06)
    write_cmd(0x01)

#自定义符
def self_char(char_list):
    for char in char_list:
        busy()
        GPIO.output([RS,RW,E],(GPIO.HIGH,GPIO.LOW,GPIO.LOW))
        bchar=bin(char)[2:].zfill(8)
        for i in range(8):
            if bchar[i] == '1':
                GPIO.output(D_LIST[i],GPIO.HIGH)
            else:
                GPIO.output(D_LIST[i],GPIO.LOW)
        E_HIGH()


#写字符串
def send_string(string):
    if len(string)>16:
        string=string[:16]
    #print(string)
    for i in range(len(string)):
        write_data(string[i])


if __name__ == '__main__':
    init()
    write_cmd(0x80)
    send_string('CPU:')
    write_cmd(0x80|0x40)
    send_string('IP:{}'.format(get_ip()))
    while True:
        count=0
        while count<5:
            c=[0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00]
            write_cmd(0x40)
            self_char(c)
            write_cmd(0x80|0x04)
            cpu_temp='Temp={}'.format(getCPUtemp())
            send_string(cpu_temp)
            write_data(0x00)
            time.sleep(2)
            count+=1
        while count<10:
            write_cmd(0x80|0x04)
            cpu_use='Use={}%'.format(getCPUuse()).ljust(12)
            send_string(cpu_use)
            time.sleep(2)
            count+=1
    GPIO.cleanup()

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐