M5StickC Plus2 と Ir Remote Unit を組み合わせてNECフォーマットの赤外線リモコン信号簡易解析スケッチを作成しました。
スケッチ
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include <M5StickCPlus2.h> const int IR_RECEIVE_PORT_NO = 33; const int NUM_OF_BURST_BITS = 16 * 2; const int NUM_OF_FRAME_BITS = 100 * 2; const int BIT_PERIOD = 562; // microseconds int received_counter = 0; int frame_counter = 0; bool is_started = false; void setup() { M5.begin(); M5.Power.begin(); M5.Display.setRotation(1); Serial.begin(115200); pinMode(IR_RECEIVE_PORT_NO, INPUT); M5.Lcd.setTextSize(2); M5.Lcd.setCursor(0, 0); M5.Lcd.print("Checking IR receiver"); } void loop() { const int received_bit = digitalRead(IR_RECEIVE_PORT_NO) ? 0 : 1; if (received_bit != 0) { // detected Infrared Signal received_counter++; } else { // NOT detected Infrared Signal received_counter = 0; } if (received_counter >= NUM_OF_BURST_BITS) { frame_counter = NUM_OF_FRAME_BITS; } if (frame_counter >= 0) { if (frame_counter == NUM_OF_FRAME_BITS) { M5.Lcd.setCursor(0, 25); M5.Lcd.print("detected!"); Serial.print("\n"); } else if (frame_counter > 0) { if (is_started == true) { Serial.print(received_bit); } else { if (received_bit != 0) { Serial.print(received_bit); is_started = true; } else { ; // skip space bits } } } else { M5.Lcd.setCursor(0, 25); M5.Lcd.print(" "); is_started = false; } frame_counter--; } delayMicroseconds(BIT_PERIOD / 2); } |
出力例
|
1 |
1100110011001100110011011101100110000001100000111000001100000...001100 |
赤外線2次変調(On/Off)周期 562.5マイクロ秒 に対して端数を切り捨てているため、ところどころで 0 と 1 の周期がずれていますが、ほぼ一定間隔で "00" と "11" を繰り返していることが観察できます。
パルス距離符号化 (PDM : Pulse-Distance Encoding)
NECプロトコルのばあい
| 符号論理 | パルス距離 | パルス列の例 |
|---|---|---|
| 0 (false) | 1 | 1100 |
| 1 (true) | 3 | 11000000 |
パルス幅とスペース幅
- パルス幅 : 符号に関わらず一定
- スペース幅 : 符号により長短
※ スペース : パルスとパルスの間の無信号時間。ギャップ。
パルス距離
パルス幅とスペース幅の比。パルス距離が 1 のときパルス列は "1100" や "111000", "11110000" となる。