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
40
41 Switch() {}
43
52 void
54
60 void Init(dsy_gpio_pin pin, float update_rate = 0.f);
61
67 void Debounce();
68
70 inline bool RisingEdge() const { return updated_ ? state_ == 0x7f : false; }
71
73 inline bool FallingEdge() const
74 {
75 return updated_ ? state_ == 0x80 : false;
76 }
77
79 inline bool Pressed() const { return state_ == 0xff; }
80
82 inline bool RawState()
83 {
84 return flip_ ? !dsy_gpio_read(&hw_gpio_) : dsy_gpio_read(&hw_gpio_);
85 }
86
88 inline float TimeHeldMs() const
89 {
90 return Pressed() ? System::GetNow() - rising_edge_time_ : 0;
91 }
92
96 inline void SetUpdateRate(float update_rate) {}
97
98 private:
99 uint32_t last_update_;
100 bool updated_;
101 Type t_;
102 dsy_gpio hw_gpio_;
103 uint8_t state_;
104 bool flip_;
105 float rising_edge_time_;
106};
107
108} // namespace daisy
109#endif
Definition leddriver.h:33
Definition switch.h:18
void Init(dsy_gpio_pin pin, float update_rate=0.f)
float TimeHeldMs() const
Definition switch.h:88
bool RisingEdge() const
Definition switch.h:70
bool RawState()
Definition switch.h:82
~Switch()
Definition switch.h:42
void Init(dsy_gpio_pin pin, float update_rate, Type t, Polarity pol, Pull pu)
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:96
Pull
Definition switch.h:35
@ PULL_UP
Definition switch.h:36
@ PULL_NONE
Definition switch.h:38
@ PULL_DOWN
Definition switch.h:37
bool Pressed() const
Definition switch.h:79
Switch()
Definition switch.h:41
bool FallingEdge() const
Definition switch.h:73
Polarity
Definition switch.h:28
@ POLARITY_INVERTED
Definition switch.h:30
@ POLARITY_NORMAL
Definition switch.h:29
static uint32_t GetNow()
uint8_t dsy_gpio_read(const dsy_gpio *p)
Hardware defines and helpers for daisy field platform.
Definition index.h:2
Definition daisy_core.h:205
Definition gpio.h:175