Skip to content

Commit 24a9365

Browse files
authored
Merge pull request #4 from Forairaaaaa/main
update app ir to manual sending
2 parents 7767ce0 + 338a557 commit 24a9365

10 files changed

Lines changed: 296 additions & 100 deletions

File tree

main/apps/app_ir/app_ir.cpp

Lines changed: 224 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file app_ir.cpp
33
* @author Forairaaaaa
4-
* @brief http://gitlab.m5stack.com/Forairaaaaa/stamps3_keypad_factory_test/blob/master/code/src/factory_test/ir/ir_test.cpp
4+
* @brief
55
* @version 0.6
66
* @date 2023-09-20
77
*
@@ -19,95 +19,255 @@
1919
using namespace MOONCAKE::APPS;
2020

2121

22+
#define _keyboard _data.hal->keyboard()
2223
#define _canvas _data.hal->canvas()
24+
#define _canvas_update _data.hal->canvas_update
25+
#define _canvas_clear() _canvas->fillScreen(THEME_COLOR_BG)
2326

2427

25-
void AppIR::onCreate()
28+
void AppIR::_update_input()
2629
{
27-
spdlog::info("{} onCreate", getAppName());
30+
// spdlog::info("{} {}", _keyboard->keyList().size(), _data.last_key_num);
2831

29-
// Get hal
30-
_data.hal = mcAppGetDatabase()->Get("HAL")->value<HAL::Hal *>();
32+
// If changed
33+
if (_keyboard->keyList().size() != _data.last_key_num)
34+
{
35+
// If key pressed
36+
if (_keyboard->keyList().size() != 0)
37+
{
38+
// Update states and values
39+
_keyboard->updateKeysState();
3140

41+
// If enter
42+
if (_keyboard->keysState().enter)
43+
{
44+
// New line
45+
_canvas->print(" \n");
3246

33-
_data.hal->canvas()->fillScreen(THEME_COLOR_BG);
34-
_data.hal->canvas()->setFont(FONT_REPL);
35-
_data.hal->canvas()->setTextColor(TFT_ORANGE, THEME_COLOR_BG);
36-
_data.hal->canvas()->setTextSize(1);
37-
_data.hal->canvas()->setTextScroll(false);
38-
ir_wrap_init();
47+
// // Reset buffer
48+
// _data.repl_input_buffer = "";
49+
50+
_data.current_state = state_fire;
51+
}
52+
53+
// If delete
54+
else if (_keyboard->keysState().del)
55+
{
56+
if (_data.repl_input_buffer.size())
57+
{
58+
// Pop input buffer
59+
_data.repl_input_buffer.pop_back();
60+
61+
// Pop canvas display
62+
int cursor_x = _canvas->getCursorX();
63+
int cursor_y = _canvas->getCursorY();
64+
65+
if (cursor_x - FONT_REPL_WIDTH < 0)
66+
{
67+
// Last line
68+
cursor_y -= FONT_REPL_HEIGHT;
69+
cursor_x = _canvas->width() - FONT_REPL_WIDTH;
70+
}
71+
else
72+
{
73+
cursor_x -= FONT_REPL_WIDTH;
74+
}
75+
76+
spdlog::info("new cursor {} {}", cursor_x, cursor_y);
77+
78+
_canvas->setCursor(cursor_x, cursor_y);
79+
_canvas->print(" ");
80+
_canvas->setCursor(cursor_x, cursor_y);
81+
}
82+
}
83+
84+
// Normal chars
85+
else
86+
{
87+
for (auto& i : _keyboard->keysState().values)
88+
{
89+
// spdlog::info("{}", i);
90+
91+
_canvas->print(i);
92+
_data.repl_input_buffer += i;
93+
}
94+
}
95+
96+
_canvas_update();
97+
98+
// Update last key num
99+
_data.last_key_num = _keyboard->keyList().size();
100+
}
101+
else
102+
{
103+
// Reset last key num
104+
_data.last_key_num = 0;
105+
}
106+
}
39107
}
40108

41109

