neostrip code
NeoStrip.h@4:623b0c643dd6, 2016-12-11 (annotated)
- Committer:
- otis22894
- Date:
- Sun Dec 11 21:12:28 2016 +0000
- Revision:
- 4:623b0c643dd6
- Parent:
- 3:00f3f32e6a1e
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 0:9f237b11f0a8 | 1 | |
aswild | 0:9f237b11f0a8 | 2 | |
aswild | 0:9f237b11f0a8 | 3 | #ifndef NEOSTRIP_H |
aswild | 0:9f237b11f0a8 | 4 | #define NEOSTRIP_H |
aswild | 0:9f237b11f0a8 | 5 | |
aswild | 0:9f237b11f0a8 | 6 | #ifndef TARGET_LPC1768 |
aswild | 0:9f237b11f0a8 | 7 | #error NeoStrip only supports the NXP LPC1768! |
aswild | 0:9f237b11f0a8 | 8 | #endif |
aswild | 0:9f237b11f0a8 | 9 | |
aswild | 0:9f237b11f0a8 | 10 | // NeoColor struct definition to hold 24 bit |
aswild | 0:9f237b11f0a8 | 11 | // color data for each pixel, in GRB order |
aswild | 0:9f237b11f0a8 | 12 | typedef struct _NeoColor |
aswild | 0:9f237b11f0a8 | 13 | { |
aswild | 0:9f237b11f0a8 | 14 | uint8_t green; |
aswild | 0:9f237b11f0a8 | 15 | uint8_t red; |
aswild | 0:9f237b11f0a8 | 16 | uint8_t blue; |
aswild | 0:9f237b11f0a8 | 17 | } NeoColor; |
aswild | 0:9f237b11f0a8 | 18 | |
aswild | 0:9f237b11f0a8 | 19 | /** |
aswild | 0:9f237b11f0a8 | 20 | * NeoStrip objects manage the buffering and assigning of |
aswild | 0:9f237b11f0a8 | 21 | * addressable NeoPixels |
aswild | 0:9f237b11f0a8 | 22 | */ |
aswild | 0:9f237b11f0a8 | 23 | class NeoStrip |
aswild | 0:9f237b11f0a8 | 24 | { |
aswild | 0:9f237b11f0a8 | 25 | public: |
aswild | 0:9f237b11f0a8 | 26 | |
aswild | 0:9f237b11f0a8 | 27 | /** |
aswild | 0:9f237b11f0a8 | 28 | * Create a NeoStrip object |
aswild | 0:9f237b11f0a8 | 29 | * |
aswild | 0:9f237b11f0a8 | 30 | * @param pin The mbed data pin name |
aswild | 0:9f237b11f0a8 | 31 | * @param N The number of pixels in the strip |
aswild | 0:9f237b11f0a8 | 32 | */ |
aswild | 0:9f237b11f0a8 | 33 | NeoStrip(PinName pin, int N); |
aswild | 0:9f237b11f0a8 | 34 | |
aswild | 0:9f237b11f0a8 | 35 | /** |
aswild | 0:9f237b11f0a8 | 36 | * Set an overall brightness scale for the entire strip. |
aswild | 0:9f237b11f0a8 | 37 | * When colors are set using setPixel(), they are scaled |
aswild | 0:9f237b11f0a8 | 38 | * according to this brightness. |
aswild | 0:9f237b11f0a8 | 39 | * |
aswild | 0:9f237b11f0a8 | 40 | * Because NeoPixels are very bright and draw a lot of current, |
aswild | 0:9f237b11f0a8 | 41 | * the brightness is set to 0.5 by default. |
aswild | 0:9f237b11f0a8 | 42 | * |
aswild | 0:9f237b11f0a8 | 43 | * @param bright The brightness scale between 0 and 1.0 |
aswild | 0:9f237b11f0a8 | 44 | */ |
aswild | 0:9f237b11f0a8 | 45 | void setBrightness(float bright); |
aswild | 0:9f237b11f0a8 | 46 | |
aswild | 0:9f237b11f0a8 | 47 | /** |
aswild | 0:9f237b11f0a8 | 48 | * Set a single pixel to the specified color. |
aswild | 0:9f237b11f0a8 | 49 | * |
aswild | 0:9f237b11f0a8 | 50 | * This method (and all other setPixel methods) uses modulo-N arithmetic |
aswild | 0:9f237b11f0a8 | 51 | * when addressing pixels. If p >= N, the pixel address will wrap around. |
aswild | 0:9f237b11f0a8 | 52 | * |
aswild | 0:9f237b11f0a8 | 53 | * @param p The pixel number (starting at 0) to set |
aswild | 0:9f237b11f0a8 | 54 | * @param color A 24-bit color packed into a single int, |
aswild | 0:9f237b11f0a8 | 55 | * using standard hex color codes (e.g. 0xFF0000 is red) |
aswild | 0:9f237b11f0a8 | 56 | */ |
aswild | 0:9f237b11f0a8 | 57 | void setPixel(int p, int color); |
aswild | 0:9f237b11f0a8 | 58 | |
aswild | 0:9f237b11f0a8 | 59 | /** |
aswild | 0:9f237b11f0a8 | 60 | * Set a single pixel to the specified color, with red, green, and blue |
aswild | 0:9f237b11f0a8 | 61 | * values in separate arguments. |
aswild | 0:9f237b11f0a8 | 62 | */ |
aswild | 0:9f237b11f0a8 | 63 | void setPixel(int p, uint8_t red, uint8_t green, uint8_t blue); |
aswild | 0:9f237b11f0a8 | 64 | |
aswild | 0:9f237b11f0a8 | 65 | /** |
aswild | 0:9f237b11f0a8 | 66 | * Set n pixels starting at pixel p. |
aswild | 0:9f237b11f0a8 | 67 | * |
aswild | 0:9f237b11f0a8 | 68 | * @param p The first pixel in the strip to set. |
aswild | 0:9f237b11f0a8 | 69 | * @param n The number of pixels to set. |
aswild | 0:9f237b11f0a8 | 70 | * @param colors An array of length n containing the 24-bit colors for each pixel |
aswild | 0:9f237b11f0a8 | 71 | */ |
aswild | 0:9f237b11f0a8 | 72 | void setPixels(int p, int n, const int *colors); |
aswild | 0:9f237b11f0a8 | 73 | |
aswild | 0:9f237b11f0a8 | 74 | /** |
aswild | 0:9f237b11f0a8 | 75 | * Reset all pixels in the strip to be of (0x000000) |
aswild | 0:9f237b11f0a8 | 76 | */ |
aswild | 0:9f237b11f0a8 | 77 | void clear(); |
aswild | 0:9f237b11f0a8 | 78 | |
aswild | 0:9f237b11f0a8 | 79 | /** |
aswild | 0:9f237b11f0a8 | 80 | * Write the colors out to the strip; this method must be called |
aswild | 0:9f237b11f0a8 | 81 | * to see any hardware effect. |
aswild | 0:9f237b11f0a8 | 82 | * |
aswild | 0:9f237b11f0a8 | 83 | * This function disables interrupts while the strip data is being sent, |
aswild | 0:9f237b11f0a8 | 84 | * each pixel takes approximately 30us to send, plus a 50us reset pulse |
aswild | 0:9f237b11f0a8 | 85 | * at the end. |
aswild | 0:9f237b11f0a8 | 86 | */ |
aswild | 0:9f237b11f0a8 | 87 | void write(); |
jamesmsong | 2:d607600a697d | 88 | |
jamesmsong | 2:d607600a697d | 89 | void initialize(); |
jamesmsong | 2:d607600a697d | 90 | |
jamesmsong | 2:d607600a697d | 91 | void progress(float time); |
otis22894 | 3:00f3f32e6a1e | 92 | |
otis22894 | 3:00f3f32e6a1e | 93 | void training(); |
otis22894 | 3:00f3f32e6a1e | 94 | |
otis22894 | 3:00f3f32e6a1e | 95 | void training_mode(int mode); |
otis22894 | 4:623b0c643dd6 | 96 | |
otis22894 | 4:623b0c643dd6 | 97 | void start_up(); |
otis22894 | 4:623b0c643dd6 | 98 | |
otis22894 | 4:623b0c643dd6 | 99 | void bad_breath(); |
aswild | 0:9f237b11f0a8 | 100 | |
aswild | 0:9f237b11f0a8 | 101 | protected: |
aswild | 0:9f237b11f0a8 | 102 | NeoColor *strip; // pixel data buffer modified by setPixel() and used by neo_out() |
aswild | 0:9f237b11f0a8 | 103 | int N; // the number of pixels in the strip |
aswild | 0:9f237b11f0a8 | 104 | int Nbytes; // the number of bytes of pixel data (always N*3) |
aswild | 0:9f237b11f0a8 | 105 | float bright; // the master strip brightness |
aswild | 0:9f237b11f0a8 | 106 | gpio_t gpio; // gpio struct for initialization and getting register addresses |
otis22894 | 4:623b0c643dd6 | 107 | Ticker ticker; |
aswild | 0:9f237b11f0a8 | 108 | }; |
aswild | 0:9f237b11f0a8 | 109 | |
aswild | 0:9f237b11f0a8 | 110 | #endif |
aswild | 0:9f237b11f0a8 | 111 | |
aswild | 0:9f237b11f0a8 | 112 |