Testing WS2801

Dependencies:   mbed

Fork of ws2801 by Thomas Olsson

Committer:
wertyfrog
Date:
Sat Jun 18 01:15:55 2011 +0000
Revision:
4:5e71151b8ad7
Parent:
3:2b362d164405
Child:
5:2585a833a44b

        

Who changed what in which revision?

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