NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Pygame (no rect object)

June 19, 2011
by missle3944
missle3944's Avatar

Hi All,

I'm trying to change the color of a rectangle by turning a pot on my MCU and then a python script just reads the temp value and inserts it into a rectangle object.

But when I substitute data as a variable in for Red it says that it is not a rect style object. But when I have x = 1 and x is red then it works fine. I know this is a very specific question but I've looked high and low for it. Here is my python code

import serial
import sys
import os
import pygame, sys
from pygame.locals import *

serial = serial.Serial("COM3", 115200)
    #uses the serial port
data = serial.read()

#center
pygame.init()

#create the screen
screen = pygame.display.set_mode((640, 480),0,32)

screen.lock()
pygame.draw.rect(screen, (data,7,0), Rect((200,40),(200,170), 6))
screen.unlock()

pygame.display.update()

-missle3944

June 20, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi missle3944,

Couple of things seem strange to me in your program. First there is no main while loop, so your program is just going to run through once and exit, which is probably not what you want. I'm not a fan of your pygame.locals import *, its a little clearer too use pygame.Rect when you need to instantiate a Rect object.

The documentation has the constructor for Rects taking either 4 or two arguments (left, top, width, height), or ((left, top), (width, height)). You have an extra 6 there at the end which I think is causing your current error.

I also made the background white so I could see what is going on. The code below displays a red rectangle.

import serial
import sys
import os
import pygame, sys
#from pygame.locals import *

#serial = serial.Serial("COM3", 115200)
#uses the serial port
#data = serial.read()
data = 1

#center
pygame.init()

#create the screen
screen = pygame.display.set_mode((640, 480),0,32)
white = (255,255,255)
screen.fill(white)

while True:
  #TODO: read data from serial port, turn into interger from 0-255
  data=255
  pygame.draw.rect(screen, (data,0,0), pygame.Rect((200,40),(200,170)))
  pygame.display.update()
  pygame.time.wait(10)
June 20, 2011
by missle3944
missle3944's Avatar

Hi Hevans,

How would I do this? Should I do the conversion in python or in the MCU code? should I use a linear equation similar to the one used in the meat temp sensor project? Why does python not let me use a number from the serial port? is it becuase it thinks it is a string?

-missle3944

June 22, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi missle3944,

My guess is that its not working when you do the read from the serial port (other than the fact that you were not doing it in a while loop) is that using the read() method of the file like pySerial interface reads exactly one byte from the serial port. This will work ok if you are sending exactly one byte over the serial port from your NerdKit. You will still have to turn it into an integer, then feed it to the data parameter. It might be useful to you to print the the value you get from the serial port to the command line from python, just so you get a better idea of what you are sending.

Humberto

June 22, 2011
by missle3944
missle3944's Avatar

Hi Humberto, I used the code that you sugessted and i just modified it alittle bit. But would it help if i only sent one byte at a time to python buy setting a delay on my nerdkit? Would it help if I placed a timeout on the serial side? I saw a guys code that he is doing the same thing I am but with an arduino but in python he used uspp and he used the read byte command instead of just read(). Is one integer one byte? I am very new to this binary and bits and byte thing.

Heres my code if you have any suggestions:

import serial
import sys
import os
import pygame, sys
import math
#from pygame.locals import *

serial = serial.Serial("COM3", 115200)
#uses the serial port
data = serial.read()

#center
pygame.init()

#create the screen
screen = pygame.display.set_mode((640, 480),0,32)
white = (255,255,255)
screen.fill(white)

while True:
  #TODO: read data from serial port, turn into interger from 0-255
  pygame.draw.rect(screen, (data,0,0), pygame.Rect((200,40),(200,170)))
  pygame.display.update()
  pygame.time.wait(10)

-Dan

Post a Reply

Please log in to post a reply.

Did you know that a piezoelectric buzzer can be used in reverse as a microphone? Learn more...