test led strip

Fork of apa102 by Alan Rager

Committer:
rsmits
Date:
Sat Feb 17 11:04:25 2018 +0000
Revision:
1:c0e649ca77af
Parent:
0:cd4bcb59e36e
initial test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlanRager 0:cd4bcb59e36e 1 /*
AlanRager 0:cd4bcb59e36e 2 Copyright (c) 2011 Thomas Olsson.
AlanRager 0:cd4bcb59e36e 3
AlanRager 0:cd4bcb59e36e 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AlanRager 0:cd4bcb59e36e 5 of this software and associated documentation files (the "Software"), to deal
AlanRager 0:cd4bcb59e36e 6 in the Software without restriction, including without limitation the rights
AlanRager 0:cd4bcb59e36e 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AlanRager 0:cd4bcb59e36e 8 copies of the Software, and to permit persons to whom the Software is
AlanRager 0:cd4bcb59e36e 9 furnished to do so, subject to the following conditions:
AlanRager 0:cd4bcb59e36e 10
AlanRager 0:cd4bcb59e36e 11 The above copyright notice and this permission notice shall be included in
AlanRager 0:cd4bcb59e36e 12 all copies or substantial portions of the Software.
AlanRager 0:cd4bcb59e36e 13
AlanRager 0:cd4bcb59e36e 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AlanRager 0:cd4bcb59e36e 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AlanRager 0:cd4bcb59e36e 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AlanRager 0:cd4bcb59e36e 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AlanRager 0:cd4bcb59e36e 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AlanRager 0:cd4bcb59e36e 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AlanRager 0:cd4bcb59e36e 20 THE SOFTWARE.
AlanRager 0:cd4bcb59e36e 21 */
AlanRager 0:cd4bcb59e36e 22
AlanRager 0:cd4bcb59e36e 23 #ifndef APA102_H
AlanRager 0:cd4bcb59e36e 24 #define APA102_H
AlanRager 0:cd4bcb59e36e 25
AlanRager 0:cd4bcb59e36e 26 #include "mbed.h"
AlanRager 0:cd4bcb59e36e 27 #include "HTML_color.h"
AlanRager 0:cd4bcb59e36e 28
AlanRager 0:cd4bcb59e36e 29 #define APA102_MAX_LEVEL 100
AlanRager 0:cd4bcb59e36e 30
AlanRager 0:cd4bcb59e36e 31 /** APA102 class, controlling LED strips or arrays like this http://www.sparkfun.com/products/10312
AlanRager 0:cd4bcb59e36e 32 *
AlanRager 0:cd4bcb59e36e 33 * the APA102 IC's are 5V but works well connected directly to mbed IO pins.
AlanRager 0:cd4bcb59e36e 34 *
AlanRager 0:cd4bcb59e36e 35 * You need a 5V power supply capable of about 2A to light up a 32 led strip, do NOT power from mbed(usb).
AlanRager 0:cd4bcb59e36e 36 *
AlanRager 0:cd4bcb59e36e 37 * the required reset delay is a blocking wait(), it can be lowered (or set to 0) if you want to use the
AlanRager 0:cd4bcb59e36e 38 * time for executing code instead, data sheet says 500us but i found it need at least 800us.
AlanRager 0:cd4bcb59e36e 39 *
AlanRager 0:cd4bcb59e36e 40 * Example:
AlanRager 0:cd4bcb59e36e 41 * @code
AlanRager 0:cd4bcb59e36e 42 #include "mbed.h"
AlanRager 0:cd4bcb59e36e 43 #include "apa102.h"
AlanRager 0:cd4bcb59e36e 44
AlanRager 0:cd4bcb59e36e 45 #define STRIP_LENGTH 32
AlanRager 0:cd4bcb59e36e 46
AlanRager 0:cd4bcb59e36e 47 apa102 mystrip(p9, p10, STRIP_LENGTH);
AlanRager 0:cd4bcb59e36e 48
AlanRager 0:cd4bcb59e36e 49 int dir=1, level=10;
AlanRager 0:cd4bcb59e36e 50 int rainbow[] = {0xff00ff,0xff00cc,0xff0099,0xff0066,0xff0033,0xff0000,0xff3300,0xff6600,
AlanRager 0:cd4bcb59e36e 51 0xff9900,0xffcc00,0xffff00,0xccff00,0x99ff00,0x66ff00,0x33ff00,0x00ff00,
AlanRager 0:cd4bcb59e36e 52 0x00ff33,0x00ff66,0x00ff99,0x00ffcc,0x00ffff,0x00ccff,0x0099ff,0x0066ff,
AlanRager 0:cd4bcb59e36e 53 0x0033ff,0x0000ff,0x3300ff,0x6600ff,0x9900ff,0xcc00ff,0x9900ff,0x6600ff
AlanRager 0:cd4bcb59e36e 54 };
AlanRager 0:cd4bcb59e36e 55
AlanRager 0:cd4bcb59e36e 56 void move(void){
AlanRager 0:cd4bcb59e36e 57 int temp = rainbow[31];
AlanRager 0:cd4bcb59e36e 58 for (int x = (STRIP_LENGTH - 1) ; x > 0 ; x--) rainbow[x] = rainbow[x - 1];
AlanRager 0:cd4bcb59e36e 59 rainbow[0] = temp;
AlanRager 0:cd4bcb59e36e 60 }
AlanRager 0:cd4bcb59e36e 61
AlanRager 0:cd4bcb59e36e 62 void pulse(void){
AlanRager 0:cd4bcb59e36e 63 if(dir)
AlanRager 0:cd4bcb59e36e 64 {
AlanRager 0:cd4bcb59e36e 65 mystrip.level(level+=2);
AlanRager 0:cd4bcb59e36e 66 if(level >= 100)dir = 0;
AlanRager 0:cd4bcb59e36e 67 }
AlanRager 0:cd4bcb59e36e 68 else if(!dir)
AlanRager 0:cd4bcb59e36e 69 {
AlanRager 0:cd4bcb59e36e 70 mystrip.level(level--);
AlanRager 0:cd4bcb59e36e 71 if(level <= 5)dir = 1;
AlanRager 0:cd4bcb59e36e 72 }
AlanRager 0:cd4bcb59e36e 73 }
AlanRager 0:cd4bcb59e36e 74
AlanRager 0:cd4bcb59e36e 75 int main() {
AlanRager 0:cd4bcb59e36e 76 mystrip.level(level);
AlanRager 0:cd4bcb59e36e 77 while(1)
AlanRager 0:cd4bcb59e36e 78 {
AlanRager 0:cd4bcb59e36e 79 move();
AlanRager 0:cd4bcb59e36e 80 pulse();
AlanRager 0:cd4bcb59e36e 81 mystrip.post(rainbow);
AlanRager 0:cd4bcb59e36e 82 wait_ms(100);
AlanRager 0:cd4bcb59e36e 83 }
AlanRager 0:cd4bcb59e36e 84 }
AlanRager 0:cd4bcb59e36e 85 * @endcode
AlanRager 0:cd4bcb59e36e 86 */
AlanRager 0:cd4bcb59e36e 87 class apa102
AlanRager 0:cd4bcb59e36e 88 {
AlanRager 0:cd4bcb59e36e 89 public:
AlanRager 0:cd4bcb59e36e 90 /** Create a new apa102 object
AlanRager 0:cd4bcb59e36e 91 *
AlanRager 0:cd4bcb59e36e 92 *
AlanRager 0:cd4bcb59e36e 93 * @param CKI clock pin
AlanRager 0:cd4bcb59e36e 94 * @param SDI data pin
AlanRager 0:cd4bcb59e36e 95 * @param STRIP_LENGTH number of apa102 IC's i strip or array defaults to 32
AlanRager 0:cd4bcb59e36e 96 * @param reset_delay delay in us to allow latching data defaults to 800
AlanRager 0:cd4bcb59e36e 97 * @returns nothing
AlanRager 0:cd4bcb59e36e 98 */
AlanRager 0:cd4bcb59e36e 99 apa102(PinName CKI, PinName SDI, int STRIP_LENGTH = 32, int reset_delay = 800);
AlanRager 0:cd4bcb59e36e 100 /** write RGB color data to strip or array
AlanRager 0:cd4bcb59e36e 101 *
AlanRager 0:cd4bcb59e36e 102 * color data for each LED is 3 bytes int the order of rrggbb.
AlanRager 0:cd4bcb59e36e 103 * array must have STRIP_LENGTH number of elements
AlanRager 0:cd4bcb59e36e 104 *
AlanRager 0:cd4bcb59e36e 105 * @param strip_colors array of color data
AlanRager 0:cd4bcb59e36e 106 */
AlanRager 0:cd4bcb59e36e 107 void post(int *strip_colors);
AlanRager 0:cd4bcb59e36e 108 /** clears the array or strip (all off)
AlanRager 0:cd4bcb59e36e 109 */
AlanRager 0:cd4bcb59e36e 110 void clear(void);
AlanRager 0:cd4bcb59e36e 111 /** set level of the entire array 0-100%
AlanRager 0:cd4bcb59e36e 112 *
AlanRager 0:cd4bcb59e36e 113 * at low levels the colors may change because R,G or B
AlanRager 0:cd4bcb59e36e 114 * reaches 0.
AlanRager 0:cd4bcb59e36e 115 *
AlanRager 0:cd4bcb59e36e 116 * @param level level in percent
AlanRager 0:cd4bcb59e36e 117 * @returns current level
AlanRager 0:cd4bcb59e36e 118 */
AlanRager 0:cd4bcb59e36e 119 int level(int level=0);
AlanRager 0:cd4bcb59e36e 120 /** get/set reset/write delay
AlanRager 0:cd4bcb59e36e 121 *
AlanRager 0:cd4bcb59e36e 122 * mainly for experimenting with reset values without recompiling,
AlanRager 0:cd4bcb59e36e 123 * leave at default to begin with & then lower to get faster execution.
AlanRager 0:cd4bcb59e36e 124 *
AlanRager 0:cd4bcb59e36e 125 * @param delay delay in us
AlanRager 0:cd4bcb59e36e 126 * @returns delay in us
AlanRager 0:cd4bcb59e36e 127 */
AlanRager 0:cd4bcb59e36e 128 int delay(uint32_t reset_delay=100);
AlanRager 0:cd4bcb59e36e 129
AlanRager 0:cd4bcb59e36e 130 private:
AlanRager 0:cd4bcb59e36e 131 DigitalOut _CKI;
AlanRager 0:cd4bcb59e36e 132 DigitalOut _SDI;
AlanRager 0:cd4bcb59e36e 133 int _STRIP_LENGTH;
AlanRager 0:cd4bcb59e36e 134 int _level;
AlanRager 0:cd4bcb59e36e 135 int _reset_delay;
AlanRager 0:cd4bcb59e36e 136 inline void write_hi();
AlanRager 0:cd4bcb59e36e 137 inline void write_lo();
AlanRager 0:cd4bcb59e36e 138 inline void write_bit(bool bit);
AlanRager 0:cd4bcb59e36e 139 inline void write_word(uint32_t word, int bits = 32);
AlanRager 0:cd4bcb59e36e 140 inline void write_done();
AlanRager 0:cd4bcb59e36e 141 inline void preamble();
AlanRager 0:cd4bcb59e36e 142 inline void afterword();
AlanRager 0:cd4bcb59e36e 143 };
AlanRager 0:cd4bcb59e36e 144
AlanRager 0:cd4bcb59e36e 145 #endif