M5Stack Beginner: PLUS2 Button User Guide
Introduction
The button on the M5StickC Plus2 is located on the side of the device and is typically used for user input or control functions. The button can be configured to perform various operations, such as short press, long press, and double-click, to meet different control needs. Through programming, users can assign different functions to the button, such as initiating a specific task or controlling the device's operating mode.
How do you view the underlying logic?
If you are interested in the underlying logic or functions during the learning process, you can click on the relevant links to explore the details further.
1. Dependent Library:
- M5GFX: https://github.com/m5stack/M5GFX
- M5Unified: https://github.com/m5stack/M5Unified
- M5StickCPlus2: https://github.com/m5stack/M5StickCPlus2
2. It is usually located in the src directory.
Code Comments and Function Descriptions
In M5Stack, the commonly used rotation settings are as follows:
- 0: Default orientation (portrait).
- 1: Rotate 90 degrees clockwise (landscape).
- 2: Rotate 180 degrees clockwise (upside-down portrait).
- 3: Rotate 270 degrees clockwise (landscape, opposite to 1).
2. StickCP2.Display.drawString('Button Test', StickCP2.Display.width() / 2,StickCP2.Display.height() / 2);
- Button Test -> Displays the text content.
- StickCP2.Display.width() / 2 -> Calculates half of the screen width to determine the X-coordinate for positioning the text at the horizontal center of the screen.
- StickCP2.Display.height() / 2 -> Calculates half of the screen height to determine the Y-coordinate for positioning the text at the vertical center of the screen.
The update() function is used to update the device status or handle events and is commonly found in code for M5StickC Plus 2 (or similar devices). This function is often used in the following scenarios:
- Updating Display Content: If the screen content needs to be refreshed or updated continuously, the update() function can be used to periodically refresh the display.
- Handling Button Input: When the device has buttons or touch inputs, the update() function can detect these inputs and update the button states accordingly.
- Timed Tasks or Event Handling: The update() function may also be used for executing timed tasks or handling periodic events to keep the device operating smoothly.
In the development libraries for the M5Stack series, update() is typically employed as a standard practice for maintaining device status and managing sensors or inputs. Each time the update() function is called, the device checks the current state and performs the necessary updates.
Function to Detect if Button A is Pressed:
- wasPressed() method is used to check whether Button A has been pressed during the current or previous loop. It returns a boolean value: true if Button A has been pressed, and false if it has not.
- wasPressed() is typically used for event triggers, such as switching display content, activating a function, or controlling the behavior of the device. Unlike the regular isPressed(), wasPressed() is more suitable for detecting a single press event rather than a continuous pressed state.
Checks if Button A has been released. If Button A is released, the wasReleased() function will return true, and the code inside the braces will be executed. This function is suitable for detecting the release event of the button rather than its pressed state.