0

I am using python to turn leds on and off in a little project (raspbian) but I don't know why sometimes the leds turn on and sometimes they don't.

You can find the code below, could you please tell me if I use the right structure?

Thanks.

class Leds(object):
    'Class used to define and turn on or off the different leds'
    configFile = '/home/pi/compteur/leds.cfg'
    lightsFile = '/home/pi/compteur/lights.cfg'

    def __init__(self, verbose = False):
        #Turning Warnings off for the control of the GPIO pins
        GPIO.setwarnings(False)

        #Set mode of GPIOs to numbering of the board
        GPIO.setmode(GPIO.BCM)

        #Initialising the load config
        config = SafeConfigParser()
        config.read(Leds.configFile)
        configlights = SafeConfigParser()
        configlights.read(Leds.lightsFile)

        #Getting the list of leds with their BCM number
        pinstext = config.get('leds','pin_numbers')
        self.pins = pinstext.split(',')
        self.numberOf = len(self.pins)

        self.lightstext = configlights.get('camera','lights')
        self.lightstext.replace(' ','')
        if self.lightstext == '':
            print 'empty light'
            for i in xrange(1, self.numberOf + 1):
                self.lightstext = self.lightstext + str(i)
                if i < self.numberOf:
                    self.lightstext = self.lightstext + ','
        print 'using instead: %s' % self.lightstext
        self.lights = self.lightstext.split(',')
        self.numberOfLights = len(self.lights)

        #Setting all pins to the right mode
        for pin in self.pins:
            GPIO.setup(int(pin), GPIO.OUT, initial=GPIO.LOW)

        #Setting Verbose
        self.verbose = verbose

        if self.verbose:
            print 'Object constructed'

    def SetPin(self, lednum, state):
        if self.verbose:
            print 'Turning led %s to state %s' % (lednum,state)
        lednum = int(lednum) - 1
        #Turning the pin on
        GPIO.output(int(self.pins[lednum]), int(state))

    def CleanGPIO(self):
        GPIO.cleanup()

    def __del__(self):
        if self.verbose:
            class_name = self.__class__.__name__
            print class_name, 'object destroyed'

The file leds.cfg

[leds]
pin_numbers = 13,19,26,16,20,21

The file lights.cfg

[camera]
lights = 

So, I created a class in order to use it in another class. It is then pretty easy as I just need to use the SetPin, give a pin number which is the led number, 1, 2, 3, .. without remembering the BCM number and I give the state.

I am just wondering if I do the right initialisation.

  • Is there any output besides leds switching on and off? – Piotr Kamoda Jul 8 '15 at 8:28
  • I can't see the point of the class. I find it impossible to usefully comment as we don't know how the class is instantiated and we don't know the format/contents of the confuguration files. – joan Jul 8 '15 at 8:45
  • Yes there is a camera that is being used so that the led turns on the camera takes a picture and then the led turns off. – John Jul 8 '15 at 8:54
  • Any console output? – Piotr Kamoda Jul 8 '15 at 9:15

Your Answer

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

Browse other questions tagged or ask your own question.