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