3

I have a python script that uses a library to talk to GPIO in a constant loop. How can I detect keyboard input inside the loop and choose to exit the loop?

while True:
        for item in pics[1:]:
                matrix.SetImage(item.im.id, 0, 0)
                time.sleep(.1)

matrix.Clear()
3

I suggest you use the curses module. It will let you check for keyboard input.

Here is an example. It uses my pigpio library to read the gpios but the gpio library you use will be irrelevant to curses.

#!/usr/bin/env python

# gpio_status.py
# 2015-03-04
# Public Domain

import time
import curses
import atexit

import pigpio 

GPIOS=32

MODES=["INPUT", "OUTPUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"]

def cleanup():
   curses.nocbreak()
   curses.echo()
   curses.endwin()
   pi.stop()

pi = pigpio.pi()

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()

atexit.register(cleanup)

cb = []

for g in range(GPIOS):
   cb.append(pi.callback(g, pigpio.EITHER_EDGE))

stdscr.nodelay(1)

stdscr.addstr(0, 23, "Status of gpios 0-31", curses.A_REVERSE)

while True:

   for g in range(GPIOS):
      tally = cb[g].tally()
      mode = pi.get_mode(g)

      col = (g / 11) * 25
      row = (g % 11) + 2

      stdscr.addstr(row, col, "{:2}".format(g), curses.A_BOLD)

      stdscr.addstr(
         "={} {:>6}: {:<10}".format(pi.read(g), MODES[mode], tally))

   stdscr.refresh()

   time.sleep(0.1)

   c = stdscr.getch()

   if c != curses.ERR:
      break
  • I am not clear on how I would implement this. I updated my post to add more detail. I want the loop to end and the call to matrix.Clear(), and the script to end on input. Would I do this in def cleanup()? – Roger Mar 4 '15 at 14:41
  • The code is just an example of using the Python curses module. curses is used to draw to a terminal window and handle keyboard entry. You are only interested in keyboard entry so the getch method is of particular interest. If you search for Python curses and keyboard you should be able to find other examples/tutorials on-line. – joan Mar 4 '15 at 15:04
1

Can be done with ttyas well, like this:

import tty, sys

tty.setraw(sys.stdin.fileno())
while 1:
    ch = sys.stdin.read(1)
    if ch == 'a':
        print "Wohoo"

This will garble your tty, though, so if you need it for anything later, you need to restore it.

0

everything i try to post here looks so garbled, it's unreadable . i stripped down the code for the pygame lib and made some discoveries about it

import  pygame, sys
from pygame.locals import*
print("esc key to exit the game window must be focused for this to work")
while True:
    DISPLAYSURF = pygame.display.set_mode((50,10)) 
    while True:                          
        for event in pygame.event.get():
            if event.type == QUIT:       
                pygame.quit()            
                sys.exit()               
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()        
                    sys.exit()           
                print(event.key, end=" ")
#               print(event.key)         

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.