libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
switch.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_SWITCH_H
3#define DSY_SWITCH_H
4#include "daisy_core.h"
5#include "per/gpio.h"
6#include "sys/system.h"
7
8namespace daisy
9{
17class Switch
18{
19 public:
32
33 Switch() {}
35
44 void Init(Pin pin,
45 float update_rate,
46 Type t,
49
55 void Init(Pin pin, float update_rate = 0.f);
56
62 void Debounce();
63
65 inline bool RisingEdge() const { return updated_ ? state_ == 0x7f : false; }
66
68 inline bool FallingEdge() const
69 {
70 return updated_ ? state_ == 0x80 : false;
71 }
72
74 inline bool Pressed() const { return state_ == 0xff; }
75
77 inline bool RawState()
78 {
79 const bool raw = hw_gpio_.Read();
80 return flip_ ? !raw : raw;
81 }
82
84 inline float TimeHeldMs() const
85 {
86 return Pressed() ? System::GetNow() - rising_edge_time_ : 0;
87 }
88
92 inline void SetUpdateRate(float update_rate) {}
93
94 private:
95 uint32_t last_update_;
96 bool updated_;
97 Type t_;
98 GPIO hw_gpio_;
99 uint8_t state_;
100 bool flip_;
101 float rising_edge_time_;
102};
103
104} // namespace daisy
105#endif
General Purpose I/O control.
Definition gpio.h:22
bool Read()
Reads the state of the GPIO.
Pull
Configures whether an internal Pull up or Pull down resistor is used.
Definition gpio.h:42
Definition leddriver.h:33
Definition switch.h:18
float TimeHeldMs() const
Definition switch.h:84
bool RisingEdge() const
Definition switch.h:65
bool RawState()
Definition switch.h:77
~Switch()
Definition switch.h:34
Type
Definition switch.h:22
@ TYPE_TOGGLE
Definition switch.h:23
@ TYPE_MOMENTARY
Definition switch.h:24
void SetUpdateRate(float update_rate)
Definition switch.h:92
void Init(Pin pin, float update_rate, Type t, Polarity pol, GPIO::Pull pu=GPIO::Pull::PULLUP)
bool Pressed() const
Definition switch.h:74
Switch()
Definition switch.h:33
bool FallingEdge() const
Definition switch.h:68
void Init(Pin pin, float update_rate=0.f)
Polarity
Definition switch.h:28
@ POLARITY_INVERTED
Definition switch.h:30
@ POLARITY_NORMAL
Definition switch.h:29
static uint32_t GetNow()
Hardware defines and helpers for daisy field platform.
Definition index.h:2
representation of hardware port/pin combination
Definition daisy_core.h:193