-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (133 loc) · 4.72 KB
/
Copy pathmain.cpp
File metadata and controls
164 lines (133 loc) · 4.72 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "M5StickCPlus2.h"
#include <IRrecv.h>
#include <IRutils.h>
// 赤外線受信ピン
const uint16_t RECV_PIN = 33;
// IR受信オブジェクトを作成
IRrecv irrecv(RECV_PIN);
decode_results results;
// ★★★ IRボタン情報を管理する構造体 ★★★
struct IRButton {
const char* name; // ボタン名
uint64_t value; // 完全なValue値
uint8_t command; // コマンドコード
IRButton(const char* n, uint64_t v, uint8_t c)
: name(n), value(v), command(c) {}
};
// ★★★ IRプロトコルを管理するクラス ★★★
class IRProtocol {
private:
const char* protocolName;
uint16_t address;
IRButton* buttons;
int buttonCount;
public:
IRProtocol(const char* name, uint16_t addr, IRButton* btns, int count)
: protocolName(name), address(addr), buttons(btns), buttonCount(count) {}
// アドレスが一致するかチェック
bool matchAddress(uint16_t addr) const {
return addr == address;
}
// Value値からボタンを検索
const char* findButtonByValue(uint64_t value) const {
for (int i = 0; i < buttonCount; i++) {
if (buttons[i].value == value) {
return buttons[i].name;
}
}
return nullptr;
}
// コマンドコードからボタンを検索
const char* findButtonByCommand(uint8_t command) const {
for (int i = 0; i < buttonCount; i++) {
if (buttons[i].command == command) {
return buttons[i].name;
}
}
return nullptr;
}
// アドレス&コマンドで一致するか確認
bool matchButton(uint16_t addr, uint8_t cmd) const {
if (addr != address) return false;
return findButtonByCommand(cmd) != nullptr;
}
// Valueで一致するか確認
bool matchButtonByValue(uint64_t value) const {
return findButtonByValue(value) != nullptr;
}
const char* getProtocolName() const { return protocolName; }
};
// ★★★ PANASONICプロトコルのボタン定義 ★★★
IRButton panasonicButtons[] = {
IRButton("TV_POWER", 0x555AF148688B, 0x00),
IRButton("CH_1", 0x555AF148724C, 0x00),
IRButton("CH_2", 0x555AF148F244, 0x00),
IRButton("CH_3", 0x555AF1480A43, 0x00),
IRButton("CH_4", 0x555AF1488A4B, 0x00),
IRButton("CH_5", 0x555AF1484A47, 0x00),
IRButton("CH_6", 0x555AF148CA4F, 0x00),
IRButton("CH_7", 0x555AF1482A41, 0x00),
IRButton("CH_8", 0x555AF148AA49, 0x00),
IRButton("CH_9", 0x555AF1486A45, 0x00),
IRButton("CH_10", 0x555AF148EA4D, 0x00),
IRButton("CH_11", 0x555AF1481A42, 0x00),
IRButton("CH_12", 0x555AF1489A4A, 0x00)
};
// PANASONICプロトコルオブジェクト
IRProtocol panasonicProtocol("PANASONIC", 0x00, panasonicButtons, 13);
const bool FILTER_ENABLED = true; // trueにすると特定の信号のみ検出
void setup() {
M5.begin();
Serial.begin(115200);
// ディスプレイの初期設定
M5.Lcd.setRotation(3); // 横向きに設定
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(2); // フォントサイズを大きく設定
M5.Lcd.setCursor(0, 0);
M5.Lcd.println("IR Receiver Ready");
irrecv.enableIRIn(); // IR受信を開始
}
// ★★★ ボタン識別用の関数(完全なValue値で検索) ★★★
String identifyButton(uint64_t value) {
// Value値で検索
const char* buttonName = panasonicProtocol.findButtonByValue(value);
if (buttonName != nullptr) {
return String(buttonName);
}
return "UNKNOWN";
}
void loop() {
// 赤外線信号を受信した場合
if (irrecv.decode(&results)) {
// ★★★ 完全なValue値でフィルタリング ★★★
if (FILTER_ENABLED) {
// Value値が登録されているボタンと一致するかチェック
if (!panasonicProtocol.matchButtonByValue(results.value)) {
irrecv.resume(); // 次の信号を受信準備
return; // 登録されていない信号は無視
}
}
// ボタンを識別(完全なValue値で検索)
String buttonName = identifyButton(results.value);
// ★★★ PCへ送信するシンプルなテキストメッセージ ★★★
Serial.println(buttonName);
// ディスプレイをクリア
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
// ディスプレイに受信情報を表示
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setTextSize(3);
M5.Lcd.println(buttonName);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(2);
// プロトコル名を表示
String protocol = typeToString(results.decode_type);
M5.Lcd.printf("Type: %s\n", protocol.c_str());
// 値を表示
M5.Lcd.printf("Value:\n0x%llX\n", (unsigned long long)results.value);
// 受信した信号を再度準備
irrecv.resume();
}
delay(100);
}