Simple neopixel (WS2812) library, tuned for stm32 (L432) at 80 MHz Should be compatible with any stm32, different clock speed may require timing adjustments in neopixel.c

Dependents:   NEOPIXEL_SAMPLE

Committer:
tichise
Date:
Thu Jan 02 07:56:44 2020 +0000
Revision:
1:415459e3f93b
Parent:
0:a81364d9a67b
Fixed __nop from lowercase to uppercase to work with PlatformIO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MightyPork 0:a81364d9a67b 1 #ifndef NEOPIXEL_H
MightyPork 0:a81364d9a67b 2 #define NEOPIXEL_H
MightyPork 0:a81364d9a67b 3 #include "mbed.h"
MightyPork 0:a81364d9a67b 4
MightyPork 0:a81364d9a67b 5 /*
MightyPork 0:a81364d9a67b 6 // Example
MightyPork 0:a81364d9a67b 7
MightyPork 0:a81364d9a67b 8 NeoPixelOut npx(D12);
MightyPork 0:a81364d9a67b 9
MightyPork 0:a81364d9a67b 10 int main() {
MightyPork 0:a81364d9a67b 11 wait(0.2); // wait for HSE to stabilize
MightyPork 0:a81364d9a67b 12
MightyPork 0:a81364d9a67b 13 npx.global_scale = 1.0f; // Adjust brightness
MightyPork 0:a81364d9a67b 14 npx.normalize = true; // Equalize brightness to make r + g + b = 255
MightyPork 0:a81364d9a67b 15
MightyPork 0:a81364d9a67b 16 Pixel strip[6];
MightyPork 0:a81364d9a67b 17 strip[0].hex = 0xFF0000;
MightyPork 0:a81364d9a67b 18 strip[1].hex = 0xFFFF00;
MightyPork 0:a81364d9a67b 19 strip[2].hex = 0x00FF00;
MightyPork 0:a81364d9a67b 20 strip[3].hex = 0x00FFFF;
MightyPork 0:a81364d9a67b 21 strip[4].hex = 0x0000FF;
MightyPork 0:a81364d9a67b 22 strip[5].hex = 0xFF00FF;
MightyPork 0:a81364d9a67b 23
MightyPork 0:a81364d9a67b 24 npx.send(strip, 6);
MightyPork 0:a81364d9a67b 25
MightyPork 0:a81364d9a67b 26 while(1);
MightyPork 0:a81364d9a67b 27 }
MightyPork 0:a81364d9a67b 28 */
MightyPork 0:a81364d9a67b 29
MightyPork 0:a81364d9a67b 30
MightyPork 0:a81364d9a67b 31
MightyPork 0:a81364d9a67b 32 /**
MightyPork 0:a81364d9a67b 33 * @brief Struct for easy manipulation of RGB colors.
MightyPork 0:a81364d9a67b 34 *
MightyPork 0:a81364d9a67b 35 * Set components in the xrgb.r (etc.) and you will get
MightyPork 0:a81364d9a67b 36 * the hex in xrgb.num.
MightyPork 0:a81364d9a67b 37 */
MightyPork 0:a81364d9a67b 38 union Pixel {
MightyPork 0:a81364d9a67b 39 /** Struct for access to individual color components */
MightyPork 0:a81364d9a67b 40 struct __attribute__((packed)) {
MightyPork 0:a81364d9a67b 41 uint8_t b;
MightyPork 0:a81364d9a67b 42 uint8_t g;
MightyPork 0:a81364d9a67b 43 uint8_t r;
MightyPork 0:a81364d9a67b 44 uint8_t a; // unused
MightyPork 0:a81364d9a67b 45 };
MightyPork 0:a81364d9a67b 46
MightyPork 0:a81364d9a67b 47 /** RGB color as a single uint32_t */
MightyPork 0:a81364d9a67b 48 uint32_t hex;
MightyPork 0:a81364d9a67b 49 };
MightyPork 0:a81364d9a67b 50
MightyPork 0:a81364d9a67b 51
MightyPork 0:a81364d9a67b 52 class NeoPixelOut : DigitalOut {
MightyPork 0:a81364d9a67b 53 private:
MightyPork 0:a81364d9a67b 54 void byte(uint32_t b);
MightyPork 0:a81364d9a67b 55
MightyPork 0:a81364d9a67b 56 public:
MightyPork 0:a81364d9a67b 57 bool normalize;
MightyPork 0:a81364d9a67b 58 float global_scale;
MightyPork 0:a81364d9a67b 59
MightyPork 0:a81364d9a67b 60 NeoPixelOut(PinName pin);
MightyPork 0:a81364d9a67b 61
MightyPork 0:a81364d9a67b 62 void send(Pixel *colors, uint32_t count, bool flipwait=true);
MightyPork 0:a81364d9a67b 63
MightyPork 0:a81364d9a67b 64 /** Wait long enough to make the colors show up */
MightyPork 0:a81364d9a67b 65 void flip(void);
MightyPork 0:a81364d9a67b 66 };
MightyPork 0:a81364d9a67b 67
MightyPork 0:a81364d9a67b 68
MightyPork 0:a81364d9a67b 69 #endif /* NEOPIXEL_H */