Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. Skip to content
  2.  
  3. Search or jump to…
  4.  
  5. Pull requests
  6. Issues
  7. Marketplace
  8. Explore
  9. @lxzlxz Sign out
  10. 1
  11. 0 1 nextbowen/ECE4564-P3 Private
  12. Code Issues 0 Pull requests 0 Projects 0 Wiki Insights
  13. ECE4564-P3/LED_PWM.py
  14. aed0a04 5 days ago
  15. @lxzlxz lxzlxz fixed PWM_LED
  16. @lxzlxz @guancongyi
  17.  
  18. 85 lines (66 sloc) 2.66 KB
  19. import RPi.GPIO as GPIO
  20. import time
  21.  
  22.  
  23. class LED_PWM_CLASS:
  24. def __init__(self):
  25. GPIO.setwarnings(False)
  26. GPIO.setmode(GPIO.BCM)
  27. GPIO.setup(12, GPIO.OUT) # Green
  28. GPIO.setup(16, GPIO.OUT) # Red
  29. GPIO.setup(25, GPIO.OUT) # Blue
  30.  
  31. self.GreenPWM = GPIO.PWM(12, 100)
  32. self.RedPWM = GPIO.PWM(16, 100)
  33. self.BluePWM = GPIO.PWM(25, 100)
  34.  
  35. self.BlueIntensity = 0
  36. self.GreenIntensity = 0
  37. self.RedIntensity = 0
  38.  
  39. self.LED_status = 0
  40.  
  41. self.GreenPWM.start(self.GreenIntensity)
  42. self.RedPWM.start(self.RedIntensity)
  43. self.BluePWM.start(self.BlueIntensity)
  44. self.color = ''
  45.  
  46. def blink_once(self, gpio_port_number):
  47. GPIO.output(gpio_port_number, GPIO.HIGH)
  48. time.sleep(0.4)
  49. GPIO.output(gpio_port_number, GPIO.LOW)
  50. time.sleep(0.4)
  51.  
  52. def set_LED(self, new_color, new_intensity):
  53. if self.LED_status == 1:
  54. if new_color == 'red':
  55. #self.RedPWM.start(self.RedIntensity)
  56. self.RedIntensity = new_intensity
  57. self.RedPWM.ChangeDutyCycle(new_intensity)
  58. temp = "Successfully set red's intensity to " + str(self.RedIntensity)
  59. print(temp)
  60. elif new_color == 'green':
  61. #self.GreenPWM.start(self.GreenIntensity)
  62. self.GreenIntensity = new_intensity
  63. self.GreenPWM.ChangeDutyCycle(new_intensity)
  64. temp = "Successfully set green's intensity to " + str(self.GreenIntensity)
  65. print(temp)
  66. elif new_color == 'blue':
  67. #self.BluePWM.start(self.BlueIntensity)
  68. self.BlueIntensity = new_intensity
  69. self.BluePWM.ChangeDutyCycle(new_intensity)
  70. temp = "Successfully set blue's intensity to " + str(self.BlueIntensity)
  71. print(temp)
  72. elif self.LED_status == 0:
  73. pass
  74.  
  75. def turn_on_led(self):
  76. self.LED_status = 1
  77. self.RedPWM.start(self.RedIntensity)
  78. self.GreenPWM.start(self.GreenIntensity)
  79. self.BluePWM.start(self.BlueIntensity)
  80. print("LED on")
  81.  
  82.  
  83. def turn_off_led(self):
  84. #GPIO.output(12, GPIO.LOW)
  85. #GPIO.output(16, GPIO.LOW)
  86. #GPIO.output(25, GPIO.LOW)
  87. self.GreenPWM.stop()
  88. self.BluePWM.stop()
  89. self.RedPWM.stop()
  90. self.LED_status = 0
  91. print("LED off")
  92.  
  93. def get_led_info(self):
  94. return {'blue':self.BlueIntensity, 'green': self.GreenIntensity, 'red': self.RedIntensity, 'status': self.LED_status}
  95.  
  96. def parse_command(self, dic):
  97. self.set_LED(dic['color'], dic['intensity'])
  98.  
  99.  
  100.  
  101.  
  102.  
  103. © 2018 GitHub, Inc.
  104. Terms
  105. Privacy
  106. Security
  107. Status
  108. Help
  109. Contact GitHub
  110. Pricing
  111. API
  112. Training
  113. Blog
  114. About
  115. Press h to open a hovercard with more details.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement