-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathm5stack_tab5_example.cpp
More file actions
559 lines (491 loc) · 20.3 KB
/
m5stack_tab5_example.cpp
File metadata and controls
559 lines (491 loc) · 20.3 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**
* @file m5stack_tab5_example.cpp
* @brief M5Stack Tab5 BSP Example
*
* This example demonstrates the comprehensive functionality of the M5Stack Tab5
* development board including display, touch, audio, camera, IMU, power management,
* and communication interfaces.
*/
#include <chrono>
#include <deque>
#include <stdlib.h>
#include <vector>
#include "m5stack-tab5.hpp"
#include "kalman_filter.hpp"
#include "madgwick_filter.hpp"
using namespace std::chrono_literals;
static constexpr size_t MAX_CIRCLES = 100;
static std::deque<lv_obj_t *> circles;
static std::vector<uint8_t> audio_bytes;
static std::recursive_mutex lvgl_mutex;
static void draw_circle(int x0, int y0, int radius);
static void clear_circles();
static bool load_audio(size_t &out_size, size_t &out_sample_rate);
static void play_click(espp::M5StackTab5 &tab5);
extern "C" void app_main(void) {
espp::Logger logger({.tag = "M5Stack Tab5 Example", .level = espp::Logger::Verbosity::DEBUG});
logger.info("Starting example!");
//! [m5stack tab5 example]
espp::M5StackTab5 &tab5 = espp::M5StackTab5::get();
// tab5.set_log_level(espp::Logger::Verbosity::DEBUG);
logger.info("Running on M5Stack Tab5");
// first let's get the internal i2c bus and probe for all devices on the bus
logger.info("Probing internal I2C bus...");
auto &i2c = tab5.internal_i2c();
std::vector<uint8_t> found_addresses;
for (uint8_t address = 1; address < 128; address++) {
if (i2c.probe_device(address)) {
found_addresses.push_back(address);
}
}
logger.info("Found devices at addresses: {::#02x}", found_addresses);
// Initialize the IO expanders
logger.info("Initializing IO expanders...");
if (!tab5.initialize_io_expanders()) {
logger.error("Failed to initialize IO expanders!");
return;
}
logger.info("Initializing lcd...");
// initialize the LCD
if (!tab5.initialize_lcd()) {
logger.error("Failed to initialize LCD!");
return;
}
// initialize the display with a pixel buffer (Tab5 is 1280x720 with 2 bytes per pixel)
logger.info("Initializing display...");
auto pixel_buffer_size = tab5.display_width() * 10; // tab5.display_height();
if (!tab5.initialize_display(pixel_buffer_size)) {
logger.error("Failed to initialize display!");
return;
}
auto touch_callback = [&](const auto &touch) {
// NOTE: since we're directly using the touchpad data, and not using the
// TouchpadInput + LVGL, we'll need to ensure the touchpad data is
// converted into proper screen coordinates instead of simply using the
// raw values.
static auto previous_touchpad_data = tab5.touchpad_convert(touch);
auto touchpad_data = tab5.touchpad_convert(touch);
if (touchpad_data != previous_touchpad_data) {
logger.info("Touch: {}", touchpad_data);
previous_touchpad_data = touchpad_data;
// if the button is pressed, clear the circles
if (touchpad_data.btn_state) {
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
clear_circles();
}
// if there is a touch point, draw a circle and play a click sound
if (touchpad_data.num_touch_points > 0) {
play_click(tab5);
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
draw_circle(touchpad_data.x, touchpad_data.y, 10);
}
}
};
logger.info("Initializing touch...");
if (!tab5.initialize_touch(touch_callback)) {
logger.error("Failed to initialize touch!");
return;
}
// make the filter we'll use for the IMU to compute the orientation
static constexpr float angle_noise = 0.001f;
static constexpr float rate_noise = 0.1f;
static espp::KalmanFilter<2> kf;
kf.set_process_noise(rate_noise);
kf.set_measurement_noise(angle_noise);
static constexpr float beta = 0.5f; // higher = more accelerometer, lower = more gyro
static espp::MadgwickFilter f(beta);
using Imu = espp::M5StackTab5::Imu;
auto kalman_filter_fn = [](float dt, const Imu::Value &accel,
const Imu::Value &gyro) -> Imu::Value {
// Apply Kalman filter
float accelRoll = atan2(accel.y, accel.z);
float accelPitch = atan2(-accel.x, sqrt(accel.y * accel.y + accel.z * accel.z));
kf.predict({espp::deg_to_rad(gyro.x), espp::deg_to_rad(gyro.y)}, dt);
kf.update({accelRoll, accelPitch});
float roll, pitch;
std::tie(roll, pitch) = kf.get_state();
// return the computed orientation
Imu::Value orientation{};
orientation.roll = roll;
orientation.pitch = pitch;
orientation.yaw = 0.0f;
return orientation;
};
auto madgwick_filter_fn = [](float dt, const Imu::Value &accel,
const Imu::Value &gyro) -> Imu::Value {
// Apply Madgwick filter
f.update(dt, accel.x, accel.y, accel.z, espp::deg_to_rad(gyro.x), espp::deg_to_rad(gyro.y),
espp::deg_to_rad(gyro.z));
float roll, pitch, yaw;
f.get_euler(roll, pitch, yaw);
// return the computed orientation
Imu::Value orientation{};
orientation.roll = espp::deg_to_rad(roll);
orientation.pitch = espp::deg_to_rad(pitch);
orientation.yaw = espp::deg_to_rad(yaw);
return orientation;
};
logger.info("Initializing IMU...");
// initialize the IMU
if (!tab5.initialize_imu(kalman_filter_fn)) {
logger.error("Failed to initialize IMU!");
return;
}
// initialize the uSD card
using SdCardConfig = espp::M5StackTab5::SdCardConfig;
SdCardConfig sdcard_config{};
if (!tab5.initialize_sdcard(sdcard_config)) {
logger.warn("Failed to initialize uSD card, there may not be a uSD card inserted!");
} else {
uint32_t size_mb = 0;
uint32_t free_mb = 0;
if (tab5.get_sd_card_info(&size_mb, &free_mb)) {
logger.info("uSD card size: {} MB, free space: {} MB", size_mb, free_mb);
} else {
logger.warn("Failed to get uSD card info");
}
}
logger.info("Initializing RTC...");
// initialize the RTC
if (!tab5.initialize_rtc()) {
logger.error("Failed to initialize RTC!");
return;
}
auto current_time = std::tm{};
if (!tab5.get_rtc_time(current_time)) {
logger.error("Failed to get RTC time");
return;
}
// only set the time if the year is before 2024
if (current_time.tm_year < 124) {
// set the RTC time to a known value (2024-01-15 14:30:45)
// Set time using std::tm
std::tm time = {};
time.tm_year = 124; // 2024 - 1900
time.tm_mon = 0; // January (0-based)
time.tm_mday = 15; // 15th
time.tm_hour = 14; // 2 PM
time.tm_min = 30;
time.tm_sec = 45;
time.tm_wday = 1; // Monday
if (!tab5.set_rtc_time(time)) {
logger.error("Failed to set RTC time");
return;
}
} else {
logger.info("RTC time is already set to a valid value {:%Y-%m-%d %H:%M:%S}", current_time);
}
logger.info("Initializing battery management...");
// initialize battery monitoring
if (!tab5.initialize_battery_monitoring()) {
logger.error("Failed to initialize battery monitoring!");
return;
}
// enable charging
tab5.set_charging_enabled(true);
logger.info("Initializing sound...");
// initialize the sound
if (!tab5.initialize_audio()) {
logger.error("Failed to initialize sound!");
return;
}
// Brightness control with button
logger.info("Initializing button...");
auto button_callback = [&](const auto &state) {
logger.info("Button state: {}", state.active);
if (state.active) {
// Cycle through brightness levels: 25%, 50%, 75%, 100%
static int brightness_level = 0;
float brightness_values[] = {0.25f, 0.5f, 0.75f, 1.0f};
brightness_level = (brightness_level + 1) % 4;
float new_brightness = brightness_values[brightness_level];
tab5.brightness(new_brightness);
logger.info("Set brightness to {:.0f}%", new_brightness * 100);
}
};
if (!tab5.initialize_button(button_callback)) {
logger.warn("Failed to initialize button");
}
logger.info("Setting up LVGL UI...");
// set the background color to black
lv_obj_t *bg = lv_obj_create(lv_screen_active());
lv_obj_set_size(bg, tab5.display_width(), tab5.display_height());
lv_obj_set_style_bg_color(bg, lv_color_make(0, 0, 0), 0);
// add text in the center of the screen
lv_obj_t *label = lv_label_create(lv_screen_active());
static std::string label_text = "\n\n\n\nTouch the screen!";
lv_label_set_text(label, label_text.c_str());
lv_obj_align(label, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_LEFT, 0);
// Create style for line 0 (blue line, used for kalman filter)
static lv_style_t style_line0;
lv_style_init(&style_line0);
lv_style_set_line_width(&style_line0, 8);
lv_style_set_line_color(&style_line0, lv_palette_main(LV_PALETTE_BLUE));
lv_style_set_line_rounded(&style_line0, true);
// make a line for showing the direction of "down"
lv_obj_t *line0 = lv_line_create(lv_screen_active());
static lv_point_precise_t line_points0[] = {{0, 0},
{tab5.display_width(), tab5.display_height()}};
lv_line_set_points(line0, line_points0, 2);
lv_obj_add_style(line0, &style_line0, 0);
// Create style for line 1 (red line, used for madgwick filter)
static lv_style_t style_line1;
lv_style_init(&style_line1);
lv_style_set_line_width(&style_line1, 8);
lv_style_set_line_color(&style_line1, lv_palette_main(LV_PALETTE_RED));
lv_style_set_line_rounded(&style_line1, true);
// make a line for showing the direction of "down"
lv_obj_t *line1 = lv_line_create(lv_screen_active());
static lv_point_precise_t line_points1[] = {{0, 0},
{tab5.display_width(), tab5.display_height()}};
lv_line_set_points(line1, line_points1, 2);
lv_obj_add_style(line1, &style_line1, 0);
static auto rotate_display = [&]() {
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
clear_circles();
static auto rotation = LV_DISPLAY_ROTATION_0;
rotation = static_cast<lv_display_rotation_t>((static_cast<int>(rotation) + 1) % 4);
lv_display_t *disp = lv_display_get_default();
lv_disp_set_rotation(disp, rotation);
// update the size of the screen
lv_obj_set_size(bg, tab5.rotated_display_width(), tab5.rotated_display_height());
// refresh the display
};
// add a button in the top left which (when pressed) will rotate the display
// through 0, 90, 180, 270 degrees
lv_obj_t *btn = lv_btn_create(lv_screen_active());
lv_obj_set_size(btn, 50, 50);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_t *label_btn = lv_label_create(btn);
lv_label_set_text(label_btn, LV_SYMBOL_REFRESH);
// center the text in the button
lv_obj_align(label_btn, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(
btn, [](auto event) { rotate_display(); }, LV_EVENT_PRESSED, nullptr);
// disable scrolling on the screen (so that it doesn't behave weirdly when
// rotated and drawing with your finger)
lv_obj_set_scrollbar_mode(lv_screen_active(), LV_SCROLLBAR_MODE_OFF);
lv_obj_clear_flag(lv_screen_active(), LV_OBJ_FLAG_SCROLLABLE);
// start a simple thread to do the lv_task_handler every 16ms
logger.info("Starting LVGL task...");
espp::Task lv_task({.callback = [](std::mutex &m, std::condition_variable &cv) -> bool {
auto start_time = std::chrono::high_resolution_clock::now();
{
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
lv_task_handler();
}
std::unique_lock<std::mutex> lock(m);
cv.wait_until(lock, start_time + 16ms, []() { return false; });
return false;
},
.task_config = {
.name = "lv_task",
.stack_size_bytes = 10 * 1024,
.priority = 20,
.core_id = 1,
}});
lv_task.start();
// load the audio file (wav file bundled in memory)
size_t wav_size = 0;
size_t wav_sample_rate = 0;
if (!load_audio(wav_size, wav_sample_rate)) {
logger.error("Failed to load audio file!");
return;
}
logger.info("Loaded {} bytes of audio", wav_size);
logger.info("Setting audio sample rate to {} Hz", wav_sample_rate);
tab5.audio_sample_rate(wav_sample_rate);
// unmute the audio and set the volume to 60%
tab5.mute(false);
tab5.volume(60.0f);
// set the brightness to 75%
tab5.brightness(75.0f);
// make a task to read out various data such as IMU, battery monitoring, etc.
// and print it to screen
logger.info("Starting data display task...");
espp::Task imu_task(
{.callback = [&](std::mutex &m, std::condition_variable &cv) -> bool {
// sleep first in case we don't get IMU data and need to exit early
{
std::unique_lock<std::mutex> lock(m);
cv.wait_for(lock, 20ms);
}
static auto &tab5 = espp::M5StackTab5::get();
static auto imu = tab5.imu();
//////////////////////////////////////////////////////////////////////////
// Update the Date/Time from the RTC
//////////////////////////////////////////////////////////////////////////
std::tm rtc_time;
std::string rtc_text = "";
if (tab5.get_rtc_time(rtc_time)) {
rtc_text = fmt::format("\n{:%Y-%m-%d %H:%M:%S}\n", rtc_time);
}
//////////////////////////////////////////////////////////////////////////
// Update the battery status
//////////////////////////////////////////////////////////////////////////
auto battery_status = tab5.read_battery_status();
std::string battery_text =
fmt::format("\nBattery: {:0.2f} V, {:0.1f} mA, {:0.1f} %, Charging: {}\n",
battery_status.voltage_v, battery_status.current_ma,
battery_status.charge_percent, battery_status.is_charging ? "Yes" : "No");
auto now = esp_timer_get_time(); // time in microseconds
static auto t0 = now;
auto t1 = now;
float dt = (t1 - t0) / 1'000'000.0f; // convert us to s
t0 = t1;
//////////////////////////////////////////////////////////////////////////
// Update the IMU data
//////////////////////////////////////////////////////////////////////////
std::error_code ec;
// update the imu data
if (!imu->update(dt, ec)) {
return false;
}
// get accel
auto accel = imu->get_accelerometer();
auto gyro = imu->get_gyroscope();
auto temp = imu->get_temperature();
auto orientation = imu->get_orientation();
auto gravity_vector = imu->get_gravity_vector();
// invert the axes
gravity_vector.y = -gravity_vector.y;
gravity_vector.x = -gravity_vector.x;
// now update the gravity vector line to show the direction of "down"
// taking into account the configured rotation of the display
auto rotation = lv_display_get_rotation(lv_display_get_default());
if (rotation == LV_DISPLAY_ROTATION_90) {
std::swap(gravity_vector.x, gravity_vector.y);
gravity_vector.x = -gravity_vector.x;
} else if (rotation == LV_DISPLAY_ROTATION_180) {
gravity_vector.x = -gravity_vector.x;
gravity_vector.y = -gravity_vector.y;
} else if (rotation == LV_DISPLAY_ROTATION_270) {
std::swap(gravity_vector.x, gravity_vector.y);
gravity_vector.y = -gravity_vector.y;
}
// separator for imu
std::string imu_text = "\nIMU Data:\n";
imu_text += fmt::format("Accel: {:02.2f} {:02.2f} {:02.2f}\n", accel.x, accel.y, accel.z);
imu_text += fmt::format("Gyro: {:03.2f} {:03.2f} {:03.2f}\n", espp::deg_to_rad(gyro.x),
espp::deg_to_rad(gyro.y), espp::deg_to_rad(gyro.z));
imu_text += fmt::format("Angle: {:03.2f} {:03.2f}\n", espp::rad_to_deg(orientation.roll),
espp::rad_to_deg(orientation.pitch));
imu_text += fmt::format("Temp: {:02.1f} C\n", temp);
// use the pitch to to draw a line on the screen indiating the
// direction from the center of the screen to "down"
int x0 = tab5.rotated_display_width() / 2;
int y0 = tab5.rotated_display_height() / 2;
int x1 = x0 + 50 * gravity_vector.x;
int y1 = y0 + 50 * gravity_vector.y;
static lv_point_precise_t line_points0[] = {{x0, y0}, {x1, y1}};
line_points0[0].x = x0;
line_points0[0].y = y0;
line_points0[1].x = x1;
line_points0[1].y = y1;
// Now show the madgwick filter
auto madgwick_orientation = madgwick_filter_fn(dt, accel, gyro);
float roll = madgwick_orientation.roll;
float pitch = madgwick_orientation.pitch;
[[maybe_unused]] float yaw = madgwick_orientation.yaw;
float vx = sin(pitch);
float vy = -cos(pitch) * sin(roll);
[[maybe_unused]] float vz = -cos(pitch) * cos(roll);
// invert the axes
vx = -vx;
vy = -vy;
// now update the line to show the direction of "down" based on the
// configured rotation of the display
if (rotation == LV_DISPLAY_ROTATION_90) {
std::swap(vx, vy);
vx = -vx;
} else if (rotation == LV_DISPLAY_ROTATION_180) {
vx = -vx;
vy = -vy;
} else if (rotation == LV_DISPLAY_ROTATION_270) {
std::swap(vx, vy);
vy = -vy;
}
x1 = x0 + 50 * vx;
y1 = y0 + 50 * vy;
static lv_point_precise_t line_points1[] = {{x0, y0}, {x1, y1}};
line_points1[0].x = x0;
line_points1[0].y = y0;
line_points1[1].x = x1;
line_points1[1].y = y1;
std::string text = fmt::format("{}\n\n\n\n\n", label_text);
text += battery_text;
text += rtc_text;
text += imu_text;
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
lv_label_set_text(label, text.c_str());
lv_line_set_points(line0, line_points0, 2);
lv_line_set_points(line1, line_points1, 2);
return false;
},
.task_config = {
.name = "Data Display Task",
.stack_size_bytes = 6 * 1024,
.priority = 10,
.core_id = 1,
}});
imu_task.start();
// loop forever
while (true) {
std::this_thread::sleep_for(1s);
}
//! [m5stack tab5 example]
}
static void draw_circle(int x0, int y0, int radius) {
lv_obj_t *circle = lv_obj_create(lv_scr_act());
lv_obj_set_size(circle, radius * 2, radius * 2);
lv_obj_set_pos(circle, x0 - radius, y0 - radius);
lv_obj_set_style_radius(circle, radius, 0);
lv_obj_set_style_bg_opa(circle, LV_OPA_50, 0);
lv_obj_set_style_border_width(circle, 0, 0);
lv_obj_set_style_bg_color(circle, lv_color_hex(0xFF0000), 0); // Red color
circles.push_back(circle);
// Limit the number of circles to prevent memory issues
if (circles.size() > MAX_CIRCLES) {
lv_obj_del(circles.front());
circles.pop_front();
}
}
static void clear_circles() {
for (auto circle : circles) {
lv_obj_del(circle);
}
circles.clear();
}
static bool load_audio(size_t &out_size, size_t &out_sample_rate) {
// if the audio_bytes vector is already populated, return the size
if (audio_bytes.size() > 0) {
return true;
}
// load the audio data. these are configured in the CMakeLists.txt file
// cppcheck-suppress syntaxError
extern const uint8_t click_wav_start[] asm("_binary_click_wav_start");
// cppcheck-suppress syntaxError
extern const uint8_t click_wav_end[] asm("_binary_click_wav_end");
audio_bytes = std::vector<uint8_t>(click_wav_start, click_wav_end);
// ensure we have at least a wav header
if (audio_bytes.size() < 44) {
audio_bytes.clear();
return false;
}
// get the sample rate from the wav header (bytes 24-27)
uint32_t sample_rate = *(reinterpret_cast<const uint32_t *>(&audio_bytes[24]));
// set the audio sample rate accordingly
// decode the wav file header (first 44 bytes) and remove it
if (audio_bytes.size() > 44) {
audio_bytes.erase(audio_bytes.begin(), audio_bytes.begin() + 44);
}
out_size = audio_bytes.size();
out_sample_rate = sample_rate;
return true;
}
static void play_click(espp::M5StackTab5 &tab5) {
if (audio_bytes.size() > 0) {
tab5.play_audio(audio_bytes);
}
}