42-
void AppIR::onRunning()
110+
void AppIR::_update_cursor()
43111
{
44-
if ((millis() - _data.count) > 800)
112+
if ((millis() - _data.cursor_update_time_count) > _data.cursor_update_period)
45113
{
46-
_data.cmd++;
114+
// Get cursor
115+
int cursor_x = _canvas->getCursorX();
116+
int cursor_y = _canvas->getCursorY();
117+
118+
// spdlog::info("cursor {} {}", cursor_x, cursor_y);
47119

48-
// sendNEC(IR_SEND_PIN, 0x10, _data.cmd, 0);
49-
ir_wrap_send(0x10, _data.cmd);
120+
_canvas->print(_data.cursor_state ? '_' : ' ');
121+
_canvas->setCursor(cursor_x, cursor_y);
122+
_canvas_update();
50123

51-
printf("ir sended: addr: 0x%02X cmd:0x%02X\n", 0x10, _data.cmd);
52-
_data.count = millis();
124+
_data.cursor_state = !_data.cursor_state;
125+
_data.cursor_update_time_count = millis();
53126
}
127+
}
54128

55129

130+
void AppIR::_update_state()
131+
{
132+
if (_data.current_state == state_init)
133+
{
134+
_canvas->setTextColor(TFT_ORANGE, THEME_COLOR_BG);
135+
_canvas->printf("Input NEC msg in:\naddr(dec),data(dec)\ne.g. 16,1\nHit Enter to send\n");
136+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
137+
_canvas->printf(">>> ");
138+
_canvas_update();
139+
140+
_data.current_state = state_wait_input;
141+
}
56142

143+
else if (_data.current_state == state_fire)
144+
{
145+
// ir_wrap_send(16, 1);
146+
spdlog::info("raw input: {}", _data.repl_input_buffer);
57147

58-
if ((millis() - _data.anim_count) > 400) {
148+
// Try parse input shit
149+
bool is_parse_ok = false;
150+
int ir_addr = 0;
151+
int ir_data = 0;
59152

60-
_data.anim_pos++;
61-
if (_data.anim_pos > 3) {
62-
_data.anim_pos = 0;
63-
}
153+
_customSplit(_data.repl_input_buffer, ',', _data.parse_result);
64154

155+
spdlog::info("parse get {}:", _data.parse_result.size());
156+
for (const auto& i : _data.parse_result)
157+
spdlog::info("{}", i);
65158

66-
67-
// _canvas->fillRect(0, 0, 240, 135, TFT_BLACK);
68-
// // _canvas->setSwapBytes(true);
69-
// // _canvas->pushImage(180, 16, 48, 48, (uint16_t *)icon_ir);
70-
// _canvas->setTextColor(_canvas->color565(180, 180, 180));
71-
// _canvas->setTextSize(3);
72-
// _canvas->setCursor(12, 20);
73-
// _canvas->printf("IR Send ");
74-
// _canvas->setTextSize(5);
75-
76-
_data.hal->canvas()->setTextColor(TFT_ORANGE, THEME_COLOR_BG);
77-
_data.hal->canvas()->setCursor(10, 5);
78-
_data.hal->canvas()->setTextSize(1);
79-
_data.hal->canvas()->print("IR Send");
159+
if (_data.parse_result.size() >= 2)
160+
{
161+
// https://docs.espressif.com/projects/esp-idf/zh_CN/v4.4.6/esp32/api-guides/error-handling.html
162+
try
163+
{
164+
ir_addr = std::stoi(_data.parse_result[0]);
165+
ir_data = std::stoi(_data.parse_result[1]);
166+
spdlog::info("get addr: {} data: {}", ir_addr, ir_data);
80167

81-
82-
_canvas->setCursor(12, 40);
83-
_canvas->setTextSize(3);
84-
85-
86-
switch (_data.anim_pos) {
87-
case 0:
88-
_canvas->printf("0x%02X > ", _data.cmd);
89-
break;
90-
case 1:
91-
_canvas->printf("0x%02X >> ", _data.cmd);
92-
break;
93-
case 2:
94-
_canvas->printf("0x%02X >> ", _data.cmd);
95-
break;
96-
case 3:
97-
_canvas->printf("0x%02X > ", _data.cmd);
98-
break;
99-
default:
100-
break;
168+
is_parse_ok = true;
169+
}
170+
catch(const std::exception& e)
171+
{
172+
// std::cerr << e.what() << '\n';
173+
spdlog::info("parse failed: {}", e.what());
174+
175+
is_parse_ok = false;
176+
}
101177
}
102178

103179

104-
_data.hal->canvas_update();
180+
181+
// Send the shit out
182+
if (is_parse_ok)
183+
{
184+
ir_wrap_send((uint8_t)ir_addr, (uint8_t)ir_data);
185+
186+
_canvas->setTextColor(TFT_GREEN, THEME_COLOR_BG);
187+
_canvas->printf("Msg Send:\naddr: 0x%02X data: 0x%02X\n", (uint8_t)ir_addr, (uint8_t)ir_data);
188+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
189+
}
190+
else
191+
{
192+
_canvas->setTextColor(TFT_RED, THEME_COLOR_BG);
193+
_canvas->printf("Parse %s failed\n", _data.repl_input_buffer.c_str());
194+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
195+
}
105196

106197

107-
_data.anim_count = millis();
198+
199+
// // New line
200+
// _canvas->print(">>> ");
201+
// _canvas_update();
202+
// // Reset buffer
203+
// _data.repl_input_buffer = "";
204+
205+
206+
// Keep that shit still
207+
_canvas->printf(">>> %s", _data.repl_input_buffer.c_str());
208+
_canvas_update();
209+
210+
_data.current_state = state_wait_input;
108211
}
212+
}
109213

110214

215+
// Cpp sucks
216+
// https://favtutor.com/blogs/split-string-cpp
217+
void AppIR::_customSplit(const std::string& str, char separator, std::vector<std::string>& result)
218+
{
219+
result.clear();
220+
221+
int startIndex = 0, endIndex = 0;
222+
std::string temp;
223+
for (int i = 0; i <= str.size(); i++)
224+
{
225+
// If we reached the end of the word or the end of the input.
226+
if (str[i] == separator || i == str.size())
227+
{
228+
endIndex = i;
229+
temp.clear();
230+
temp.append(str, startIndex, endIndex - startIndex);
231+
result.push_back(temp);
232+
startIndex = endIndex + 1;
233+
}
234+
}
235+
}
236+
237+
238+
void AppIR::onCreate()
239+
{
240+
spdlog::info("{} onCreate", getAppName());
241+
242+
// Get hal
243+
_data.hal = mcAppGetDatabase()->Get("HAL")->value<HAL::Hal*>();
244+
245+
ir_wrap_init();
246+
}
247+
248+
249+
void AppIR::onResume()
250+
{
251+
ANIM_APP_OPEN();
252+
253+
_canvas_clear();
254+
_canvas->setTextScroll(true);
255+
_canvas->setBaseColor(THEME_COLOR_BG);
256+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
257+
_canvas->setFont(FONT_REPL);
258+
_canvas->setTextSize(FONT_SIZE_REPL);
259+
_canvas->setCursor(0, 0);
260+
261+
_data.current_state = state_init;
262+
}
263+
264+
265+
void AppIR::onRunning()
266+
{
267+
_update_state();
268+
_update_input();
269+
_update_cursor();
270+
111271
if (_data.hal->homeButton()->pressed())
112272
{
113273
_data.hal->playNextSound();
@@ -116,3 +276,8 @@ void AppIR::onRunning()
116276
}
117277
}
118278

279+
280+
void AppIR::onDestroy()
281+
{
282+
_canvas->setTextScroll(false);
283+
}

main/apps/app_ir/app_ir.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,41 @@ namespace MOONCAKE
2626
class AppIR : public APP_BASE
2727
{
2828
private:
29+
enum State_t
30+
{
31+
state_init = 0,
32+
state_wait_input,
33+
state_fire,
34+
};
35+
2936
struct Data_t
3037
{
3138
HAL::Hal* hal = nullptr;
3239

33-
uint8_t cmd = 0x00;
34-
unsigned long count = millis();
35-
unsigned long anim_count = millis();
36-
uint8_t anim_pos = 0;
40+
int last_key_num = 0;
41+
std::string repl_input_buffer;
42+
bool is_caps = false;
43+
char* value_buffer = nullptr;
44+
45+
uint32_t cursor_update_time_count = 0;
46+
uint32_t cursor_update_period = 500;
47+
bool cursor_state = false;
48+
49+
State_t current_state = state_init;
50+
std::vector<std::string> parse_result;
3751
};
3852
Data_t _data;
3953

54+
void _update_input();
55+
void _update_cursor();
56+
void _update_state();
57+
void _customSplit(const std::string& str, char separator, std::vector<std::string>& result);
58+
4059
public:
4160
void onCreate() override;
42-
void onResume() override { ANIM_APP_OPEN(); }
61+
void onResume() override;
4362
void onRunning() override;
63+
void onDestroy() override;
4464
};
4565

4666
class AppIR_Packer : public APP_PACKER_BASE

main/apps/app_repl/app_repl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void AppREPL::_update_input()
7373
return;
7474
}
7575

76-
// New line
76+
// New line (little space at front is to cover the cursor)
7777
_canvas->print(" \n");
7878

7979
// Run cmd

0 commit comments

Comments
 (0)