m5unified-sys 0.3.0

Raw Rust bindings for M5Unified via a C ABI shim.
Documentation
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(clippy::missing_safety_doc)]

//! Raw bindings for a small C ABI shim over M5Unified.
//!
//! This crate intentionally does not bind M5Unified's C++ classes directly.
//! Instead, `native/m5u_shim.cpp` exposes stable `extern "C"` functions that
//! Rust can call from the higher-level `m5unified` crate. On non-ESP-IDF host
//! targets these functions are stubbed so the safe wrapper and translated
//! samples can be checked in CI without hardware.
//!
//! Firmware projects targeting ESP-IDF must provide the native shim component
//! from this crate's `native/` directory in their ESP-IDF component graph. The
//! host stubs are compile-time conveniences only and do not simulate M5Stack
//! hardware behavior.

use core::ffi::{c_char, c_float, c_int, c_void};

pub type m5u_log_callback_t = Option<unsafe extern "C" fn(c_int, bool, *const c_char, *mut c_void)>;

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct m5u_sd_spi_config_t {
    pub pin_sclk: c_int,
    pub pin_mosi: c_int,
    pub pin_miso: c_int,
    pub pin_cs: c_int,
    pub host_id: c_int,
    pub frequency_khz: u32,
    pub max_files: c_int,
    pub format_if_mount_failed: u8,
}

