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: Nucleo_neopixel_ovgu Nucleo_neopixel_ovgu1 Nucleo_neopixel_ovgu3
Fork of NeoPixel by
neopixel.cpp@1:037882a8e193, 2017-12-11 (annotated)
- Committer:
- Hinz
- Date:
- Mon Dec 11 12:54:00 2017 +0000
- Revision:
- 1:037882a8e193
- Parent:
- 0:a81364d9a67b
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MightyPork | 0:a81364d9a67b | 1 | #include "mbed.h" |
MightyPork | 0:a81364d9a67b | 2 | #include "neopixel.h" |
MightyPork | 0:a81364d9a67b | 3 | |
Hinz | 1:037882a8e193 | 4 | NeoPixel::NeoPixel(PinName pin, int n) : DigitalOut(pin) |
MightyPork | 0:a81364d9a67b | 5 | { |
Hinz | 1:037882a8e193 | 6 | _pixels = (Pixel*)malloc(n*sizeof(Pixel)); |
Hinz | 1:037882a8e193 | 7 | memset(_pixels, 0, n*sizeof(Pixel)); |
Hinz | 1:037882a8e193 | 8 | _npixels = n; |
MightyPork | 0:a81364d9a67b | 9 | } |
MightyPork | 0:a81364d9a67b | 10 | |
MightyPork | 0:a81364d9a67b | 11 | // The timing should be approximately 800ns/300ns, 300ns/800ns |
Hinz | 1:037882a8e193 | 12 | void NeoPixel::byte(register uint32_t byte) |
MightyPork | 0:a81364d9a67b | 13 | { |
MightyPork | 0:a81364d9a67b | 14 | for (int i = 0; i < 8; i++) { |
MightyPork | 0:a81364d9a67b | 15 | gpio_write(&gpio, 1); |
MightyPork | 0:a81364d9a67b | 16 | |
MightyPork | 0:a81364d9a67b | 17 | // duty cycle determines bit value |
MightyPork | 0:a81364d9a67b | 18 | if (byte & 0x80) { |
MightyPork | 0:a81364d9a67b | 19 | // one |
MightyPork | 0:a81364d9a67b | 20 | for(int j = 0; j < 6; j++) __nop(); |
MightyPork | 0:a81364d9a67b | 21 | |
MightyPork | 0:a81364d9a67b | 22 | gpio_write(&gpio, 0); |
MightyPork | 0:a81364d9a67b | 23 | for(int j = 0; j < 2; j++) __nop(); |
MightyPork | 0:a81364d9a67b | 24 | } |
MightyPork | 0:a81364d9a67b | 25 | else { |
MightyPork | 0:a81364d9a67b | 26 | // zero |
MightyPork | 0:a81364d9a67b | 27 | for(int j = 0; j < 2; j++) __nop(); |
MightyPork | 0:a81364d9a67b | 28 | |
MightyPork | 0:a81364d9a67b | 29 | gpio_write(&gpio, 0); |
MightyPork | 0:a81364d9a67b | 30 | for(int j = 0; j < 5; j++) __nop(); |
MightyPork | 0:a81364d9a67b | 31 | } |
MightyPork | 0:a81364d9a67b | 32 | |
MightyPork | 0:a81364d9a67b | 33 | byte = byte << 1; // shift to next bit |
MightyPork | 0:a81364d9a67b | 34 | } |
MightyPork | 0:a81364d9a67b | 35 | |
MightyPork | 0:a81364d9a67b | 36 | } |
MightyPork | 0:a81364d9a67b | 37 | |
Hinz | 1:037882a8e193 | 38 | void NeoPixel::show(void) |
MightyPork | 0:a81364d9a67b | 39 | { |
MightyPork | 0:a81364d9a67b | 40 | // Disable interrupts in the critical section |
MightyPork | 0:a81364d9a67b | 41 | __disable_irq(); |
Hinz | 1:037882a8e193 | 42 | |
Hinz | 1:037882a8e193 | 43 | for (int i = 0; i < _npixels; i++) { |
MightyPork | 0:a81364d9a67b | 44 | // Black magic to fix distorted timing |
MightyPork | 0:a81364d9a67b | 45 | #ifdef __HAL_FLASH_INSTRUCTION_CACHE_DISABLE |
MightyPork | 0:a81364d9a67b | 46 | __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); |
MightyPork | 0:a81364d9a67b | 47 | #endif |
MightyPork | 0:a81364d9a67b | 48 | |
Hinz | 1:037882a8e193 | 49 | byte((_pixels+i)->g); |
Hinz | 1:037882a8e193 | 50 | byte((_pixels+i)->r); |
Hinz | 1:037882a8e193 | 51 | byte((_pixels+i)->b); |
MightyPork | 0:a81364d9a67b | 52 | |
MightyPork | 0:a81364d9a67b | 53 | #ifdef __HAL_FLASH_INSTRUCTION_CACHE_ENABLE |
MightyPork | 0:a81364d9a67b | 54 | __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); |
MightyPork | 0:a81364d9a67b | 55 | #endif |
MightyPork | 0:a81364d9a67b | 56 | } |
MightyPork | 0:a81364d9a67b | 57 | |
MightyPork | 0:a81364d9a67b | 58 | __enable_irq(); |
Hinz | 1:037882a8e193 | 59 | wait_us(50); |
MightyPork | 0:a81364d9a67b | 60 | } |
MightyPork | 0:a81364d9a67b | 61 | |
MightyPork | 0:a81364d9a67b | 62 | |
Hinz | 1:037882a8e193 | 63 | void NeoPixel::setColor(int i, uint32_t color) |
MightyPork | 0:a81364d9a67b | 64 | { |
Hinz | 1:037882a8e193 | 65 | if ((i>=0) && (i<_npixels)) |
Hinz | 1:037882a8e193 | 66 | (_pixels+i)->hex = color; |
MightyPork | 0:a81364d9a67b | 67 | } |
Hinz | 1:037882a8e193 | 68 | |
Hinz | 1:037882a8e193 | 69 | |
Hinz | 1:037882a8e193 | 70 | void NeoPixel::clear(void) |
Hinz | 1:037882a8e193 | 71 | { |
Hinz | 1:037882a8e193 | 72 | for (int i=0; i<_npixels; i++) |
Hinz | 1:037882a8e193 | 73 | (_pixels+i)->hex = 0; |
Hinz | 1:037882a8e193 | 74 | } |