### NeoPixel Rainbow Effect Example Source: https://context7.com/m5stack/m5pm1/llms.txt Implements a rainbow effect for NeoPixel LEDs. The `wheel` function generates colors based on position, and the effect is updated in a loop. ```cpp m5pm1_rgb_t wheel(uint8_t pos) { pos = 255 - pos; m5pm1_rgb_t c = {0, 0, 0}; if (pos < 85) { c.r = 255 - pos * 3; c.b = pos * 3; } else if (pos < 170) { pos -= 85; c.g = pos * 3; c.b = 255 - pos * 3; } else { pos -= 170; c.r = pos * 3; c.g = 255 - pos * 3; } return c; } // In loop: static uint8_t offset = 0; for (uint8_t i = 0; i < LED_COUNT; ++i) { pm1.setLedColor(i, wheel(i * 256 / LED_COUNT + offset)); } pm1.refreshLeds(); offset++; ``` -------------------------------- ### Configure and Control PWM Output Source: https://context7.com/m5stack/m5pm1/llms.txt Configures GPIO3 for PWM output, sets the PWM frequency, and controls the duty cycle. The duty cycle can be set from 0-100%%. Includes an example for a breathing LED effect. ```cpp // Configure GPIO3 for PWM output pm1.pinMode(M5PM1_GPIO_NUM_3, M5PM1_OTHER); // Set PWM frequency (shared across all channels) pm1.setPwmFrequency(1000); // 1 kHz // Set duty cycle (0-100%) pm1.setPwmDuty(M5PM1_PWM_CH_0, 50, false, true); // 50% duty, normal polarity, enabled // Breathing LED effect for (int duty = 0; duty <= 100; duty += 2) { pm1.setPwmDuty(M5PM1_PWM_CH_0, duty, false, true); delay(20); } ``` -------------------------------- ### Get Device Information Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Retrieves various device identification details from the M5PM1 module. ```cpp uint32_t getDeviceId() ``` ```cpp uint32_t getDeviceModel() ``` ```cpp uint32_t getHwVersion() ``` ```cpp uint32_t getSwVersion() ``` -------------------------------- ### Power Hold Configuration Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Control the power hold state for GPIO, LDO, and Boost converters. Use `get` functions to read the current state. ```cpp m5pm1_err_t gpioSetPowerHold(uint8_t pin, bool enable) ``` ```cpp bool gpioGetPowerHold(uint8_t pin) ``` ```cpp m5pm1_err_t ldoSetPowerHold(uint8_t pin, bool enable) ``` ```cpp bool ldoGetPowerHold(uint8_t pin) ``` ```cpp m5pm1_err_t boostSetPowerHold(uint8_t pin, bool enable) ``` ```cpp bool boostGetPowerHold(uint8_t pin) ``` -------------------------------- ### Set Battery Low Voltage Protection Source: https://context7.com/m5stack/m5pm1/llms.txt Set the battery low voltage protection threshold in millivolts. For example, 3000 sets the threshold at 3.0V. ```cpp // Set low voltage protection at 3.0V pm1.setBatteryLvp(3000); ``` -------------------------------- ### Read and Write Data to RTC RAM Source: https://context7.com/m5stack/m5pm1/llms.txt Stores and retrieves data from the RTC RAM, which is persistent across sleep cycles. Up to 32 bytes of data can be stored, starting from a specified offset. ```cpp // Write data to RTC RAM uint8_t data[] = {0x12, 0x34, 0x56, 0x78}; pm1.writeRtcRAM(0, data, 4); // Read data from RTC RAM uint8_t buffer[4]; pm1.readRtcRAM(0, buffer, 4); ``` -------------------------------- ### Initialize M5PM1 with M5GFX/M5Unified Present Source: https://github.com/m5stack/m5pm1/blob/main/README.md If M5GFX or M5Unified is already in use, initialize M5PM1 using the `begin(&M5.In_I2C, addr, freq)` method. This is necessary because M5GFX already manages the I2C bus, and the two drivers cannot share it directly. ```cpp begin(&M5.In_I2C, addr, freq) ``` -------------------------------- ### Initialization and Logging Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs for initializing the M5PM1 driver and controlling the logging level. ```APIDOC ## Initialization ### Arduino - `begin(TwoWire* wire, uint8_t addr = 0x6E, int sda = -1, int scl = -1, uint32_t speed = 100000)` ### Arduino / ESP-IDF (M5Unified detected) - `begin(m5::I2C_Class* i2c, uint8_t addr = 0x6E, uint32_t speed = 100000)` ### ESP-IDF - `begin(i2c_port_t port, uint8_t addr = 0x6E, int sda = -1, int scl = -1, uint32_t speed = 100000)` ### ESP-IDF (IDF >= 5.3) - `begin(i2c_master_bus_handle_t bus, uint8_t addr = 0x6E, uint32_t speed = 100000)` ### ESP-IDF (Compatibility Mode) - `begin(i2c_bus_handle_t bus, uint8_t addr = 0x6E, uint32_t speed = 100000)` ## Logging - `setLogLevel(m5pm1_log_level_t level)`: Set the logging level. - `getLogLevel()`: Get the current logging level. ``` -------------------------------- ### Initialization Source: https://context7.com/m5stack/m5pm1/llms.txt Initializes the PM1 device with I2C communication parameters for both Arduino and ESP-IDF frameworks. ```APIDOC ## Initialization ### begin() Initializes the PM1 device with I2C communication parameters. ```cpp M5PM1 pm1; // Arduino: Initialize with Wire instance m5pm1_err_t err = pm1.begin(&Wire, M5PM1_DEFAULT_ADDR, PM1_I2C_SDA, PM1_I2C_SCL, M5PM1_I2C_FREQ_100K); if (err != M5PM1_OK) { Serial.printf("PM1 begin failed: %d\n", err); } // ESP-IDF: Initialize with port number m5pm1_err_t err = pm1.begin(I2C_NUM_0, M5PM1_DEFAULT_ADDR, 21, 22, M5PM1_I2C_FREQ_100K); ``` ``` -------------------------------- ### Initialize M5PM1 Driver (ESP-IDF >= 5.3) Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Initializes the M5PM1 driver for ESP-IDF version 5.3 and later. Uses the I2C master bus handle, address, and speed. ```cpp bool begin(i2c_master_bus_handle_t bus_handle, uint8_t addr = M5PM1_DEFAULT_ADDR, uint32_t speed = 100000) ``` -------------------------------- ### Initialize M5PM1 Driver (Arduino/ESP-IDF with M5Unified) Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Initializes the M5PM1 driver when M5Unified is detected. Uses the M5Unified I2C class and allows specifying the address and speed. ```cpp bool begin(m5::I2C_Class* i2c_class, uint8_t addr = M5PM1_DEFAULT_ADDR, uint32_t speed = 100000) ``` -------------------------------- ### Initialize M5PM1 Driver (ESP-IDF) Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Initializes the M5PM1 driver for ESP-IDF. Requires specifying the I2C port, address, SDA/SCL pins, and speed. ```cpp bool begin(i2c_port_t port, uint8_t addr = M5PM1_DEFAULT_ADDR, uint8_t sda = 21, uint8_t scl = 22, uint32_t speed = 100000) ``` -------------------------------- ### Initialize M5PM1 Driver (ESP-IDF Compatibility Mode) Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Initializes the M5PM1 driver in ESP-IDF compatibility mode. Requires an I2C bus handle, address, and speed. ```cpp bool begin(i2c_bus_handle_t bus_handle, uint8_t addr = M5PM1_DEFAULT_ADDR, uint32_t speed = 100000) ``` -------------------------------- ### Power Management Configuration Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Configure various power-related settings such as charge, DCDC, LDO, boost converters, and LED enable level. Also includes battery low voltage protection. ```cpp m5pm1_power_source_t getPowerSource() ``` ```cpp m5pm1_wake_source_t getWakeSource() ``` ```cpp void clearWakeSource() ``` ```cpp m5pm1_err_t setPowerConfig(m5pm1_power_config_t config) ``` ```cpp m5pm1_power_config_t getPowerConfig() ``` ```cpp void clearPowerConfig() ``` ```cpp m5pm1_err_t setChargeEnable(bool enable) ``` ```cpp m5pm1_err_t setDcdcEnable(bool enable) ``` ```cpp m5pm1_err_t setLdoEnable(bool enable) ``` ```cpp m5pm1_err_t setBoostEnable(bool enable) ``` ```cpp m5pm1_err_t setLedEnLevel(m5pm1_led_en_level_t level) ``` ```cpp m5pm1_err_t setBatteryLvp(bool enable) ``` -------------------------------- ### Configure and Monitor System Interrupts Source: https://context7.com/m5stack/m5pm1/llms.txt Sets up system interrupt masks for events like USB insertion/removal and configures GPIO1 as an IRQ output pin. It also shows how to check the system interrupt status. ```cpp // Mask all interrupts first pm1.irqSetSysMaskAll(M5PM1_IRQ_MASK_ENABLE); // Enable only 5VIN insert/remove interrupts pm1.irqSetSysMask(M5PM1_IRQ_SYS_5VIN_REMOVE, M5PM1_IRQ_MASK_DISABLE); pm1.irqSetSysMask(M5PM1_IRQ_SYS_5VIN_INSERT, M5PM1_IRQ_MASK_DISABLE); // Configure GPIO1 as IRQ output pin pm1.gpioSetFunc(M5PM1_GPIO_NUM_1, M5PM1_GPIO_FUNC_IRQ); // In loop, check for interrupts: uint8_t sysIrq; if (pm1.irqGetSysStatus(&sysIrq, M5PM1_CLEAN_ONCE) == M5PM1_OK) { if (sysIrq & M5PM1_IRQ_SYS_5VIN_REMOVE) { Serial.println("USB Removed!"); } if (sysIrq & M5PM1_IRQ_SYS_5VIN_INSERT) { Serial.println("USB Inserted!"); } } ``` -------------------------------- ### Initialize M5PM1 Driver Source: https://context7.com/m5stack/m5pm1/llms.txt Initializes the PM1 device with I2C communication parameters. Use the Arduino Wire instance for Arduino framework or the I2C port number for ESP-IDF. ```cpp M5PM1 pm1; // Arduino: Initialize with Wire instance m5pm1_err_t err = pm1.begin(&Wire, M5PM1_DEFAULT_ADDR, PM1_I2C_SDA, PM1_I2C_SCL, M5PM1_I2C_FREQ_100K); if (err != M5PM1_OK) { Serial.printf("PM1 begin failed: %d\n", err); } // ESP-IDF: Initialize with port number m5pm1_err_t err = pm1.begin(I2C_NUM_0, M5PM1_DEFAULT_ADDR, 21, 22, M5PM1_I2C_FREQ_100K); ``` -------------------------------- ### System Commands Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs for executing system-level commands such as shutdown, reboot, and entering download mode. ```APIDOC ## System Commands - `sysCmd(m5pm1_sys_cmd_t cmd)`: Execute a system command. - `shutdown()`: Shut down the device. - `reboot()`: Reboot the device. - `enterDownloadMode()`: Enter download mode. - `setDownloadLock(bool lock)`: Set the download mode lock (high risk). - `getDownloadLock()`: Get the download mode lock status. ``` -------------------------------- ### System Commands Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Execute system-level commands such as shutdown, reboot, and entering download mode. Includes options to lock/unlock download mode. ```cpp m5pm1_err_t sysCmd(m5pm1_sys_cmd_t cmd) ``` ```cpp m5pm1_err_t shutdown() ``` ```cpp m5pm1_err_t reboot() ``` ```cpp m5pm1_err_t enterDownloadMode() ``` ```cpp m5pm1_err_t setDownloadLock(bool lock) ``` ```cpp bool getDownloadLock() ``` -------------------------------- ### Button Configuration and State Source: https://context7.com/m5stack/m5pm1/llms.txt Configure button behavior (click, double-click, long press delays) and read button states and flags. ```APIDOC ## btnSetConfig(), btnGetState(), btnGetFlag() ### Description Configure button timing for click, double-click, and long press events. Read the current button state and check if a button event flag has been set. ### Methods - `btnSetConfig(button_type, delay_time)`: Configures the delay time for a specific button event type. - `btnGetState(bool *pressed)`: Reads the current state of the button, setting the `pressed` variable to true if the button is currently held down. - `btnGetFlag(bool *wasPressed)`: Checks if a button event has occurred. Sets `wasPressed` to true if an event was detected and automatically clears the flag. ### Parameters #### `btnSetConfig` - **button_type** (enum) - Required - The type of button event to configure (e.g., M5PM1_BTN_TYPE_CLICK). - **delay_time** (enum) - Required - The delay time for the specified button event (e.g., M5PM1_BTN_CLICK_DELAY_250MS). #### `btnGetState` - **pressed** (pointer to bool) - Required - Pointer to a boolean variable that will be updated with the button's current state. #### `btnGetFlag` - **wasPressed** (pointer to bool) - Required - Pointer to a boolean variable that will be updated if a button event flag is set. ``` -------------------------------- ### Configure and Control GPIO Pins Source: https://context7.com/m5stack/m5pm1/llms.txt Configures GPIO pins for input/output operations and sets their function, drive mode, and pull-up/down resistors. Use M5PM1_GPIO_NUM_x constants for pin selection. ```cpp // Configure GPIO0 as output pm1.pinMode(M5PM1_GPIO_NUM_0, OUTPUT); pm1.digitalWrite(M5PM1_GPIO_NUM_0, HIGH); // Configure GPIO1 as input with pull-up pm1.pinMode(M5PM1_GPIO_NUM_1, INPUT_PULLUP); int level = pm1.digitalRead(M5PM1_GPIO_NUM_1); // Configure GPIO0 for NeoPixel (OTHER function) pm1.gpioSetFunc(M5PM1_GPIO_NUM_0, M5PM1_GPIO_FUNC_OTHER); pm1.gpioSetDrive(M5PM1_GPIO_NUM_0, M5PM1_GPIO_DRIVE_PUSHPULL); // Configure GPIO1 for interrupt function pm1.gpioSetFunc(M5PM1_GPIO_NUM_1, M5PM1_GPIO_FUNC_IRQ); ``` -------------------------------- ### Configure I2C Parameters Source: https://context7.com/m5stack/m5pm1/llms.txt Configure I2C sleep timeout and speed. Use constants like M5PM1_I2C_SPEED_400K and M5PM1_I2C_SPEED_100K. ```cpp // Set I2C sleep timeout to 5 seconds, speed to 400KHz pm1.setI2cConfig(5, M5PM1_I2C_SPEED_400K); // Switch I2C speed pm1.switchI2cSpeed(M5PM1_I2C_SPEED_100K); ``` -------------------------------- ### Button Configuration and State Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Configure button behavior, read button state, and check button flags. Includes options to disable single reset and double off functions. ```cpp m5pm1_err_t btnSetConfig(uint8_t btn_id, m5pm1_button_config_t config) ``` ```cpp m5pm1_button_state_t btnGetState(uint8_t btn_id) ``` ```cpp uint32_t btnGetFlag(uint8_t btn_id) ``` ```cpp m5pm1_err_t setSingleResetDisable(bool disable) ``` ```cpp bool getSingleResetDisable() ``` ```cpp m5pm1_err_t setDoubleOffDisable(bool disable) ``` ```cpp bool getDoubleOffDisable() ``` -------------------------------- ### Control NeoPixel LEDs Source: https://context7.com/m5stack/m5pm1/llms.txt Initializes GPIO0 for NeoPixel control, sets the number of LEDs, and configures their colors using RGB values. The colors are applied to the LEDs upon calling refreshLeds(). ```cpp // Initialize GPIO0 for NeoPixel pm1.pinMode(M5PM1_GPIO_NUM_0, M5PM1_OTHER); pm1.setLedEnLevel(true); // Set LED count (max 32) pm1.setLedCount(1); // Set LED color using RGB struct m5pm1_rgb_t color = {255, 0, 0}; // Red pm1.setLedColor(0, color); // Or using individual RGB values pm1.setLedColor(0, 0, 255, 0); // Green // Apply colors to LEDs pm1.refreshLeds(); ``` -------------------------------- ### Initialize M5PM1 Driver (Arduino) Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Initializes the M5PM1 driver for Arduino. Specify the I2C interface, address, and optionally SDA/SCL pins and speed. ```cpp bool begin(TwoWire* wire = &Wire, uint8_t addr = M5PM1_DEFAULT_ADDR, uint8_t sda = 21, uint8_t scl = 22, uint32_t speed = 100000) ``` -------------------------------- ### Watchdog Timer Configuration and Feeding Source: https://context7.com/m5stack/m5pm1/llms.txt Sets the watchdog timer timeout period and demonstrates how to feed the watchdog to prevent system resets. The watchdog can be disabled by setting the timeout to 0. ```cpp // Set 30-second watchdog timeout pm1.wdtSet(30); // In loop, feed the watchdog periodically pm1.wdtFeed(); // Disable watchdog pm1.wdtSet(0); ``` -------------------------------- ### Interrupt Handling Source: https://context7.com/m5stack/m5pm1/llms.txt Configure and monitor system, GPIO, and button interrupts. ```APIDOC ## irqSetSysMask(), irqGetSysStatus() ### Description Configure and monitor system interrupts for USB/battery events. ### Method `irqSetSysMaskAll(mask)` `irqSetSysMask(irqType, mask)` `irqGetSysStatus(status, clearFlag)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Mask all interrupts first pm1.irqSetSysMaskAll(M5PM1_IRQ_MASK_ENABLE); // Enable only 5VIN insert/remove interrupts pm1.irqSetSysMask(M5PM1_IRQ_SYS_5VIN_REMOVE, M5PM1_IRQ_MASK_DISABLE); pm1.irqSetSysMask(M5PM1_IRQ_SYS_5VIN_INSERT, M5PM1_IRQ_MASK_DISABLE); // Configure GPIO1 as IRQ output pin pm1.gpioSetFunc(M5PM1_GPIO_NUM_1, M5PM1_GPIO_FUNC_IRQ); // In loop, check for interrupts: uint8_t sysIrq; if (pm1.irqGetSysStatus(&sysIrq, M5PM1_CLEAN_ONCE) == M5PM1_OK) { if (sysIrq & M5PM1_IRQ_SYS_5VIN_REMOVE) { Serial.println("USB Removed!"); } if (sysIrq & M5PM1_IRQ_SYS_5VIN_INSERT) { Serial.println("USB Inserted!"); } } ``` ### Response #### Success Response (200) None (status is passed via pointer) #### Response Example None ``` ```APIDOC ## irqSetGpioMask(), irqSetBtnMask() ### Description Configure GPIO and button interrupt masks. ### Method `irqSetGpioMaskAll(mask)` `irqSetBtnMaskAll(mask)` `irqClearGpioAll()` `irqClearBtnAll()` `irqClearSysAll()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Mask all GPIO and button interrupts pm1.irqSetGpioMaskAll(M5PM1_IRQ_MASK_ENABLE); pm1.irqSetBtnMaskAll(M5PM1_IRQ_MASK_ENABLE); // Clear any pending interrupts pm1.irqClearGpioAll(); pm1.irqClearBtnAll(); pm1.irqClearSysAll(); ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Basic GPIO Control (Arduino Style) Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Provides Arduino-style functions for basic GPIO pin manipulation: setting mode, writing state, and reading state. ```cpp void pinMode(uint8_t pin, m5pm1_pin_mode_t mode) ``` ```cpp void digitalWrite(uint8_t pin, m5pm1_pin_state_t state) ``` ```cpp m5pm1_pin_state_t digitalRead(uint8_t pin) ``` -------------------------------- ### Correct Include Order for ESP-IDF with M5Unified Source: https://github.com/m5stack/m5pm1/blob/main/README.md When using M5PM1 with M5Unified on ESP-IDF, include M5Unified before M5PM1 to prevent potential i2c_config_t conflicts, especially on ESP-IDF versions 5.3.0 and later without CONFIG_I2C_BUS_BACKWARD_CONFIG enabled. ```cpp #include // ✓ must come first #include ``` ```cpp #include // ✗ causes i2c_config_t conflict #include ``` -------------------------------- ### Power Management and Battery Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs for managing power sources, wake-up events, charging, and power rail enable/disable. ```APIDOC ## Power Management and Battery - `getPowerSource()`: Get the current power source. - `getWakeSource()`: Get the source that woke the device. - `clearWakeSource()`: Clear the wake-up source flags. - `setPowerConfig(m5pm1_power_config_t config)`: Set power configuration. - `getPowerConfig()`: Get the current power configuration. - `clearPowerConfig()`: Clear all power configuration flags. - `setChargeEnable(bool enable)`: Enable or disable battery charging. - `setDcdcEnable(bool enable)`: Enable or disable the DCDC converter. - `setLdoEnable(bool enable)`: Enable or disable the LDO. - `setBoostEnable(bool enable)`: Enable or disable the boost converter. - `setLedEnLevel(uint8_t level)`: Set the LED enable level. - `setBatteryLvp(bool enable)`: Enable or disable Low Voltage Protection for the battery. ``` -------------------------------- ### Advanced GPIO Configuration Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Functions for advanced GPIO configuration, including setting function, output type, pull-up/down, drive strength, and wake enable. ```cpp m5pm1_err_t gpioSet(uint8_t pin, m5pm1_pin_state_t state) ``` ```cpp m5pm1_err_t gpioSetFunc(uint8_t pin, m5pm1_pin_func_t func) ``` ```cpp m5pm1_err_t gpioSetMode(uint8_t pin, m5pm1_pin_mode_t mode) ``` ```cpp m5pm1_err_t gpioSetOutput(uint8_t pin, m5pm1_pin_state_t state) ``` ```cpp m5pm1_err_t gpioGetInput(uint8_t pin, m5pm1_pin_state_t* state) ``` ```cpp m5pm1_err_t gpioSetPull(uint8_t pin, m5pm1_pin_pull_t pull) ``` ```cpp m5pm1_err_t ledEnSetDrive(uint8_t pin, m5pm1_pin_drive_t drive) ``` ```cpp m5pm1_err_t gpioSetWakeEnable(uint8_t pin, bool enable) ``` ```cpp m5pm1_err_t gpioSetWakeEdge(uint8_t pin, m5pm1_pin_wake_edge_t edge) ``` -------------------------------- ### GPIO Control Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs for basic and advanced GPIO configuration and control. ```APIDOC ## GPIO Control ### Arduino Style - `pinMode(uint8_t pin, m5pm1_pinmode_t mode)` - `digitalWrite(uint8_t pin, int level)` - `digitalRead(uint8_t pin)` ### With Return Status - `pinModeWithRes(uint8_t pin, m5pm1_pinmode_t mode)` - `digitalWriteWithRes(uint8_t pin, int level)` - `digitalReadWithRes(uint8_t pin)` ### Advanced Configuration - `gpioSet(uint8_t pin, int level)` - `gpioSetFunc(uint8_t pin, m5pm1_gpio_func_t func)` - `gpioSetMode(uint8_t pin, m5pm1_pinmode_t mode)` - `gpioSetOutput(uint8_t pin, int level)` - `gpioGetInput(uint8_t pin)` - `gpioSetPull(uint8_t pin, m5pm1_gpio_pull_t pull)` - `gpioSetDrive(uint8_t pin, m5pm1_gpio_drive_t drive)` - `ledEnSetDrive(uint8_t pin, m5pm1_gpio_drive_t drive)` - `gpioSetWakeEnable(uint8_t pin, bool enable)` - `gpioSetWakeEdge(uint8_t pin, m5pm1_gpio_wake_edge_t edge)` ### Pin Status and Validation - `dumpPinStatus()` - `verifyPinConfig(uint8_t pin)` - `getPinStatus(uint8_t pin)` - `getPinStatusArray()` ``` -------------------------------- ### System Power Control and Timed Wake-up Source: https://context7.com/m5stack/m5pm1/llms.txt Provides functions to shut down, reboot, or set a timer for system wake-up. The timer can be configured to power on the device after a specified delay. ```cpp // Configure timer to wake up in 10 seconds pm1.timerSet(10, M5PM1_TIM_ACTION_POWERON); // Shutdown the system pm1.shutdown(); // Reboot the system pm1.reboot(); ``` -------------------------------- ### Sleep and Wake Functions Source: https://context7.com/m5stack/m5pm1/llms.txt Control system power states and configure wake-up sources. ```APIDOC ## shutdown(), reboot(), timerSet() ### Description System power control and timed wake-up. ### Method `timerSet(seconds, action)` `shutdown()` `reboot()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Configure timer to wake up in 10 seconds pm1.timerSet(10, M5PM1_TIM_ACTION_POWERON); // Shutdown the system pm1.shutdown(); // Reboot the system pm1.reboot(); ``` ### Response #### Success Response (200) None #### Response Example None ``` ```APIDOC ## getWakeSource() ### Description Determine what caused the system to wake up. ### Method `getWakeSource(wakeSource, clearFlag)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp uint8_t wakeSrc; pm1.getWakeSource(&wakeSrc, M5PM1_CLEAN_ONCE); if (wakeSrc & M5PM1_WAKE_SRC_TIM) Serial.println("Woke by Timer"); if (wakeSrc & M5PM1_WAKE_SRC_VIN) Serial.println("Woke by VIN"); if (wakeSrc & M5PM1_WAKE_SRC_PWRBTN) Serial.println("Woke by Power Button"); if (wakeSrc & M5PM1_WAKE_SRC_EXT_WAKE) Serial.println("Woke by External GPIO"); ``` ### Response #### Success Response (200) None (values are passed via pointer) #### Response Example None ``` -------------------------------- ### Watchdog and Timer Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs for configuring and interacting with the watchdog timer and general timers. ```APIDOC ## Watchdog and Timer ### Watchdog Timer - `wdtSet(uint32_t timeout_ms)`: Set the watchdog timer timeout. - `wdtFeed()`: Feed the watchdog timer to prevent reset. - `wdtGetCount()`: Get the watchdog timer count. ### General Timers - `timerSet(uint32_t timer_id, uint32_t timeout_ms)`: Set a timer. - `timerClear(uint32_t timer_id)`: Clear a timer. ``` -------------------------------- ### Button Handling Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs for configuring and reading button states and flags, including disabling reset and double-off functions. ```APIDOC ## Button Handling - `btnSetConfig(m5pm1_button_config_t config)`: Configure button behavior. - `btnGetState(m5pm1_button_t button)`: Get the current state of a button. - `btnGetFlag(m5pm1_button_t button)`: Get the flags associated with a button event. - `setSingleResetDisable(bool disable)`: Disable the single-press reset function (high risk). - `getSingleResetDisable()`: Check if the single-press reset function is disabled. - `setDoubleOffDisable(bool disable)`: Disable the double-press power-off function (high risk). - `getDoubleOffDisable()`: Check if the double-press power-off function is disabled. ``` -------------------------------- ### Watchdog and Timer Control Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Configure and manage the watchdog timer and general-purpose timers for system monitoring and timing tasks. ```cpp m5pm1_err_t wdtSet(uint32_t timeout_ms) ``` ```cpp m5pm1_err_t wdtFeed() ``` ```cpp uint32_t wdtGetCount() ``` ```cpp m5pm1_err_t timerSet(uint8_t timer_id, uint32_t timeout_ms) ``` ```cpp m5pm1_err_t timerClear(uint8_t timer_id) ``` -------------------------------- ### GPIO Functions Source: https://context7.com/m5stack/m5pm1/llms.txt Configure and control GPIO pins for input/output, advanced functions, and wake-from-sleep. ```APIDOC ## GPIO Functions ### pinMode(), digitalWrite(), digitalRead() Arduino-compatible GPIO functions for basic input/output operations. ```cpp // Configure GPIO0 as output pm1.pinMode(M5PM1_GPIO_NUM_0, OUTPUT); pm1.digitalWrite(M5PM1_GPIO_NUM_0, HIGH); // Configure GPIO1 as input with pull-up pm1.pinMode(M5PM1_GPIO_NUM_1, INPUT_PULLUP); int level = pm1.digitalRead(M5PM1_GPIO_NUM_1); ``` ### gpioSetFunc(), gpioSetDrive(), gpioSetPull() Advanced GPIO configuration for function selection and drive mode. ```cpp // Configure GPIO0 for NeoPixel (OTHER function) pm1.gpioSetFunc(M5PM1_GPIO_NUM_0, M5PM1_GPIO_FUNC_OTHER); pm1.gpioSetDrive(M5PM1_GPIO_NUM_0, M5PM1_GPIO_DRIVE_PUSHPULL); // Configure GPIO1 for interrupt function pm1.gpioSetFunc(M5PM1_GPIO_NUM_1, M5PM1_GPIO_FUNC_IRQ); ``` ### gpioSetWakeEnable(), gpioSetWakeEdge() Configure GPIO pins for wake-from-sleep functionality. ```cpp // Enable GPIO2 as wake source on falling edge pm1.gpioSetFunc(M5PM1_GPIO_NUM_2, M5PM1_GPIO_FUNC_WAKE); pm1.gpioSetWakeEnable(M5PM1_GPIO_NUM_2, true); pm1.gpioSetWakeEdge(M5PM1_GPIO_NUM_2, M5PM1_GPIO_WAKE_FALLING); ``` ``` -------------------------------- ### Watchdog Timer Source: https://context7.com/m5stack/m5pm1/llms.txt Configure and manage the watchdog timer for system stability. ```APIDOC ## wdtSet(), wdtFeed() ### Description Configure and maintain the watchdog timer for system reliability. ### Method `wdtSet(timeoutSeconds)` `wdtFeed()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Set 30-second watchdog timeout pm1.wdtSet(30); // In loop, feed the watchdog periodically pm1.wdtFeed(); // Disable watchdog pm1.wdtSet(0); ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Power Management Source: https://context7.com/m5stack/m5pm1/llms.txt Control battery charging, power rails (DCDC, LDO, BOOST), and read power source information. ```APIDOC ## Power Management ### setChargeEnable(), setDcdcEnable(), setLdoEnable(), setBoostEnable() Control individual power rails and charging functionality. ```cpp // Enable battery charging pm1.setChargeEnable(true); // Enable 5V DCDC output pm1.setDcdcEnable(true); // Enable 3.3V LDO output pm1.setLdoEnable(true); // Enable BOOST/GROVE (5VINOUT) power pm1.setBoostEnable(true); ``` ### getPowerConfig(), setPowerConfig() Read or modify the complete power configuration register. ```cpp uint8_t config; pm1.getPowerConfig(&config); Serial.printf("Power config: 0x%02X\n", config); // Modify specific bits using mask pm1.setPowerConfig(M5PM1_PWR_CFG_CHG_EN | M5PM1_PWR_CFG_LDO_EN, 0xFF); ``` ### getPowerSource() Determine the current power source (USB, 5VINOUT, or battery). ```cpp m5pm1_pwr_src_t src; pm1.getPowerSource(&src); switch (src) { case M5PM1_PWR_SRC_5VIN: Serial.println("Powered by USB/DC"); break; case M5PM1_PWR_SRC_5VINOUT: Serial.println("Powered by 5VINOUT"); break; case M5PM1_PWR_SRC_BAT: Serial.println("Powered by Battery"); break; } ``` ### setLedEnLevel() Set the LED_EN pin default level for NeoPixel power control. ```cpp // Enable NeoPixel power (mainly for Stamp-S3Bat compatibility) pm1.setLedEnLevel(true); ``` ``` -------------------------------- ### Configure Button Timing Source: https://context7.com/m5stack/m5pm1/llms.txt Configure button timing for click, double-click, and long-press events. Use M5PM1_BTN_CLICK_DELAY_250MS, M5PM1_BTN_DOUBLE_CLICK_DELAY_500MS, and M5PM1_BTN_LONG_PRESS_DELAY_2000MS constants. ```cpp // Configure button timing pm1.btnSetConfig(M5PM1_BTN_TYPE_CLICK, M5PM1_BTN_CLICK_DELAY_250MS); pm1.btnSetConfig(M5PM1_BTN_TYPE_DOUBLE, M5PM1_BTN_DOUBLE_CLICK_DELAY_500MS); pm1.btnSetConfig(M5PM1_BTN_TYPE_LONG, M5PM1_BTN_LONG_PRESS_DELAY_2000MS); ``` -------------------------------- ### Read Voltage Measurements Source: https://context7.com/m5stack/m5pm1/llms.txt Reads various voltage measurements including VREF, battery voltage (VBAT), VIN, and 5VINOUT in millivolts. Ensure the device is initialized. ```cpp uint16_t vref_mv, vbat_mv, vin_mv, v5inout_mv; pm1.readVref(&vref_mv); pm1.readVbat(&vbat_mv); pm1.readVin(&vin_mv); pm1.read5VInOut(&v5inout_mv); Serial.printf("VREF: %d mV, VBAT: %d mV, VIN: %d mV, 5VINOUT: %d mV\n", vref_mv, vbat_mv, vin_mv, v5inout_mv); ``` -------------------------------- ### Read Device Identification and Version Source: https://context7.com/m5stack/m5pm1/llms.txt Reads device identification and version information from the PM1 IC. Ensure the device is initialized before calling these functions. ```cpp uint8_t deviceId, model, hwVer, swVer; pm1.getDeviceId(&deviceId); pm1.getDeviceModel(&model); pm1.getHwVersion(&hwVer); pm1.getSwVersion(&swVer); Serial.printf("Device ID: 0x%02X, Model: %u, HW: %u, SW: %u\n", deviceId, model, hwVer, swVer); ``` -------------------------------- ### I2C Configuration Source: https://context7.com/m5stack/m5pm1/llms.txt Configure I2C communication parameters including speed and sleep timeout. ```APIDOC ## setI2cConfig(), switchI2cSpeed() ### Description Configure I2C parameters such as sleep timeout and communication speed. Allows switching the I2C speed dynamically. ### Methods - `setI2cConfig(uint8_t seconds, uint8_t speed)`: Sets the I2C sleep timeout and the initial I2C speed. - `switchI2cSpeed(uint8_t speed)`: Switches the I2C communication speed to a new value. ### Parameters #### `setI2cConfig` - **seconds** (uint8_t) - Required - The I2C sleep timeout duration in seconds. - **speed** (uint8_t) - Required - The initial I2C speed (e.g., M5PM1_I2C_SPEED_400K). #### `switchI2cSpeed` - **speed** (uint8_t) - Required - The new I2C speed to switch to (e.g., M5PM1_I2C_SPEED_100K). ``` -------------------------------- ### ADC and Temperature Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs for analog-to-digital conversion and temperature readings. ```APIDOC ## ADC and Temperature - `analogRead(uint8_t pin)`: Read analog value from a pin. - `isAdcBusy()`: Check if ADC is currently busy. - `disableAdc()`: Disable the ADC. - `readTemperature()`: Read the internal temperature sensor. ``` -------------------------------- ### NeoPixel Control Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs for controlling NeoPixel LEDs, including setting colors and refreshing the strip. ```APIDOC ## NeoPixel Control - `setLeds(uint8_t count)`: Set the number of NeoPixels. - `setLedCount(uint8_t count)`: Set the number of NeoPixels. - `setLedColor(uint8_t index, uint8_t r, uint8_t g, uint8_t b)`: Set the color of a specific NeoPixel. - `refreshLeds()`: Update the NeoPixel strip with the set colors. - `disableLeds()`: Turn off all NeoPixels. ``` -------------------------------- ### Control Power Rails and Charging Source: https://context7.com/m5stack/m5pm1/llms.txt Enables or disables individual power rails (DCDC, LDO, BOOST) and battery charging. Call these after successful initialization. ```cpp // Enable battery charging pm1.setChargeEnable(true); // Enable 5V DCDC output pm1.setDcdcEnable(true); // Enable 3.3V LDO output pm1.setLdoEnable(true); // Enable BOOST/GROVE (5VINOUT) power pm1.setBoostEnable(true); ``` -------------------------------- ### PWM Functions Source: https://context7.com/m5stack/m5pm1/llms.txt Configure and control PWM output on specified GPIO pins. ```APIDOC ## setPwmFrequency(), setPwmDuty() ### Description Configure PWM output on GPIO3 (PWM_CH_0) or GPIO4 (PWM_CH_1). ### Method `setPwmFrequency(frequency)` `setPwmDuty(channel, duty, polarity, enable)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Configure GPIO3 for PWM output pm1.pinMode(M5PM1_GPIO_NUM_3, M5PM1_OTHER); // Set PWM frequency (shared across all channels) pm1.setPwmFrequency(1000); // 1 kHz // Set duty cycle (0-100%) pm1.setPwmDuty(M5PM1_PWM_CH_0, 50, false, true); // 50% duty, normal polarity, enabled // Breathing LED effect for (int duty = 0; duty <= 100; duty += 2) { pm1.setPwmDuty(M5PM1_PWM_CH_0, duty, false, true); delay(20); } ``` ### Response #### Success Response (200) None #### Response Example None ``` ```APIDOC ## setPwmDuty12bit() ### Description Set PWM duty cycle with 12-bit precision (0-4095). ### Method `setPwmDuty12bit(channel, duty, polarity, enable)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp pm1.setPwmDuty12bit(M5PM1_PWM_CH_0, 2048, false, true); // 50% with 12-bit precision ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Set Log Level Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md Sets the logging level for the M5PM1 driver. Use `getLogLevel` to retrieve the current level. ```cpp void setLogLevel(m5pm1_log_level_t level) ``` ```cpp m5pm1_log_level_t getLogLevel() ``` -------------------------------- ### Configure GPIO for Wake-from-Sleep Source: https://context7.com/m5stack/m5pm1/llms.txt Configures GPIO pins to act as wake sources when the device is in sleep mode. Specify the wake enable status and the edge trigger (falling or rising). ```cpp // Enable GPIO2 as wake source on falling edge pm1.gpioSetFunc(M5PM1_GPIO_NUM_2, M5PM1_GPIO_FUNC_WAKE); pm1.gpioSetWakeEnable(M5PM1_GPIO_NUM_2, true); pm1.gpioSetWakeEdge(M5PM1_GPIO_NUM_2, M5PM1_GPIO_WAKE_FALLING); ``` -------------------------------- ### Battery Protection Configuration Source: https://context7.com/m5stack/m5pm1/llms.txt Set the low voltage protection threshold for the battery. ```APIDOC ## setBatteryLvp() ### Description Set the threshold for low voltage protection to prevent battery damage due to over-discharge. ### Method - `setBatteryLvp(uint32_t threshold_mV)`: Sets the low voltage protection threshold in millivolts. ### Parameters #### `setBatteryLvp` - **threshold_mV** (uint32_t) - Required - The low voltage protection threshold in millivolts (e.g., 3000 for 3.0V). ``` -------------------------------- ### Device Information Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs to retrieve device identification and version information. ```APIDOC ## Device Information - `getDeviceId()`: Get the device ID. - `getDeviceModel()`: Get the device model. - `getHwVersion()`: Get the hardware version. - `getSwVersion()`: Get the software version. ``` -------------------------------- ### Power Hold Source: https://github.com/m5stack/m5pm1/blob/main/README_FUNCTION_EN.md APIs to manage power hold states for GPIO, LDO, and Boost converters. ```APIDOC ## Power Hold - `gpioSetPowerHold(uint8_t pin, bool enable)` - `gpioGetPowerHold(uint8_t pin)` - `ldoSetPowerHold(bool enable)` - `ldoGetPowerHold()` - `boostSetPowerHold(bool enable)` - `boostGetPowerHold()` ``` -------------------------------- ### Set NeoPixel Power Control Level Source: https://context7.com/m5stack/m5pm1/llms.txt Sets the LED_EN pin default level, primarily for controlling NeoPixel power, especially for Stamp-S3Bat compatibility. Call after initialization. ```cpp // Enable NeoPixel power (mainly for Stamp-S3Bat compatibility) pm1.setLedEnLevel(true); ``` -------------------------------- ### NeoPixel LED Control Source: https://context7.com/m5stack/m5pm1/llms.txt Control WS2812/NeoPixel LEDs connected to GPIO0. ```APIDOC ## setLedCount(), setLedColor(), refreshLeds() ### Description Control WS2812/NeoPixel LEDs connected to GPIO0. ### Method `setLedCount(count)` `setLedColor(index, color)` `setLedColor(index, r, g, b)` `refreshLeds()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Initialize GPIO0 for NeoPixel pm1.pinMode(M5PM1_GPIO_NUM_0, M5PM1_OTHER); pm1.setLedEnLevel(true); // Set LED count (max 32) pm1.setLedCount(1); // Set LED color using RGB struct m5pm1_rgb_t color = {255, 0, 0}; // Red pm1.setLedColor(0, color); // Or using individual RGB values pm1.setLedColor(0, 0, 255, 0); // Green // Apply colors to LEDs pm1.refreshLeds(); ``` ### Response #### Success Response (200) None #### Response Example None ``` ```APIDOC ## Rainbow Effect Example ### Description Example code for creating a rainbow effect with NeoPixel LEDs. ### Method (Helper function `wheel` and usage of `setLedColor`, `refreshLeds`) ### Parameters None ### Request Example ```cpp m5pm1_rgb_t wheel(uint8_t pos) { pos = 255 - pos; m5pm1_rgb_t c = {0, 0, 0}; if (pos < 85) { c.r = 255 - pos * 3; c.b = pos * 3; } else if (pos < 170) { pos -= 85; c.g = pos * 3; c.b = 255 - pos * 3; } else { pos -= 170; c.r = pos * 3; c.g = 255 - pos * 3; } return c; } // In loop: static uint8_t offset = 0; for (uint8_t i = 0; i < LED_COUNT; ++i) { pm1.setLedColor(i, wheel(i * 256 / LED_COUNT + offset)); } pm1.refreshLeds(); offset++; ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Determine System Wake Source Source: https://context7.com/m5stack/m5pm1/llms.txt Retrieves the source that caused the system to wake up. It can be from a timer, VIN, power button, or external GPIO. The wake source is cleared once after reading. ```cpp uint8_t wakeSrc; pm1.getWakeSource(&wakeSrc, M5PM1_CLEAN_ONCE); if (wakeSrc & M5PM1_WAKE_SRC_TIM) Serial.println("Woke by Timer"); if (wakeSrc & M5PM1_WAKE_SRC_VIN) Serial.println("Woke by VIN"); if (wakeSrc & M5PM1_WAKE_SRC_PWRBTN) Serial.println("Woke by Power Button"); if (wakeSrc & M5PM1_WAKE_SRC_EXT_WAKE) Serial.println("Woke by External GPIO"); ```