File switch.h¶
File List > external-docs > libDaisy > src > hid > switch.h
Go to the documentation of this file
Source Code¶
#pragma once
#ifndef DSY_SWITCH_H
#define DSY_SWITCH_H
#include "daisy_core.h"
#include "per/gpio.h"
#include "sys/system.h"
namespace daisy
{
class Switch
{
public:
enum Type
{
TYPE_TOGGLE,
TYPE_MOMENTARY,
};
enum Polarity
{
POLARITY_NORMAL,
POLARITY_INVERTED,
};
Switch() {}
~Switch() {}
void Init(Pin pin,
float update_rate,
Type t,
Polarity pol,
GPIO::Pull pu = GPIO::Pull::PULLUP);
void Init(Pin pin, float update_rate = 0.f);
void Debounce();
inline bool RisingEdge() const { return updated_ ? state_ == 0x7f : false; }
inline bool FallingEdge() const
{
return updated_ ? state_ == 0x80 : false;
}
inline bool Pressed() const { return state_ == 0xff; }
inline bool RawState()
{
const bool raw = hw_gpio_.Read();
return flip_ ? !raw : raw;
}
inline float TimeHeldMs() const
{
return Pressed() ? System::GetNow() - rising_edge_time_ : 0;
}
inline void SetUpdateRate(float update_rate) {}
private:
uint32_t last_update_;
bool updated_;
Type t_;
GPIO hw_gpio_;
uint8_t state_;
bool flip_;
float rising_edge_time_;
};
} // namespace daisy
#endif