WS2812
WS2812.h@3:adfd3b002caa, 2021-12-13 (annotated)
- Committer:
- villemejane
- Date:
- Mon Dec 13 08:57:11 2021 +0000
- Revision:
- 3:adfd3b002caa
- Parent:
- 2:6e647820f587
WS2812
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bridadan | 0:0b79cafcb387 | 1 | /* Copyright (c) 2012 cstyles, MIT License |
bridadan | 0:0b79cafcb387 | 2 | * |
bridadan | 0:0b79cafcb387 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
bridadan | 0:0b79cafcb387 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
bridadan | 0:0b79cafcb387 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
bridadan | 0:0b79cafcb387 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
bridadan | 0:0b79cafcb387 | 7 | * furnished to do so, subject to the following conditions: |
bridadan | 0:0b79cafcb387 | 8 | * |
bridadan | 0:0b79cafcb387 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
bridadan | 0:0b79cafcb387 | 10 | * substantial portions of the Software. |
bridadan | 0:0b79cafcb387 | 11 | * |
bridadan | 0:0b79cafcb387 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
bridadan | 0:0b79cafcb387 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bridadan | 0:0b79cafcb387 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
bridadan | 0:0b79cafcb387 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bridadan | 0:0b79cafcb387 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bridadan | 0:0b79cafcb387 | 17 | */ |
bridadan | 0:0b79cafcb387 | 18 | |
bridadan | 0:0b79cafcb387 | 19 | #ifndef WS2812_H |
bridadan | 0:0b79cafcb387 | 20 | #define WS2812_H |
bridadan | 0:0b79cafcb387 | 21 | |
bridadan | 0:0b79cafcb387 | 22 | #include "mbed.h" |
bridadan | 0:0b79cafcb387 | 23 | |
villemejane | 3:adfd3b002caa | 24 | #define TYPE_BANDEAU_NEO 4 |
villemejane | 3:adfd3b002caa | 25 | #define TYPE_BANDEAU_NONO 3 |
villemejane | 3:adfd3b002caa | 26 | #define FRAME_SIZE_LED 8 |
bridadan | 0:0b79cafcb387 | 27 | |
bridadan | 0:0b79cafcb387 | 28 | //!Library for the WS2812 RGB LED with integrated controller |
bridadan | 0:0b79cafcb387 | 29 | /*! |
bridadan | 0:0b79cafcb387 | 30 | The WS2812 is controller that is built into a range of LEDs |
bridadan | 0:0b79cafcb387 | 31 | */ |
bridadan | 0:0b79cafcb387 | 32 | class WS2812 |
bridadan | 0:0b79cafcb387 | 33 | { |
bridadan | 0:0b79cafcb387 | 34 | public: |
bridadan | 1:aadbf08c62a2 | 35 | enum BrightnessControl { OFF, GLOBAL, PER_PIXEL }; |
bridadan | 1:aadbf08c62a2 | 36 | |
bridadan | 1:aadbf08c62a2 | 37 | /** |
bridadan | 1:aadbf08c62a2 | 38 | * Constructor |
bridadan | 1:aadbf08c62a2 | 39 | * |
bridadan | 1:aadbf08c62a2 | 40 | * @param pin Output pin. Connect to "Din" on the first WS2812 in the strip |
bridadan | 1:aadbf08c62a2 | 41 | * @param size Number of LEDs in your strip |
bridadan | 1:aadbf08c62a2 | 42 | * @param zeroHigh How many NOPs to insert to ensure TOH is properly generated. See library description for more information. |
bridadan | 1:aadbf08c62a2 | 43 | * @param zeroLow How many NOPs to insert to ensure TOL is properly generated. See library description for more information. |
bridadan | 1:aadbf08c62a2 | 44 | * @param oneHigh How many NOPs to insert to ensure T1H is properly generated. See library description for more information. |
bridadan | 1:aadbf08c62a2 | 45 | * @param oneLow How many NOPs to insert to ensure T1L is properly generated. See library description for more information. |
bridadan | 1:aadbf08c62a2 | 46 | * |
bridadan | 0:0b79cafcb387 | 47 | */ |
villemejane | 3:adfd3b002caa | 48 | WS2812(PinName pin, int size, int zeroHigh, int zeroLow, int oneHigh, int oneLow, int typeBandeau = 0); |
bridadan | 0:0b79cafcb387 | 49 | |
bridadan | 0:0b79cafcb387 | 50 | /*! |
bridadan | 0:0b79cafcb387 | 51 | Destroys instance. |
bridadan | 0:0b79cafcb387 | 52 | */ |
bridadan | 0:0b79cafcb387 | 53 | ~WS2812(); |
bridadan | 0:0b79cafcb387 | 54 | |
bridadan | 1:aadbf08c62a2 | 55 | /** |
bridadan | 1:aadbf08c62a2 | 56 | * Sets the timing parameters for the bit-banged signal |
bridadan | 1:aadbf08c62a2 | 57 | * |
bridadan | 1:aadbf08c62a2 | 58 | * @param zeroHigh How many NOPs to insert to ensure TOH is properly generated. See library description for more information. |
bridadan | 1:aadbf08c62a2 | 59 | * @param zeroLow How many NOPs to insert to ensure TOL is properly generated. See library description for more information. |
bridadan | 1:aadbf08c62a2 | 60 | * @param oneHigh How many NOPs to insert to ensure T1H is properly generated. See library description for more information. |
bridadan | 1:aadbf08c62a2 | 61 | * @param oneLow How many NOPs to insert to ensure T1L is properly generated. See library description for more information. |
bridadan | 1:aadbf08c62a2 | 62 | * |
bridadan | 1:aadbf08c62a2 | 63 | */ |
bridadan | 0:0b79cafcb387 | 64 | void setDelays(int zeroHigh, int zeroLow, int oneHigh, int oneLow); |
bridadan | 0:0b79cafcb387 | 65 | |
bridadan | 1:aadbf08c62a2 | 66 | /** |
bridadan | 2:6e647820f587 | 67 | * Writes the given buffer to the LED strip with the given offsets. |
bridadan | 2:6e647820f587 | 68 | * NOTE: This function is timing critical, therefore interrupts are disabled during the transmission section. |
bridadan | 1:aadbf08c62a2 | 69 | * |
bridadan | 1:aadbf08c62a2 | 70 | * @param buf Pointer to the PixelArray buffer |
bridadan | 1:aadbf08c62a2 | 71 | * @param r_offset The offset where each each pixel pulls its red component. Wraps to beginning if end is reached. |
bridadan | 1:aadbf08c62a2 | 72 | * @param g_offset The offset where each each pixel pulls its green component. Wraps to beginning if end is reached. |
bridadan | 1:aadbf08c62a2 | 73 | * @param b_offset The offset where each each pixel pulls its blue component. Wraps to beginning if end is reached. |
bridadan | 1:aadbf08c62a2 | 74 | * |
bridadan | 1:aadbf08c62a2 | 75 | */ |
bridadan | 1:aadbf08c62a2 | 76 | void write_offsets(int buf[], int r_offset = 0, int g_offset = 0, int b_offset = 0); |
bridadan | 1:aadbf08c62a2 | 77 | |
bridadan | 1:aadbf08c62a2 | 78 | |
bridadan | 1:aadbf08c62a2 | 79 | /** |
bridadan | 2:6e647820f587 | 80 | * Writes the given buffer to the LED strip |
bridadan | 2:6e647820f587 | 81 | * NOTE: This function is timing critical, therefore interrupts are disabled during the transmission section. |
bridadan | 1:aadbf08c62a2 | 82 | * |
bridadan | 1:aadbf08c62a2 | 83 | * @param buf Pointer to the PixelArray buffer |
bridadan | 1:aadbf08c62a2 | 84 | * |
bridadan | 1:aadbf08c62a2 | 85 | */ |
bridadan | 0:0b79cafcb387 | 86 | void write(int buf[]); |
bridadan | 1:aadbf08c62a2 | 87 | |
bridadan | 1:aadbf08c62a2 | 88 | /** |
bridadan | 1:aadbf08c62a2 | 89 | * Sets the brightness mode |
bridadan | 1:aadbf08c62a2 | 90 | * |
bridadan | 1:aadbf08c62a2 | 91 | * @param bc The brightness control. Defaults to OFF. Possible values include OFF, GLOBAL, and PER_PIXEL |
bridadan | 1:aadbf08c62a2 | 92 | * |
bridadan | 1:aadbf08c62a2 | 93 | */ |
bridadan | 1:aadbf08c62a2 | 94 | void useII(BrightnessControl bc); |
bridadan | 1:aadbf08c62a2 | 95 | |
bridadan | 1:aadbf08c62a2 | 96 | /** |
bridadan | 1:aadbf08c62a2 | 97 | * Sets the global brightness level. |
bridadan | 1:aadbf08c62a2 | 98 | * |
bridadan | 1:aadbf08c62a2 | 99 | * @param II The brightness level. Possible values include 0 - 255 (0x00 - 0xFF). |
bridadan | 1:aadbf08c62a2 | 100 | * |
bridadan | 1:aadbf08c62a2 | 101 | */ |
bridadan | 0:0b79cafcb387 | 102 | void setII(unsigned char II); |
bridadan | 0:0b79cafcb387 | 103 | |
bridadan | 0:0b79cafcb387 | 104 | |
bridadan | 0:0b79cafcb387 | 105 | |
bridadan | 0:0b79cafcb387 | 106 | private: |
bridadan | 0:0b79cafcb387 | 107 | |
bridadan | 0:0b79cafcb387 | 108 | int __size; |
bridadan | 0:0b79cafcb387 | 109 | int __zeroHigh, __zeroLow, __oneHigh, __oneLow; |
bridadan | 0:0b79cafcb387 | 110 | unsigned char __II; |
bridadan | 1:aadbf08c62a2 | 111 | BrightnessControl __use_II; |
bridadan | 0:0b79cafcb387 | 112 | bool *__transmitBuf; |
bridadan | 0:0b79cafcb387 | 113 | void __loadBuf(int buf[],int r_offset=0, int g_offset=0, int b_offset=0); |
bridadan | 0:0b79cafcb387 | 114 | PinName __outPin; |
bridadan | 0:0b79cafcb387 | 115 | DigitalOut __gpo; |
bridadan | 0:0b79cafcb387 | 116 | }; |
bridadan | 0:0b79cafcb387 | 117 | |
bridadan | 0:0b79cafcb387 | 118 | #endif |