Warmango

key_demo.py

Sep 13th, 2020
772
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding:utf-8 -*-
  2. import LCD_1in44
  3. import LCD_Config
  4.  
  5. import RPi.GPIO as GPIO
  6.  
  7. import time
  8. from PIL import Image,ImageDraw,ImageFont,ImageColor
  9.  
  10. KEY_UP_PIN     = 6
  11. KEY_DOWN_PIN   = 19
  12. KEY_LEFT_PIN   = 5
  13. KEY_RIGHT_PIN  = 26
  14. KEY_PRESS_PIN  = 13
  15. KEY1_PIN       = 21
  16. KEY2_PIN       = 20
  17. KEY3_PIN       = 16
  18.  
  19. #init GPIO
  20. GPIO.setmode(GPIO.BCM)
  21. GPIO.cleanup()
  22. GPIO.setup(KEY_UP_PIN,      GPIO.IN, pull_up_down=GPIO.PUD_UP)    # Input with pull-up
  23. GPIO.setup(KEY_DOWN_PIN,    GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Input with pull-up
  24. GPIO.setup(KEY_LEFT_PIN,    GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Input with pull-up
  25. GPIO.setup(KEY_RIGHT_PIN,   GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
  26. GPIO.setup(KEY_PRESS_PIN,   GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
  27. GPIO.setup(KEY1_PIN,        GPIO.IN, pull_up_down=GPIO.PUD_UP)      # Input with pull-up
  28. GPIO.setup(KEY2_PIN,        GPIO.IN, pull_up_down=GPIO.PUD_UP)      # Input with pull-up
  29. GPIO.setup(KEY3_PIN,        GPIO.IN, pull_up_down=GPIO.PUD_UP)      # Input with pull-up
  30.  
  31. # 240x240 display with hardware SPI:
  32. disp = LCD_1in44.LCD()
  33. Lcd_ScanDir = LCD_1in44.SCAN_DIR_DFT  #SCAN_DIR_DFT = D2U_L2R
  34. disp.LCD_Init(Lcd_ScanDir)
  35. disp.LCD_Clear()
  36.  
  37. # Create blank image for drawing.
  38. # Make sure to create image with mode '1' for 1-bit color.
  39. width = 128
  40. height = 128
  41. image = Image.new('RGB', (width, height))
  42.  
  43. # Get drawing object to draw on image.
  44. draw = ImageDraw.Draw(image)
  45.  
  46. # Draw a black filled box to clear the image.
  47. draw.rectangle((0,0,width,height), outline=0, fill=0)
  48. disp.LCD_ShowImage(image,0,0)
  49.  
  50. # try:
  51. while 1:
  52.     # with canvas(device) as draw:
  53.     if GPIO.input(KEY_UP_PIN) == 0: # button is released      
  54.         draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=0xff00)  #Up        
  55.         print "Up"        
  56.     else: # button is pressed:
  57.         draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=0)  #Up filled
  58.        
  59.     if GPIO.input(KEY_LEFT_PIN) == 0: # button is released
  60.         draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=0xff00)  #left
  61.         print "left"        
  62.     else: # button is pressed:      
  63.         draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=0)  #left filled
  64.        
  65.     if GPIO.input(KEY_RIGHT_PIN) == 0: # button is released
  66.         draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=0xff00) #right
  67.         print "right"
  68.     else: # button is pressed:
  69.         draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=0) #right filled      
  70.        
  71.     if GPIO.input(KEY_DOWN_PIN) == 0: # button is released
  72.         draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=0xff00) #down
  73.         print "down"
  74.     else: # button is pressed:
  75.         draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=0) #down filled
  76.        
  77.     if GPIO.input(KEY_PRESS_PIN) == 0: # button is released
  78.         draw.rectangle((20, 22,40,40), outline=255, fill=0xff00) #center
  79.         print "center"
  80.     else: # button is pressed:
  81.         draw.rectangle((20, 22,40,40), outline=255, fill=0) #center filled
  82.        
  83.     if GPIO.input(KEY1_PIN) == 0: # button is released
  84.         draw.ellipse((70,0,90,20), outline=255, fill=0xff00) #A button
  85.         print "KEY1"
  86.     else: # button is pressed:
  87.         draw.ellipse((70,0,90,20), outline=255, fill=0) #A button filled
  88.        
  89.     if GPIO.input(KEY2_PIN) == 0: # button is released
  90.         draw.ellipse((100,20,120,40), outline=255, fill=0xff00) #B button]
  91.         print "KEY2"
  92.     else: # button is pressed:
  93.         draw.ellipse((100,20,120,40), outline=255, fill=0) #B button filled
  94.        
  95.     if GPIO.input(KEY3_PIN) == 0: # button is released
  96.         draw.ellipse((70,40,90,60), outline=255, fill=0xff00) #A button
  97.         print "KEY3"
  98.     else: # button is pressed:
  99.         draw.ellipse((70,40,90,60), outline=255, fill=0) #A button filled
  100.     disp.LCD_ShowImage(image,0,0)
  101. # except:
  102.     # print("except")
  103.     # GPIO.cleanup()
  104.  
RAW Paste Data