Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Glowing.h@0:26c09c4aba81, 2010-12-29 (annotated)
- Committer:
- sitti
- Date:
- Wed Dec 29 03:32:39 2010 +0000
- Revision:
- 0:26c09c4aba81
- Child:
- 1:eef7593bdcc5
1
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:26c09c4aba81 | 38 | * // Method A use LED1, 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 | * // Method B use LED2, run full cycle in 3 seconds in |
sitti | 0:26c09c4aba81 | 48 | * // 100 step (default) and begins with step 0 (default). |
sitti | 0:26c09c4aba81 | 49 | * { |
sitti | 0:26c09c4aba81 | 50 | * Glowing led(LED2, 3.0); |
sitti | 0:26c09c4aba81 | 51 | * led.Start(); |
sitti | 0:26c09c4aba81 | 52 | * . . . |
sitti | 0:26c09c4aba81 | 53 | * led.Stop(); |
sitti | 0:26c09c4aba81 | 54 | * } |
sitti | 0:26c09c4aba81 | 55 | * @endcode |
sitti | 0:26c09c4aba81 | 56 | * |
sitti | 0:26c09c4aba81 | 57 | */ |
sitti | 0:26c09c4aba81 | 58 | |
sitti | 0:26c09c4aba81 | 59 | class Glowing: public Ticker { |
sitti | 0:26c09c4aba81 | 60 | |
sitti | 0:26c09c4aba81 | 61 | public: |
sitti | 0:26c09c4aba81 | 62 | /** Select a PIN, cycle length in seconds, steps of change, offset in steps |
sitti | 0:26c09c4aba81 | 63 | * |
sitti | 0:26c09c4aba81 | 64 | * @param pin PwmOut pin to connect to |
sitti | 0:26c09c4aba81 | 65 | * @param period cycle length in seconds |
sitti | 0:26c09c4aba81 | 66 | * @param steps number of steps in a cycle, default to 100 |
sitti | 0:26c09c4aba81 | 67 | * @param offset number of steps offset from beginning, default to 0 |
sitti | 0:26c09c4aba81 | 68 | * LED off. Apply the different offset values to multiple LEDs can |
sitti | 0:26c09c4aba81 | 69 | * create a cascading pattern. |
sitti | 0:26c09c4aba81 | 70 | */ |
sitti | 0:26c09c4aba81 | 71 | Glowing(PinName pin, float period, int steps=100, int offset=0); |
sitti | 0:26c09c4aba81 | 72 | /** |
sitti | 0:26c09c4aba81 | 73 | * Stop the glowing cycle and delete the object |
sitti | 0:26c09c4aba81 | 74 | */ |
sitti | 0:26c09c4aba81 | 75 | virtual ~Glowing(); |
sitti | 0:26c09c4aba81 | 76 | /** |
sitti | 0:26c09c4aba81 | 77 | * Start the cycle with offset |
sitti | 0:26c09c4aba81 | 78 | */ |
sitti | 0:26c09c4aba81 | 79 | void Start(void); |
sitti | 0:26c09c4aba81 | 80 | /** |
sitti | 0:26c09c4aba81 | 81 | * Stop the cycle |
sitti | 0:26c09c4aba81 | 82 | */ |
sitti | 0:26c09c4aba81 | 83 | void Stop(void); |
sitti | 0:26c09c4aba81 | 84 | |
sitti | 0:26c09c4aba81 | 85 | protected: |
sitti | 0:26c09c4aba81 | 86 | PwmOut *s_port; |
sitti | 0:26c09c4aba81 | 87 | int s_steps; |
sitti | 0:26c09c4aba81 | 88 | int s_step_offset; |
sitti | 0:26c09c4aba81 | 89 | int s_next_n; |
sitti | 0:26c09c4aba81 | 90 | int s_next_v; |
sitti | 0:26c09c4aba81 | 91 | bool s_running; |
sitti | 0:26c09c4aba81 | 92 | |
sitti | 0:26c09c4aba81 | 93 | float c_circle; |
sitti | 0:26c09c4aba81 | 94 | float c_step_time; |
sitti | 0:26c09c4aba81 | 95 | float c_step_gradient; |
sitti | 0:26c09c4aba81 | 96 | |
sitti | 0:26c09c4aba81 | 97 | void next(void); |
sitti | 0:26c09c4aba81 | 98 | int scale(float v); |
sitti | 0:26c09c4aba81 | 99 | }; |
sitti | 0:26c09c4aba81 | 100 | |
sitti | 0:26c09c4aba81 | 101 | #endif // SA_GLOWING_H |