1

I'm stuck :-)

I've build a simple program with pygame. I have a rectangle (pygame.draw.rect(screen, state_color1 , ( 50, 450, 150, 5)) that changes color when it's pressed. Changing the state_color1.

Is it possible to have it change when a GPIO output changes to HIGH or LOW?

I've tried stuff like this:

while true:
    if GPIO.OUTPUT(18) == 1:
        state_color1 = green
    else:
        state_color2 = red

I've also successfully hooked up another Pi and made them communicate with PIGPIO. Works great but also would like use this with PIGPIO.

I tried almost the same

while true:
    pi2.read(4)
        if pi2.read(4) == 1:
            state_color2 = green
        else:
            state_color2 = red

EDIT:

Here's my entire code:

import pygame
import time
import RPi.GPIO as GPIO
from pygame.locals import *
import glob
import sys
import os
import pigpio


pi1 = pigpio.pi()
pi2 = pigpio.pi('192.168.0.101', 8888)

os.environ["SDL_FBDEV"] = "/dev/fb1"
os.environ["SDL_MOUSEDEV"] = "/dev/input/touchscreen"
os.environ["SDL_MOUSEDRV"] = "TSLIB"

#setup gpio
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup( 5, GPIO.OUT)
GPIO.setup( 6, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)

#color    R    G    B
white = (255, 255, 255)
red   = (255,   0,   0)
green = (  0, 255,   0)
blue  = (  3, 114, 186)
black = (  0,   0,   0)
cyan  = (  0, 255, 255)

#initial state colors for indicators
state_color1 = red
state_color2 = red
state_color3 = red
state_color4 = red

def restart_program():
    python = sys.executable
    os.execl(python, python, * sys.argv)



pygame.init()

screen = pygame.display.set_mode((800,480),pygame.FULLSCREEN)

#mouse visible?
#pygame.mouse.set_visible(0)

background = os.path.join("combi-topview-v2.jpg")

background_surface = pygame.image.load(background)

screen.blit(background_surface, (0,0))
pygame.display.flip()        

font = pygame.font.Font(None, 100)

gpio_state4  = False
gpio_state5  = False
gpio_state6  = False
gpio_state12 = False
gpio_state17 = False
gpio_state18 = False

def state(gpio, level, tick):
    if level == 0:
        state_color1 = green
    elif level == 1:
        state_color1 = red


running = True


