COSA
An Object-Oriented Platform for Arduino Programming
Servo.cpp
Go to the documentation of this file.
1 
21 #include "Cosa/Board.hh"
22 #if !defined(BOARD_ATTINYX5)
23 #include "Servo.hh"
24 
25 Servo* Servo::servo[2] = { 0, 0 };
26 
27 #define US_TO_TICKS(us) ((I_CPU * (us)) / 8)
28 
29 bool
31 {
32  TCCR1A = 0;
33  TCCR1B = _BV(CS11);
34  TCNT1 = 0;
35  TIMSK1 |= _BV(OCIE1A);
36  TIMSK1 |= _BV(OCIE1B);
37  OCR1A = TCNT1 + PERIOD;
38  OCR1B = TCNT1 + PERIOD;
39  return (true);
40 }
41 
42 bool
44 {
45  TIMSK1 &= ~_BV(OCIE1A);
46  TIMSK1 &= ~_BV(OCIE1B);
47  return (true);
48 }
49 
50 void
51 Servo::angle(uint8_t degree)
52 {
53  if (UNLIKELY(degree > 180)) degree = 180;
54  uint16_t width = (((uint32_t) (m_max - m_min)) * degree) / 180L;
55  synchronized {
56  m_width = m_min + width;
57  m_angle = degree;
58  }
59 }
60 
62 {
63  Servo* servo = Servo::servo[0];
64  if (UNLIKELY(servo == NULL)) return;
65  servo->toggle();
66  if (servo->is_set())
67  OCR1A = TCNT1 + US_TO_TICKS(servo->m_width);
68  else
69  OCR1A = TCNT1 + US_TO_TICKS(Servo::PERIOD - servo->m_width);
70 }
71 
73 {
74  Servo* servo = Servo::servo[1];
75  if (UNLIKELY(servo == NULL)) return;
76  servo->toggle();
77  if (servo->is_set())
78  OCR1B = TCNT1 + US_TO_TICKS(servo->m_width);
79  else
80  OCR1B = TCNT1 + US_TO_TICKS(Servo::PERIOD - servo->m_width);
81 }
82 
83 #endif
#define NULL
Definition: Types.h:101
ISR(TIMER1_COMPA_vect)
Definition: Servo.cpp:61
#define TIMSK1
Definition: ATtinyX5.hh:229
friend void TIMER1_COMPB_vect(void)
static bool begin()
Definition: Servo.cpp:30
uint16_t width() const
Definition: Servo.hh:78
bool is_set() const
Definition: Pin.hh:112
uint8_t angle() const
Definition: Servo.hh:93
void toggle() const
Definition: OutputPin.hh:163
static bool end()
Definition: Servo.cpp:43
#define UNLIKELY(x)
Definition: Types.h:153
#define US_TO_TICKS(us)
Definition: Servo.cpp:27
friend void TIMER1_COMPA_vect(void)
Definition: Servo.hh:34