Page 1 of 1

help in classes

Posted: Wed Mar 18, 2015 7:04 am
by davidwong750
hi everyone. i need help as i am currently doing a project. this is my script below and i need to insert integer into class nut i not sure how to do. pls help me. this is about the led circuit. thx

code:
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)

class LED():
def blink(pin):
GPIO.output(pin, GPIO.HIGH)
time.sleep(1)
GPIO.output(pin, GPIO.LOW)
time.sleep(1)
return

def on(pin):
GPIO.output(pin, GPIO.HIGH)


def off(pin):
GPIO.output(pin, GPIO.LOW)


for i in range(0, 10):
blink(7)

off(5)
on(5)
time.sleep(10)



class cleanup:
pass

Re: help in classes

Posted: Wed Mar 18, 2015 1:45 pm
by RST8
Hi David,
some questions for you :
what is it exactly you need the integer to do, I'm not sure what you mean by passing an integer into the class.
What would the class use the integer for?
Does it need to be accessible by all of the methods in your class, or just one of them?
Will it need to be changed once you have set it?

There are different approaches depending on what you want.
As an example, perhaps you want to be able to change the pin that you want to blink. I've taken out the GPIO lines for this example.
The __init__ section of the class lets you pass in things when you create it that you can then make available to every method in your class.
The changePin() method lets change the pin you originally specified.
Of course, you might not want that, but it's there to show one way of doing once the class has been created.

Code: Select all

class LED():
    def __init__(self, pin):
        self.pin = pin

    def blink(self):
        print "Blinking pin " + str(self.pin)
        return
    def changePin(self, newpin):
        self.pin = newpin

myLed = LED(5)
myLed.blink()
myLed.changePin(6)
myLed.blink()
Hope that helps.

Joe

Re: help in classes

Posted: Thu Mar 19, 2015 1:12 am
by davidwong750
Thank Joe for the script. It do help but I do still need help.in some way because python is new to me...... basically my project is from raspberry pi connect to ultimate GPS breakout version 3. And need to do some threading and also some like every hours the GPS will lock and send into text file... So I need as much help as I can from you all..... Thx everyone. And btw when you send the script, do state at the comment so that I can understand....... I still need help on the threading and the locking GPS & send text file( every hour)