├── README.md ├── library.properties ├── src ├── M5Capsule.cpp └── M5Capsule.h ├── library.json └── examples └── Basic ├── button └── button.ino ├── buzzer └── buzzer.ino ├── wakeup └── wakeup.ino ├── mic └── mic.ino └── rtc └── rtc.ino /README.md: -------------------------------------------------------------------------------- 1 | 2 | # M5Capsule 3 | 4 | ## Basic library for M5Stack M5Capsule Board 5 | 6 | 7 | License 8 | ---------------- 9 | M5GFX : [MIT](https://github.com/m5stack/M5GFX/blob/master/LICENSE) 10 | M5Unified : [MIT](https://github.com/m5stack/M5Unified/blob/master/LICENSE) 11 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=M5Capsule 2 | version=1.0.1 3 | author=M5Stack 4 | maintainer=M5Stack 5 | sentence=Library for M5Stack M5Capsule Board 6 | paragraph=M5Stack, M5Capsule, See more on http://M5Stack.com 7 | category=Display 8 | url=https://github.com/m5stack/M5Capsule.git 9 | architectures=esp32 10 | includes=M5Capsule.h 11 | depends=M5Unified,M5GFX -------------------------------------------------------------------------------- /src/M5Capsule.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 3 | * 4 | *SPDX-License-Identifier: MIT 5 | */ 6 | 7 | #include "M5Capsule.h" 8 | 9 | using namespace m5; 10 | 11 | M5_CAPSULE M5Capsule; 12 | 13 | void M5_CAPSULE::begin() 14 | { 15 | M5.begin(); 16 | } 17 | 18 | void M5_CAPSULE::begin(m5::M5Unified::config_t cfg) 19 | { 20 | M5.begin(cfg); 21 | } 22 | 23 | void M5_CAPSULE::update(void) 24 | { 25 | M5.update(); 26 | } 27 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "M5Capsule", 3 | "description": "Library for M5Stack M5Capsule Board", 4 | "keywords": "M5GFX,M5Stack,M5Capsule", 5 | "authors": { 6 | "name": "M5Stack, Sean", 7 | "url": "http://www.m5stack.com" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/m5stack/M5Capsule.git" 12 | }, 13 | "version": "1.0.1", 14 | "frameworks": ["arduino"], 15 | "platforms": ["espressif32", "native"], 16 | "headers": "M5Capsule.h" 17 | } 18 | -------------------------------------------------------------------------------- /examples/Basic/button/button.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * @file button.ino 3 | * @author SeanKwok (shaoxiang@m5stack.com) 4 | * @brief M5Capsule Button Test 5 | * @version 0.1 6 | * @date 2023-10-08 7 | * 8 | * 9 | * @Hardwares: M5Capsule 10 | * @Platform Version: Arduino M5Stack Board Manager v2.0.7 11 | * @Dependent Library: 12 | * M5GFX: https://github.com/m5stack/M5GFX 13 | * M5Unified: https://github.com/m5stack/M5Unified 14 | */ 15 | 16 | #include "M5Capsule.h" 17 | 18 | void setup() { 19 | auto cfg = M5.config(); 20 | M5Capsule.begin(cfg); 21 | } 22 | 23 | void loop() { 24 | M5Capsule.update(); 25 | if (M5Capsule.BtnA.wasPressed()) { 26 | M5Capsule.Speaker.tone(10000, 20); 27 | } 28 | } -------------------------------------------------------------------------------- /examples/Basic/buzzer/buzzer.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * @file buzzer.ino 3 | * @author SeanKwok (shaoxiang@m5stack.com) 4 | * @brief M5Capsule Buzzer Test 5 | * @version 0.1 6 | * @date 2023-10-08 7 | * 8 | * 9 | * @Hardwares: M5Capsule 10 | * @Platform Version: Arduino M5Stack Board Manager v2.0.7 11 | * @Dependent Library: 12 | * M5GFX: https://github.com/m5stack/M5GFX 13 | * M5Unified: https://github.com/m5stack/M5Unified 14 | */ 15 | 16 | #include "M5Capsule.h" 17 | 18 | void setup() { 19 | auto cfg = M5.config(); 20 | M5Capsule.begin(cfg); 21 | } 22 | 23 | void loop() { 24 | M5Capsule.Speaker.tone(10000, 100); 25 | delay(1000); 26 | M5Capsule.Speaker.tone(4000, 20); 27 | delay(1000); 28 | } -------------------------------------------------------------------------------- /src/M5Capsule.h: -------------------------------------------------------------------------------- 1 | /* 2 | *SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 3 | * 4 | *SPDX-License-Identifier: MIT 5 | */ 6 | 7 | #ifndef M5CAPSULE_H 8 | #define M5CAPSULE_H 9 | 10 | #include "M5Unified.h" 11 | 12 | namespace m5 { 13 | class M5_CAPSULE { 14 | private: 15 | /* data */ 16 | 17 | public: 18 | void begin(); 19 | void begin(m5::M5Unified::config_t cfg); 20 | Power_Class &Power = M5.Power; 21 | RTC8563_Class &Rtc = M5.Rtc; 22 | Speaker_Class &Speaker = M5.Speaker; 23 | Mic_Class &Mic = M5.Mic; 24 | Button_Class &BtnA = M5.getButton(0); 25 | 26 | /// for internal I2C device 27 | I2C_Class &In_I2C = m5::In_I2C; 28 | 29 | /// for external I2C device (Port.A) 30 | I2C_Class &Ex_I2C = m5::Ex_I2C; 31 | void update(void); 32 | }; 33 | 34 | } // namespace m5 35 | 36 | extern m5::M5_CAPSULE M5Capsule; 37 | 38 | #endif -------------------------------------------------------------------------------- /examples/Basic/wakeup/wakeup.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * @file wakeup.ino 3 | * @author SeanKwok (shaoxiang@m5stack.com) 4 | * @brief M5Capsule Timer Wakeup Test 5 | * @version 0.1 6 | * @date 2023-10-08 7 | * 8 | * 9 | * @Hardwares: M5Capsule 10 | * @Platform Version: Arduino M5Stack Board Manager v2.0.7 11 | * @Dependent Library: 12 | * M5GFX: https://github.com/m5stack/M5GFX 13 | * M5Unified: https://github.com/m5stack/M5Unified 14 | */ 15 | 16 | #include 17 | 18 | void setup(void) { 19 | auto cfg = M5.config(); 20 | M5Capsule.begin(cfg); 21 | 22 | Serial.println("Press Btn to sleep"); 23 | Serial.println("After 5s Wakeup"); 24 | } 25 | 26 | void loop(void) { 27 | M5Capsule.update(); 28 | 29 | if (M5Capsule.BtnA.wasPressed()) { 30 | M5Capsule.Power.timerSleep(5); 31 | // M5Capsule.Power.timerSleep(const rtc_time_t& time); 32 | // M5Capsule.Power.timerSleep(const rtc_date_t& date, const rtc_time_t& 33 | // time); 34 | // M5Capsule.Power.powerOff(); shutdown 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /examples/Basic/mic/mic.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * @file mic.ino 3 | * @author SeanKwok (shaoxiang@m5stack.com) 4 | * @brief M5Capsule Microphone Test 5 | * @version 0.1 6 | * @date 2023-10-08 7 | * 8 | * 9 | * @Hardwares: M5Capsule 10 | * @Platform Version: Arduino M5Stack Board Manager v2.0.7 11 | * @Dependent Library: 12 | * M5GFX: https://github.com/m5stack/M5GFX 13 | * M5Unified: https://github.com/m5stack/M5Unified 14 | */ 15 | 16 | #include "M5Capsule.h" 17 | 18 | #define MIC_SAMPLE_RATE 16000 19 | #define MIC_DATA_PIN (41) 20 | #define MIC_CLOCK_PIN (40) 21 | 22 | static void init_microphone(void) { 23 | i2s_config_t i2s_config = { 24 | .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM), 25 | .sample_rate = MIC_SAMPLE_RATE, 26 | .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, 27 | .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, 28 | .communication_format = I2S_COMM_FORMAT_STAND_MSB, 29 | .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, 30 | .dma_buf_count = 4, 31 | .dma_buf_len = 512, 32 | .use_apll = 0, 33 | }; 34 | 35 | i2s_pin_config_t pin_config = { 36 | .mck_io_num = I2S_PIN_NO_CHANGE, 37 | .bck_io_num = I2S_PIN_NO_CHANGE, 38 | .ws_io_num = MIC_CLOCK_PIN, 39 | .data_out_num = I2S_PIN_NO_CHANGE, 40 | .data_in_num = MIC_DATA_PIN, 41 | }; 42 | 43 | // Call driver installation function before any I2S R/W operation. 44 | ESP_ERROR_CHECK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL)); 45 | ESP_ERROR_CHECK(i2s_set_pin(I2S_NUM_0, &pin_config)); 46 | } 47 | 48 | void setup(void) { 49 | auto cfg = M5.config(); 50 | M5Capsule.begin(cfg); 51 | init_microphone(); 52 | } 53 | 54 | static int16_t rec_buf[1024] = {0}; 55 | static size_t bytes_read; 56 | 57 | void loop(void) { 58 | i2s_read(I2S_NUM_0, (char*)rec_buf, 1024, &bytes_read, portMAX_DELAY); 59 | // use monitor tool to see the data line 60 | for (int i = 0; i < bytes_read; i++) { 61 | Serial.printf("%d\r\n", rec_buf[i]); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /examples/Basic/rtc/rtc.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * @file rtc.ino 3 | * @author SeanKwok (shaoxiang@m5stack.com) 4 | * @brief M5Capsule RTC SNTP Test 5 | * @version 0.1 6 | * @date 2023-10-08 7 | * 8 | * 9 | * @Hardwares: M5Capsule 10 | * @Platform Version: Arduino M5Stack Board Manager v2.0.7 11 | * @Dependent Library: 12 | * M5GFX: https://github.com/m5stack/M5GFX 13 | * M5Unified: https://github.com/m5stack/M5Unified 14 | */ 15 | 16 | #if defined(ARDUINO) 17 | 18 | #define WIFI_SSID "YOUR WIFI SSID NAME" 19 | #define WIFI_PASSWORD "YOUR WIFI PASSWORD" 20 | #define NTP_TIMEZONE "UTC-8" 21 | #define NTP_SERVER1 "0.pool.ntp.org" 22 | #define NTP_SERVER2 "1.pool.ntp.org" 23 | #define NTP_SERVER3 "2.pool.ntp.org" 24 | 25 | #include 26 | 27 | // Different versions of the framework have different SNTP header file names and 28 | // availability. 29 | #if __has_include() 30 | #include 31 | #define SNTP_ENABLED 1 32 | #elif __has_include() 33 | #include 34 | #define SNTP_ENABLED 1 35 | #endif 36 | 37 | #endif 38 | 39 | #ifndef SNTP_ENABLED 40 | #define SNTP_ENABLED 0 41 | #endif 42 | 43 | #include 44 | 45 | void setup(void) { 46 | M5Capsule.begin(); 47 | 48 | if (!M5Capsule.Rtc.isEnabled()) { 49 | Serial.println("RTC not found."); 50 | for (;;) { 51 | vTaskDelay(500); 52 | } 53 | } 54 | 55 | Serial.println("RTC found."); 56 | 57 | // It is recommended to set UTC for the RTC and ESP32 internal clocks. 58 | /* /// setup RTC ( direct setting ) 59 | // YYYY MM DD hh mm ss 60 | M5Capsule.Rtc.setDateTime( { { 2021, 12, 31 }, { 12, 34, 56 } } ); 61 | 62 | //*/ 63 | 64 | /// setup RTC ( NTP auto setting ) 65 | 66 | WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 67 | while (WiFi.status() != WL_CONNECTED) { 68 | Serial.print('.'); 69 | delay(500); 70 | } 71 | Serial.println("\r\n WiFi Connected."); 72 | 73 | configTzTime(NTP_TIMEZONE, NTP_SERVER1, NTP_SERVER2, NTP_SERVER3); 74 | 75 | #if SNTP_ENABLED 76 | while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) { 77 | Serial.print('.'); 78 | delay(1000); 79 | } 80 | #else 81 | delay(1600); 82 | struct tm timeInfo; 83 | while (!getLocalTime(&timeInfo, 1000)) { 84 | Serial.print('.'); 85 | }; 86 | #endif 87 | 88 | Serial.println("\r\n NTP Connected."); 89 | 90 | time_t t = time(nullptr) + 1; // Advance one second. 91 | while (t > time(nullptr)) 92 | ; /// Synchronization in seconds 93 | M5Capsule.Rtc.setDateTime(gmtime(&t)); 94 | } 95 | 96 | void loop(void) { 97 | static constexpr const char* const wd[7] = {"Sun", "Mon", "Tue", "Wed", 98 | "Thr", "Fri", "Sat"}; 99 | 100 | delay(500); 101 | 102 | auto dt = M5Capsule.Rtc.getDateTime(); 103 | Serial.printf("RTC UTC :%04d/%02d/%02d (%s) %02d:%02d:%02d\r\n", 104 | dt.date.year, dt.date.month, dt.date.date, 105 | wd[dt.date.weekDay], dt.time.hours, dt.time.minutes, 106 | dt.time.seconds); 107 | 108 | /// ESP32 internal timer 109 | auto t = time(nullptr); 110 | { 111 | auto tm = gmtime(&t); // for UTC. 112 | Serial.printf("ESP32 UTC :%04d/%02d/%02d (%s) %02d:%02d:%02d\r\n", 113 | tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 114 | wd[tm->tm_wday], tm->tm_hour, tm->tm_min, tm->tm_sec); 115 | } 116 | 117 | { 118 | auto tm = localtime(&t); // for local timezone. 119 | Serial.printf("ESP32 %s:%04d/%02d/%02d (%s) %02d:%02d:%02d\r\n", 120 | NTP_TIMEZONE, tm->tm_year + 1900, tm->tm_mon + 1, 121 | tm->tm_mday, wd[tm->tm_wday], tm->tm_hour, tm->tm_min, 122 | tm->tm_sec); 123 | } 124 | } 125 | --------------------------------------------------------------------------------