libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
daisy::GPIO Class Reference

General Purpose I/O control. More...

Detailed Description

General Purpose I/O control.

peripheral control over a single GPIO

Button Input (with internal Pullup resistor)

// 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;
int main(void)
{
// Initialize the Daisy Seed
hw.Init();
// Start the Serial Logger
hw.StartLog();
// Create a GPIO object
// Initialize the GPIO object
// Mode: INPUT - because we want to read from the button
// Pullup: Internal resistor to prevent needing extra components
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
// Print the Button state
hw.PrintLine("Button State: %s", button_state ? "true" : "false");
}
}
This is the higher-level interface for the Daisy board. All basic peripheral configuration/initiali...
Definition daisy_seed.h:19
General Purpose I/O control.
Definition gpio.h:22
Definition leddriver.h:33
void Init(I2CHandle i2c, const uint8_t(&addresses)[numDrivers], DmaBuffer dma_buffer_a, DmaBuffer dma_buffer_b, dsy_gpio_pin oe_pin={DSY_GPIOX, 0})
Definition leddriver.h:65
void SetLed(int ledIndex, float brightness)
Definition leddriver.h:109
Definition daisy_seed.h:195
Hardware defines and helpers for daisy field platform.
Definition index.h:2

Output Example (perfect for blinking an LED)

// 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;
int main(void)
{
// Initialize the Daisy Seed hardware
hw.Init();
// Create an LED
// Initialize it to pin D1 as an 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)
// Set the pin LOW
my_led.Write(false);
// Wait another half a second (500ms)
// You can also use Toggle to change the state
my_led.Toggle();
// Wait another half a second (500ms)
// And once more to flip it back
my_led.Toggle();
}
}
static void Delay(uint32_t delay_ms)

#include <gpio.h>

Classes

struct  Config
 Configuration for a given GPIO. More...
 

Public Types

enum class  Mode { INPUT , OUTPUT , OPEN_DRAIN , ANALOG }
 Mode of operation for the specified GPIO. More...
 
enum class  Pull { NOPULL , PULLUP , PULLDOWN }
 Configures whether an internal Pull up or Pull down resistor is used. More...
 
enum class  Speed { LOW , MEDIUM , HIGH , VERY_HIGH }
 Output speed controls the drive strength, and slew rate of the pin. More...
 

Public Member Functions

 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.
 
ConfigGetConfig ()
 

Member Enumeration Documentation

◆ Mode

Mode of operation for the specified GPIO.

Enumerator
INPUT 

Input for reading state of pin

OUTPUT 

Output w/ push-pull configuration

OPEN_DRAIN 

Output w/ open-drain configuration

ANALOG 

Analog for connection to ADC or DAC peripheral

◆ Pull

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.

Enumerator
NOPULL 

No pull up resistor

PULLUP 

Internal pull up enabled

PULLDOWN 

Internal pull down enabled

◆ Speed

Output speed controls the drive strength, and slew rate of the pin.

Enumerator
LOW 
MEDIUM 
HIGH 
VERY_HIGH 

Constructor & Destructor Documentation

◆ GPIO()

daisy::GPIO::GPIO ( )
inline

Member Function Documentation

◆ DeInit()

void daisy::GPIO::DeInit ( )

Deinitializes the GPIO pin.

◆ GetConfig()

Config & daisy::GPIO::GetConfig ( )
inline

Return a reference to the internal Config struct

◆ Init() [1/3]

void daisy::GPIO::Init ( const Config cfg)

Initialize the GPIO from a Config struct.

Parameters
cfgreference to a Config struct populated with the desired settings

◆ Init() [2/3]

void daisy::GPIO::Init ( Pin  p,
const Config cfg 
)

Initialize the GPIO with a Configuration struct, and explicit pin.

Parameters
pPin specifying the physical connection on the hardware
cfgreference to a Config struct populated with the desired settings. Config::pin will be overwritten

◆ Init() [3/3]

void daisy::GPIO::Init ( Pin  p,
Mode  m = Mode::INPUT,
Pull  pu = Pull::NOPULL,
Speed  sp = Speed::LOW 
)

Explicity initialize all configuration for the GPIO.

Parameters
pPin specifying the physical connection on the hardware
mMode specifying the behavior of the GPIO (input, output, etc.). Defaults to Mode::INPUT
puPull up/down state for the GPIO. Defaults to Pull::NOPULL
spSpeed setting for drive strength/slew rate. Defaults to Speed::Slow

◆ Read()

bool daisy::GPIO::Read ( )

Reads the state of the GPIO.

Returns
State of the GPIO unless Mode is set to Mode::Analog, then always false

◆ Toggle()

void daisy::GPIO::Toggle ( )

flips the current state of the GPIO. If it was HIGH, it will go LOW, and vice versa.

◆ Write()

void daisy::GPIO::Write ( bool  state)

Changes the state of the GPIO hardware when configured as an OUTPUT.

Parameters
statesetting true writes an output HIGH, while setting false writes an output LOW.

The documentation for this class was generated from the following file: