COSA
An Object-Oriented Platform for Arduino Programming
AnalogPin.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_ANALOG_PIN_HH
22 #define COSA_ANALOG_PIN_HH
23 
24 #include "Cosa/Interrupt.hh"
25 #include "Cosa/Event.hh"
26 #include "Cosa/Types.h"
27 
33 public:
41  m_pin(pin),
42  m_reference(ref),
43  m_value(0),
44  m_event(Event::NULL_TYPE)
45  {}
46 
52  {
53  m_reference = ref;
54  }
55 
61  {
62  return (m_reference);
63  }
64 
70  {
71  return (m_pin);
72  }
73 
79  uint16_t value() const
80  {
81  uint16_t res;
82  synchronized res = m_value;
83  return (res);
84  }
85 
90  static void prescale(uint8_t factor);
91 
99  static uint16_t sample(Board::AnalogPin pin,
101 
108  static uint16_t bandgap(uint16_t vref = 1100);
109 
113  static void powerup()
114  __attribute__((always_inline))
115  {
117  bit_set(ADCSRA, ADEN);
118  }
119 
123  static void powerdown()
124  __attribute__((always_inline))
125  {
126  bit_clear(ADCSRA, ADEN);
128  }
129 
135  uint16_t sample()
136  __attribute__((always_inline))
137  {
139  }
140 
147  AnalogPin& operator>>(uint16_t& var)
148  __attribute__((always_inline))
149  {
150  var = sample();
151  return (*this);
152  }
153 
159  operator uint16_t()
160  __attribute__((always_inline))
161  {
162  return (sample());
163  }
164 
171  bool sample_request(uint8_t event = Event::NULL_TYPE)
172  {
173  m_event = event;
175  }
176 
181  uint16_t sample_await();
182 
188  virtual void on_interrupt(uint16_t arg);
189 
195  virtual void on_change(uint16_t value)
196  {
197  UNUSED(value);
198  }
199 
200 protected:
204  uint16_t m_value;
205  uint8_t m_event;
206 
214  bool sample_request(Board::AnalogPin pin, uint8_t ref);
215 
224  virtual void on_event(uint8_t type, uint16_t value);
225 
227  friend void ADC_vect(void);
228 };
229 
230 #endif
Board::AnalogPin pin() const
Definition: AnalogPin.hh:69
static void prescale(uint8_t factor)
uint8_t m_event
Event to push on completion.
Definition: AnalogPin.hh:205
friend void ADC_vect(void)
#define bit_set(p, b)
Definition: Bits.h:35
static void powerdown()
Definition: AnalogPin.hh:123
AnalogPin & operator>>(uint16_t &var)
Definition: AnalogPin.hh:147
#define bit_clear(p, b)
Definition: Bits.h:36
static void powerup()
Definition: AnalogPin.hh:113
const Board::AnalogPin m_pin
Analog channel number.
Definition: AnalogPin.hh:202
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
static void adc_disable()
Definition: Power.hh:62
#define UNUSED(x)
Definition: ATmega328P.hh:31
static uint16_t bandgap(uint16_t vref=1100)
AnalogPin(Board::AnalogPin pin, Board::Reference ref=Board::AVCC_REFERENCE)
Definition: AnalogPin.hh:40
Board::Reference reference() const
Definition: AnalogPin.hh:60
Definition: Event.hh:39
static void adc_enable()
Definition: Power.hh:55
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
uint16_t sample()
Definition: AnalogPin.hh:135
void reference(Board::Reference ref)
Definition: AnalogPin.hh:51
Board::Reference m_reference
ADC reference voltage type.
Definition: AnalogPin.hh:203
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