//Chainable_LED_XIAO_ESP32C6_1
//1個専用のカラーRGB LEDのライブラリ
//送信クロックの発生
void clk() {
digitalWrite(D9, LOW);//clk
delay(20); // wait for a second
digitalWrite(D9, HIGH);//clk
delay(20); // wait for a second
}//clk
//1バイトを送信する
void sendByte(int b) {
for (int i = 0; i < 8; i++) {
if ( (b & 0x80) == 0) {
digitalWrite(D10, LOW);//data
} else {
digitalWrite(D10, HIGH);//data
}//endif
clk();
b = b << 1;
}//for i
}//sendByte
//1個のLED専用
void setColorRGB(int red, int green, int blue) {
//開始
sendByte(0);
sendByte(0);
sendByte(0);
sendByte(0);
//ここから
int prefix = 0b11000000;
if( (blue & 0x80)==0 ) {prefix = prefix | 0b00100000;}
if( (blue & 0x40)==0 ) {prefix = prefix | 0b00010000;}
if( (green & 0x80)==0 ) {prefix = prefix | 0b00001000;}
if( (green & 0x40)==0 ) {prefix = prefix | 0b00000100;}
if( (red & 0x80)==0 ) {prefix = prefix | 0b00000010;}
if( (red & 0x40)==0 ) {prefix = prefix | 0b00000001;}
sendByte(prefix);
sendByte(blue);
sendByte(green);
sendByte(red);
//ここまで
//終了
sendByte(0);
sendByte(0);
sendByte(0);
sendByte(0);
}//setColorRGB
// the setup function runs once when you press reset or power the board
void setup() {
//GPIOポートの初期化
// initialize digital pin LED_BUILTIN as an output.
pinMode(D10, OUTPUT); //data
pinMode(D9, OUTPUT); //clk
}//setup
// the loop function runs over and over again forever
void loop() {
//赤
setColorRGB(0x55, 0x00, 0x00);
delay(500); // wait for a second
//緑
setColorRGB(0x00, 0x55, 0x00);
delay(500); // wait for a second
//青
setColorRGB(0x00, 0x00, 0x55);
delay(500); // wait for a second
}//loop