COSA
An Object-Oriented Platform for Arduino Programming
Touch.cpp
Go to the documentation of this file.
1 
21 #include "Touch.hh"
22 #include "Cosa/RTT.hh"
23 
24 Touch::Touch(Job::Scheduler* scheduler, Board::DigitalPin pin, uint16_t threshold) :
25  IOPin(pin),
26  Periodic(scheduler, SAMPLE_RATE),
27  THRESHOLD(threshold),
28  m_sampling(false),
29  m_touched(false)
30 {
32  clear();
33 }
34 
35 void
37 {
38  // Check if sampling should be initiated
39  if (!m_sampling) {
41  m_sampling = true;
42  return;
43  }
44 
45  // Sample the pin and discharge
46  uint8_t state = is_clear();
48  clear();
49  m_sampling = false;
50 
51  // Was the pin discharge during the sampling period
52  if (state) {
53  m_start = RTT::millis();
54  if (!m_touched) {
55  on_touch();
56  m_touched = true;
57  }
58  return;
59  }
60 
61  // The pin was discharge; low-pass filter pin change
62  if (m_touched && (RTT::since(m_start) > THRESHOLD)) {
63  m_touched = false;
64  }
65 }
66 
uint8_t m_touched
Definition: Touch.hh:73
Mode mode() const
Definition: IOPin.hh:71
Touch(Job::Scheduler *scheduler, Board::DigitalPin pin, uint16_t threshold=250)
Definition: Touch.cpp:24
static uint32_t since(uint32_t start)
Definition: RTT.hh:107
uint32_t m_start
Definition: Touch.hh:67
virtual void on_touch()=0
static uint32_t millis()
Definition: RTT.cpp:121
virtual void run()
Definition: Touch.cpp:36
Definition: IOPin.hh:29
bool is_clear() const
Definition: Pin.hh:142
void clear() const
Definition: OutputPin.hh:124
const uint16_t THRESHOLD
Definition: Touch.hh:64
uint8_t m_sampling
Definition: Touch.hh:70