FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
pwm.h
Go to the documentation of this file.
1// Copyright 2016-2023 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
29namespace 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
41namespace 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&) = delete;
70 PWMOutput& operator=(const PWMOutput&) = 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
108 void set_output_mode(TimerOutputMode output_mode)
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 */
Construct a new handler for a PWM output pin.
Definition: pwm.h:54
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
timer::Timer< TRAIT::TIMER > TIMER
The actual timer::Timer type associated to this PWMOutput.
Definition: pwm.h:82
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
static constexpr const board::DigitalPin PIN
The digital pin for this PWMOutput.
Definition: pwm.h:73
static constexpr const TYPE MAX
The maximum acceptable value for duty in set_duty().
Definition: pwm.h:126
static constexpr const board::PWMPin PWMPIN
The PWM pin for this PWMOutput.
Definition: pwm.h:57
static constexpr const bool PULSED
Whether this PWMOutput uses a timer::PulseTimer instead of a timer::Timer.
Definition: pwm.h:62
void set_output_mode(TimerOutputMode output_mode)
Change the connection output mode of this PWM pin to its timer.
Definition: pwm.h:108
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
static void set_mode(PinMode mode, bool value=false)
Set mode (direction) and value (if output) of DPIN.
Definition: gpio.h:580
General API to handle an AVR timer.
Definition: timer.h:705
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57
General Purpose (digital) Input Output API.
Defines all API to manipulate analog input/output.
Defines all types and constants specific to support a specific MCU target.
Definition: empty.h:38
PWMPin
Defines all digital output pins of target MCU, capable of PWM output.
Definition: empty.h:84
DigitalPin
Defines all available digital input/output pins of the target MCU.
Definition: empty.h:56
constexpr DigitalPin PWM_PIN() INLINE
Convert an PWMPin to the matching DigitalPin.
Definition: pwm.h:35
@ OUTPUT
Digital pin is configured as output.
Defines all API to manipulate AVR Timers.
Definition: pulse_timer.h:116
TimerOutputMode
Defines the "connection" between this timer and specific PWM output pins.
Definition: timer.h:263
Timer API.