4 Replies Latest reply on Feb 16, 2016 11:54 AM by BrechtW

    Python Blink Example Intel Galileo Gen2

    BrechtW

      I tried making a Python Blink Example.

       

      # -*- coding: utf-8 -*-
      import os
      import os.path
      import time
      
      # --Special thanks to--
      # http://www.emutexlabs.com/component/content/article?id=203:getting-started-with-intel-galileo-gen-2
      # -->4.3 Pin configuration options for Galileo Gen 2
      # https://communities.intel.com/message/249663
      
      ## Blink example for Intel Galileo Gen 2
      # This example wil blink led 13 (=GPIO 7) on the Intel Galileo Gen 2
      # Pin 13 is located near the full size USB port
      
      def init():
          pinsetup('46','out','0')
          pinsetup('31','out','1')
          pinsetup('30','out','0')
      
      def pinsetup(pinexp,pindir,pinval):
          strexport=("echo \"%s\" > /sys/class/gpio/export" % pinexp)
          strdir=("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir,pinexp))
          strval=("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval,pinexp))
          # os.system("echo \"46\" > D:\TEST.TXT") ;
          os.system(strexport)    # write a "30" to the export file, which reserves gpio30 for use
          os.system(strdir)   # sets gpio30 as an output
          os.system(strval)       # sets gpio30 low, enable dir_out
          print strexport
          print strdir
          print strval
      
      def pinHLDir(pinexp,pindir,pinval):
          strdir=("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir,pinexp))
          strval=("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval,pinexp))
          os.system(strdir)   # sets gpio30 as an output
          os.system(strval)       # sets gpio30 low, enable dir_out
      
      def pinHL(pinexp,pindir,pinval):
          strval=("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval,pinexp))
          os.system(strval)       # sets gpio30 low, enable dir_out
      
      init()
      
      while(1):
          pinHL('7','out','1')
          time.sleep(2)
          pinHL('7','out','0')
          time.sleep(2)
      
      

       

      I get this output.

      /bin/sh: line 0: echo: write error: Device or resource busy

      echo "46" > /sys/class/gpio/export

      echo "out" > /sys/class/gpio/gpio46/direction

      echo "0" > /sys/class/gpio/gpio46/value

      /bin/sh: line 0: echo: write error: Device or resource busy

      echo "31" > /sys/class/gpio/export

      echo "out" > /sys/class/gpio/gpio31/direction

      echo "1" > /sys/class/gpio/gpio31/value

      /bin/sh: line 0: echo: write error: Device or resource busy

      echo "30" > /sys/class/gpio/export

      echo "out" > /sys/class/gpio/gpio30/direction

      echo "0" > /sys/class/gpio/gpio30/value

       

      How do i get rid of the error?

        • 1. Re: Python Blink Example Intel Galileo
          PabloM_Intel

          Hi BrechtW,

           

          Given the message, it is possible that the GPIO pin that you’re exporting is already exported. Did you make sure that this is not the case?

           

          Regards,

          PabloM_Intel

          • 2. Re: Python Blink Example Intel Galileo
            BrechtW

            How can i check if the pin is already exported in python code, so I can avoid this error?

            • 3. Re: Python Blink Example Intel Galileo
              PabloM_Intel

              Hi BrechtW,

               

              It should be possible to write a program that reads within the file and checks if the pin has already been exported. However, you can first check it using Linux, if you determine that this is actually true and that the pin is already set it won’t be necessary to write a program to check this. Your code should run fine even with this message (“Device or resource busy”).

               

              Regards,

              PabloM_Intel

              • 4. Re: Python Blink Example Intel Galileo
                BrechtW

                It works with the error, but now I check if the directory already exists.

                 

                # -*- coding: utf-8 -*-
                import os
                import os.path
                import time
                
                
                ## Blink example for Intel Galileo Gen 2
                # This example wil blink led 13 (=GPIO 7) on the Intel Galileo Gen 2
                # Pin 13 is located near the full size USB port
                
                
                # --Special thanks to--
                # http://www.emutexlabs.com/component/content/article?id=203:getting-started-with-intel-galileo-gen-2
                # -->4.3 Pin configuration options for Galileo Gen 2
                # https://communities.intel.com/message/249663
                # http://wiki.ros.org/IntelGalileo/IntelGalileoGPIO
                
                
                def init():
                    # Use GPIO table from right to left
                    # pinSetup('46','out','0') # Mux H/L: L
                    # pinSetup('31','out','0') # Pull up/down: pull-down
                    # pinSetup('30','out','0') # Direction out/in: output
                    # pinDir('7','out')   # GPIO pin
                    pinExport('46')
                    pinExport('30')
                    pinExport('31')
                    pinExport('7')
                    pinHL('30', '0')
                    pinHL('31', '0')
                    pinDir('7', 'out')
                    # if this doesn't work comment these out and use the code out at the beginning of init() in comments
                
                
                def pinSetup(pinexp, pindir, pinval):
                    if os.path.exists("/sys/class/gpio/gpio%s" % pinexp) == False:
                        strexport = ("echo \"%s\" > /sys/class/gpio/export" % pinexp)
                        os.system(strexport)  # write a GPIO number to the export file, which reserves it for use
                        print strexport
                    else:
                        print "INFO: GPIO %s already exists, skipping export" % pinexp
                    strdir = ("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir, pinexp))
                    strval = ("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval, pinexp))
                    os.system(strdir)  # sets GPIO as an output/input('out'/'in')
                    os.system(strval)  # sets GPIO high/low('1'/'0'), enable dir_out
                    print strdir
                    print strval
                
                
                def pinExport(pinexp):
                    if os.path.exists("/sys/class/gpio/gpio%s" % pinexp) == False:
                        strexport = ("echo \"%s\" > /sys/class/gpio/export" % pinexp)
                        os.system(strexport)  # write a GPIO number to the export file, which reserves it for use
                        print strexport
                    else:
                        print "INFO: GPIO %s already exists, skipping export" % pinexp
                
                
                def pinUnExport(pinexp):
                    strunexport = ("echo \"%s\" > /sys/class/gpio/unexport" % pinexp)
                    os.system(strunexport)  # write a GPIO number to the unexport file, the pin can now be used by something/someone else
                    print strunexport
                
                
                def pinHLDir(pinexp, pindir, pinval):
                    strdir = ("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir, pinexp))
                    strval = ("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval, pinexp))
                    # first %s is replaced with first parameter, second %s is replaced with second parameter
                    # \" = "
                    os.system(strdir)  # sets GPIO as an output/input('out'/'in')
                    os.system(strval)  # sets GPIO high/low('1'/'0'), enable dir_out
                    # print strdir
                    # print strval
                
                
                def pinDir(pinexp, pindir):
                    strdir = ("echo \"%s\" > /sys/class/gpio/gpio%s/direction" % (pindir, pinexp))
                    os.system(strdir)  # sets GPIO as an output/input('out'/'in')
                    # print strdir
                
                
                def pinHL(pinexp, pinval):
                    strval = ("echo \"%s\" > /sys/class/gpio/gpio%s/value" % (pinval, pinexp))
                    os.system(strval)  # sets GPIO high/low('1'/'0'), enable dir_out
                    print strval
                
                
                init()
                
                
                while (1):
                    pinHL('7', '1')
                    time.sleep(2)
                    pinHL('7', '0')
                    time.sleep(2)
                
                
                
                
                1 of 1 people found this helpful