Continuously vary LED brightness between completely dark and fully lite.

Committer:
sitti
Date:
Thu Dec 30 04:22:45 2010 +0000
Revision:
1:eef7593bdcc5
Parent:
0:26c09c4aba81
2010-12-29 Remove a member variable and revise the example.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sitti 0:26c09c4aba81 1 /*
sitti 0:26c09c4aba81 2 * Glowing LED Library
sitti 0:26c09c4aba81 3 * Copyright (c) 2010 Sitti Amarittapark
sitti 0:26c09c4aba81 4 *
sitti 0:26c09c4aba81 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
sitti 0:26c09c4aba81 6 * of this software and associated documentation files (the "Software"), to deal
sitti 0:26c09c4aba81 7 * in the Software without restriction, including without limitation the rights
sitti 0:26c09c4aba81 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
sitti 0:26c09c4aba81 9 * copies of the Software, and to permit persons to whom the Software is
sitti 0:26c09c4aba81 10 * furnished to do so, subject to the following conditions:
sitti 0:26c09c4aba81 11 *
sitti 0:26c09c4aba81 12 * The above copyright notice and this permission notice shall be included in
sitti 0:26c09c4aba81 13 * all copies or substantial portions of the Software.
sitti 0:26c09c4aba81 14 *
sitti 0:26c09c4aba81 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
sitti 0:26c09c4aba81 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
sitti 0:26c09c4aba81 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
sitti 0:26c09c4aba81 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
sitti 0:26c09c4aba81 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sitti 0:26c09c4aba81 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
sitti 0:26c09c4aba81 21 * THE SOFTWARE.
sitti 0:26c09c4aba81 22 */
sitti 0:26c09c4aba81 23
sitti 0:26c09c4aba81 24 #ifndef SA_GLOWING_H
sitti 0:26c09c4aba81 25 #define SA_GLOWING_H
sitti 0:26c09c4aba81 26
sitti 0:26c09c4aba81 27 #include "mbed.h"
sitti 0:26c09c4aba81 28
sitti 0:26c09c4aba81 29 /** Glowing LED class, based on Ticker and PwmOut
sitti 0:26c09c4aba81 30 *
sitti 0:26c09c4aba81 31 * Author: Sitti Amarittapark
sitti 0:26c09c4aba81 32 *
sitti 0:26c09c4aba81 33 * Glow an LED from dark to bright continously until stop
sitti 0:26c09c4aba81 34 *
sitti 0:26c09c4aba81 35 * Example:
sitti 0:26c09c4aba81 36 * @code
sitti 0:26c09c4aba81 37 * #include "Glowing.h"
sitti 1:eef7593bdcc5 38 * // Run full cycle in 2 seconds in
sitti 0:26c09c4aba81 39 * // 500 steps and begins with step 100
sitti 0:26c09c4aba81 40 * {
sitti 0:26c09c4aba81 41 * Glowing *glow = new Glowing(LED1, 2.0, 500, 100);
sitti 0:26c09c4aba81 42 * glow->Start();
sitti 0:26c09c4aba81 43 * . . .
sitti 0:26c09c4aba81 44 * glow->Stop();
sitti 0:26c09c4aba81 45 * }
sitti 0:26c09c4aba81 46 *
sitti 0:26c09c4aba81 47 * @endcode
sitti 0:26c09c4aba81 48 *
sitti 0:26c09c4aba81 49 */
sitti 0:26c09c4aba81 50
sitti 0:26c09c4aba81 51 class Glowing: public Ticker {
sitti 0:26c09c4aba81 52
sitti 0:26c09c4aba81 53 public:
sitti 0:26c09c4aba81 54 /** Select a PIN, cycle length in seconds, steps of change, offset in steps
sitti 0:26c09c4aba81 55 *
sitti 0:26c09c4aba81 56 * @param pin PwmOut pin to connect to
sitti 0:26c09c4aba81 57 * @param period cycle length in seconds
sitti 0:26c09c4aba81 58 * @param steps number of steps in a cycle, default to 100
sitti 0:26c09c4aba81 59 * @param offset number of steps offset from beginning, default to 0
sitti 0:26c09c4aba81 60 * LED off. Apply the different offset values to multiple LEDs can
sitti 0:26c09c4aba81 61 * create a cascading pattern.
sitti 0:26c09c4aba81 62 */
sitti 0:26c09c4aba81 63 Glowing(PinName pin, float period, int steps=100, int offset=0);
sitti 0:26c09c4aba81 64 /**
sitti 0:26c09c4aba81 65 * Stop the glowing cycle and delete the object
sitti 0:26c09c4aba81 66 */
sitti 0:26c09c4aba81 67 virtual ~Glowing();
sitti 0:26c09c4aba81 68 /**
sitti 0:26c09c4aba81 69 * Start the cycle with offset
sitti 0:26c09c4aba81 70 */
sitti 0:26c09c4aba81 71 void Start(void);
sitti 0:26c09c4aba81 72 /**
sitti 0:26c09c4aba81 73 * Stop the cycle
sitti 0:26c09c4aba81 74 */
sitti 0:26c09c4aba81 75 void Stop(void);
sitti 0:26c09c4aba81 76
sitti 0:26c09c4aba81 77 protected:
sitti 0:26c09c4aba81 78 PwmOut *s_port;
sitti 0:26c09c4aba81 79 int s_steps;
sitti 0:26c09c4aba81 80 int s_step_offset;
sitti 0:26c09c4aba81 81 int s_next_n;
sitti 0:26c09c4aba81 82 int s_next_v;
sitti 0:26c09c4aba81 83 bool s_running;
sitti 0:26c09c4aba81 84
sitti 0:26c09c4aba81 85 float c_step_time;
sitti 0:26c09c4aba81 86 float c_step_gradient;
sitti 0:26c09c4aba81 87
sitti 0:26c09c4aba81 88 void next(void);
sitti 0:26c09c4aba81 89 int scale(float v);
sitti 0:26c09c4aba81 90 };
sitti 0:26c09c4aba81 91
sitti 0:26c09c4aba81 92 #endif // SA_GLOWING_H