# AppTwelite\_Recv

TWELITE DIPに工場出荷時に書き込まれている標準アプリ(App\_Twelite)のシリアルメッセージを解釈してLCD画面上に表示します。

![画面例](/files/-M075t8ubyfn1uXj0amX)

{% hint style="info" %}
M5stickに接続する前に書き込んだTWELITE DIPが無線パケットを受信してデータを出力するかPCで確認しておいてください。
{% endhint %}

M5Stack側のTWELITE DIPは、以下のような接続をしておきます。以下の配線はTWELITE DIPを親機設定(M1=GND)とした配線をしています。AI1-4はアナログポートがオープンになり不定な入力を防ぐためです(App\_TweliteではVCCレベルを入力した場合、そのポートを未使用とする意味を持たせています）

```
                          TWELITE DIP
                 -01:GND              VCC:28-------M5 3V3
                 -02:D14(SCL)    (M3 ) D3:27-
  M5 GPIO16-------03:D7 (RX )    (M2 ) D2:26-
                 -04:D5 (Pw1)    (AI4) D1:25----VCC
                 -05:D18(DO1)    (AI3) A2:24----VCC
                 -06:C  (Pw2)    (AI2) D0:23----VCC
                 -07:M  (Pw3)    (AI1) A1:22----VCC
                 -08:D19(DO2)         RST:21-
                 -09:D4 (DO3)    (BPS)D17:20-
  M5 GPIO17-------10:D6 (TX )    (SDA)D15:19-
                 -11:D8 (Pw4)    (DI4)D16:18-
                 -12:D9 (DO4)    (DI3)D11:17-
           GND----13:D10(M1 )    (DI2)D13:16-
  M5 GND----------14:GND         (DI1)D12:15-
```

## setup()

```cpp
Serial2.setRxBufferSize(512);
Serial2.begin(115200, SERIAL_8N1, 16, 17);
```

シリアルポートの初期化を行っています。

```
setup_screen();
```

LCDスクリーンの初期化を行います。処理の内容はscreen.cにあり、[ターミナル](/getting-started/using-library/terminal.md)画面の初期設定を行います。

## loop()

```
check_for_serial();
```

シリアルポートの入力チェックを行います。入力したデータを `the_input_uart` キューにいったん投入します。

```
process_input();
```

シリアルポートから入力データを処理します。ここでは[パーサー](/getting-started/using-library/parser.md)に文字列を投入します。パーサーによりシリアル電文が解釈できた場合は、`update_screen()`を呼び出しターミナル画面に文字を更新します。

```
check_for_refresh();
```

LCD画面上のターミナル画面領域を書き換えます。処理の内容はscreen.cにあります。

## update\_screen()

受信したパケットデータを読み取り、画面表示を更新します。

```cpp
  trm << "\033[H"; // カーソルホーム
...
		if (b) trm << "\033[1;1H" // カーソルを１行目に移動
			"\033[1;30;45mﾀｲﾑCT\033[0m" "  " // ボールド、文字色、背景色設定
			"\033[1;30;45mId#\033[0m" " "
			"\033[1;30;45mｼﾘｱﾙ番号\033[0m";
		if (b) trm << "\033[3;1H" // カーソルを３行目に移動
			"\033[1;30;45m D1 \033[0m"
			" \033[1;30;45m D2 \033[0m"
			" \033[1;30;45m D3 \033[0m"
			" \033[1;30;45m D4 \033[0m";
```

この関数内では[エスケープシーケンス](/references/untitled/tweterm/esc-sequence.md)を用いて画面の表示位置などを制御しています。

```cpp
		TwePacketTwelite& x = refTwePacketTwelite(spLastPacket);
		if (c) trm << "\033[2;1H"
		   << printfmt("%5d  %3d %8X", 
		      x.u16timestamp, x.u8addr_src, x.u32addr_src);
		if (c) trm << "\033[4;1H " 
			<< (x.DI1 ? "\033[31m" "●" : "\033[35m" "〇") << "\033[0m   "
			<< (x.DI2 ? "\033[32m" "●" : "\033[35m" "〇") << "\033[0m   "
			<< (x.DI3 ? "\033[33m" "●" : "\033[35m" "〇") << "\033[0m   "
			<< (x.DI4 ? "\033[34m" "●" : "\033[35m" "〇") << "\033[0m";
```

パケットデータは`spLastPacket`に格納されています。これの内容を紐解くには[`retTwePacketTwelite()`](/references/parser/twefmt/packet-types/twepackettwelite.md#sptwepacketkarano)を呼び出します。xの値を読み出して、これに対応して画面を更新します。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mwm5.twelite.info/getting-started/examples/apptwelite_recv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
