FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
pwm.h
Go to the documentation of this file.
1 // Copyright 2016-2021 Jean-Francois Poilpret
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
21 #ifndef PWM_H
22 #define PWM_H
23 
24 #include "boards/board.h"
25 #include "boards/board_traits.h"
26 #include "gpio.h"
27 #include "timer.h"
28 
29 namespace board
30 {
31  template<PWMPin PWM> constexpr DigitalPin PWM_PIN() INLINE;
35  template<PWMPin PWM> constexpr DigitalPin PWM_PIN()
36  {
37  return board_traits::PWMPin_trait<PWM>::ACTUAL_PIN;
38  }
39 };
40 
41 namespace analog
42 {
44 
53  template<board::PWMPin PWMPIN_, bool PULSED_ = false> class PWMOutput
54  {
55  public:
57  static constexpr const board::PWMPin PWMPIN = PWMPIN_;
62  static constexpr const bool PULSED = PULSED_;
63 
64  private:
65  using TRAIT = board_traits::PWMPin_trait<PWMPIN>;
66  using TIMER_TRAIT = board_traits::Timer_trait<TRAIT::TIMER>;
67 
68  public:
69  PWMOutput(const PWMOutput<PWMPIN_, PULSED_>&) = delete;
70  PWMOutput<PWMPIN_, PULSED_>& operator=(const PWMOutput<PWMPIN_, PULSED_>&) = delete;
71 
73  static constexpr const board::DigitalPin PIN = TRAIT::ACTUAL_PIN;
74 
76  static constexpr const uint8_t COM = TRAIT::COM;
78 
83 
93  PWMOutput(TIMER& timer, TimerOutputMode output_mode = TimerOutputMode::NON_INVERTING) : timer_{timer}
94  {
95  // Initialize pin as output
97  if (TIMER_TRAIT::IS_16BITS || (!PULSED))
98  // Set com mode for pin
99  timer_.template set_output_mode<COM>(output_mode);
100  }
101 
109  {
110  if (TIMER_TRAIT::IS_16BITS || (!PULSED)) timer_.template set_output_mode<COM>(output_mode);
111  }
112 
119  using TYPE = typename TIMER_TRAIT::TYPE;
120 
126  static constexpr const TYPE MAX = TIMER_TRAIT::MAX_PWM;
127 
135  void set_duty(TYPE duty)
136  {
137  timer_.template set_max<COM>(duty);
138  }
139 
140  private:
141  TIMER& timer_;
142  };
143 }
144 
145 #endif /* PWM_H */
146 
gpio::FastPinType::set_mode
static void set_mode(PinMode mode, bool value=false)
Set mode (direction) and value (if output) of DPIN.
Definition: gpio.h:574
analog::PWMOutput::set_output_mode
void set_output_mode(TimerOutputMode output_mode)
Change the connection output mode of this PWM pin to its timer.
Definition: pwm.h:108
timer
Defines all API to manipulate AVR Timers.
Definition: pulse_timer.h:116
board::PWM_PIN
constexpr DigitalPin PWM_PIN() INLINE
Convert an PWMPin to the matching DigitalPin.
Definition: pwm.h:35
board::PWMPin
PWMPin
Defines all digital output pins of ATmega644, capable of PWM output.
Definition: atmega_xx4.h:219
board
Defines all types and constants specific to support Arduino MEGA board (ATmega644 MCU target).
Definition: atmega_xx4.h:39
timer.h
Timer API.
timer::Timer
General API to handle an AVR timer.
Definition: timer.h:705
analog::PWMOutput::MAX
static constexpr const TYPE MAX
The maximum acceptable value for duty in set_duty().
Definition: pwm.h:126
analog::PWMOutput
Construct a new handler for a PWM output pin.
Definition: pwm.h:54
gpio.h
General Purpose (digital) Input Output API.
analog::PWMOutput::PWMPIN
static constexpr const board::PWMPin PWMPIN
The PWM pin for this PWMOutput.
Definition: pwm.h:57
analog::PWMOutput::TYPE
typename TIMER_TRAIT::TYPE TYPE
The type (either uint8_t or uint16_t) of values acceptable for duty in set_duty().
Definition: pwm.h:119
analog::PWMOutput::PWMOutput
PWMOutput(TIMER &timer, TimerOutputMode output_mode=TimerOutputMode::NON_INVERTING)
Construct a new PWM output pin, connected to timer, using output_mode.
Definition: pwm.h:93
analog::PWMOutput::set_duty
void set_duty(TYPE duty)
Set the duty cycle for this PWM pin, from 0 (0% duty cycle) to MAX (100%), any value above MAX will b...
Definition: pwm.h:135
timer::TimerOutputMode
TimerOutputMode
Defines the "connection" between this timer and specific PWM output pins.
Definition: timer.h:263
analog::PWMOutput::PULSED
static constexpr const bool PULSED
Whether this PWMOutput uses a timer::PulseTimer instead of a timer::Timer.
Definition: pwm.h:62
gpio::PinMode::OUTPUT
@ OUTPUT
Digital pin is configured as output.
analog::PWMOutput::TIMER
timer::Timer< TRAIT::TIMER > TIMER
The actual timer::Timer type associated to this PWMOutput.
Definition: pwm.h:82
analog
Defines all API to manipulate analog input/output.
Definition: analog_comparator.h:78
board::DigitalPin
DigitalPin
Defines all available digital input/output pins of ATmega644, with reference to Arduino MEGA pins.
Definition: atmega_xx4.h:76
INLINE
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57
analog::PWMOutput::PIN
static constexpr const board::DigitalPin PIN
The digital pin for this PWMOutput.
Definition: pwm.h:73