PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
Pokitto
Date:
Wed Oct 11 20:35:27 2017 +0000
Revision:
5:ea7377f3d1af
Fixed PokittoLib. Includes a working custom mbed-src

Who changed what in which revision?

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