明石高専ロボ研 mbedライブラリ

Dependencies:   mbed

Dependents:   MDD_L432KC USB2RS485 pathtracking odometry ... more

neopixel.cpp

Committer:
TanakaRobo
Date:
2021-10-13
Revision:
14:bdd394483434
Parent:
11:eaf2e3166d20

File content as of revision 14:bdd394483434:

#include "mbed.h"
#include "neopixel.h"

#if defined(TARGET_NUCLEO_L432KC)

#define PIXEL_WAIT_1_ (6)
#define PIXEL_WAIT_2_ (2)
#define PIXEL_WAIT_3_ (2)
#define PIXEL_WAIT_4_ (5)

#elif defined(TARGET_NUCLEO_F446RE)

#define PIXEL_WAIT_1_ (9)
#define PIXEL_WAIT_2_ (5)
#define PIXEL_WAIT_3_ (4)
#define PIXEL_WAIT_4_ (9)

#else
#error This board is not supported
#endif

NeoPixelOut::NeoPixelOut(PinName pin, int num) : DigitalOut(pin)
{
    normalize = false;
    global_scale = 1.0f;
    num_pixels_ = num;
    strip_.resize(num);
}

// The timing should be approximately 800ns/300ns, 300ns/800ns
void NeoPixelOut::byte(register uint32_t byte)
{        
    for (int i = 0; i < 8; i++) {
        gpio_write(&gpio, 1);
        
        // duty cycle determines bit value
        if (byte & 0x80) {
            // one
            for(int j = 0; j < PIXEL_WAIT_1_; j++) asm("NOP");//6 9
            
            gpio_write(&gpio, 0);
            for(int j = 0; j < PIXEL_WAIT_2_; j++) asm("NOP");//2 5
        }
        else {
            // zero
            for(int j = 0; j < PIXEL_WAIT_3_; j++) asm("NOP");//2 4
            
            gpio_write(&gpio, 0);
            for(int j = 0; j < PIXEL_WAIT_4_; j++) asm("NOP");//5 9
        }

        byte = byte << 1; // shift to next bit
    }
    
}

void NeoPixelOut::setPixelColor(uint32_t i,uint32_t color){
    if(i < num_pixels_){
        strip_[i].hex = color;
    }
}
    
void NeoPixelOut::show(){
    // Disable interrupts in the critical section
    __disable_irq();

    float fr,fg,fb;
    for (int i = 0; i < num_pixels_; i++) {
        fr = strip_[i].r;
        fg = strip_[i].g;
        fb = strip_[i].b;
        
        if (normalize) {
            float scale = 255.0f/(fr+fg+fb);           
            fr *= scale;
            fg *= scale;
            fb *= scale;            
        }
        
        fr *= global_scale;
        fg *= global_scale;
        fb *= global_scale;
        
        if (fr > 255) fr = 255; 
        if (fg > 255) fg = 255;
        if (fb > 255) fb = 255;
        if (fr < 0) fr = 0; 
        if (fg < 0) fg = 0;
        if (fb < 0) fb = 0;
        
        // Black magic to fix distorted timing
        #ifdef __HAL_FLASH_INSTRUCTION_CACHE_DISABLE
        __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
        #endif
        
        byte((int)fg);
        byte((int)fr);
        byte((int)fb);
        
        #ifdef __HAL_FLASH_INSTRUCTION_CACHE_ENABLE
        __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
        #endif
    }

    __enable_irq();

    flip();
}

void NeoPixelOut::send(Pixel *colors, uint32_t count, bool flipwait)
{
    // Disable interrupts in the critical section
    __disable_irq();

    Pixel* rgb;
    float fr,fg,fb;
    for (int i = 0; i < count; i++) {
        rgb = colors++;
        fr = (int)rgb->r;
        fg = (int)rgb->g;
        fb = (int)rgb->b;
        
        if (normalize) {
            float scale = 255.0f/(fr+fg+fb);           
            fr *= scale;
            fg *= scale;
            fb *= scale;            
        }
        
        fr *= global_scale;
        fg *= global_scale;
        fb *= global_scale;
        
        if (fr > 255) fr = 255; 
        if (fg > 255) fg = 255;
        if (fb > 255) fb = 255;
        if (fr < 0) fr = 0; 
        if (fg < 0) fg = 0;
        if (fb < 0) fb = 0;
        
        // Black magic to fix distorted timing
        #ifdef __HAL_FLASH_INSTRUCTION_CACHE_DISABLE
        __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
        #endif
        
        byte((int)fg);
        byte((int)fr);
        byte((int)fb);
        
        #ifdef __HAL_FLASH_INSTRUCTION_CACHE_ENABLE
        __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
        #endif
    }

    __enable_irq();

    if (flipwait) flip();
}

uint32_t NeoPixelOut::color(uint8_t b,uint8_t g,uint8_t r){
    return (uint32_t)b + ((uint32_t)g << 8) + ((uint32_t)r << 16);
}
 
int NeoPixelOut::numPixels(){
    return num_pixels_;
}

void NeoPixelOut::off(bool flag){
    for(int i = 0;i < num_pixels_;i++){
        strip_[i].hex = 0;
    }
    if(flag){
        show();
    }
}

void NeoPixelOut::changeNum(uint32_t num){
    strip_.resize(num);
    num_pixels_ = num;
}

void NeoPixelOut::setBrightness(float brightness){
    global_scale = brightness;
}

void NeoPixelOut::flip(void)
{
    wait_us(50);    
}