karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

RPi.GPIO not working with curses?

Sun Sep 30, 2012 5:22 pm

Hello,
I'm trying to write a python program that responds to a switch attached to GPIO pin 11, using the push button circuit from Mag Pi issue 2 http://www.themagpi.com/. Using the code in the magazine the button pressing works as expected, however when I try to do the same using python curses, nothing is displayed on screen, it doesn't seem to be reading the GPIO state.

Can anyone see what I'm doing wrong? I am using Python 2.7

Code: Select all

#! /usr/bin/python
# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import curses
GPIO.setup(11, GPIO.IN)  

def swichtest():
    count = 0
    screen = curses.initscr()
    curses.noecho()
    curses.curs_set(0)
    screen.keypad(1)
    top = 2
    left = 0
    screen.addstr("Press a Little Button (or press 'q' to quit)")
    while True:
        mybutton = GPIO.input(11)
        if mybutton == False:
            count += 1
            screen.addstr(top, left, "Button Pressed")
        else:
            screen.addstr(top, left, "              ")

        event = screen.getch()
        if event == ord("q"): 
            break

    curses.endwin()
    return count

if __name__ == "__main__":
    count = swichtest()
    print count, "fin."
Thanks
Karl.

User avatar
Grumpy Mike
Posts: 917
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: RPi.GPIO not working with curses?

Sun Sep 30, 2012 7:20 pm

Are you sure what is being referred to as pin 11 is the same in both examples. It could be that one uses physical pin numbers and the other uses GPIO numbers.

Physical pin 11 has on it GPIO 17
Where as:-
GPIO 11 is on physical pin 23

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: RPi.GPIO not working with curses?

Sun Sep 30, 2012 7:50 pm

Grumpy Mike wrote:Are you sure what is being referred to as pin 11 is the same in both examples. It could be that one uses physical pin numbers and the other uses GPIO numbers.

Physical pin 11 has on it GPIO 17
Where as:-
GPIO 11 is on physical pin 23
No, the hardware is working, and it's connected to the correct GPIO pin. My non-curses test code works as expected:

Code: Select all

import RPi.GPIO as GPIO
from time import sleep

GPIO.setup(11, GPIO.IN) 

def dialtest():

    count = 0
    while True:
        mybutton = GPIO.input(11)
        if mybutton == False:
            count = count +1
            print "count ", count
            sleep(.2)
    return

if __name__ == "__main__":
    dialtest()
Karl.

User avatar
mister_wavey
Posts: 98
Joined: Sun Sep 02, 2012 8:23 am
Location: Abergavenny, Wales, UK
Contact: Website

Re: RPi.GPIO not working with curses?

Sun Sep 30, 2012 8:13 pm

is the first one still running when you test the curses one?

also, set the pin reference mode first. Later versions won't even run without it:

[email protected] ~/dev/curses $ sudo python cursestest.py
Traceback (most recent call last):
File "cursestest.py", line 6, in <module>
GPIO.setup(11, GPIO.IN)
RPi.GPIO.ModeNotSetException: Please set mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: RPi.GPIO not working with curses?

Sun Sep 30, 2012 8:50 pm

mister_wavey wrote:is the first one still running when you test the curses one?

also, set the pin reference mode first. Later versions won't even run without it:

[email protected] ~/dev/curses $ sudo python cursestest.py
Traceback (most recent call last):
File "cursestest.py", line 6, in <module>
GPIO.setup(11, GPIO.IN)
RPi.GPIO.ModeNotSetException: Please set mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Looks like I had an old version (v0.2) of RPi.GPIO so I didn't get that error. However upgrading to v0.3a from the apt repository and adding GPIO.setmode(GPIO.BOARD) before the pin designation has made no difference. The line 'Button Pressed' is not displayed when the switch is pressed but changing the non-curses version in a simmilar fashion works how I would expect.

Karl.

smiffydotcom
Posts: 5
Joined: Wed Jan 30, 2013 3:18 pm

Re: RPi.GPIO not working with curses?

Sat Feb 23, 2013 11:21 am

Considering the date of the original post, its probably sorted...

I'm having "similar issues" - but looking at your code example I cant see the line

refresh()

which I understand you have to include after writing to the curses screen...
Thanks for your code example though, its helped me :)

Return to “Python”