NXPCUP-libary
Library for car's control board on NXPCUP competition based on the Mbed framework.
SoftPWM.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013-2018 syouichi imamori
3  * (https://os.mbed.com/users/komaida424/code/SoftPWM/)
4  * Copyright 2019 JarekParal (https://github.com/JarekParal)
5  *
6  * Partially reformat by JarekParal (github.com/JarekParal) in 2019.
7  * - rewrite to one header file
8  * - add namespace nxpcup
9  * - rewrite _ticker.attach() params due to deprecated variant:
10  * https://os.mbed.com/docs/mbed-os/v5.7/mbed-os-api-doxy/deprecated.html#_deprecated000007
11  *
12  * This file is under the Apache License 2.0
13  * (https://choosealicense.com/licenses/apache-2.0/).
14  */
15 
16 #pragma once
17 
18 #include "mbed.h"
19 
20 #include "SoftPWM.h"
21 
22 namespace nxpcup {
23 
24 class SoftPWM {
25 public:
26  SoftPWM(PinName _outpin, bool _positive = true)
27  : pulse(_outpin)
28  {
29  if (_positive) {
30  pulse = 0;
31  } else {
32  pulse = 1;
33  }
34  positive = _positive;
35  interval = 0.02;
36  width = 0;
37  start();
38  }
39 
40  operator float()
41  {
42  if (width <= 0.0) {
43  return 0.0;
44  }
45  if (width > 1.0) {
46  return 1.0;
47  }
48  return width / interval;
49  }
50 
51  SoftPWM& operator=(float duty)
52  {
53  width = interval * duty;
54  if (duty <= 0.0) {
55  width = 0.0;
56  }
57  if (duty > 1.0) {
58  width = interval;
59  }
60  return *this;
61  }
62 
63  float read()
64  {
65  if (width <= 0.0) {
66  return 0.0;
67  }
68  if (width > 1.0) {
69  return 1.0;
70  }
71  return width / interval;
72  }
73 
74  void write(float duty)
75  {
76  width = interval * duty;
77  if (duty <= 0.0) {
78  width = 0.0;
79  }
80  if (duty > 1.0) {
81  width = interval;
82  }
83  }
84 
85  void start()
86  {
87  _ticker.attach(callback(this, &SoftPWM::TickerInterrapt), interval);
88  }
89 
90  void stop()
91  {
92  _ticker.detach();
93  if (positive) {
94  pulse = 0;
95  } else {
96  pulse = 1;
97  }
98  wait(width);
99  }
100 
101  void period(float _period)
102  {
103  interval = _period;
104  start();
105  }
106 
107  void period_ms(int _period)
108  {
109  period(float(_period) / 1000);
110  start();
111  }
112 
113  void period_us(int _period)
114  {
115  period(float(_period) / 1000000);
116  start();
117  }
118 
119  void pulsewidth(float _width)
120  {
121  width = _width;
122  if (width < 0.0) {
123  width = 0.0;
124  }
125  }
126 
127  void pulsewidth_ms(int _width) { pulsewidth(float(_width) / 1000); }
128 
129  void pulsewidth_us(int _width) { pulsewidth(float(_width) / 1000000); }
130 
131 private:
132  Timeout _timeout;
133  Ticker _ticker;
134  DigitalOut pulse;
135  bool positive;
136  float width;
137  float interval;
138 
139  void end()
140  {
141  if (positive) {
142  pulse = 0;
143  } else {
144  pulse = 1;
145  }
146  // _timeout.detach();
147  }
148 
149  void TickerInterrapt()
150  {
151  if (width <= 0) {
152  return;
153  }
154  _timeout.attach(callback(this, &SoftPWM::end), width);
155  if (positive) {
156  pulse = 1;
157  } else {
158  pulse = 0;
159  }
160  }
161 };
162 
163 } // namespace nxpcup
void pulsewidth_us(int _width)
Definition: SoftPWM.h:129
void period_ms(int _period)
Definition: SoftPWM.h:107
void write(float duty)
Definition: SoftPWM.h:74
SoftPWM & operator=(float duty)
Definition: SoftPWM.h:51
void period_us(int _period)
Definition: SoftPWM.h:113
Definition: BorderDetector.h:6
SoftPWM(PinName _outpin, bool _positive=true)
Definition: SoftPWM.h:26
Definition: SoftPWM.h:24
void start()
Definition: SoftPWM.h:85
float read()
Definition: SoftPWM.h:63
void period(float _period)
Definition: SoftPWM.h:101
void pulsewidth(float _width)
Definition: SoftPWM.h:119
void pulsewidth_ms(int _width)
Definition: SoftPWM.h:127
void stop()
Definition: SoftPWM.h:90