欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python多線程并發(fā)讓兩個LED同時亮的方法

 更新時間:2019年02月18日 10:45:02   作者:JensLee  
今天小編就為大家分享一篇python多線程并發(fā)讓兩個LED同時亮的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在做畢業(yè)設(shè)計的過程中,想對多個傳感器讓他們同時并發(fā)執(zhí)行。之前想到

light_red()

light_blue()

分別在兩個shell腳本中同時運行,但是這樣太麻煩了。后來學(xué)到了Python多線程,讓程序并發(fā)執(zhí)行。

下面具體介紹步驟:

兩個led燈,一個藍燈,一個紅燈

藍燈正極接13,負極接14

紅燈正極接12,負極接14

下面是代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import RPi.GPIO as GPIO
import threading
import time
 
class led_blue(threading.Thread): #繼承父類threading.Thread
 def __init__(self, threadID, name, counter):
  threading.Thread.__init__(self)
  self.threadID = threadID
  self.name = name
  self.counter = counter
 def run(self):     #把要執(zhí)行的代碼寫到run函數(shù)里面 線程在創(chuàng)建后會直接運行run函數(shù)
  print "Starting " + self.name
  led_blue_on()
  print "Exiting " + self.name
 
class led_red (threading.Thread): #繼承父類threading.Thread
 def __init__(self, threadID, name, counter):
  threading.Thread.__init__(self)
  self.threadID = threadID
  self.name = name
  self.counter = counter
 def run(self):     #把要執(zhí)行的代碼寫到run函數(shù)里面 線程在創(chuàng)建后會直接運行run函數(shù)
  print "Starting " + self.name
  led_red_on()
  print "Exiting " + self.name
 
def led_blue_on():
 PIN_NO=13
 GPIO.setmode(GPIO.BOARD)
 GPIO.setup(PIN_NO, GPIO.OUT)
 GPIO.output(PIN_NO,GPIO.HIGH)
	
def led_red_on():
 PIN=12
 GPIO.setmode(GPIO.BOARD)
 GPIO.setup(PIN, GPIO.OUT)
 GPIO.output(PIN,GPIO.HIGH)
 
# 創(chuàng)建新線程
thread1 = led_blue(1, "light_blue_on_on", 1)
thread2 = led_red(2, "light_red_on", 2)
 
# 開啟線程
thread1.start()
thread2.start()
 
print "Exiting Main Thread"
time.sleep(20)
GPIO.cleanup()

效果圖,像素很渣:

python多線程并發(fā)讓兩個LED同時亮

以上這篇python多線程并發(fā)讓兩個LED同時亮的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論