STM32L010とWS2812Bを使い、シリアルから入力された値で光らす。


x9600bps
x3個
x500msでリセット

目的
PCからWS2812Bをコントロールしたい


![serial_ws2812_010_1_1.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/7e377715-e586-93de-b876-90b333c90e18.jpeg)


![serial_ws2812_010_1_2.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/4e1a727f-740a-b9f7-3379-c29538970558.jpeg)



```c++

#include "mbed.h"

//Serial pc(USBTX, USBRX); // tx, rx
//Serial pc(SERIAL_TX, SERIAL_RX); //767 303
RawSerial pc(PA_2, PA_3); //010

//myled         1.827Mhz 010
//GPIOA->ODR    7,995Mhz 010

#define on1   0b0000000000010000
#define off1  0b0000000000000000

DigitalOut myled(PA_4);

int l[90]; //max30 led

void bit_on1();
void bit_off1();

//             12345678   12345678   12345678   12345678
int b8[8] = {0b10000000,0b01000000,0b00100000,0b00010000,
             0b00001000,0b00000100,0b00000010,0b00000001, };

int ws_led(int num1)
{
    int on_off;

    //__disable_irq(); // disable interrupt
    //__enable_irq(); // enable interrupt

    //GPIO bkup
    //int g_bk  = GPIOA->ODR;


    for(int ii=0;ii<num1;ii++){
        //pc.printf("  -%d\t%d\r\n",ii,l[ii]); //303
        
        //8ビット分送る
        for(int jj=0;jj<8;jj++){
            //pc.printf("  -%d\r\n",jj); //303
            
            on_off = l[ii] & b8[jj];
        
            if( on_off == 0 ){
                //ビットが0
                //pc.printf("0"); //303
                bit_off1();
            } else {
                //ビットが1 
                //pc.printf("1"); //303
                bit_on1();
            }//endif
        
        }//for 8bit
        
        
    }//for max30byt

    //__disable_irq(); // disable interrupt
    //__enable_irq(); // enable interrupt

    //GPIO bkup
    //GPIOA->ODR = g_bk;

    return(0);
}

char *hex_b_c(int i_num);

int  ch_len;
char ch_b[9+1]; //文字列のバッファー
char *ch_input()
{
    int  ch_i=0;    //文字列のループカウンター
    int  ch_y;      //一文字分の一時バッファー
    int  ii;        //ループカウンター
    
    ch_len=0;
    //最初データ待ち
    while(1) {      if(pc.readable() == 1) {break;}  }
    
    while(1) {

        //バッファーにデータがあるか
        for(ii=0;ii<500;ii++){
            wait_ms(1);
            if(pc.readable() == 1) {break;}
        }//for


        if(ii == 500){/*pc.printf("time out\n\r")*/;return(ch_b);}
        //if(ii != 500){pc.printf("not5000\n\r");} ///{ //jump bbb

        //1バイト分のデータの入力
        ch_y = pc.getc();
        //テータの転記
        ch_b[ch_i]=ch_y;ch_i++;ch_b[ch_i]=0;ch_len++;
        //pc.putc(ch_y);
        pc.printf( hex_b_c( ch_y ) ); 
        if(ch_i >= 9){ch_b[ch_i]=0;/*pc.printf("\n\r")*/;break;}     
    } //while
    return(ch_b);
} //ch_input

char *hex_table="01234567890ABCDEF";
char hex_b[2+1];
char *hex_b_c(int i_num)
{
    hex_b[0] = hex_table[i_num>>4];
    hex_b[1] = hex_table[i_num & 0xf];
    hex_b[2] = 0;
    return(hex_b);
}

//メイン
int main() {
    
    //pc.printf("\r\n303\r\n"); //303

    //pc.printf("\r\n010\r\n"); //010


//char buffer[30];
while(1) {
    //int h=atoi( ch_input() ); //文字列の入力
    ch_input();
    //pc.printf( ch_input() ); //010 //文字列の入力
    //pc.printf("\r\n"); //010
    
    //sprintf(buffer,"%d",ch_len);
    //pc.printf( buffer );
    pc.printf("\r\n"); //010

    for(int ii=0;ii<ch_len;ii++){
        l[ii] = ch_b[ii];
    }

    ws_led(ch_len);
    
}

//    l[0] = 0;
//    l[1] = 0;
//    l[2] = 12;
//
//    ws_led(3);
    

} //main

// 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 
void bit_off1(){
   //0.3us 800khz
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
       
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;


}//bit_off1


// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
void bit_on1(){
   //1us 800khz
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
        GPIOA->ODR = on1;
       
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
}//bit_on1

//容量削減
void error(const char* format, ...){}




```