The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:20e0c61e0684 1 /* mbed Microcontroller Library
ganlikun 0:20e0c61e0684 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:20e0c61e0684 3 *
ganlikun 0:20e0c61e0684 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:20e0c61e0684 5 * you may not use this file except in compliance with the License.
ganlikun 0:20e0c61e0684 6 * You may obtain a copy of the License at
ganlikun 0:20e0c61e0684 7 *
ganlikun 0:20e0c61e0684 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:20e0c61e0684 9 *
ganlikun 0:20e0c61e0684 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:20e0c61e0684 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:20e0c61e0684 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:20e0c61e0684 13 * See the License for the specific language governing permissions and
ganlikun 0:20e0c61e0684 14 * limitations under the License.
ganlikun 0:20e0c61e0684 15 */
ganlikun 0:20e0c61e0684 16 #ifndef MBED_PWMOUT_H
ganlikun 0:20e0c61e0684 17 #define MBED_PWMOUT_H
ganlikun 0:20e0c61e0684 18
ganlikun 0:20e0c61e0684 19 #include "platform/platform.h"
ganlikun 0:20e0c61e0684 20
ganlikun 0:20e0c61e0684 21 #if defined (DEVICE_PWMOUT) || defined(DOXYGEN_ONLY)
ganlikun 0:20e0c61e0684 22 #include "hal/pwmout_api.h"
ganlikun 0:20e0c61e0684 23 #include "platform/mbed_critical.h"
ganlikun 0:20e0c61e0684 24 #include "platform/mbed_sleep.h"
ganlikun 0:20e0c61e0684 25
ganlikun 0:20e0c61e0684 26 namespace mbed {
ganlikun 0:20e0c61e0684 27 /** \addtogroup drivers */
ganlikun 0:20e0c61e0684 28
ganlikun 0:20e0c61e0684 29 /** A pulse-width modulation digital output
ganlikun 0:20e0c61e0684 30 *
ganlikun 0:20e0c61e0684 31 * @note Synchronization level: Interrupt safe
ganlikun 0:20e0c61e0684 32 *
ganlikun 0:20e0c61e0684 33 * Example
ganlikun 0:20e0c61e0684 34 * @code
ganlikun 0:20e0c61e0684 35 * // Fade a led on.
ganlikun 0:20e0c61e0684 36 * #include "mbed.h"
ganlikun 0:20e0c61e0684 37 *
ganlikun 0:20e0c61e0684 38 * PwmOut led(LED1);
ganlikun 0:20e0c61e0684 39 *
ganlikun 0:20e0c61e0684 40 * int main() {
ganlikun 0:20e0c61e0684 41 * while(1) {
ganlikun 0:20e0c61e0684 42 * led = led + 0.01;
ganlikun 0:20e0c61e0684 43 * wait(0.2);
ganlikun 0:20e0c61e0684 44 * if(led == 1.0) {
ganlikun 0:20e0c61e0684 45 * led = 0;
ganlikun 0:20e0c61e0684 46 * }
ganlikun 0:20e0c61e0684 47 * }
ganlikun 0:20e0c61e0684 48 * }
ganlikun 0:20e0c61e0684 49 * @endcode
ganlikun 0:20e0c61e0684 50 * @ingroup drivers
ganlikun 0:20e0c61e0684 51 */
ganlikun 0:20e0c61e0684 52 class PwmOut {
ganlikun 0:20e0c61e0684 53
ganlikun 0:20e0c61e0684 54 public:
ganlikun 0:20e0c61e0684 55
ganlikun 0:20e0c61e0684 56 /** Create a PwmOut connected to the specified pin
ganlikun 0:20e0c61e0684 57 *
ganlikun 0:20e0c61e0684 58 * @param pin PwmOut pin to connect to
ganlikun 0:20e0c61e0684 59 */
ganlikun 0:20e0c61e0684 60 PwmOut(PinName pin) : _deep_sleep_locked(false) {
ganlikun 0:20e0c61e0684 61 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 62 pwmout_init(&_pwm, pin);
ganlikun 0:20e0c61e0684 63 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 64 }
ganlikun 0:20e0c61e0684 65
ganlikun 0:20e0c61e0684 66 ~PwmOut() {
ganlikun 0:20e0c61e0684 67 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 68 unlock_deep_sleep();
ganlikun 0:20e0c61e0684 69 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 70 }
ganlikun 0:20e0c61e0684 71
ganlikun 0:20e0c61e0684 72 /** Set the ouput duty-cycle, specified as a percentage (float)
ganlikun 0:20e0c61e0684 73 *
ganlikun 0:20e0c61e0684 74 * @param value A floating-point value representing the output duty-cycle,
ganlikun 0:20e0c61e0684 75 * specified as a percentage. The value should lie between
ganlikun 0:20e0c61e0684 76 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
ganlikun 0:20e0c61e0684 77 * Values outside this range will be saturated to 0.0f or 1.0f.
ganlikun 0:20e0c61e0684 78 */
ganlikun 0:20e0c61e0684 79 void write(float value) {
ganlikun 0:20e0c61e0684 80 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 81 lock_deep_sleep();
ganlikun 0:20e0c61e0684 82 pwmout_write(&_pwm, value);
ganlikun 0:20e0c61e0684 83 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 84 }
ganlikun 0:20e0c61e0684 85
ganlikun 0:20e0c61e0684 86 /** Return the current output duty-cycle setting, measured as a percentage (float)
ganlikun 0:20e0c61e0684 87 *
ganlikun 0:20e0c61e0684 88 * @returns
ganlikun 0:20e0c61e0684 89 * A floating-point value representing the current duty-cycle being output on the pin,
ganlikun 0:20e0c61e0684 90 * measured as a percentage. The returned value will lie between
ganlikun 0:20e0c61e0684 91 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
ganlikun 0:20e0c61e0684 92 *
ganlikun 0:20e0c61e0684 93 * @note
ganlikun 0:20e0c61e0684 94 * This value may not match exactly the value set by a previous write().
ganlikun 0:20e0c61e0684 95 */
ganlikun 0:20e0c61e0684 96 float read() {
ganlikun 0:20e0c61e0684 97 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 98 float val = pwmout_read(&_pwm);
ganlikun 0:20e0c61e0684 99 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 100 return val;
ganlikun 0:20e0c61e0684 101 }
ganlikun 0:20e0c61e0684 102
ganlikun 0:20e0c61e0684 103 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
ganlikun 0:20e0c61e0684 104 *
ganlikun 0:20e0c61e0684 105 * @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
ganlikun 0:20e0c61e0684 106 * @note
ganlikun 0:20e0c61e0684 107 * The resolution is currently in microseconds; periods smaller than this
ganlikun 0:20e0c61e0684 108 * will be set to zero.
ganlikun 0:20e0c61e0684 109 */
ganlikun 0:20e0c61e0684 110 void period(float seconds) {
ganlikun 0:20e0c61e0684 111 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 112 pwmout_period(&_pwm, seconds);
ganlikun 0:20e0c61e0684 113 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 114 }
ganlikun 0:20e0c61e0684 115
ganlikun 0:20e0c61e0684 116 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
ganlikun 0:20e0c61e0684 117 * @param ms Change the period of a PWM signal in milli-seconds without modifying the duty cycle
ganlikun 0:20e0c61e0684 118 */
ganlikun 0:20e0c61e0684 119 void period_ms(int ms) {
ganlikun 0:20e0c61e0684 120 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 121 pwmout_period_ms(&_pwm, ms);
ganlikun 0:20e0c61e0684 122 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 123 }
ganlikun 0:20e0c61e0684 124
ganlikun 0:20e0c61e0684 125 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
ganlikun 0:20e0c61e0684 126 * @param us Change the period of a PWM signal in micro-seconds without modifying the duty cycle
ganlikun 0:20e0c61e0684 127 */
ganlikun 0:20e0c61e0684 128 void period_us(int us) {
ganlikun 0:20e0c61e0684 129 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 130 pwmout_period_us(&_pwm, us);
ganlikun 0:20e0c61e0684 131 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 132 }
ganlikun 0:20e0c61e0684 133
ganlikun 0:20e0c61e0684 134 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
ganlikun 0:20e0c61e0684 135 * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
ganlikun 0:20e0c61e0684 136 */
ganlikun 0:20e0c61e0684 137 void pulsewidth(float seconds) {
ganlikun 0:20e0c61e0684 138 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 139 pwmout_pulsewidth(&_pwm, seconds);
ganlikun 0:20e0c61e0684 140 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 141 }
ganlikun 0:20e0c61e0684 142
ganlikun 0:20e0c61e0684 143 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
ganlikun 0:20e0c61e0684 144 * @param ms Change the pulse width of a PWM signal specified in milli-seconds
ganlikun 0:20e0c61e0684 145 */
ganlikun 0:20e0c61e0684 146 void pulsewidth_ms(int ms) {
ganlikun 0:20e0c61e0684 147 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 148 pwmout_pulsewidth_ms(&_pwm, ms);
ganlikun 0:20e0c61e0684 149 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 150 }
ganlikun 0:20e0c61e0684 151
ganlikun 0:20e0c61e0684 152 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
ganlikun 0:20e0c61e0684 153 * @param us Change the pulse width of a PWM signal specified in micro-seconds
ganlikun 0:20e0c61e0684 154 */
ganlikun 0:20e0c61e0684 155 void pulsewidth_us(int us) {
ganlikun 0:20e0c61e0684 156 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 157 pwmout_pulsewidth_us(&_pwm, us);
ganlikun 0:20e0c61e0684 158 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 159 }
ganlikun 0:20e0c61e0684 160
ganlikun 0:20e0c61e0684 161 /** A operator shorthand for write()
ganlikun 0:20e0c61e0684 162 * \sa PwmOut::write()
ganlikun 0:20e0c61e0684 163 */
ganlikun 0:20e0c61e0684 164 PwmOut& operator= (float value) {
ganlikun 0:20e0c61e0684 165 // Underlying call is thread safe
ganlikun 0:20e0c61e0684 166 write(value);
ganlikun 0:20e0c61e0684 167 return *this;
ganlikun 0:20e0c61e0684 168 }
ganlikun 0:20e0c61e0684 169
ganlikun 0:20e0c61e0684 170 /** A operator shorthand for write()
ganlikun 0:20e0c61e0684 171 * \sa PwmOut::write()
ganlikun 0:20e0c61e0684 172 */
ganlikun 0:20e0c61e0684 173 PwmOut& operator= (PwmOut& rhs) {
ganlikun 0:20e0c61e0684 174 // Underlying call is thread safe
ganlikun 0:20e0c61e0684 175 write(rhs.read());
ganlikun 0:20e0c61e0684 176 return *this;
ganlikun 0:20e0c61e0684 177 }
ganlikun 0:20e0c61e0684 178
ganlikun 0:20e0c61e0684 179 /** An operator shorthand for read()
ganlikun 0:20e0c61e0684 180 * \sa PwmOut::read()
ganlikun 0:20e0c61e0684 181 */
ganlikun 0:20e0c61e0684 182 operator float() {
ganlikun 0:20e0c61e0684 183 // Underlying call is thread safe
ganlikun 0:20e0c61e0684 184 return read();
ganlikun 0:20e0c61e0684 185 }
ganlikun 0:20e0c61e0684 186
ganlikun 0:20e0c61e0684 187 protected:
ganlikun 0:20e0c61e0684 188 /** Lock deep sleep only if it is not yet locked */
ganlikun 0:20e0c61e0684 189 void lock_deep_sleep() {
ganlikun 0:20e0c61e0684 190 if (_deep_sleep_locked == false) {
ganlikun 0:20e0c61e0684 191 sleep_manager_lock_deep_sleep();
ganlikun 0:20e0c61e0684 192 _deep_sleep_locked = true;
ganlikun 0:20e0c61e0684 193 }
ganlikun 0:20e0c61e0684 194 }
ganlikun 0:20e0c61e0684 195
ganlikun 0:20e0c61e0684 196 /** Unlock deep sleep in case it is locked */
ganlikun 0:20e0c61e0684 197 void unlock_deep_sleep() {
ganlikun 0:20e0c61e0684 198 if (_deep_sleep_locked == true) {
ganlikun 0:20e0c61e0684 199 sleep_manager_unlock_deep_sleep();
ganlikun 0:20e0c61e0684 200 _deep_sleep_locked = false;
ganlikun 0:20e0c61e0684 201 }
ganlikun 0:20e0c61e0684 202 }
ganlikun 0:20e0c61e0684 203
ganlikun 0:20e0c61e0684 204 pwmout_t _pwm;
ganlikun 0:20e0c61e0684 205 bool _deep_sleep_locked;
ganlikun 0:20e0c61e0684 206 };
ganlikun 0:20e0c61e0684 207
ganlikun 0:20e0c61e0684 208 } // namespace mbed
ganlikun 0:20e0c61e0684 209
ganlikun 0:20e0c61e0684 210 #endif
ganlikun 0:20e0c61e0684 211
ganlikun 0:20e0c61e0684 212 #endif
ganlikun 0:20e0c61e0684 213