while running:

        for event in pygame.event.get():
                if event.type == pygame.QUIT:

                        GPIO.cleanup()        
                        running = 0


                if event.type == pygame.KEYDOWN:
                        if event.key == K_ESCAPE:
                                pygame.quit()
                                sys.exit()




                #if touchscreen pressed
            if event.type == pygame.MOUSEBUTTONDOWN:
            #get position
                    pos = (pygame.mouse.get_pos() [0], pygame.mouse.get_pos() [1])


                    #check
            if 50 <= pos[0] <= 200 and 100 <= pos[1] <= 450:
                            gpio_state4 = not gpio_state4
                            if gpio_state4 == True:
                                    os.system('mpg321 -q bing.mp3 &')
                                        GPIO.output(17, GPIO.LOW)
                                        GPIO.output(18, GPIO.HIGH)
                                        state_color4 = green
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.HIGH)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.HIGH)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.HIGH)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.HIGH)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(18, GPIO.HIGH)
                                        time.sleep(0.5)

                            else:
                                        os.system('mpg321 -q bong.mp3 &')
                                        GPIO.output(18, GPIO.LOW)
                                        GPIO.output(17, GPIO.HIGH)
                                        time.sleep(0.5)
                                        GPIO.output(17, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(17, GPIO.HIGH)
                                        time.sleep(0.5)
                                        GPIO.output(17, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(17, GPIO.HIGH)
                                        time.sleep(0.5)
                                        GPIO.output(17, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(17, GPIO.HIGH)
                                        time.sleep(0.5)
                                        GPIO.output(17, GPIO.LOW)
                                        time.sleep(0.5)
                                        GPIO.output(17, GPIO.HIGH)
                                        state_color4 = red
                                        time.sleep(0.5)

                    if 205 <= pos[0] <= 355 and 100 <= pos[1] <= 450:
                            gpio_state5 = not gpio_state5
                            if gpio_state5 == True:
                                        os.system('mpg321 -q bing1.mp3 &')
                                        pi2.write(17,0)
                                        pi2.write(25, 1)
                                        time.sleep(0.5)
                                        pi2.write(25, 0)
                                        time.sleep(0.5)
                                        pi2.write(25, 1)
                                        time.sleep(0.5)
                                        pi2.write(25, 0)
                                        time.sleep(0.5)
                                        pi2.write(25, 1)
                                        time.sleep(0.5)
                                        pi2.write(25, 0)
                                        time.sleep(0.5)
                                        pi2.write(25, 1)
                                        time.sleep(0.5)
                                        pi2.write(25, 0)
                                        time.sleep(0.5)
                                        pi2.write(25, 1)
                                        state_color2 = green
                                        time.sleep(0.5)

                            else:
                                        os.system('mpg321 -q bong1.mp3 &')
                                        state_color2 = red
                                        pi2.write(25, 0)
                                        pi2.write(17, 1)
                                        time.sleep(0.5)
                                        pi2.write(17, 0)
                                        time.sleep(0.5)
                                        pi2.write(17, 1)
                                        time.sleep(0.5)
                                        pi2.write(17, 0)
                                        time.sleep(0.5)
                                        pi2.write(17, 1)
                                        time.sleep(0.5)
                                        pi2.write(17, 0)
                                        time.sleep(0.5)
                                        pi2.write(17, 1)
                                        time.sleep(0.5)
                                        pi2.write(17, 0)
                                        time.sleep(0.5)
                                        pi2.write(17, 1)
                                        time.sleep(0.5)
                                        pi2.write(17, 0)
                                        time.sleep(0.5)
                                        pi2.write(17, 1)
                                        time.sleep(0.5)

                    if 125 <= pos[0] <= 180 and 5 <= pos[1] <= 60:
                            gpio_state6 = not gpio_state6
                            if gpio_state6 == True:
                                    GPIO.output(6, GPIO.HIGH)
                                    state_color3 = green
                                    time.sleep(0.2)

                            else:
                                    GPIO.output(6, GPIO.LOW)
                                    state_color3 = red
                                    time.sleep(0.2)

                    if 720 <= pos[0] <= 800 and 230 <= pos[1] <= 340:
                            gpio_state12 = not gpio_state12
                            if gpio_state12 == True:
                                        GPIO.output(18, GPIO.LOW)
                                        GPIO.output(17, GPIO.HIGH)
                                        pi2.write (25, 0)
                                        pi2.write (17, 1)
                                        state_color1 = red
                                        state_color2 = red
                                        time.sleep(0.2)
                                        restart_program()

                            else:
                                    GPIO.output(12, GPIO.LOW)
                                    state_color4 = red
                                    time.sleep(0.2)

                    if 720 <= pos[0] <= 800 and 100 <= pos[1] <= 210:
                            gpio_state17 = not gpio_state17
                            if gpio_state17 == True:
                                        os.system('mpg321 -q beng.mp3 &')
                                        GPIO.output(17, GPIO.HIGH)
                                        time.sleep(1)
                                        GPIO.output(17, GPIO.LOW)
                                        time.sleep(1)
                                        GPIO.output(17, GPIO.HIGH)
                                        time.sleep(1)
                                        GPIO.output(17, GPIO.LOW)
                                        time.sleep(1)
                                        GPIO.output(17, GPIO.HIGH)
                                        state_color5 = green
                                        time.sleep(10)


                            else:
                                        GPIO.output(17, GPIO.LOW)
                                        time.sleep(10)              


        pygame.draw.rect(screen, state_color1 , ( 50,  450, 150,  5))
        pygame.draw.rect(screen, state_color2 , (205,  450, 150,  5))
        pygame.draw.rect(screen, state_color3 , (360,  450, 150,  5))
        pygame.draw.rect(screen, state_color4 , (515,  450, 150,  5))

        #refresh screen        
        pygame.display.update()
        time.sleep(0.01)
  • Callbacks would be the prefferred way to do this with pigpio. In the callback check the level, if 1 set green, if 0 set red. Do you use that GPIO for any other purpose? If not, I can answer with an example. – joan Nov 20 '15 at 8:19
  • that GPIO only turns on/off a LED. So my idea is that if the LED is on, the rectangle is green and red when it's off. I could do that simply by changing the 'state_color' with the change of the GPIO state, but I want something to read the GPIO state and then change the 'state_color'. This way the visual confirmation is always synced to the physical LED. – jojonl Nov 20 '15 at 8:36
1

Have a look at the following.

#!/usr/bin/env python

import time

import pigpio

STOPGO=18

green=1
red=2

state_color1=None
state_color2=None

def stopgo_func(gpio, level, tick):
   global state_color1, state_color2
   if level == 0:
      state_color1 = green
   elif level == 1:
      state_color2 = red

pi = pigpio.pi() # Connect to Pi.

cb = pi.callback(STOPGO, pigpio.EITHER_EDGE, stopgo_func)

time.sleep(30)

cb.cancel()

pi.stop()

It might make more sense to actually update the rectangle in the callback rather than just record a state.

Do you mean state_color_1 and state_color_2 rather than just state_color? state_color_1 will always be green and state_color_2 will always be red.

  • Thanks! I will test this asap. State_color1 and state_color2 are two different rectangles. I could've just called them rectangle1 and rectangle2. – jojonl Nov 20 '15 at 11:43
  • unfortunately it's not working. But my explanation is crap, my apologies. I'll try to explain it as simple as possible. I have two RPi's which both run the same python pygame script. They show two buttons. One button turns on a LED (After blinking a couple of times) and with that, the button turns green. The other button turns on a LED on the other RPi, using pigpiod. When a LED is on and the button has turned green. I want the button on the other RPi to turn green also. And if I turn on the LED on the other RPi, I want the button assigned to that LED to turn green also. – jojonl Nov 20 '15 at 13:15

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.