impl Default for m5u_sd_spi_config_t {
    fn default() -> Self {
        Self {
            pin_sclk: -1,
            pin_mosi: -1,
            pin_miso: -1,
            pin_cs: -1,
            host_id: -1,
            frequency_khz: 20_000,
            max_files: 5,
            format_if_mount_failed: 0,
        }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct m5u_config_t {
    pub serial_baudrate: u32,
    pub external_speaker_value: u8,
    pub external_display_value: u16,
    pub clear_display: u8,
    pub output_power: u8,
    pub pmic_button: u8,
    pub internal_imu: u8,
    pub internal_rtc: u8,
    pub internal_mic: u8,
    pub internal_spk: u8,
    pub external_imu: u8,
    pub external_rtc: u8,
    pub disable_rtc_irq: u8,
    pub led_brightness: u8,
    pub fallback_board: c_int,
}

impl Default for m5u_config_t {
    fn default() -> Self {
        Self {
            serial_baudrate: 0,
            external_speaker_value: 0x00,
            external_display_value: 0xFFFF,
            clear_display: 1,
            output_power: 1,
            pmic_button: 1,
            internal_imu: 1,
            internal_rtc: 1,
            internal_mic: 1,
            internal_spk: 1,
            external_imu: 0,
            external_rtc: 0,
            disable_rtc_irq: 1,
            led_brightness: 0,
            fallback_board: -1,
        }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct m5u_mic_config_t {
    pub pin_data_in: c_int,
    pub pin_bck: c_int,
    pub pin_mck: c_int,
    pub pin_ws: c_int,
    pub sample_rate: u32,
    pub left_channel: u8,
    pub stereo: u8,
    pub over_sampling: u8,
    pub magnification: u8,
    pub noise_filter_level: u8,
    pub use_adc: u8,
    pub dma_buf_len: usize,
    pub dma_buf_count: usize,
    pub task_priority: u8,
    pub task_pinned_core: u8,
    pub i2s_port: c_int,
}

impl Default for m5u_mic_config_t {
    fn default() -> Self {
        Self {
            pin_data_in: -1,
            pin_bck: -1,
            pin_mck: -1,
            pin_ws: -1,
            sample_rate: 16_000,
            left_channel: 0,
            stereo: 0,
            over_sampling: 2,
            magnification: 16,
            noise_filter_level: 0,
            use_adc: 0,
            dma_buf_len: 128,
            dma_buf_count: 8,
            task_priority: 2,
            task_pinned_core: u8::MAX,
            i2s_port: 0,
        }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct m5u_speaker_config_t {
    pub pin_data_out: c_int,
    pub pin_bck: c_int,
    pub pin_mck: c_int,
    pub pin_ws: c_int,
    pub sample_rate: u32,
    pub stereo: u8,
    pub buzzer: u8,
    pub use_dac: u8,
    pub dac_zero_level: u8,
    pub magnification: u8,
    pub dma_buf_len: usize,
    pub dma_buf_count: usize,
    pub task_priority: u8,
    pub task_pinned_core: u8,
    pub i2s_port: c_int,
}

impl Default for m5u_speaker_config_t {
    fn default() -> Self {
        Self {
            pin_data_out: -1,
            pin_bck: -1,
            pin_mck: -1,
            pin_ws: -1,
            sample_rate: 48_000,
            stereo: 0,
            buzzer: 0,
            use_dac: 0,
            dac_zero_level: 0,
            magnification: 16,
            dma_buf_len: 256,
            dma_buf_count: 8,
            task_priority: 2,
            task_pinned_core: u8::MAX,
            i2s_port: 0,
        }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct m5u_touch_detail_t {
    pub x: c_int,
    pub y: c_int,
    pub prev_x: c_int,
    pub prev_y: c_int,
    pub is_pressed: bool,
    pub was_pressed: bool,
    pub was_released: bool,
    pub was_clicked: bool,
    pub was_hold: bool,
    pub is_holding: bool,
    pub click_count: c_int,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct m5u_rtc_datetime_t {
    pub year: c_int,
    pub month: c_int,
    pub day: c_int,
    pub weekday: c_int,
    pub hour: c_int,
    pub minute: c_int,
    pub second: c_int,
}

impl Default for m5u_rtc_datetime_t {
    fn default() -> Self {
        Self {
            year: 2026,
            month: 1,
            day: 1,
            weekday: 4,
            hour: 0,
            minute: 0,
            second: 0,
        }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct m5u_imu_data_t {
    pub usec: u32,
    pub accel_x: f32,
    pub accel_y: f32,
    pub accel_z: f32,
    pub gyro_x: f32,
    pub gyro_y: f32,
    pub gyro_z: f32,
    pub mag_x: f32,
    pub mag_y: f32,
    pub mag_z: f32,
}

#[cfg(target_os = "espidf")]
extern "C" {
    pub fn m5u_begin() -> bool;
    pub fn m5u_begin_with_config(config: *const m5u_config_t) -> bool;
    pub fn m5u_update();
    pub fn m5u_delay_ms(ms: u32);
    pub fn m5u_get_board() -> c_int;
    pub fn m5u_get_pin(name: c_int) -> c_int;
    pub fn m5u_set_primary_display_index(index: usize) -> bool;
    pub fn m5u_set_primary_display_type(kind: c_int) -> bool;
    pub fn m5u_set_log_display_index(index: usize);
    pub fn m5u_set_log_display_type(kind: c_int);
    pub fn m5u_set_touch_button_height(pixel: u16);
    pub fn m5u_set_touch_button_height_by_ratio(ratio: u8);
    pub fn m5u_get_touch_button_height() -> u16;

    pub fn m5u_display_width() -> c_int;
    pub fn m5u_display_height() -> c_int;
    pub fn m5u_display_fill_screen(color: u16);
    pub fn m5u_display_set_cursor(x: c_int, y: c_int);
    pub fn m5u_display_set_text_size(size: c_int);
    pub fn m5u_display_set_text_color(fg: u16, bg: u16);
    pub fn m5u_display_print(text: *const c_char);
    pub fn m5u_display_println(text: *const c_char);
    pub fn m5u_display_draw_line(x0: c_int, y0: c_int, x1: c_int, y1: c_int, color: u16);
    pub fn m5u_display_draw_rect(x: c_int, y: c_int, w: c_int, h: c_int, color: u16);
    pub fn m5u_display_fill_rect(x: c_int, y: c_int, w: c_int, h: c_int, color: u16);
    pub fn m5u_display_draw_circle(x: c_int, y: c_int, r: c_int, color: u16);
    pub fn m5u_display_fill_circle(x: c_int, y: c_int, r: c_int, color: u16);
    pub fn m5u_display_set_rotation(rotation: c_int);

    pub fn m5u_btn_a_is_pressed() -> bool;
    pub fn m5u_btn_a_was_pressed() -> bool;
    pub fn m5u_btn_a_was_released() -> bool;
    pub fn m5u_btn_b_is_pressed() -> bool;
    pub fn m5u_btn_b_was_pressed() -> bool;
    pub fn m5u_btn_b_was_released() -> bool;
    pub fn m5u_btn_c_is_pressed() -> bool;
    pub fn m5u_btn_c_was_pressed() -> bool;
    pub fn m5u_btn_c_was_released() -> bool;

    pub fn m5u_mic_begin() -> bool;
    pub fn m5u_mic_record_i16(buffer: *mut i16, samples: usize) -> bool;
    pub fn m5u_speaker_begin() -> bool;
    pub fn m5u_speaker_set_volume(volume: u8);
    pub fn m5u_speaker_tone(frequency_hz: u32, duration_ms: u32) -> bool;
    pub fn m5u_speaker_play_i16(samples: *const i16, len: usize, sample_rate_hz: u32) -> bool;

    pub fn m5u_imu_begin() -> bool;
    pub fn m5u_imu_get_accel(x: *mut f32, y: *mut f32, z: *mut f32) -> bool;
    pub fn m5u_imu_get_gyro(x: *mut f32, y: *mut f32, z: *mut f32) -> bool;
    pub fn m5u_imu_get_mag(x: *mut f32, y: *mut f32, z: *mut f32) -> bool;
    pub fn m5u_imu_get_data(out: *mut m5u_imu_data_t) -> bool;
    pub fn m5u_imu_get_temp_c(temp: *mut f32) -> bool;

    pub fn m5u_touch_count() -> c_int;
    pub fn m5u_touch_get(index: c_int, x: *mut c_int, y: *mut c_int) -> bool;

    pub fn m5u_rtc_is_enabled() -> bool;
    pub fn m5u_rtc_get_volt_low() -> bool;
    pub fn m5u_rtc_get_datetime(
        year: *mut c_int,
        month: *mut c_int,
        day: *mut c_int,
        hour: *mut c_int,
        minute: *mut c_int,
        second: *mut c_int,
    ) -> bool;
    pub fn m5u_rtc_get_datetime_detail(out: *mut m5u_rtc_datetime_t) -> bool;
    pub fn m5u_rtc_set_datetime(
        year: c_int,
        month: c_int,
        day: c_int,
        hour: c_int,
        minute: c_int,
        second: c_int,
    ) -> bool;
    pub fn m5u_rtc_set_datetime_detail(datetime: *const m5u_rtc_datetime_t) -> bool;

    pub fn m5u_battery_level() -> c_int;
    pub fn m5u_battery_voltage_mv() -> c_int;
    pub fn m5u_power_get_type() -> c_int;
    pub fn m5u_power_get_charge_state() -> c_int;
    pub fn m5u_power_is_charging() -> bool;
    pub fn m5u_power_set_led(brightness: u8);
    pub fn m5u_power_set_ext_output(enable: bool, port_mask: u16);
    pub fn m5u_power_get_ext_output() -> bool;
    pub fn m5u_power_set_usb_output(enable: bool);
    pub fn m5u_power_get_usb_output() -> bool;
    pub fn m5u_power_set_battery_charge(enable: bool);
    pub fn m5u_power_set_charge_current(max_ma: u16);
    pub fn m5u_power_set_charge_voltage(max_mv: u16);
    pub fn m5u_power_get_vbus_voltage_mv() -> c_int;
    pub fn m5u_power_get_battery_current_ma() -> c_int;
    pub fn m5u_power_get_key_state() -> u8;
    pub fn m5u_power_set_vibration(level: u8);

    pub fn m5u_display_get_rotation() -> c_int;
    pub fn m5u_display_set_brightness(brightness: u8);
    pub fn m5u_display_set_epd_fastest();
    pub fn m5u_display_set_epd_mode(mode: c_int);
    pub fn m5u_display_set_text_scroll(scroll: bool);
    pub fn m5u_display_set_font(font: c_int) -> bool;
    pub fn m5u_display_start_write();
    pub fn m5u_display_end_write();
    pub fn m5u_display_display();
    pub fn m5u_display_display_busy() -> bool;
    pub fn m5u_display_wait_display();
    pub fn m5u_display_get_cursor_y() -> c_int;
    pub fn m5u_display_font_height() -> c_int;
    pub fn m5u_display_get_base_color() -> u16;
    pub fn m5u_display_set_color(color: u16);
    pub fn m5u_display_set_text_wrap(wrap_x: bool, wrap_y: bool);
    pub fn m5u_display_set_text_datum(datum: c_int);
    pub fn m5u_display_draw_string(text: *const c_char, x: c_int, y: c_int) -> c_int;
    pub fn m5u_display_write_pixel(x: c_int, y: c_int, color: u16);
    pub fn m5u_display_write_fast_vline(x: c_int, y: c_int, h: c_int, color: u16);
    pub fn m5u_display_set_clip_rect(x: c_int, y: c_int, w: c_int, h: c_int);
    pub fn m5u_display_clear_clip_rect();
    pub fn m5u_display_color888(r: u8, g: u8, b: u8) -> u16;
    pub fn m5u_display_count() -> c_int;
    pub fn m5u_display_index_for_kind(kind: c_int) -> c_int;
    pub fn m5u_display_width_at(index: c_int) -> c_int;
    pub fn m5u_display_height_at(index: c_int) -> c_int;
    pub fn m5u_display_set_text_size_at(index: c_int, size: c_int);
    pub fn m5u_display_start_write_at(index: c_int);
    pub fn m5u_display_end_write_at(index: c_int);
    pub fn m5u_display_print_at(index: c_int, text: *const c_char);
    pub fn m5u_display_println_at(index: c_int, text: *const c_char);
    pub fn m5u_display_draw_string_at(
        index: c_int,
        text: *const c_char,
        x: c_int,
        y: c_int,
    ) -> c_int;
    pub fn m5u_display_fill_rect_at(
        index: c_int,
        x: c_int,
        y: c_int,
        w: c_int,
        h: c_int,
        color: u16,
    );
    pub fn m5u_display_fill_circle_at(index: c_int, x: c_int, y: c_int, r: c_int, color: u16);
    pub fn m5u_display_write_pixel_at(index: c_int, x: c_int, y: c_int, color: u16);
    pub fn m5u_display_draw_pixel_at(index: c_int, x: c_int, y: c_int, color: u16);

    pub fn m5u_button_is_pressed(button: c_int) -> bool;
    pub fn m5u_button_was_pressed(button: c_int) -> bool;
    pub fn m5u_button_was_released(button: c_int) -> bool;
    pub fn m5u_button_was_clicked(button: c_int) -> bool;
    pub fn m5u_button_was_hold(button: c_int) -> bool;
    pub fn m5u_button_is_holding(button: c_int) -> bool;
    pub fn m5u_button_was_decide_click_count(button: c_int) -> bool;
    pub fn m5u_button_get_click_count(button: c_int) -> c_int;

    pub fn m5u_mic_is_enabled() -> bool;
    pub fn m5u_mic_is_recording() -> bool;
    pub fn m5u_mic_end();
    pub fn m5u_mic_record_i16_at(buffer: *mut i16, samples: usize, sample_rate_hz: u32) -> bool;
    pub fn m5u_mic_get_config(out: *mut m5u_mic_config_t) -> bool;
    pub fn m5u_mic_set_config(config: *const m5u_mic_config_t) -> bool;
    pub fn m5u_mic_get_noise_filter_level() -> c_int;
    pub fn m5u_mic_set_noise_filter_level(level: c_int) -> bool;

    pub fn m5u_speaker_is_enabled() -> bool;
    pub fn m5u_speaker_end();
    pub fn m5u_speaker_get_volume() -> u8;
    pub fn m5u_speaker_get_config(out: *mut m5u_speaker_config_t) -> bool;
    pub fn m5u_speaker_set_config(config: *const m5u_speaker_config_t) -> bool;
    pub fn m5u_speaker_tone_ex(frequency_hz: c_float, duration_ms: u32, channel: c_int) -> bool;
    pub fn m5u_speaker_play_u8(samples: *const u8, len: usize, sample_rate_hz: u32) -> bool;
    pub fn m5u_speaker_play_wav(data: *const u8, len: usize) -> bool;
    pub fn m5u_speaker_is_playing(channel: c_int) -> bool;
    pub fn m5u_speaker_stop(channel: c_int);
    pub fn m5u_speaker_get_channel_volume(channel: c_int) -> u8;
    pub fn m5u_speaker_set_channel_volume(channel: c_int, volume: u8);
    pub fn m5u_speaker_set_all_channel_volume(volume: u8);

    pub fn m5u_imu_is_enabled() -> bool;
    pub fn m5u_imu_get_type() -> c_int;
    pub fn m5u_imu_update() -> bool;
    pub fn m5u_imu_load_offset_from_nvs() -> bool;
    pub fn m5u_imu_save_offset_to_nvs() -> bool;
    pub fn m5u_imu_get_offset_data(index: c_int) -> c_float;
    pub fn m5u_imu_set_calibration(x: c_float, y: c_float, z: c_float);

    pub fn m5u_touch_get_detail(index: c_int, out: *mut m5u_touch_detail_t) -> bool;
    pub fn m5u_rtc_set_system_time_from_rtc();
    pub fn m5u_rtc_set_timer_irq(timer_msec: u32) -> u32;
    pub fn m5u_rtc_set_alarm_irq_after_seconds(after_seconds: c_int) -> c_int;
    pub fn m5u_rtc_set_alarm_irq_datetime(datetime: *const m5u_rtc_datetime_t) -> c_int;
    pub fn m5u_rtc_get_irq_status() -> bool;
    pub fn m5u_rtc_clear_irq();
    pub fn m5u_rtc_disable_irq();

    pub fn m5u_power_axp2101_disable_irq(mask: u64) -> bool;
    pub fn m5u_power_axp2101_enable_irq(mask: u64) -> bool;
    pub fn m5u_power_axp2101_clear_irq_statuses() -> bool;
    pub fn m5u_power_axp2101_get_irq_statuses() -> u64;
    pub fn m5u_power_axp2101_is_bat_charger_under_temperature_irq() -> bool;
    pub fn m5u_power_axp2101_is_bat_charger_over_temperature_irq() -> bool;
    pub fn m5u_power_axp2101_is_vbus_insert_irq() -> bool;
    pub fn m5u_power_axp2101_is_vbus_remove_irq() -> bool;

    pub fn m5u_led_begin() -> bool;
    pub fn m5u_led_display();
    pub fn m5u_led_set_auto_display(enable: bool);
    pub fn m5u_led_count() -> usize;
    pub fn m5u_led_set_brightness(brightness: u8);
    pub fn m5u_led_set_color_rgb(index: usize, r: u8, g: u8, b: u8);
    pub fn m5u_led_set_all_color_rgb(r: u8, g: u8, b: u8);
    pub fn m5u_led_is_enabled() -> bool;

    pub fn m5u_log_print(text: *const c_char);
    pub fn m5u_log_println(text: *const c_char);
    pub fn m5u_log_level(level: c_int, text: *const c_char);
    pub fn m5u_log_set_callback(callback: m5u_log_callback_t, user_data: *mut c_void) -> bool;
    pub fn m5u_log_set_enable_color(target: c_int, enable: bool) -> bool;
    pub fn m5u_log_get_enable_color(target: c_int) -> bool;
    pub fn m5u_log_set_level(target: c_int, level: c_int) -> bool;
    pub fn m5u_log_get_level(target: c_int) -> c_int;
    pub fn m5u_log_set_suffix(target: c_int, suffix: *const c_char) -> bool;
    pub fn m5u_sd_begin() -> bool;
    pub fn m5u_sd_begin_spi(config: *const m5u_sd_spi_config_t) -> bool;
    pub fn m5u_sd_is_mounted() -> bool;
    pub fn m5u_sd_end();
}

#[cfg(not(target_os = "espidf"))]
mod host_stubs {
    use super::*;
    use core::ptr;

    pub unsafe fn m5u_begin() -> bool {
        true
    }
    pub unsafe fn m5u_begin_with_config(_config: *const m5u_config_t) -> bool {
        true
    }
    pub unsafe fn m5u_update() {}
    pub unsafe fn m5u_delay_ms(_ms: u32) {}
    pub unsafe fn m5u_get_board() -> c_int {
        0
    }
    pub unsafe fn m5u_get_pin(_name: c_int) -> c_int {
        -1
    }
    pub unsafe fn m5u_set_primary_display_index(index: usize) -> bool {
        index == 0
    }
    pub unsafe fn m5u_set_primary_display_type(_kind: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_set_log_display_index(_index: usize) {}
    pub unsafe fn m5u_set_log_display_type(_kind: c_int) {}
    pub unsafe fn m5u_set_touch_button_height(_pixel: u16) {}
    pub unsafe fn m5u_set_touch_button_height_by_ratio(_ratio: u8) {}
    pub unsafe fn m5u_get_touch_button_height() -> u16 {
        0
    }

    pub unsafe fn m5u_display_width() -> c_int {
        320
    }
    pub unsafe fn m5u_display_height() -> c_int {
        240
    }
    pub unsafe fn m5u_display_fill_screen(_color: u16) {}
    pub unsafe fn m5u_display_set_cursor(_x: c_int, _y: c_int) {}
    pub unsafe fn m5u_display_set_text_size(_size: c_int) {}
    pub unsafe fn m5u_display_set_text_color(_fg: u16, _bg: u16) {}
    pub unsafe fn m5u_display_print(_text: *const c_char) {}
    pub unsafe fn m5u_display_println(_text: *const c_char) {}
    pub unsafe fn m5u_display_draw_line(
        _x0: c_int,
        _y0: c_int,
        _x1: c_int,
        _y1: c_int,
        _color: u16,
    ) {
    }
    pub unsafe fn m5u_display_draw_rect(_x: c_int, _y: c_int, _w: c_int, _h: c_int, _color: u16) {}
    pub unsafe fn m5u_display_fill_rect(_x: c_int, _y: c_int, _w: c_int, _h: c_int, _color: u16) {}
    pub unsafe fn m5u_display_draw_circle(_x: c_int, _y: c_int, _r: c_int, _color: u16) {}
    pub unsafe fn m5u_display_fill_circle(_x: c_int, _y: c_int, _r: c_int, _color: u16) {}
    pub unsafe fn m5u_display_set_rotation(_rotation: c_int) {}

    pub unsafe fn m5u_btn_a_is_pressed() -> bool {
        false
    }
    pub unsafe fn m5u_btn_a_was_pressed() -> bool {
        false
    }
    pub unsafe fn m5u_btn_a_was_released() -> bool {
        false
    }
    pub unsafe fn m5u_btn_b_is_pressed() -> bool {
        false
    }
    pub unsafe fn m5u_btn_b_was_pressed() -> bool {
        false
    }
    pub unsafe fn m5u_btn_b_was_released() -> bool {
        false
    }
    pub unsafe fn m5u_btn_c_is_pressed() -> bool {
        false
    }
    pub unsafe fn m5u_btn_c_was_pressed() -> bool {
        false
    }
    pub unsafe fn m5u_btn_c_was_released() -> bool {
        false
    }

    pub unsafe fn m5u_mic_begin() -> bool {
        true
    }
    pub unsafe fn m5u_mic_record_i16(buffer: *mut i16, samples: usize) -> bool {
        if !buffer.is_null() {
            for i in 0..samples {
                ptr::write(buffer.add(i), 0);
            }
        }
        true
    }
    pub unsafe fn m5u_speaker_begin() -> bool {
        true
    }
    pub unsafe fn m5u_speaker_set_volume(_volume: u8) {}
    pub unsafe fn m5u_speaker_tone(_frequency_hz: u32, _duration_ms: u32) -> bool {
        true
    }
    pub unsafe fn m5u_speaker_play_i16(
        _samples: *const i16,
        _len: usize,
        _sample_rate_hz: u32,
    ) -> bool {
        true
    }

    pub unsafe fn m5u_imu_begin() -> bool {
        true
    }
    pub unsafe fn m5u_imu_get_accel(x: *mut f32, y: *mut f32, z: *mut f32) -> bool {
        if !x.is_null() {
            *x = 0.0;
        }
        if !y.is_null() {
            *y = 0.0;
        }
        if !z.is_null() {
            *z = 1.0;
        }
        true
    }
    pub unsafe fn m5u_imu_get_gyro(x: *mut f32, y: *mut f32, z: *mut f32) -> bool {
        if !x.is_null() {
            *x = 0.0;
        }
        if !y.is_null() {
            *y = 0.0;
        }
        if !z.is_null() {
            *z = 0.0;
        }
        true
    }
    pub unsafe fn m5u_imu_get_mag(x: *mut f32, y: *mut f32, z: *mut f32) -> bool {
        if !x.is_null() {
            *x = 0.0;
        }
        if !y.is_null() {
            *y = 0.0;
        }
        if !z.is_null() {
            *z = 0.0;
        }
        true
    }
    pub unsafe fn m5u_imu_get_data(out: *mut m5u_imu_data_t) -> bool {
        if !out.is_null() {
            *out = m5u_imu_data_t {
                accel_z: 1.0,
                ..m5u_imu_data_t::default()
            };
        }
        true
    }
    pub unsafe fn m5u_imu_get_temp_c(temp: *mut f32) -> bool {
        if !temp.is_null() {
            *temp = 25.0;
        }
        true
    }

    pub unsafe fn m5u_touch_count() -> c_int {
        0
    }
    pub unsafe fn m5u_touch_get(_index: c_int, _x: *mut c_int, _y: *mut c_int) -> bool {
        false
    }

    pub unsafe fn m5u_rtc_is_enabled() -> bool {
        true
    }
    pub unsafe fn m5u_rtc_get_volt_low() -> bool {
        false
    }
    pub unsafe fn m5u_rtc_get_datetime(
        year: *mut c_int,
        month: *mut c_int,
        day: *mut c_int,
        hour: *mut c_int,
        minute: *mut c_int,
        second: *mut c_int,
    ) -> bool {
        if !year.is_null() {
            *year = 2026;
        }
        if !month.is_null() {
            *month = 1;
        }
        if !day.is_null() {
            *day = 1;
        }
        if !hour.is_null() {
            *hour = 0;
        }
        if !minute.is_null() {
            *minute = 0;
        }
        if !second.is_null() {
            *second = 0;
        }
        true
    }
    pub unsafe fn m5u_rtc_get_datetime_detail(out: *mut m5u_rtc_datetime_t) -> bool {
        if !out.is_null() {
            *out = m5u_rtc_datetime_t::default();
        }
        true
    }
    pub unsafe fn m5u_rtc_set_datetime(
        _year: c_int,
        _month: c_int,
        _day: c_int,
        _hour: c_int,
        _minute: c_int,
        _second: c_int,
    ) -> bool {
        true
    }
    pub unsafe fn m5u_rtc_set_datetime_detail(datetime: *const m5u_rtc_datetime_t) -> bool {
        !datetime.is_null()
    }

    pub unsafe fn m5u_battery_level() -> c_int {
        100
    }
    pub unsafe fn m5u_battery_voltage_mv() -> c_int {
        4200
    }
    pub unsafe fn m5u_power_get_type() -> c_int {
        0
    }
    pub unsafe fn m5u_power_get_charge_state() -> c_int {
        2
    }
    pub unsafe fn m5u_power_is_charging() -> bool {
        false
    }
    pub unsafe fn m5u_power_set_led(_brightness: u8) {}
    pub unsafe fn m5u_power_set_ext_output(_enable: bool, _port_mask: u16) {}
    pub unsafe fn m5u_power_get_ext_output() -> bool {
        false
    }
    pub unsafe fn m5u_power_set_usb_output(_enable: bool) {}
    pub unsafe fn m5u_power_get_usb_output() -> bool {
        false
    }
    pub unsafe fn m5u_power_set_battery_charge(_enable: bool) {}
    pub unsafe fn m5u_power_set_charge_current(_max_ma: u16) {}
    pub unsafe fn m5u_power_set_charge_voltage(_max_mv: u16) {}
    pub unsafe fn m5u_power_get_vbus_voltage_mv() -> c_int {
        -1
    }
    pub unsafe fn m5u_power_get_battery_current_ma() -> c_int {
        0
    }
    pub unsafe fn m5u_power_get_key_state() -> u8 {
        0
    }
    pub unsafe fn m5u_power_set_vibration(_level: u8) {}

    pub unsafe fn m5u_display_get_rotation() -> c_int {
        0
    }
    pub unsafe fn m5u_display_set_brightness(_brightness: u8) {}
    pub unsafe fn m5u_display_set_epd_fastest() {}
    pub unsafe fn m5u_display_set_epd_mode(_mode: c_int) {}
    pub unsafe fn m5u_display_set_text_scroll(_scroll: bool) {}
    pub unsafe fn m5u_display_set_font(font: c_int) -> bool {
        (0..=3).contains(&font)
    }
    pub unsafe fn m5u_display_start_write() {}
    pub unsafe fn m5u_display_end_write() {}
    pub unsafe fn m5u_display_display() {}
    pub unsafe fn m5u_display_display_busy() -> bool {
        false
    }
    pub unsafe fn m5u_display_wait_display() {}
    pub unsafe fn m5u_display_get_cursor_y() -> c_int {
        0
    }
    pub unsafe fn m5u_display_font_height() -> c_int {
        16
    }
    pub unsafe fn m5u_display_get_base_color() -> u16 {
        0
    }
    pub unsafe fn m5u_display_set_color(_color: u16) {}
    pub unsafe fn m5u_display_set_text_wrap(_wrap_x: bool, _wrap_y: bool) {}
    pub unsafe fn m5u_display_set_text_datum(_datum: c_int) {}
    pub unsafe fn m5u_display_draw_string(_text: *const c_char, _x: c_int, _y: c_int) -> c_int {
        0
    }
    pub unsafe fn m5u_display_write_pixel(_x: c_int, _y: c_int, _color: u16) {}
    pub unsafe fn m5u_display_write_fast_vline(_x: c_int, _y: c_int, _h: c_int, _color: u16) {}
    pub unsafe fn m5u_display_set_clip_rect(_x: c_int, _y: c_int, _w: c_int, _h: c_int) {}
    pub unsafe fn m5u_display_clear_clip_rect() {}
    pub unsafe fn m5u_display_color888(r: u8, g: u8, b: u8) -> u16 {
        ((u16::from(r & 0xF8)) << 8) | ((u16::from(g & 0xFC)) << 3) | u16::from(b >> 3)
    }
    pub unsafe fn m5u_display_count() -> c_int {
        1
    }
    pub unsafe fn m5u_display_index_for_kind(_kind: c_int) -> c_int {
        -1
    }
    pub unsafe fn m5u_display_width_at(_index: c_int) -> c_int {
        320
    }
    pub unsafe fn m5u_display_height_at(_index: c_int) -> c_int {
        240
    }
    pub unsafe fn m5u_display_set_text_size_at(_index: c_int, _size: c_int) {}
    pub unsafe fn m5u_display_start_write_at(_index: c_int) {}
    pub unsafe fn m5u_display_end_write_at(_index: c_int) {}
    pub unsafe fn m5u_display_print_at(_index: c_int, _text: *const c_char) {}
    pub unsafe fn m5u_display_println_at(_index: c_int, _text: *const c_char) {}
    pub unsafe fn m5u_display_draw_string_at(
        _index: c_int,
        _text: *const c_char,
        _x: c_int,
        _y: c_int,
    ) -> c_int {
        0
    }
    pub unsafe fn m5u_display_fill_rect_at(
        _index: c_int,
        _x: c_int,
        _y: c_int,
        _w: c_int,
        _h: c_int,
        _color: u16,
    ) {
    }
    pub unsafe fn m5u_display_fill_circle_at(
        _index: c_int,
        _x: c_int,
        _y: c_int,
        _r: c_int,
        _color: u16,
    ) {
    }
    pub unsafe fn m5u_display_write_pixel_at(_index: c_int, _x: c_int, _y: c_int, _color: u16) {}
    pub unsafe fn m5u_display_draw_pixel_at(_index: c_int, _x: c_int, _y: c_int, _color: u16) {}

    pub unsafe fn m5u_button_is_pressed(_button: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_button_was_pressed(_button: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_button_was_released(_button: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_button_was_clicked(_button: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_button_was_hold(_button: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_button_is_holding(_button: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_button_was_decide_click_count(_button: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_button_get_click_count(_button: c_int) -> c_int {
        0
    }

    pub unsafe fn m5u_mic_is_enabled() -> bool {
        true
    }
    pub unsafe fn m5u_mic_is_recording() -> bool {
        false
    }
    pub unsafe fn m5u_mic_end() {}
    pub unsafe fn m5u_mic_record_i16_at(
        buffer: *mut i16,
        samples: usize,
        _sample_rate_hz: u32,
    ) -> bool {
        m5u_mic_record_i16(buffer, samples)
    }
    pub unsafe fn m5u_mic_get_noise_filter_level() -> c_int {
        0
    }
    pub unsafe fn m5u_mic_set_noise_filter_level(_level: c_int) -> bool {
        true
    }
    pub unsafe fn m5u_mic_get_config(out: *mut m5u_mic_config_t) -> bool {
        if !out.is_null() {
            *out = m5u_mic_config_t::default();
        }
        true
    }
    pub unsafe fn m5u_mic_set_config(config: *const m5u_mic_config_t) -> bool {
        !config.is_null()
    }

    pub unsafe fn m5u_speaker_is_enabled() -> bool {
        true
    }
    pub unsafe fn m5u_speaker_end() {}
    pub unsafe fn m5u_speaker_get_volume() -> u8 {
        64
    }
    pub unsafe fn m5u_speaker_get_config(out: *mut m5u_speaker_config_t) -> bool {
        if !out.is_null() {
            *out = m5u_speaker_config_t::default();
        }
        true
    }
    pub unsafe fn m5u_speaker_set_config(config: *const m5u_speaker_config_t) -> bool {
        !config.is_null()
    }
    pub unsafe fn m5u_speaker_tone_ex(
        _frequency_hz: c_float,
        _duration_ms: u32,
        _channel: c_int,
    ) -> bool {
        true
    }
    pub unsafe fn m5u_speaker_play_u8(
        _samples: *const u8,
        _len: usize,
        _sample_rate_hz: u32,
    ) -> bool {
        true
    }
    pub unsafe fn m5u_speaker_play_wav(_data: *const u8, _len: usize) -> bool {
        true
    }
    pub unsafe fn m5u_speaker_is_playing(_channel: c_int) -> bool {
        false
    }
    pub unsafe fn m5u_speaker_stop(_channel: c_int) {}
    pub unsafe fn m5u_speaker_get_channel_volume(_channel: c_int) -> u8 {
        255
    }
    pub unsafe fn m5u_speaker_set_channel_volume(_channel: c_int, _volume: u8) {}
    pub unsafe fn m5u_speaker_set_all_channel_volume(_volume: u8) {}

    pub unsafe fn m5u_imu_is_enabled() -> bool {
        true
    }
    pub unsafe fn m5u_imu_get_type() -> c_int {
        0
    }
    pub unsafe fn m5u_imu_update() -> bool {
        true
    }
    pub unsafe fn m5u_imu_load_offset_from_nvs() -> bool {
        false
    }
    pub unsafe fn m5u_imu_save_offset_to_nvs() -> bool {
        false
    }
    pub unsafe fn m5u_imu_get_offset_data(_index: c_int) -> c_float {
        0.0
    }
    pub unsafe fn m5u_imu_set_calibration(_x: c_float, _y: c_float, _z: c_float) {}

    pub unsafe fn m5u_touch_get_detail(_index: c_int, out: *mut m5u_touch_detail_t) -> bool {
        if !out.is_null() {
            *out = m5u_touch_detail_t::default();
        }
        false
    }
    pub unsafe fn m5u_rtc_set_system_time_from_rtc() {}
    pub unsafe fn m5u_rtc_set_timer_irq(timer_msec: u32) -> u32 {
        timer_msec
    }
    pub unsafe fn m5u_rtc_set_alarm_irq_after_seconds(after_seconds: c_int) -> c_int {
        after_seconds
    }
    pub unsafe fn m5u_rtc_set_alarm_irq_datetime(datetime: *const m5u_rtc_datetime_t) -> c_int {
        if datetime.is_null() {
            -1
        } else {
            0
        }
    }
    pub unsafe fn m5u_rtc_get_irq_status() -> bool {
        false
    }
    pub unsafe fn m5u_rtc_clear_irq() {}
    pub unsafe fn m5u_rtc_disable_irq() {}

    pub unsafe fn m5u_power_axp2101_disable_irq(_mask: u64) -> bool {
        false
    }
    pub unsafe fn m5u_power_axp2101_enable_irq(_mask: u64) -> bool {
        false
    }
    pub unsafe fn m5u_power_axp2101_clear_irq_statuses() -> bool {
        false
    }
    pub unsafe fn m5u_power_axp2101_get_irq_statuses() -> u64 {
        0
    }
    pub unsafe fn m5u_power_axp2101_is_bat_charger_under_temperature_irq() -> bool {
        false
    }
    pub unsafe fn m5u_power_axp2101_is_bat_charger_over_temperature_irq() -> bool {
        false
    }
    pub unsafe fn m5u_power_axp2101_is_vbus_insert_irq() -> bool {
        false
    }
    pub unsafe fn m5u_power_axp2101_is_vbus_remove_irq() -> bool {
        false
    }

    pub unsafe fn m5u_led_begin() -> bool {
        false
    }
    pub unsafe fn m5u_led_display() {}
    pub unsafe fn m5u_led_set_auto_display(_enable: bool) {}
    pub unsafe fn m5u_led_count() -> usize {
        0
    }
    pub unsafe fn m5u_led_set_brightness(_brightness: u8) {}
    pub unsafe fn m5u_led_set_color_rgb(_index: usize, _r: u8, _g: u8, _b: u8) {}
    pub unsafe fn m5u_led_set_all_color_rgb(_r: u8, _g: u8, _b: u8) {}
    pub unsafe fn m5u_led_is_enabled() -> bool {
        false
    }

    pub unsafe fn m5u_log_print(_text: *const c_char) {}
    pub unsafe fn m5u_log_println(_text: *const c_char) {}
    pub unsafe fn m5u_log_level(_level: c_int, _text: *const c_char) {}
    pub unsafe fn m5u_log_set_callback(
        _callback: m5u_log_callback_t,
        _user_data: *mut c_void,
    ) -> bool {
        true
    }
    pub unsafe fn m5u_log_set_enable_color(target: c_int, _enable: bool) -> bool {
        (0..=2).contains(&target)
    }
    pub unsafe fn m5u_log_get_enable_color(target: c_int) -> bool {
        (0..=2).contains(&target)
    }
    pub unsafe fn m5u_log_set_level(target: c_int, level: c_int) -> bool {
        (0..=2).contains(&target) && (0..=5).contains(&level)
    }
    pub unsafe fn m5u_log_get_level(target: c_int) -> c_int {
        if (0..=2).contains(&target) {
            3
        } else {
            -1
        }
    }
    pub unsafe fn m5u_log_set_suffix(target: c_int, suffix: *const c_char) -> bool {
        (0..=2).contains(&target) && !suffix.is_null()
    }
    pub unsafe fn m5u_sd_begin() -> bool {
        false
    }
    pub unsafe fn m5u_sd_begin_spi(_config: *const m5u_sd_spi_config_t) -> bool {
        false
    }
    pub unsafe fn m5u_sd_is_mounted() -> bool {
        false
    }
    pub unsafe fn m5u_sd_end() {}
}

#[cfg(not(target_os = "espidf"))]
pub use host_stubs::*;