COSA
An Object-Oriented Platform for Arduino Programming
AnalogPin.cpp
Go to the documentation of this file.
1 
21 #include "Cosa/AnalogPin.hh"
22 
23 bool
25 {
26  loop_until_bit_is_clear(ADCSRA, ADSC);
27  if (UNLIKELY(sampling_pin != NULL)) return (false);
28  sampling_pin = this;
29  ADMUX = (ref | (pin & 0x1f));
30 #if defined(MUX5)
31  bit_write(pin & 0x20, ADCSRB, MUX5);
32 #endif
33  bit_mask_set(ADCSRA, _BV(ADEN) | _BV(ADSC) | _BV(ADIE));
34  return (true);
35 }
36 
37 uint16_t
39 {
40  if (UNLIKELY(sampling_pin != this)) return (m_value);
41  synchronized {
43  bit_clear(ADCSRA, ADIE);
44  }
45  loop_until_bit_is_clear(ADCSRA, ADSC);
46  synchronized m_value = ADCW;
47  return (m_value);
48 }
49 
50 void
51 AnalogPin::on_event(uint8_t type, uint16_t value)
52 {
53  if (type == Event::TIMEOUT_TYPE) {
55  }
56  else if (type == Event::SAMPLE_COMPLETED_TYPE) {
57  if (value != m_value) {
58  m_value = value;
59  on_change(value);
60  }
61  }
62 }
63 
64 void
66 {
67  uint8_t event = sampling_pin->m_event;
68  if (event == Event::NULL_TYPE)
70  else
71  Event::push(event, this, value);
73 }
74 
76 {
77  bit_clear(ADCSRA, ADIE);
78  if (UNLIKELY(AnalogPin::sampling_pin == NULL)) return;
80 }
uint8_t m_event
Event to push on completion.
Definition: AnalogPin.hh:205
friend void ADC_vect(void)
#define NULL
Definition: Types.h:101
#define bit_clear(p, b)
Definition: Bits.h:36
ISR(ADC_vect)
Definition: AnalogPin.cpp:75
uint16_t sample_await()
Definition: AnalogPin.cpp:38
virtual void on_change(uint16_t value)
Definition: AnalogPin.hh:195
virtual void on_interrupt(uint16_t arg)
Definition: AnalogPin.cpp:65
#define bit_write(c, p, b)
Definition: Bits.h:38
#define bit_mask_set(p, m)
Definition: Bits.h:29
virtual void on_event(uint8_t type, uint16_t value)
Definition: AnalogPin.cpp:51
static AnalogPin * sampling_pin
Current sampling pin if any.
Definition: AnalogPin.hh:201
#define UNLIKELY(x)
Definition: Types.h:153
static bool push(uint8_t type, Handler *target, uint16_t value=0)
Definition: Event.hh:180
#define ADCW
Definition: Leonardo.hh:304
bool sample_request(uint8_t event=Event::NULL_TYPE)
Definition: AnalogPin.hh:171
uint16_t value() const
Definition: AnalogPin.hh:79
uint16_t m_value
Latest sample value.
Definition: AnalogPin.hh:204