Advertisement
Guest User

Python GPIO

a guest
Aug 26th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1.  
  2. import time
  3. import RPi.GPIO as GPIO
  4. import sys
  5. import math
  6.  
  7. pins = [7, 11, 13, 15, 16, 18, 22, 29, 31, 32, 33, 35, 36, 37, 38, 40] #Available Pins
  8. bits=0 #This will be the amount of LEDs
  9. pinsalias=[] #I will rename every Pin so that parameter getting easier
  10. pinsalias.append(0)
  11.  
  12. ####DEFINE###############
  13.  
  14. def setup_gpioo (int1): #Setup Every Pin i am going to need and check for available pins
  15.     i=0
  16.     global bits
  17.     global pinsalias
  18.     for x in range(len(pins)):
  19.         if (int1 == pins[x]) : i=1
  20.     if (i == 1) :
  21.         pinsalias.append(int1)
  22.         GPIO.setup(int1, GPIO.OUT)
  23.     else:
  24.         print str(int1) + " no available pin!"
  25.         GPIO.cleanup()
  26.         sys.exit()
  27.     bits=len(pinsalias)-1
  28.     return;
  29.  
  30. def set_o (int1,bool2,sleep3): #not really needed but..
  31.     GPIO.output(pinsalias[int1],bool2)
  32.     if (sleep3 != 0) : time.sleep(sleep3)
  33.     return;
  34.  
  35. def reset_o(): #set every OUTPUT to 0, i know there is GPIO.cleanup() but its kinda buggy
  36.     for xcount in range(bits):
  37.         set_o (xcount+1,0,0)
  38.     return;
  39.  
  40. def DecToBinO (evt1,dur1,int1): #Calculator + Output in Binary
  41.     check=0
  42.     dcount=0
  43.  
  44.     while check==0: #Do until Check = 1
  45.         if (evt1==0): #If paramter evt1(WaitforEvent) == 0 then its just a for loop
  46.             dcount=dcount+1
  47.             if (dcount==int1):check=1
  48.         elif (evt1==1): #If paramter evt1(WaitforEvent) == 1 then wait for input / i am going to place some buttons on my breadbord with this event
  49.             if (raw_input()=="q"):check=1
  50.             dcount=dcount+1
  51.  
  52.         ccount=dcount
  53.  
  54.         if (ccount > (math.pow(2,bits)-1)): #Check for maximum available number for my LED Bits
  55.             dcount = 0
  56.             print "No more available Bits - Reset to 0"
  57.         else:
  58.             str1=""
  59.             a=0
  60.             c=bits-1
  61.             while ccount!=0: #Convert Dec to Bin
  62.                 str1 = ("0" if ccount%2==0 else "1")+str1
  63.                 ccount/=2
  64.             while len(str1)!=bits:
  65.                 str1="0"+str1 #Fill up the empty spaces with 0's
  66.             print str1
  67.             reset_o()
  68.             while a!=bits: #Activate the Leds in the certain pattern
  69.                 set_o (a+1,int(str1[c]),0)
  70.                 c=c-1
  71.                 a=a+1
  72.             if (evt1==0):time.sleep(dur1)
  73.     return;
  74.  
  75. def pat_o (pat1,dur1,pause1): #Another Function with Patterns for the LED. Until now only one Algorithm...
  76.     if (pat1 == 1): ####WAVE_PATTERN
  77.         reset_o()
  78.         for a in range(dur1):
  79.             reset_o()
  80.             for aa in range(1,bits+1):
  81.                 if (aa==1):
  82.                     set_o(aa,1,pause1)
  83.                 else:
  84.                     set_o(aa-1,0,0)
  85.                     set_o(aa  ,1,pause1)
  86.     else:
  87.         print str(pat1) +" is no available pattern"
  88.     reset_o()
  89.     return;
  90.  
  91. #Setup the Pins and Board mode
  92.  
  93. GPIO.setmode(GPIO.BOARD)
  94.  
  95. setup_gpioo (7)
  96. setup_gpioo (11)
  97. setup_gpioo (13)
  98. setup_gpioo (15)
  99. setup_gpioo (16)
  100. setup_gpioo (18)
  101. setup_gpioo (22)
  102. setup_gpioo (29)
  103.  
  104. ####MAIN#################
  105.  
  106. reset_o()
  107. try:
  108.     while True:
  109.         pat_o(1,4,.1) #Parameter: 1 Pattern1, 4 4 times loop, .1 sleep timer
  110.         DecToBinO(0,.2,10) #Parameter 0 For Loop, .2 Sleep between different patterns, 10 times loop
  111.         DecToBinO(1,0,0) #Parameter 1 Wait for Event, Doesnt Matter, same
  112.  
  113. except KeyboardInterrupt:
  114.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement