M♪o 之學習筆記本《巳》文章︰【䷗】中行獨復

派生碼訊

巳 蛇

來氏《 易 》易註︰

六四,中行獨復。

中行者,在中行也。五陰而四居其中,中之象也。凡卦三四皆可言中,益卦三四,皆言中行是也。此爻變震,應爻亦震,震為足,行之象也。獨復者,不從其類而從陽也,故孔子以從道象之。

六四柔而得正,在群陰之中,而獨能下應于陽剛,故有中行獨復之象。曰獨復,則與休者等矣,蓋二比而四應也。

︰雷動在初,居九地之下。雷出地奮,能得其中行。故曰︰中行獨復。

連假剛結束,今天學堂上哄哄隆隆,大家談著廟會、祭典、出遊的點點滴滴,好不熱鬧。就連學長來了,都還不知道,繼續機哩括啦的說 。

 

派︰同學們,大家好。【漸漸靜了下來】

這學期的實習課已經結束,接下來的 專題 題,可以自由報名參加。首先安排的是 視 視而不 見 見的 紅 外 線 紅外線串講 。大家都知道實習 機 機板上有一個 IR TX 發射器,假使沒有 IR RX 接收器,也是枉然。為了鼓勵同學們的踴躍參與,除了發給接收器之外,還附贈紅外線遙控器一只,希望大家一起玩的個盡興。不過報名者得寫心得報告 文章 章,將擇其優者參加明年之生生祭大賽,盼為小學堂增光。現在請大家到班長 M♪o 處登記,隨後正式開講。

☿☺ 竟然全班加入,看來這下學長可是虧大了耶!

 

生 ︰同學們不要被紅外線嚇到,雖說眼睛看不見,比方講,

NoIR

,它 Pi-Noir 可是抓的住它。如從驅動原理上說,和發光二極體 LED 差異不大︰

IR-TX

藍光LED

因為一般家用遙控器所用的訊號『規範』 Protocol ,需要作高速的資料處理,所以通常不用『派生』 Python 直接操作。更由於應用與系統整合的考慮,因此早年就有了 Linux LIRC ── Linux Infrared Remote Control ── 專案,提供了方便的程式界面以及使用架構。現在就讓我們展開紅外線之旅的吧。

 

Linux LIRC Driver Model


 

Driver Model

 


  |  +---------------------------------------------------------------+
S |  | 'gpm -t msc -m /var/run/lirc/lircm' or a well configured X    |
O |  | (see section 'Configuring lircmd') for details                |
F |  +--------------+------------------------------------------------+
T |                 |
W |   /var/run/lirc/lircm (named pipe (FIFO) ==> one connection)
A |                 |
R |     +-----------+-----------+   +----------------------------------+
E |     | mouse daemon (lircmd) |   | tools (irexec, irxevent, ...)    |
  |     | configured with       |   | configured with ~/.config/lircrc |
  |     | lircmd.conf           |   |                                  |
  |     +-----------+-----------+   +-----------+----------------------+
  |                 |                           |
  |                 +-------------+-------------+
  |                               |
  |              /var/run/lirc/lircd (socket → multiple connections)
  |                               |
S |              +----------------+--------------------------+
O |              | decoder daemon (lircd), irrecord or mode2 |    TCP/IP
F |              | lircd is configured through lircd.conf    +---  port
T | User space   |                                           |     8765
W |              +------------------+------------------------+
A |                                 |            |
R |                                 |            |
E |                                 |            |
  |                                 |   /dev/uinput (Linux input layer)
  |                                 |
  |                                 |
  +---------------------------------+----------------------------------
  | Kernel space                    |              (character device
  |                                 |                  driver →
  |                    +------------+----------+    one connection)
  |                    |                       |
  |                /dev/lirc               /dev/ttySx
  |                    |                       |
  |       +------------+-------------+   +-----+---------------+
  |       |  LIRC device driver      |   | Linux serial driver |
  |       | (with ioctl-interface)   |   |                     |
  |       +------------+-------------+   +----------+----------+
  |                    |                            |
--+--------------------+----------------------------+------------------
  |                    |                            |
  |         +----------+------------+               |
  |         |                       |               |
  | +-------+----------------+ +----+-----+ +-------+-----------------+
H | | serial / parallel port | | TV cards | | Irman/RemoteMaster/etc. |
W | +------------------------+ +----------+ +-------------------------+
  |


Formats


  • /dev/lirc:
    highly depends on the mode selected with ioctls:

    LIRC_MODE_MODE2
    outputs packets containing an int value describing a IR signal

    • bits 0-23 contain the length of the pulse/space in microseconds
    • bits 31-24 can be:
      • 0: space
      • 1: pulse
      • 2: timeout (has to be enabled using LIRC_SET_REC_TIMEOUT ioctl and is not supported by all receivers)
    • all other values are reserved

    Lengths greater than or equal to 16 seconds are clamped to 0xffffff.

    References:

    • drivers/lirc_serial/lirc_serial.c
    • drivers/lirc_parallel/lirc_parallel.c
    • tools/mode2.c (dumps the output from the driver to stdout)
    LIRC_MODE_LIRCCODE
    outputs codes of configurable length in big endian byte order
  • /var/run/lirc/lircd:
    outputs strings containing all information about the remote and the pressed button.References:

    • daemons/lircd.c
    • tools/irw.c
  • /var/run/lirc/lircm:
    • MouseSystems
      5 byte packets:

      • byte 1: button information
      • byte 2: change on X axis
      • byte 3: change on Y axis
      • byte 4,5: 0
    • IMPS/2
      4 byte packets: check the source code for details
    • IntelliMouse
      4 byte packets: check the source code for details

    References:

    • daemons/lircmd.c

 

IR protocol analyzer

IR protocol analyzer is a universal application for automatic decoding several types of infrared remote control protocol packets. The application uses microphone input of a soundcard to capture infrared signal from a remote control. As a consequence, the hardware receiver is minimalistic and easy to build; just plug a phototransistor to input of your soundcard, that’s all hardware you need.

Application processes IR signal from a remote control and compares it with its own database of known protocols. When a match is found, packet is decoded and its characteristic is displayed to user (including protocol name, description, decoded data and graph with timing).

Protocol definitions are stored in separate XML file. This XML file can be easily modified and new protocol definitions can be added by user. When an unknown packet is captured, user may still display its graph with timing details. Then, on the basis of the graph and timing, he may create new protocol description.

IR protocol analyzer screenshot IR protocol analyzer – screenshot

Documentation

Download

Licence

You can use this program for free. If you find the program useful, please consider making a donation via PayPal.

Theory

The ProtocolAnalyzer constantly samples the analog input signal, trying to recreate any digital pulses found in the signal. The so called flank detector uses a quite simple algorithm to find the flanks of the pulses:

Find sequences where the sampled value changed significanlty (Flank Swing) over a short period of time (Flank Length)

To avoid the same flank being counted twice if it is large, we wait a while (Flank Holdoff) after finding a flank until we accept a new flank. This picture shows the meaning of the settings on a real sampled signal. The red line is the analog value (where the sample points are marked with black dots). The blue line is the flank detectors interpretation of the signal:

In the example Flank Swing was set at 50 and Flank Length set to 3, so when the signal changed more than 50 over 3 samples, the flank was detected (see the blue line). Note that the signal changed more than 50 in the following samples as well, but since this was within Flank Holdoff, this was not counted.

……

 

△ 軟體安裝以及設定

【安裝】

sudo apt-get install lirc liblircclient-dev
sudo apt-get install python-lirc python3-lirc

在 /boot/config.txt 的檔案底增加一行︰

dtoverlay=lirc-rpi,gpio_in_pin=9,gpio_out_pin=12

在 //etc/modules 的檔案底增加兩行︰

lirc_dev
lirc_rpi gpio_in_pin=9 gpio_out_pin=12

 

【設定】

將 /etc/lirc/hardware.conf 修改成︰

pi@raspberrypi ~ more /etc/lirc/hardware.conf  # /etc/lirc/hardware.conf # # Arguments which will be used when launching lircd LIRCD_ARGS="--uinput"  #Don't start lircmd even if there seems to be a good config file #START_LIRCMD=false  #Don't start irexec, even if a good config file seems to exist. #START_IREXEC=false  #Try to load appropriate kernel modules LOAD_MODULES=true  # Run "lircd --driver=help" for a list of supported drivers. DRIVER="default" # usually /dev/lirc0 is the correct setting for systems using udev  DEVICE="/dev/lirc0" MODULES="lirc_rpi"  # Default configuration files for your hardware if any LIRCD_CONF="" LIRCMD_CONF="" pi@raspberrypi ~ 

 

