通过命令行参数向LCD发送消息使用树莓派

0 投票
1 回答
824 浏览
提问于 2025-04-17 23:38

我有一段代码(来自Adafruit网站),用来在LCD屏幕上写字符。请问有没有办法通过命令行参数来实现这个功能呢?

提前谢谢你。

代码如下:

#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime

lcd = Adafruit_CharLCD()

cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"

lcd.begin(16,1)

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

while 1:
        lcd.clear()
        ipaddr = run_cmd(cmd)
        lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))
        lcd.message('IP %s' % ( ipaddr ) )
        sleep(2)

1 个回答

0

这个例子中的相关代码是:

#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
lcd = Adafruit_CharLCD()
lcd.begin(16,1)
lcd.message(someKindofString)

...其中 someKindofString 是你的消息。所以,看起来写一些随便的东西并不难。如果你更关心的是“我怎么知道命令行参数是什么?”这个方面,那么你可以看看 sys.argv教程

撰写回答