# TWETerm\_M5\_Console

M5Stack の 320x240 LCD 用のターミナルの実装です。[`ITerm`](/0.9-1/references/untitled/tweterm/iterm.md)を実装しています。

本クラスは`namespace TWEARD`内に定義されます。

## メソッド

### TWETerm\_M5\_Console  - コンストラクタ

```cpp
TWETerm_M5_Console(
			uint8_t u8c, uint8_t u8l,
			Rect drawArea, M5Stack& _M5)

TWETerm_M5_Console(
			uint8_t u8c, uint8_t u8l,
			TWETERM::SimpBuf_GChar* pAryLines, TWETERM::GChar* pBuff,
			Rect drawArea, M5Stack& _M5)
```

[`ITerm`のコンストラクタ](/0.9-1/references/untitled/tweterm/iterm.md#iterm-konsutorakuta)に`drawArea`と`_M5`のパラメータが追加されています。

`drawArea`は、LCD内のターミナル描画エリアを決めます。`Rect`構造体で指定しx,y,w,hを指定します。(x,y)は領域の左上の座標、(w,h)は領域の幅と高さです。

`_M5`は、M5Stackのグローバルインスタンス `M5`を指定します。

#### 例：

```cpp
TWETerm_M5_Console the_screen(64, 20, { 0, 16, 320, 192 }, M5);
```

カラム最大値を64、行数の最大値を20、左上座標を (0, 16)、領域サイズを (320, 192) として `the_screen` オブジェクトを構築します。

## メソッド

### refresh()

`ITerm::refresh()`の実装です。この関数により画面の描画を行います。`loop()`関数内で定期的に呼び出します。

本実装では、パフォーマンスの向上のため、原則として描画変更の必要にある行のみを上書きします。画面全領域を再描画したい場合は`force_refresh()`メソッドを呼び出します。

以下の例では32msごとに描画を行います。

```cpp
void loop() {
		static uint32_t u32mills;

		uint32_t u32now = millis();
		if (u32now - u32mills > 32) {
			the_screen.refresh();

			u32mills = u32now;
		}
}
```

### set\_font()

```cpp
void set_font(uint8_t u8id, uint8_t u8col_request = 0, uint8_t u8row_request = 0)
```

フォントを指定します。

`u8id`はフォントIDを指定します。

`u8col_request`は、設定したいカラム数を指定します。指定した数値が領域サイズに対して大きい場合は指定領域に入るように値が丸められます。0を指定した場合は、領域サイズから計算できる最大のカラム数に設定されます。

`u8row_request`は、設定したい行数を指定します。指定した数値が領域サイズに対して大きい場合は指定領域に入るように値が丸められます。0を指定した場合は、領域サイズから計算できる最大の行数に設定されます。

### font\_width(), font\_height(), font\_id()

```cpp
uint8_t font_id()
uint8_t font_width()
uint8_t font_height()
```

`font_id()`は、指定したフォントのIDを返します。

`font_width()`は、指定したフォントの幅をピクセル数で返します。ダブル幅のピクセル数は、この値の２倍になります。

`font_height()`は、指定したフォントの高さをピクセル数で返します。

### set\_color()

```cpp
void set_color(uint16_t color, uint16_t bgcolor = 0)
```

ターミナルの文字色と背景色を指定します。

`color`は文字色を指定します。

`bgcolor`は背景色を指定します。

{% hint style="info" %}
色は565形式の16bit値です。`TWEARD::color565()`関数で計算します。

```cpp
uint16_t c = color565(255, 127, 0); // R:255, G:127, B:0
```

{% endhint %}

{% hint style="warning" %}
白色は `ALMOST_WHITE` で指定します。color565(255,255,255) または WHITE を指定すると描画が崩れます。
{% endhint %}

### set\_color\_table()

```cpp
void set_color_table(const uint16_t* ptbl)
```

ターミナルで使用できる８色のテーブルを指定します。`ptbl`は`uint16_t`型の配列で８つの要素が必要です。

#### 例

```cpp
static const uint16_t COLTBL_MAIN[8] = {
	BLACK,
	RED,
	GREEN,
	YELLOW,
	color565(127, 127, 255), // BLUE,
	color565(255, 0, 142), // MAGENTA,
	CYAN,
	ALMOST_WHITE
};

the_screen.set_color_table(COLTBL_MAIN);
```

上記の例では青とマゼンダの色調を変えたテーブルを指定し、ターミナルオブジェクト `the_screen`に指定しています。

## ユーティリティ関数

### color565()

```cpp
constexpr uint16_t color565(uint8_t r, uint8_t g, uint8_t b)
```

`r, g, b` を指定して、565形式の色コードを生成します。


---

# 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/0.9-1/references/untitled/tweterm/tweterm_m5_console.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.
