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

Dependencies:   mbed

Dependents:   MDD_L432KC USB2RS485 pathtracking odometry ... more

Revision:
5:a7894e6982ea
Parent:
0:ca84ed7518f5
Child:
7:4ad54efe2fdd
diff -r 39ef4d91dc34 -r a7894e6982ea neopixel.cpp
--- a/neopixel.cpp	Fri Jul 24 14:12:15 2020 +0000
+++ b/neopixel.cpp	Thu Nov 12 16:38:23 2020 +0000
@@ -2,22 +2,25 @@
 #include "neopixel.h"
 
 const int wait_time[4][2] = {
-    {6,14},
+    {6, 9},
     {2, 5},
-    {2, 6},
-    {5,11}
+    {2, 4},
+    {5, 9}
 };
     
-NeoPixelOut::NeoPixelOut(PinName pin) : DigitalOut(pin)
+NeoPixelOut::NeoPixelOut(PinName pin, int num) : DigitalOut(pin)
 {
     normalize = false;
     global_scale = 1.0f;
-    #ifdef __STM32L432xx_H
-        boad_ = 0;
+    #if  defined(TARGET_NUCLEO_L432KC)
+        board_ = 0;
+    #elif defined(TARGET_NUCLEO_F446RE)
+        board_ = 1;
+    #else
+    #error "This board is not supported"
     #endif
-    #ifdef __STM32F446xx_H
-        boad_ = 1;
-    #endif
+    num_pixels_ = num;
+    strip_.resize(num);
 }
 
 // The timing should be approximately 800ns/300ns, 300ns/800ns
@@ -29,17 +32,17 @@
         // duty cycle determines bit value
         if (byte & 0x80) {
             // one
-            for(int j = 0; j < wait_time[0][boad_]; j++) asm("NOP");//6 14
+            for(int j = 0; j < wait_time[0][board_]; j++) asm("NOP");//6 9
             
             gpio_write(&gpio, 0);
-            for(int j = 0; j < wait_time[1][boad_]; j++) asm("NOP");//2 5
+            for(int j = 0; j < wait_time[1][board_]; j++) asm("NOP");//2 5
         }
         else {
             // zero
-            for(int j = 0; j < wait_time[2][boad_]; j++) asm("NOP");//2 6
+            for(int j = 0; j < wait_time[2][board_]; j++) asm("NOP");//2 4
             
             gpio_write(&gpio, 0);
-            for(int j = 0; j < wait_time[3][boad_]; j++) asm("NOP");//5 11
+            for(int j = 0; j < wait_time[3][board_]; j++) asm("NOP");//5 9
         }
 
         byte = byte << 1; // shift to next bit
@@ -47,6 +50,61 @@
     
 }
 
+void NeoPixelOut::setPixelColor(uint32_t i,uint32_t color){
+    if(i >= num_pixels_){
+        return;
+    }
+    strip_[i].hex = color;
+}
+    
+void NeoPixelOut::show(){
+    // Disable interrupts in the critical section
+    __disable_irq();
+
+    float fr,fg,fb;
+    int count = strip_.size();
+    for (int i = 0; i < count; 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
@@ -97,6 +155,31 @@
     if (flipwait) flip();
 }
 
+uint32_t NeoPixelOut::color(uint32_t b,uint32_t g,uint32_t r){
+    return b + (g << 8) + (r << 16);
+}
+ 
+int NeoPixelOut::numPixels(){
+    return num_pixels_;
+}
+
+void NeoPixelOut::off(bool flag){
+    for(int i = 0;i < strip_.size();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)
 {