RPi Python诅咒程序在引导时运行,没有键盘焦点

2024-04-20 10:01:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一个程序,当我的树莓派启动时,将运行,并允许我立即开始用我的键盘键入东西,并让它被程序拿起。我不想在pi启动时手动启动程序。我需要使用curses(或类似的无缓冲键盘输入库),因为我在2x16i2clcd上显示我正在键入的内容,但我也需要将我键入的所有内容记录到文本文件中。在

现在,我在启动时自动启动程序,方法是插入一行rc.本地. 这是可行的,I2C显示器正确地显示程序输出,但它不响应键盘输入,而是在一个奇怪的控制台布局上显示键盘输入(当我将饼图连接到屏幕时,目标是无头运行),当我按enter键并说-bash:“what I just typed”command not found时,该布局退出。在

我已经试过了:

  1. 在程序开始时设置一个计时器,使其在初始化curses窗口和键盘捕获之前等待pi完全启动

  2. 创建一个单独的python程序,等待pi完全启动,然后通过导入来运行主脚本

不过,这两种方法都不管用,我遇到了相同的问题,只是略有不同。在

明确地说,如果我从命令行手动运行程序,它可以完美地工作。但是当我用rc.本地. 在

我的代码:

 #!/usr/bin/python
import I2C_LCD_driver, datetime, sys
from time import *
from subprocess import call

mylcd = I2C_LCD_driver.lcd()

for x in range(30): #waits for raspberry pi to boot up
    mylcd.lcd_display_string("Booting Up: "+str(x), 1)
    sleep(1)

import curses
key = curses.initscr()
curses.cbreak()
curses.noecho()
key.keypad(1)
key.nodelay(1)

escape=0
while escape==0:
    #variable initialization

    while 1:
        k=key.getch()
        if k>-1: #runs when you hit any key. getch() returns -1 until a key is pressed
            if k==27: #exits the program when you hit Esc
                break
            elif k==269:
                # a couple other special Function key cases are here
            else:
                inpt=chr(k)
                mylcd.lcd_display_string(inpt,2,step) #writes the last character to the display
                #some more code that handles writing the text to the LCD, which works flawlessly when run manually.

    file.write("%s\r\n" % entry)
    file.close()
    mylcd.lcd_display_string("Saved           ",2)
    mylcd.lcd_display_string("F1 New F2 PwrOff",1)
    while 1:
        k=key.getch()
        if k>-1:
            if k==265: #do it again! with F1
                mylcd.lcd_clear()
                break
            elif k==266: #shut down with F2
                escape=1
                break
curses.nocbreak()
key.keypad(0)
curses.echo()
curses.endwin()
call("sudo shutdown -h now", shell=True)

我的线路/rc.本地如果这很重要,则如下所示:

^{pr2}$

后面是“退出0”行。 谢谢你能提供的任何帮助。我知道这是一个非常具体的问题,将是乏味的复制,但如果有人知道任何关于自动启动功能的知识,我将非常感谢任何提示。在


Tags: thekeyimport程序string键入iflcd
2条回答

这应该是因为你怎么称呼这个程序:

python3 journal.py &

您可能需要查看bash(或您的shell)手册页的JOB CONTROL

Only foreground processes are allowed to read from ... the terminal. Background processes which attempt to read from ... the terminal are sent a SIGTTIN ... signal by the kernel's terminal driver, which, unless caught, suspends the process.

简言之,一旦诅咒(或任何与此相关的东西)试图从stdin中读取,您的进程可能会停止(在它可能已经写入到您的显示器之后)。将其放在前台,以便使用stdin(并通过扩展键盘)。在

旁注:在您的例子中,不确定rc.local的发行版和实现细节,但是init脚本不是已经用uid/gid0运行的吗(没有通过sudo包装单个调用吗?)在

好的,实际上,我所要做的就是从~/.bashrc而不是/etc运行我的程序(在对stackexchange进行了更多研究之后,this是包含我要查找的答案的线程)/rc.本地. 这个方法非常有效,正是我想要的。在

相关问题 更多 >