daisy::GPIO¶
Module: LIBDAISY / PERIPHERAL
General Purpose I/O control. More...
#include <gpio.h>
Public Classes¶
Name | |
---|---|
struct | Config Configuration for a given GPIO. |
Public Types¶
Name | |
---|---|
enum class | Mode { INPUT, OUTPUT, OUTPUT_OD, ANALOG} Mode of operation for the specified GPIO. |
enum class | Pull { NOPULL, PULLUP, PULLDOWN} Configures whether an internal Pull up or Pull down resistor is used. |
enum class | Speed { LOW, MEDIUM, HIGH, VERY_HIGH} Output speed controls the drive strength, and slew rate of the pin. |
Public Functions¶
Name | |
---|---|
GPIO() | |
void | Init(const Config & cfg) Initialize the GPIO from a Config struct. |
void | Init(Pin p, const Config & cfg) Initialize the GPIO with a Configuration struct, and explicit pin. |
void | Init(Pin p, Mode m =Mode::INPUT, Pull pu =Pull::NOPULL, Speed sp =Speed::LOW) Explicity initialize all configuration for the GPIO. |
void | DeInit() Deinitializes the GPIO pin. |
bool | Read() Reads the state of the GPIO. |
void | Write(bool state) Changes the state of the GPIO hardware when configured as an OUTPUT. |
void | Toggle() flips the current state of the GPIO. If it was HIGH, it will go LOW, and vice versa. |
Config & | GetConfig() |
Detailed Description¶
General Purpose I/O control.
peripheral control over a single GPIO
Button Input (with internal Pullup resistor) ```cpp
// GPIO Input // Example of using a simple button // // Setup: // * Connect one end of a button to GND // * Connect other end to pin D0 on the Daisy Seed //
include "daisy_seed.h"¶
using namespace daisy; using namespace daisy::seed;
DaisySeed hw;
int main(void) { // Initialize the Daisy Seed hw.Init();
// Start the Serial Logger
hw.StartLog();
// Create a GPIO object
GPIO my_button;
// Initialize the GPIO object
// Mode: INPUT - because we want to read from the button
// Pullup: Internal resistor to prevent needing extra components
my_button.Init(D0, GPIO::Mode::INPUT, GPIO::Pull::PULLUP);
while(1)
{
// And let's store the state of the button in a variable called "button_state"
bool button_state = my_button.Read();
// Set the USER LED to the button state
hw.SetLed(button_state);
// Print the Button state
hw.PrintLine("Button State: %s", button_state ? "true" : "false");
}
}
_Filename: GPIO_Input.cpp_
Output Example (perfect for blinking an LED)
cpp
// GPIO Output
// Example of toggling an LED on/off
//
// Setup:
// * Connect the CATHODE (negative leg) of the LED to GND
// * Connect the ANODE (positive leg, usually longer) of the LED to one side of a resistor (1K)
// * Connect other end of resistor to pin D1 on the daisy.
//
include "daisy_seed.h"¶
using namespace daisy; using namespace daisy::seed; DaisySeed hw; int main(void) { // Initialize the Daisy Seed hardware hw.Init(); // Create an LED GPIO my_led; // Initialize it to pin D1 as an OUTPUT my_led.Init(D1, GPIO::Mode::OUTPUT); // In an infinite loop, we'll continuously turn the LED on/off. while(1) { // Set the pin HIGH my_led.Write(true); // Wait half a second (500ms) System::Delay(500); // Set the pin LOW my_led.Write(false); // Wait another half a second (500ms) System::Delay(500); // You can also use Toggle to change the state my_led.Toggle(); // Wait another half a second (500ms) System::Delay(500); // And once more to flip it back my_led.Toggle(); } } ```
Filename: GPIO_Output.cpp
Public Types Documentation¶
enum Mode¶
Enumerator | Value | Description |
---|---|---|
INPUT | Input for reading state of pin | |
OUTPUT | Output w/ push-pull configuration | |
OUTPUT_OD | Output w/ open-drain configuration | |
ANALOG | Analog for connection to ADC or DAC peripheral |
Mode of operation for the specified GPIO.
enum Pull¶
Enumerator | Value | Description |
---|---|---|
NOPULL | No pull up resistor | |
PULLUP | Internal pull up enabled | |
PULLDOWN | Internal pull down enabled |
Configures whether an internal Pull up or Pull down resistor is used.
Internal Pull up/down resistors are typically 40k ohms, and will be between 30k and 50k
When the Pin is configured in Analog mode, the pull up/down resistors are disabled by hardware.
enum Speed¶
Enumerator | Value | Description |
---|---|---|
LOW | ||
MEDIUM | ||
HIGH | ||
VERY_HIGH |
Output speed controls the drive strength, and slew rate of the pin.
Public Functions Documentation¶
function GPIO¶
function Init¶
Initialize the GPIO from a Config struct.
Parameters:
- cfg reference to a Config struct populated with the desired settings
function Init¶
Initialize the GPIO with a Configuration struct, and explicit pin.
Parameters:
- p Pin specifying the physical connection on the hardware
- cfg reference to a Config struct populated with the desired settings. Config::pin will be overwritten
function Init¶
Explicity initialize all configuration for the GPIO.
Parameters:
- p Pin specifying the physical connection on the hardware
- m Mode specifying the behavior of the GPIO (input, output, etc.). Defaults to Mode::INPUT
- pu Pull up/down state for the GPIO. Defaults to Pull::NOPULL
- sp Speed setting for drive strength/slew rate. Defaults to Speed::Slow
function DeInit¶
Deinitializes the GPIO pin.
function Read¶
Reads the state of the GPIO.
Return: State of the GPIO unless Mode is set to Mode::Analog, then always false
function Write¶
Changes the state of the GPIO hardware when configured as an OUTPUT.
Parameters:
- state setting true writes an output HIGH, while setting false writes an output LOW.
function Toggle¶
flips the current state of the GPIO. If it was HIGH, it will go LOW, and vice versa.
function GetConfig¶
Return a reference to the internal Config struct
---¶
Updated on 2024-01-03 at 19:41:01 +0000