Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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