Test code for Grove Node BLE
Dependencies: BLE_API nRF51822
Fork of BLE_LoopbackUART by
mbed-src/api/PwmOut.h@10:22480ac31879, 2014-11-27 (annotated)
- Committer:
- yihui
- Date:
- Thu Nov 27 09:30:36 2014 +0000
- Revision:
- 10:22480ac31879
- Parent:
- 9:05f0b5a3a70a
change to new revision hardware
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 9:05f0b5a3a70a | 1 | /* mbed Microcontroller Library |
yihui | 9:05f0b5a3a70a | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 9:05f0b5a3a70a | 3 | * |
yihui | 9:05f0b5a3a70a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 9:05f0b5a3a70a | 5 | * you may not use this file except in compliance with the License. |
yihui | 9:05f0b5a3a70a | 6 | * You may obtain a copy of the License at |
yihui | 9:05f0b5a3a70a | 7 | * |
yihui | 9:05f0b5a3a70a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 9:05f0b5a3a70a | 9 | * |
yihui | 9:05f0b5a3a70a | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 9:05f0b5a3a70a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 9:05f0b5a3a70a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 9:05f0b5a3a70a | 13 | * See the License for the specific language governing permissions and |
yihui | 9:05f0b5a3a70a | 14 | * limitations under the License. |
yihui | 9:05f0b5a3a70a | 15 | */ |
yihui | 9:05f0b5a3a70a | 16 | #ifndef MBED_PWMOUT_H |
yihui | 9:05f0b5a3a70a | 17 | #define MBED_PWMOUT_H |
yihui | 9:05f0b5a3a70a | 18 | |
yihui | 9:05f0b5a3a70a | 19 | #include "platform.h" |
yihui | 9:05f0b5a3a70a | 20 | |
yihui | 9:05f0b5a3a70a | 21 | #if DEVICE_PWMOUT |
yihui | 9:05f0b5a3a70a | 22 | #include "pwmout_api.h" |
yihui | 9:05f0b5a3a70a | 23 | |
yihui | 9:05f0b5a3a70a | 24 | namespace mbed { |
yihui | 9:05f0b5a3a70a | 25 | |
yihui | 9:05f0b5a3a70a | 26 | /** A pulse-width modulation digital output |
yihui | 9:05f0b5a3a70a | 27 | * |
yihui | 9:05f0b5a3a70a | 28 | * Example |
yihui | 9:05f0b5a3a70a | 29 | * @code |
yihui | 9:05f0b5a3a70a | 30 | * // Fade a led on. |
yihui | 9:05f0b5a3a70a | 31 | * #include "mbed.h" |
yihui | 9:05f0b5a3a70a | 32 | * |
yihui | 9:05f0b5a3a70a | 33 | * PwmOut led(LED1); |
yihui | 9:05f0b5a3a70a | 34 | * |
yihui | 9:05f0b5a3a70a | 35 | * int main() { |
yihui | 9:05f0b5a3a70a | 36 | * while(1) { |
yihui | 9:05f0b5a3a70a | 37 | * led = led + 0.01; |
yihui | 9:05f0b5a3a70a | 38 | * wait(0.2); |
yihui | 9:05f0b5a3a70a | 39 | * if(led == 1.0) { |
yihui | 9:05f0b5a3a70a | 40 | * led = 0; |
yihui | 9:05f0b5a3a70a | 41 | * } |
yihui | 9:05f0b5a3a70a | 42 | * } |
yihui | 9:05f0b5a3a70a | 43 | * } |
yihui | 9:05f0b5a3a70a | 44 | * @endcode |
yihui | 9:05f0b5a3a70a | 45 | * |
yihui | 9:05f0b5a3a70a | 46 | * @note |
yihui | 9:05f0b5a3a70a | 47 | * On the LPC1768 and LPC2368, the PWMs all share the same |
yihui | 9:05f0b5a3a70a | 48 | * period - if you change the period for one, you change it for all. |
yihui | 9:05f0b5a3a70a | 49 | * Although routines that change the period maintain the duty cycle |
yihui | 9:05f0b5a3a70a | 50 | * for its PWM, all other PWMs will require their duty cycle to be |
yihui | 9:05f0b5a3a70a | 51 | * refreshed. |
yihui | 9:05f0b5a3a70a | 52 | */ |
yihui | 9:05f0b5a3a70a | 53 | class PwmOut { |
yihui | 9:05f0b5a3a70a | 54 | |
yihui | 9:05f0b5a3a70a | 55 | public: |
yihui | 9:05f0b5a3a70a | 56 | |
yihui | 9:05f0b5a3a70a | 57 | /** Create a PwmOut connected to the specified pin |
yihui | 9:05f0b5a3a70a | 58 | * |
yihui | 9:05f0b5a3a70a | 59 | * @param pin PwmOut pin to connect to |
yihui | 9:05f0b5a3a70a | 60 | */ |
yihui | 9:05f0b5a3a70a | 61 | PwmOut(PinName pin) { |
yihui | 9:05f0b5a3a70a | 62 | pwmout_init(&_pwm, pin); |
yihui | 9:05f0b5a3a70a | 63 | } |
yihui | 9:05f0b5a3a70a | 64 | |
yihui | 9:05f0b5a3a70a | 65 | ~PwmOut() { |
yihui | 9:05f0b5a3a70a | 66 | pwmout_free(&_pwm); |
yihui | 9:05f0b5a3a70a | 67 | } |
yihui | 9:05f0b5a3a70a | 68 | |
yihui | 9:05f0b5a3a70a | 69 | /** Set the ouput duty-cycle, specified as a percentage (float) |
yihui | 9:05f0b5a3a70a | 70 | * |
yihui | 9:05f0b5a3a70a | 71 | * @param value A floating-point value representing the output duty-cycle, |
yihui | 9:05f0b5a3a70a | 72 | * specified as a percentage. The value should lie between |
yihui | 9:05f0b5a3a70a | 73 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
yihui | 9:05f0b5a3a70a | 74 | * Values outside this range will be saturated to 0.0f or 1.0f. |
yihui | 9:05f0b5a3a70a | 75 | */ |
yihui | 9:05f0b5a3a70a | 76 | void write(float value) { |
yihui | 9:05f0b5a3a70a | 77 | pwmout_write(&_pwm, value); |
yihui | 9:05f0b5a3a70a | 78 | } |
yihui | 9:05f0b5a3a70a | 79 | |
yihui | 9:05f0b5a3a70a | 80 | /** Return the current output duty-cycle setting, measured as a percentage (float) |
yihui | 9:05f0b5a3a70a | 81 | * |
yihui | 9:05f0b5a3a70a | 82 | * @returns |
yihui | 9:05f0b5a3a70a | 83 | * A floating-point value representing the current duty-cycle being output on the pin, |
yihui | 9:05f0b5a3a70a | 84 | * measured as a percentage. The returned value will lie between |
yihui | 9:05f0b5a3a70a | 85 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
yihui | 9:05f0b5a3a70a | 86 | * |
yihui | 9:05f0b5a3a70a | 87 | * @note |
yihui | 9:05f0b5a3a70a | 88 | * This value may not match exactly the value set by a previous <write>. |
yihui | 9:05f0b5a3a70a | 89 | */ |
yihui | 9:05f0b5a3a70a | 90 | float read() { |
yihui | 9:05f0b5a3a70a | 91 | return pwmout_read(&_pwm); |
yihui | 9:05f0b5a3a70a | 92 | } |
yihui | 9:05f0b5a3a70a | 93 | |
yihui | 9:05f0b5a3a70a | 94 | /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. |
yihui | 9:05f0b5a3a70a | 95 | * |
yihui | 9:05f0b5a3a70a | 96 | * @note |
yihui | 9:05f0b5a3a70a | 97 | * The resolution is currently in microseconds; periods smaller than this |
yihui | 9:05f0b5a3a70a | 98 | * will be set to zero. |
yihui | 9:05f0b5a3a70a | 99 | */ |
yihui | 9:05f0b5a3a70a | 100 | void period(float seconds) { |
yihui | 9:05f0b5a3a70a | 101 | pwmout_period(&_pwm, seconds); |
yihui | 9:05f0b5a3a70a | 102 | } |
yihui | 9:05f0b5a3a70a | 103 | |
yihui | 9:05f0b5a3a70a | 104 | /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. |
yihui | 9:05f0b5a3a70a | 105 | */ |
yihui | 9:05f0b5a3a70a | 106 | void period_ms(int ms) { |
yihui | 9:05f0b5a3a70a | 107 | pwmout_period_ms(&_pwm, ms); |
yihui | 9:05f0b5a3a70a | 108 | } |
yihui | 9:05f0b5a3a70a | 109 | |
yihui | 9:05f0b5a3a70a | 110 | /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. |
yihui | 9:05f0b5a3a70a | 111 | */ |
yihui | 9:05f0b5a3a70a | 112 | void period_us(int us) { |
yihui | 9:05f0b5a3a70a | 113 | pwmout_period_us(&_pwm, us); |
yihui | 9:05f0b5a3a70a | 114 | } |
yihui | 9:05f0b5a3a70a | 115 | |
yihui | 9:05f0b5a3a70a | 116 | /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. |
yihui | 9:05f0b5a3a70a | 117 | */ |
yihui | 9:05f0b5a3a70a | 118 | void pulsewidth(float seconds) { |
yihui | 9:05f0b5a3a70a | 119 | pwmout_pulsewidth(&_pwm, seconds); |
yihui | 9:05f0b5a3a70a | 120 | } |
yihui | 9:05f0b5a3a70a | 121 | |
yihui | 9:05f0b5a3a70a | 122 | /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. |
yihui | 9:05f0b5a3a70a | 123 | */ |
yihui | 9:05f0b5a3a70a | 124 | void pulsewidth_ms(int ms) { |
yihui | 9:05f0b5a3a70a | 125 | pwmout_pulsewidth_ms(&_pwm, ms); |
yihui | 9:05f0b5a3a70a | 126 | } |
yihui | 9:05f0b5a3a70a | 127 | |
yihui | 9:05f0b5a3a70a | 128 | /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. |
yihui | 9:05f0b5a3a70a | 129 | */ |
yihui | 9:05f0b5a3a70a | 130 | void pulsewidth_us(int us) { |
yihui | 9:05f0b5a3a70a | 131 | pwmout_pulsewidth_us(&_pwm, us); |
yihui | 9:05f0b5a3a70a | 132 | } |
yihui | 9:05f0b5a3a70a | 133 | |
yihui | 9:05f0b5a3a70a | 134 | #ifdef MBED_OPERATORS |
yihui | 9:05f0b5a3a70a | 135 | /** A operator shorthand for write() |
yihui | 9:05f0b5a3a70a | 136 | */ |
yihui | 9:05f0b5a3a70a | 137 | PwmOut& operator= (float value) { |
yihui | 9:05f0b5a3a70a | 138 | write(value); |
yihui | 9:05f0b5a3a70a | 139 | return *this; |
yihui | 9:05f0b5a3a70a | 140 | } |
yihui | 9:05f0b5a3a70a | 141 | |
yihui | 9:05f0b5a3a70a | 142 | PwmOut& operator= (PwmOut& rhs) { |
yihui | 9:05f0b5a3a70a | 143 | write(rhs.read()); |
yihui | 9:05f0b5a3a70a | 144 | return *this; |
yihui | 9:05f0b5a3a70a | 145 | } |
yihui | 9:05f0b5a3a70a | 146 | |
yihui | 9:05f0b5a3a70a | 147 | /** An operator shorthand for read() |
yihui | 9:05f0b5a3a70a | 148 | */ |
yihui | 9:05f0b5a3a70a | 149 | operator float() { |
yihui | 9:05f0b5a3a70a | 150 | return read(); |
yihui | 9:05f0b5a3a70a | 151 | } |
yihui | 9:05f0b5a3a70a | 152 | #endif |
yihui | 9:05f0b5a3a70a | 153 | |
yihui | 9:05f0b5a3a70a | 154 | protected: |
yihui | 9:05f0b5a3a70a | 155 | pwmout_t _pwm; |
yihui | 9:05f0b5a3a70a | 156 | }; |
yihui | 9:05f0b5a3a70a | 157 | |
yihui | 9:05f0b5a3a70a | 158 | } // namespace mbed |
yihui | 9:05f0b5a3a70a | 159 | |
yihui | 9:05f0b5a3a70a | 160 | #endif |
yihui | 9:05f0b5a3a70a | 161 | |
yihui | 9:05f0b5a3a70a | 162 | #endif |