給定遙控器之 /etc/lirc/lircd.conf 檔案內容︰

pi@raspberrypi ~ more /etc/lirc/lircd.conf  # Please make this file available to others # by sending it to <lirc@bartelmus.de> # # this config file was automatically generated # using lirc-0.9.0-pre1(default) on Tue Oct  8 07:05:38 2013 # # contributed by # # brand:                       /home/pi/lircd.conf # model no. of remote control: # devices being controlled by this remote: #   begin remote     name  /home/pi/lircd.conf   bits           16   flags SPACE_ENC|CONST_LENGTH   eps            30   aeps          100     header       9006  4447   one           594  1648   zero          594   526   ptrail        587   repeat       9006  2210   pre_data_bits   16   pre_data       0xFD   gap          107633   toggle_bit_mask 0x0         begin codes           KEY_1                    0x08F7           KEY_2                    0x8877           KEY_3                    0x48B7           KEY_4                    0x28D7           KEY_5                    0xA857           KEY_6                    0x6897           KEY_7                    0x18E7           KEY_8                    0x9867           KEY_9                    0x58A7           KEY_0                    0x30CF           KEY_DOWN                 0xB04F           KEY_LEFT                 0x10EF           KEY_UP                   0xA05F           KEY_RIGHT                0x50AF           KEY_BACK                 0x708F           KEY_ENTER                0x906F           KEY_SETUP                0x20DF           KEY_PLAYPAUSE            0x807F           KEY_STOP                 0x609F           KEY_VOLUMEUP             0x40BF           KEY_VOLUMEDOWN           0x00FF       end codes   end remote pi@raspberrypi ~ 

 

【測試】

安裝好 IR 接收器,執 行 irw ,

IRW(1) User Commands IRW(1)

NAME
irw – sends data from Unix domain socket to stdout

SYNOPSIS
irw [socket]

DESCRIPTION
irw will connect to any Unix domain socket and will print the data that it receives to stdout. If you don’t give it a socket name argument it will watch
/var/run/lirc/lircd. Useful for debugging.

-h –help
display usage summary

-v –version
display version

SEE ALSO
The documentation for lirc is maintained as html pages. They are located under html/ in the documentation directory.

irw 0.9.0-pre1 October 2010

,對準 IR 接收器操作遙控器,結果類似如下︰

pi@raspberrypi ~ $  irw
0000000000fd08f7 00 KEY_1 /home/pi/lircd.conf
0000000000fda05f 00 KEY_UP /home/pi/lircd.conf
0000000000fd00ff 00 KEY_VOLUMEDOWN /home/pi/lircd.conf
0000000000fd00ff 01 KEY_VOLUMEDOWN /home/pi/lircd.conf
0000000000fd00ff 02 KEY_VOLUMEDOWN /home/pi/lircd.conf
0000000000fd40bf 00 KEY_VOLUMEUP /home/pi/lircd.conf
0000000000fd40bf 01 KEY_VOLUMEUP /home/pi/lircd.conf
0000000000fdb04f 00 KEY_DOWN /home/pi/lircd.conf
0000000000fdb04f 01 KEY_DOWN /home/pi/lircd.conf

 

碼 ︰會 習 。☿ 長 長︰同學們請安靜。也許有人還忙著安裝以及驗證紅外線的軟體程式。先發下今天的講義︰

‧ 數碼管討論摘要。

‧入出針文法大綱。

‧試作程式。

請大家回顧瞻前,稍候開始會習。

誰知同學們對那個『入出針語言』興奮莫名,討論的意外熱烈,直到時間已近傍晚,還興致高昂。縱使深感大家的熱情,實在難以記其要,勉強摘記︰

‧一開始顯示 1234 ,沒有什麼意思!該將實習 機 機板命名,用個代表大家的符號,彰顯小學堂彼此間的友情。

‧派生停止執行緒的辦法,聽說可以將之設定為 daemon 。

‧要是加入 IRTX 與 IRRX 就更好玩了。

‧變元只能記數值,不知道可不可以擴增函元 Functional 的呢?

‧蜂鳴器的禁音文法不好用。

‧……

,以結束今日之 習 習,明兒再 會 會。

 

訊 ︰☿ 此刻或許真的能體會,俗話所說︰吃虧就是佔便宜。