M♪o 之學習筆記本《丑》控制︰【青木東】伺候急報

派生碼訊

丑 牛

曹植‧《白馬篇

白馬飾金羈,連翩西北馳。
借問誰家子,幽並游俠兒。
少小去鄉邑,颺聲沙漠垂。
宿昔秉良弓,楛矢何參差。
控弦破左的,右發摧月支。
仰手接飛猱,俯身散馬蹄。
狡捷過猴猿,勇剽若豹螭。
邊城多警急,虜騎數遷移。
羽檄從北來,厲馬登高堤。
長驅蹈匈奴,左顧凌鮮卑。
棄身鋒刃端,性命安可懷?
父母且不顧,何言子與妻!
名編壯士籍,不得中顧私。
捐軀赴國難,視死忽如歸。

青木東伺 伺候 是誰?因何事??只緣時機不分明!人可負人誰逼迫?人定勝天豈該作!急報之始,生命早就!!

 

派︰怎可『不上心』?若圖『千秋命』??事理『能成』法自然,一《 圖 》道盡古今『物』︰

Interrupt_Process

,只問它『存』之『不存』??『生』或『難生』!!

 

生 ︰莫牽連,以為『物』『事』兩相非?且看如今誰騎白馬出函關!

Interrupts and Edge detection

An edge is the change in state of an electrical signal from LOW to HIGH (rising edge) or from HIGH to LOW (falling edge). Quite often, we are more concerned by a change in state of an input than it’s value. This change in state is an event.

To avoid missing a button press while your program is busy doing something else, there are two ways to get round this:

  • the wait_for_edge() function
  • the event_detected() function
  • a threaded callback function that is run when an edge is detected

wait_for_edge() function

The wait_for_edge() function is designed to block execution of your program until an edge is detected. In other words, the example above that waits for a button press could be rewritten as:

GPIO.wait_for_edge(channel, GPIO.RISING)

Note that you can detect edges of type GPIO.RISING, GPIO.FALLING or GPIO.BOTH. The advantage of doing it this way is that it uses a negligible amount of CPU, so there is plenty left for other tasks.

event_detected() function

The event_detected() function is designed to be used in a loop with other things, but unlike polling it is not going to miss the change in state of an input while the CPU is busy working on other things. This could be useful when using something like Pygame or PyQt where there is a main loop listening and responding to GUI events in a timely basis.

GPIO.add_event_detect(channel, GPIO.RISING)  # add rising edge detection on a channel
do_something()
if GPIO.event_detected(channel):
    print('Button pressed')

 

Note that you can detect events for GPIO.RISING, GPIO.FALLING or GPIO.BOTH.

Threaded callbacks

RPi.GPIO runs a second thread for callback functions. This means that callback functions can be run at the same time as your main program, in immediate response to an edge. For example:

def my_callback(channel):
    print('This is a edge event callback function!')
    print('Edge detected on channel %s'%channel)
    print('This is run in a different thread to your main program')

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)  # add rising edge detection on a channel
...the rest of your program...

If you wanted more than one callback function:

def my_callback_one(channel):
    print('Callback one')

def my_callback_two(channel):
    print('Callback two')

GPIO.add_event_detect(channel, GPIO.RISING)
GPIO.add_event_callback(channel, my_callback_one)
GPIO.add_event_callback(channel, my_callback_two)

Note that in this case, the callback functions are run sequentially, not concurrently. This is because there is only one thread used for callbacks, in which every callback is run, in the order in which they have been defined.

Switch debounce

You may notice that the callbacks are called more than once for each button press. This is as a result of what is known as ‘switch bounce’. There are two ways of dealing with switch bounce:

  • add a 0.1uF capacitor across your switch.
  • software debouncing
  • a combination of both

To debounce using software, add the bouncetime= parameter to a function where you specify a callback function. Bouncetime should be specified in milliseconds. For example:

# add rising edge detection on a channel, ignoring further edges for 200ms for switch bounce handling
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=200)

or

GPIO.add_event_callback(channel, my_callback, bouncetime=200)

Remove event detection

If for some reason, your program no longer wishes to detect edge events, it is possible to stop them:

GPIO.remove_event_detect(channel)

,如何說人『能』還是『不能』??

 

碼 ︰有 習 。請講述下面互動程式,發生了什麼事情?

pi@raspberrypi ~ $ sudo -s

root@raspberrypi:/home/pi# python3
Python 3.2.3 (default, Mar  1 2013, 11:53:50) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BOARD)
>>> 入低針 = 23
>>> GPIO.setup(入低針, GPIO.IN)
>>> GPIO.setup(入低針, GPIO.IN,pull_up_down = GPIO.PUD_DOWN)
>>> GPIO.input(入低針)
0
>>> GPIO.wait_for_edge(入低針, GPIO.RISING); print("上升緣"); GPIO.input(入低針)
上升緣
1
>>> GPIO.wait_for_edge(入低針, GPIO.FALLING); print("下降邊"); GPIO.input(入低針)
下降邊
0
>>> 

 

☿ 答︰ 入低針者,原低態,等待上接 3V3 ,一千能為,不省電,十千為之始相宜。連綿陳述有『 ;  』號,逐字逐句道分明,倩誰來通電,印出上升緣,此時入高態;難保忽撤離,急轉驅原態,列顯下降邊。同時取值『 0 』與『 1 』,真假義相隨。

 

訊 ︰☿☺ 終歸理事不二,空山但聞人語響!!