Diff
checker
文本
文本
图像
文档
Excel
文件夹
Legal
Enterprise
桌面版
定价
登录
下载 Diffchecker 桌面版
比较文本
查找两个文本文件之间的差异
工具
历史
实时编辑器
折叠未更改行
关闭换行
视图
拆分
统一
比对精度
智能
单词
字符
语法高亮
选择语法
忽略
文本转换
转到第一个差异
编辑输入
Diffchecker Desktop
运行Diffchecker最安全的方式。获取Diffchecker桌面应用:您的差异永远不会离开您的电脑!
获取桌面版
Untitled diff
创建于
11年前
差异永不过期
清除
导出
分享
解释
0 删除
行
总计
删除
字符
总计
删除
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
267 行
全部复制
1 添加
行
总计
添加
字符
总计
添加
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
267 行
全部复制
#include "FastLED/FastLED.h"
#include "FastLED/FastLED.h"
FASTLED_USING_NAMESPACE;
FASTLED_USING_NAMESPACE;
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
#define LED_PIN D0
#define LED_PIN D0
#define LED_TYPE WS2811
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define COLOR_ORDER GRB
#define NUM_LEDS 512
#define NUM_LEDS 512
CRGB leds[NUM_LEDS];
CRGB leds[NUM_LEDS];
#define MICROPHONE 12
#define MICROPHONE 12
#define GAIN_CONTROL 11
#define GAIN_CONTROL 11
void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount);
void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount);
CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter);
CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter);
CRGB makeDarker( const CRGB& color, fract8 howMuchDarker);
CRGB makeDarker( const CRGB& color, fract8 howMuchDarker);
// Twinkling 'holiday' lights that fade up and down in brightness.
// Twinkling 'holiday' lights that fade up and down in brightness.
// Colors are chosen from a palette; a few palettes are provided.
// Colors are chosen from a palette; a few palettes are provided.
//
//
// The basic operation is that all pixels stay black until they
// The basic operation is that all pixels stay black until they
// are 'seeded' with a relatively dim color. The dim colors
// are 'seeded' with a relatively dim color. The dim colors
// are repeatedly brightened until they reach full brightness, then
// are repeatedly brightened until they reach full brightness, then
// are darkened repeatedly until they are fully black again.
// are darkened repeatedly until they are fully black again.
//
//
// A set of 'directionFlags' is used to track whether a given
// A set of 'directionFlags' is used to track whether a given
// pixel is presently brightening up or darkening down.
// pixel is presently brightening up or darkening down.
//
//
// For illustration purposes, two implementations of directionFlags
// For illustration purposes, two implementations of directionFlags
// are provided: a simple one-byte-per-pixel flag, and a more
// are provided: a simple one-byte-per-pixel flag, and a more
// complicated, more compact one-BIT-per-pixel flag.
// complicated, more compact one-BIT-per-pixel flag.
//
//
// Darkening colors accurately is relatively easy: scale down the
// Darkening colors accurately is relatively easy: scale down the
// existing color channel values. Brightening colors is a bit more
// existing color channel values. Brightening colors is a bit more
// error prone, as there's some loss of precision. If your colors
// error prone, as there's some loss of precision. If your colors
// aren't coming our 'right' at full brightness, try increasing the
// aren't coming our 'right' at full brightness, try increasing the
// STARTING_BRIGHTNESS value.
// STARTING_BRIGHTNESS value.
//
//
// -Mark Kriegsman, December 2014
// -Mark Kriegsman, December 2014
#define MASTER_BRIGHTNESS 200
#define MASTER_BRIGHTNESS 200
#define STARTING_BRIGHTNESS 64
#define STARTING_BRIGHTNESS 64
#define FADE_IN_SPEED 32
#define FADE_IN_SPEED 32
#define FADE_OUT_SPEED 60
#define FADE_OUT_SPEED 60
#define DENSITY 255
#define DENSITY 255
void setup() {
void setup() {
delay(3000);
delay(3000);
FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(MASTER_BRIGHTNESS);
FastLED.setBrightness(MASTER_BRIGHTNESS);
initMicrophone();
initMicrophone();
// analogReference(DEFAULT);
// analogReference(DEFAULT);
// Serial.begin(57600);
// Serial.begin(57600);
}
}
void initMicrophone()
void initMicrophone()
{
{
pinMode(GAIN_CONTROL, OUTPUT);
pinMode(GAIN_CONTROL, OUTPUT);
digitalWrite(GAIN_CONTROL, LOW);
digitalWrite(GAIN_CONTROL, LOW);
}
}
CRGBPalette16 gPalette;
CRGBPalette16 gPalette;
int readMaxSound( uint8_t ms)
int readMaxSound( uint8_t ms)
{
{
uint32_t startms = millis();
uint32_t startms = millis();
while( millis() == startms) {};
while( millis() == startms) {};
startms = millis();
startms = millis();
int maxvol = 0;
int maxvol = 0;
while( (startms + ms) > millis()) {
while( (startms + ms) > millis()) {
// int mic = analogRead(MICROPHONE)-512;
// int mic = analogRead(MICROPHONE)-512;
int mic = analogRead(MICROPHONE);
int mic = analogRead(MICROPHONE);
if( mic < 0) mic = 0-mic;
if( mic < 0) mic = 0-mic;
if( mic > maxvol) maxvol = mic;
if( mic > maxvol) maxvol = mic;
}
}
return maxvol;
return maxvol;
}
}
void loop()
void loop()
{
{
chooseColorPalette();
chooseColorPalette();
colortwinkles();
colortwinkles();
// delay(2);
// delay(2);
// int mic = readMaxSound(10);
// int mic = readMaxSound(10);
// mic /= 2;
// mic /= 2;
// mic = dim8_raw( mic);
// mic = dim8_raw( mic);
// mic = dim8_raw( mic);
// mic = dim8_raw( mic);
// // Serial.println(mic);
// // Serial.println(mic);
// static int avgmic = 0;
// static int avgmic = 0;
// avgmic = ((avgmic * 3) + mic) / 4;
// avgmic = ((avgmic * 3) + mic) / 4;
// mic = avgmic;
// mic = avgmic;
// delay(2);
// delay(2);
// if( mic > 0) {
// if( mic > 0) {
// for( int j = 0; j < mic; j++) {
// for( int j = 0; j < mic; j++) {
// leds[random16(NUM_LEDS)] = ColorFromPalette( gPalette, random8());
// leds[random16(NUM_LEDS)] = ColorFromPalette( gPalette, random8());
// }
// }
// }
// }
FastLED.show();
FastLED.show();
// FastLED.delay(1);
// FastLED.delay(1);
}
}
void chooseColorPalette()
void chooseColorPalette()
{
{
uint8_t numberOfPalettes = 9;
uint8_t numberOfPalettes = 9;
uint8_t secondsPerPalette = 60;
uint8_t secondsPerPalette = 60;
uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes;
uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes;
CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(CRGB::FairyLight);
CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(CRGB::FairyLight);
复制
已复制
复制
已复制
whichPalette = 8;
//
whichPalette = 8;
switch( whichPalette) {
switch( whichPalette) {
case 0: // Red, Green, and White
case 0: // Red, Green, and White
gPalette = CRGBPalette16( r,r,r,r, r,r,r,r, g,g,g,g, w,w,w,w );
gPalette = CRGBPalette16( r,r,r,r, r,r,r,r, g,g,g,g, w,w,w,w );
break;
break;
case 1: // Blue and White
case 1: // Blue and White
//gPalette = CRGBPalette16( b,b,b,b, b,b,b,b, w,w,w,w, w,w,w,w );
//gPalette = CRGBPalette16( b,b,b,b, b,b,b,b, w,w,w,w, w,w,w,w );
gPalette = CloudColors_p; // Blues and whites!
gPalette = CloudColors_p; // Blues and whites!
break;
break;
case 2: // Rainbow of colors
case 2: // Rainbow of colors
gPalette = RainbowColors_p;
gPalette = RainbowColors_p;
break;
break;
case 3: // Incandescent "fairy lights"
case 3: // Incandescent "fairy lights"
gPalette = CRGBPalette16( l,l,l,l, l,l,l,l, l,l,l,l, l,l,l,l );
gPalette = CRGBPalette16( l,l,l,l, l,l,l,l, l,l,l,l, l,l,l,l );
break;
break;
case 4: // Snow
case 4: // Snow
gPalette = CRGBPalette16( W,W,W,W, w,w,w,w, w,w,w,w, w,w,w,w );
gPalette = CRGBPalette16( W,W,W,W, w,w,w,w, w,w,w,w, w,w,w,w );
break;
break;
case 5:
case 5:
gPalette = LavaColors_p;
gPalette = LavaColors_p;
break;
break;
case 6:
case 6:
gPalette = ForestColors_p;
gPalette = ForestColors_p;
break;
break;
case 7:
case 7:
gPalette = PartyColors_p;
gPalette = PartyColors_p;
break;
break;
case 8:
case 8:
uint8_t h = beatsin8(1);
uint8_t h = beatsin8(1);
gPalette = CHSVPalette16( CHSV(h,255,255), CHSV(h+32, 255,255));
gPalette = CHSVPalette16( CHSV(h,255,255), CHSV(h+32, 255,255));
break;
break;
}
}
}
}
enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 };
enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 };
void colortwinkles()
void colortwinkles()
{
{
// Make each pixel brighter or darker, depending on
// Make each pixel brighter or darker, depending on
// its 'direction' flag.
// its 'direction' flag.
brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED);
brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED);
// Now consider adding a new random twinkle
// Now consider adding a new random twinkle
if( random8() < DENSITY ) {
if( random8() < DENSITY ) {
int pos = random16(NUM_LEDS);
int pos = random16(NUM_LEDS);
if( leds[pos] ) pos = random16(NUM_LEDS);
if( leds[pos] ) pos = random16(NUM_LEDS);
if( !leds[pos]) {
if( !leds[pos]) {
leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
setPixelDirection(pos, GETTING_BRIGHTER);
setPixelDirection(pos, GETTING_BRIGHTER);
}
}
}
}
}
}
void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount)
void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount)
{
{
for( uint16_t i = 0; i < NUM_LEDS; i++) {
for( uint16_t i = 0; i < NUM_LEDS; i++) {
if( getPixelDirection(i) == GETTING_DARKER) {
if( getPixelDirection(i) == GETTING_DARKER) {
// This pixel is getting darker
// This pixel is getting darker
leds[i] = makeDarker( leds[i], fadeDownAmount);
leds[i] = makeDarker( leds[i], fadeDownAmount);
} else {
} else {
// This pixel is getting brighter
// This pixel is getting brighter
leds[i] = makeBrighter( leds[i], fadeUpAmount);
leds[i] = makeBrighter( leds[i], fadeUpAmount);
// now check to see if we've maxxed out the brightness
// now check to see if we've maxxed out the brightness
if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) {
if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) {
// if so, turn around and start getting darker
// if so, turn around and start getting darker
setPixelDirection(i, GETTING_DARKER);
setPixelDirection(i, GETTING_DARKER);
}
}
}
}
}
}
}
}
CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter)
CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter)
{
{
CRGB incrementalColor = color;
CRGB incrementalColor = color;
incrementalColor.nscale8( howMuchBrighter);
incrementalColor.nscale8( howMuchBrighter);
return color + incrementalColor;
return color + incrementalColor;
}
}
CRGB makeDarker( const CRGB& color, fract8 howMuchDarker)
CRGB makeDarker( const CRGB& color, fract8 howMuchDarker)
{
{
CRGB newcolor = color;
CRGB newcolor = color;
newcolor.nscale8( 255 - howMuchDarker);
newcolor.nscale8( 255 - howMuchDarker);
return newcolor;
return newcolor;
}
}
// For illustration purposes, there are two separate implementations
// For illustration purposes, there are two separate implementations
// provided here for the array of 'directionFlags':
// provided here for the array of 'directionFlags':
// - a simple one, which uses one byte (8 bits) of RAM for each pixel, and
// - a simple one, which uses one byte (8 bits) of RAM for each pixel, and
// - a compact one, which uses just one BIT of RAM for each pixel.
// - a compact one, which uses just one BIT of RAM for each pixel.
// Set this to 1 or 8 to select which implementation
// Set this to 1 or 8 to select which implementation
// of directionFlags is used. 1=more compact, 8=simpler.
// of directionFlags is used. 1=more compact, 8=simpler.
#define BITS_PER_DIRECTION_FLAG 1
#define BITS_PER_DIRECTION_FLAG 1
#if BITS_PER_DIRECTION_FLAG == 8
#if BITS_PER_DIRECTION_FLAG == 8
// Simple implementation of the directionFlags array,
// Simple implementation of the directionFlags array,
// which takes up one byte (eight bits) per pixel.
// which takes up one byte (eight bits) per pixel.
uint8_t directionFlags[NUM_LEDS];
uint8_t directionFlags[NUM_LEDS];
bool getPixelDirection( uint16_t i) {
bool getPixelDirection( uint16_t i) {
return directionFlags[i];
return directionFlags[i];
}
}
void setPixelDirection( uint16_t i, bool dir) {
void setPixelDirection( uint16_t i, bool dir) {
directionFlags[i] = dir;
directionFlags[i] = dir;
}
}
#endif
#endif
#if BITS_PER_DIRECTION_FLAG == 1
#if BITS_PER_DIRECTION_FLAG == 1
// Compact (but more complicated) implementation of
// Compact (but more complicated) implementation of
// the directionFlags array, using just one BIT of RAM
// the directionFlags array, using just one BIT of RAM
// per pixel. This requires a bunch of bit wrangling,
// per pixel. This requires a bunch of bit wrangling,
// but conserves precious RAM. The cost is a few
// but conserves precious RAM. The cost is a few
// cycles and about 100 bytes of flash program memory.
// cycles and about 100 bytes of flash program memory.
uint8_t directionFlags[ (NUM_LEDS+7) / 8];
uint8_t directionFlags[ (NUM_LEDS+7) / 8];
bool getPixelDirection( uint16_t i) {
bool getPixelDirection( uint16_t i) {
uint16_t index = i / 8;
uint16_t index = i / 8;
uint8_t bitNum = i & 0x07;
uint8_t bitNum = i & 0x07;
// using Arduino 'bitRead' function; expanded code below
// using Arduino 'bitRead' function; expanded code below
return bitRead( directionFlags[index], bitNum);
return bitRead( directionFlags[index], bitNum);
// uint8_t andMask = 1 << bitNum;
// uint8_t andMask = 1 << bitNum;
// return (directionFlags[index] & andMask) != 0;
// return (directionFlags[index] & andMask) != 0;
}
}
void setPixelDirection( uint16_t i, bool dir) {
void setPixelDirection( uint16_t i, bool dir) {
uint16_t index = i / 8;
uint16_t index = i / 8;
uint8_t bitNum = i & 0x07;
uint8_t bitNum = i & 0x07;
// using Arduino 'bitWrite' function; expanded code below
// using Arduino 'bitWrite' function; expanded code below
bitWrite( directionFlags[index], bitNum, dir);
bitWrite( directionFlags[index], bitNum, dir);
// uint8_t orMask = 1 << bitNum;
// uint8_t orMask = 1 << bitNum;
// uint8_t andMask = 255 - orMask;
// uint8_t andMask = 255 - orMask;
// uint8_t value = directionFlags[index] & andMask;
// uint8_t value = directionFlags[index] & andMask;
// if( dir ) {
// if( dir ) {
// value += orMask;
// value += orMask;
// }
// }
// directionFlags[index] = value;
// directionFlags[index] = value;
}
}
#endif
#endif
已保存差异
原始文本
打开文件
#include "FastLED/FastLED.h" FASTLED_USING_NAMESPACE; #define bitRead(value, bit) (((value) >> (bit)) & 0x01) #define bitSet(value, bit) ((value) |= (1UL << (bit))) #define bitClear(value, bit) ((value) &= ~(1UL << (bit))) #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) #define LED_PIN D0 #define LED_TYPE WS2811 #define COLOR_ORDER GRB #define NUM_LEDS 512 CRGB leds[NUM_LEDS]; #define MICROPHONE 12 #define GAIN_CONTROL 11 void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount); CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter); CRGB makeDarker( const CRGB& color, fract8 howMuchDarker); // Twinkling 'holiday' lights that fade up and down in brightness. // Colors are chosen from a palette; a few palettes are provided. // // The basic operation is that all pixels stay black until they // are 'seeded' with a relatively dim color. The dim colors // are repeatedly brightened until they reach full brightness, then // are darkened repeatedly until they are fully black again. // // A set of 'directionFlags' is used to track whether a given // pixel is presently brightening up or darkening down. // // For illustration purposes, two implementations of directionFlags // are provided: a simple one-byte-per-pixel flag, and a more // complicated, more compact one-BIT-per-pixel flag. // // Darkening colors accurately is relatively easy: scale down the // existing color channel values. Brightening colors is a bit more // error prone, as there's some loss of precision. If your colors // aren't coming our 'right' at full brightness, try increasing the // STARTING_BRIGHTNESS value. // // -Mark Kriegsman, December 2014 #define MASTER_BRIGHTNESS 200 #define STARTING_BRIGHTNESS 64 #define FADE_IN_SPEED 32 #define FADE_OUT_SPEED 60 #define DENSITY 255 void setup() { delay(3000); FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); FastLED.setBrightness(MASTER_BRIGHTNESS); initMicrophone(); // analogReference(DEFAULT); // Serial.begin(57600); } void initMicrophone() { pinMode(GAIN_CONTROL, OUTPUT); digitalWrite(GAIN_CONTROL, LOW); } CRGBPalette16 gPalette; int readMaxSound( uint8_t ms) { uint32_t startms = millis(); while( millis() == startms) {}; startms = millis(); int maxvol = 0; while( (startms + ms) > millis()) { // int mic = analogRead(MICROPHONE)-512; int mic = analogRead(MICROPHONE); if( mic < 0) mic = 0-mic; if( mic > maxvol) maxvol = mic; } return maxvol; } void loop() { chooseColorPalette(); colortwinkles(); // delay(2); // int mic = readMaxSound(10); // mic /= 2; // mic = dim8_raw( mic); // mic = dim8_raw( mic); // // Serial.println(mic); // static int avgmic = 0; // avgmic = ((avgmic * 3) + mic) / 4; // mic = avgmic; // delay(2); // if( mic > 0) { // for( int j = 0; j < mic; j++) { // leds[random16(NUM_LEDS)] = ColorFromPalette( gPalette, random8()); // } // } FastLED.show(); // FastLED.delay(1); } void chooseColorPalette() { uint8_t numberOfPalettes = 9; uint8_t secondsPerPalette = 60; uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes; CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(CRGB::FairyLight); whichPalette = 8; switch( whichPalette) { case 0: // Red, Green, and White gPalette = CRGBPalette16( r,r,r,r, r,r,r,r, g,g,g,g, w,w,w,w ); break; case 1: // Blue and White //gPalette = CRGBPalette16( b,b,b,b, b,b,b,b, w,w,w,w, w,w,w,w ); gPalette = CloudColors_p; // Blues and whites! break; case 2: // Rainbow of colors gPalette = RainbowColors_p; break; case 3: // Incandescent "fairy lights" gPalette = CRGBPalette16( l,l,l,l, l,l,l,l, l,l,l,l, l,l,l,l ); break; case 4: // Snow gPalette = CRGBPalette16( W,W,W,W, w,w,w,w, w,w,w,w, w,w,w,w ); break; case 5: gPalette = LavaColors_p; break; case 6: gPalette = ForestColors_p; break; case 7: gPalette = PartyColors_p; break; case 8: uint8_t h = beatsin8(1); gPalette = CHSVPalette16( CHSV(h,255,255), CHSV(h+32, 255,255)); break; } } enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 }; void colortwinkles() { // Make each pixel brighter or darker, depending on // its 'direction' flag. brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED); // Now consider adding a new random twinkle if( random8() < DENSITY ) { int pos = random16(NUM_LEDS); if( leds[pos] ) pos = random16(NUM_LEDS); if( !leds[pos]) { leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND); setPixelDirection(pos, GETTING_BRIGHTER); } } } void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount) { for( uint16_t i = 0; i < NUM_LEDS; i++) { if( getPixelDirection(i) == GETTING_DARKER) { // This pixel is getting darker leds[i] = makeDarker( leds[i], fadeDownAmount); } else { // This pixel is getting brighter leds[i] = makeBrighter( leds[i], fadeUpAmount); // now check to see if we've maxxed out the brightness if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) { // if so, turn around and start getting darker setPixelDirection(i, GETTING_DARKER); } } } } CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter) { CRGB incrementalColor = color; incrementalColor.nscale8( howMuchBrighter); return color + incrementalColor; } CRGB makeDarker( const CRGB& color, fract8 howMuchDarker) { CRGB newcolor = color; newcolor.nscale8( 255 - howMuchDarker); return newcolor; } // For illustration purposes, there are two separate implementations // provided here for the array of 'directionFlags': // - a simple one, which uses one byte (8 bits) of RAM for each pixel, and // - a compact one, which uses just one BIT of RAM for each pixel. // Set this to 1 or 8 to select which implementation // of directionFlags is used. 1=more compact, 8=simpler. #define BITS_PER_DIRECTION_FLAG 1 #if BITS_PER_DIRECTION_FLAG == 8 // Simple implementation of the directionFlags array, // which takes up one byte (eight bits) per pixel. uint8_t directionFlags[NUM_LEDS]; bool getPixelDirection( uint16_t i) { return directionFlags[i]; } void setPixelDirection( uint16_t i, bool dir) { directionFlags[i] = dir; } #endif #if BITS_PER_DIRECTION_FLAG == 1 // Compact (but more complicated) implementation of // the directionFlags array, using just one BIT of RAM // per pixel. This requires a bunch of bit wrangling, // but conserves precious RAM. The cost is a few // cycles and about 100 bytes of flash program memory. uint8_t directionFlags[ (NUM_LEDS+7) / 8]; bool getPixelDirection( uint16_t i) { uint16_t index = i / 8; uint8_t bitNum = i & 0x07; // using Arduino 'bitRead' function; expanded code below return bitRead( directionFlags[index], bitNum); // uint8_t andMask = 1 << bitNum; // return (directionFlags[index] & andMask) != 0; } void setPixelDirection( uint16_t i, bool dir) { uint16_t index = i / 8; uint8_t bitNum = i & 0x07; // using Arduino 'bitWrite' function; expanded code below bitWrite( directionFlags[index], bitNum, dir); // uint8_t orMask = 1 << bitNum; // uint8_t andMask = 255 - orMask; // uint8_t value = directionFlags[index] & andMask; // if( dir ) { // value += orMask; // } // directionFlags[index] = value; } #endif
更改后文本
打开文件
#include "FastLED/FastLED.h" FASTLED_USING_NAMESPACE; #define bitRead(value, bit) (((value) >> (bit)) & 0x01) #define bitSet(value, bit) ((value) |= (1UL << (bit))) #define bitClear(value, bit) ((value) &= ~(1UL << (bit))) #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) #define LED_PIN D0 #define LED_TYPE WS2811 #define COLOR_ORDER GRB #define NUM_LEDS 512 CRGB leds[NUM_LEDS]; #define MICROPHONE 12 #define GAIN_CONTROL 11 void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount); CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter); CRGB makeDarker( const CRGB& color, fract8 howMuchDarker); // Twinkling 'holiday' lights that fade up and down in brightness. // Colors are chosen from a palette; a few palettes are provided. // // The basic operation is that all pixels stay black until they // are 'seeded' with a relatively dim color. The dim colors // are repeatedly brightened until they reach full brightness, then // are darkened repeatedly until they are fully black again. // // A set of 'directionFlags' is used to track whether a given // pixel is presently brightening up or darkening down. // // For illustration purposes, two implementations of directionFlags // are provided: a simple one-byte-per-pixel flag, and a more // complicated, more compact one-BIT-per-pixel flag. // // Darkening colors accurately is relatively easy: scale down the // existing color channel values. Brightening colors is a bit more // error prone, as there's some loss of precision. If your colors // aren't coming our 'right' at full brightness, try increasing the // STARTING_BRIGHTNESS value. // // -Mark Kriegsman, December 2014 #define MASTER_BRIGHTNESS 200 #define STARTING_BRIGHTNESS 64 #define FADE_IN_SPEED 32 #define FADE_OUT_SPEED 60 #define DENSITY 255 void setup() { delay(3000); FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); FastLED.setBrightness(MASTER_BRIGHTNESS); initMicrophone(); // analogReference(DEFAULT); // Serial.begin(57600); } void initMicrophone() { pinMode(GAIN_CONTROL, OUTPUT); digitalWrite(GAIN_CONTROL, LOW); } CRGBPalette16 gPalette; int readMaxSound( uint8_t ms) { uint32_t startms = millis(); while( millis() == startms) {}; startms = millis(); int maxvol = 0; while( (startms + ms) > millis()) { // int mic = analogRead(MICROPHONE)-512; int mic = analogRead(MICROPHONE); if( mic < 0) mic = 0-mic; if( mic > maxvol) maxvol = mic; } return maxvol; } void loop() { chooseColorPalette(); colortwinkles(); // delay(2); // int mic = readMaxSound(10); // mic /= 2; // mic = dim8_raw( mic); // mic = dim8_raw( mic); // // Serial.println(mic); // static int avgmic = 0; // avgmic = ((avgmic * 3) + mic) / 4; // mic = avgmic; // delay(2); // if( mic > 0) { // for( int j = 0; j < mic; j++) { // leds[random16(NUM_LEDS)] = ColorFromPalette( gPalette, random8()); // } // } FastLED.show(); // FastLED.delay(1); } void chooseColorPalette() { uint8_t numberOfPalettes = 9; uint8_t secondsPerPalette = 60; uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes; CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(CRGB::FairyLight); //whichPalette = 8; switch( whichPalette) { case 0: // Red, Green, and White gPalette = CRGBPalette16( r,r,r,r, r,r,r,r, g,g,g,g, w,w,w,w ); break; case 1: // Blue and White //gPalette = CRGBPalette16( b,b,b,b, b,b,b,b, w,w,w,w, w,w,w,w ); gPalette = CloudColors_p; // Blues and whites! break; case 2: // Rainbow of colors gPalette = RainbowColors_p; break; case 3: // Incandescent "fairy lights" gPalette = CRGBPalette16( l,l,l,l, l,l,l,l, l,l,l,l, l,l,l,l ); break; case 4: // Snow gPalette = CRGBPalette16( W,W,W,W, w,w,w,w, w,w,w,w, w,w,w,w ); break; case 5: gPalette = LavaColors_p; break; case 6: gPalette = ForestColors_p; break; case 7: gPalette = PartyColors_p; break; case 8: uint8_t h = beatsin8(1); gPalette = CHSVPalette16( CHSV(h,255,255), CHSV(h+32, 255,255)); break; } } enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 }; void colortwinkles() { // Make each pixel brighter or darker, depending on // its 'direction' flag. brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED); // Now consider adding a new random twinkle if( random8() < DENSITY ) { int pos = random16(NUM_LEDS); if( leds[pos] ) pos = random16(NUM_LEDS); if( !leds[pos]) { leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND); setPixelDirection(pos, GETTING_BRIGHTER); } } } void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount) { for( uint16_t i = 0; i < NUM_LEDS; i++) { if( getPixelDirection(i) == GETTING_DARKER) { // This pixel is getting darker leds[i] = makeDarker( leds[i], fadeDownAmount); } else { // This pixel is getting brighter leds[i] = makeBrighter( leds[i], fadeUpAmount); // now check to see if we've maxxed out the brightness if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) { // if so, turn around and start getting darker setPixelDirection(i, GETTING_DARKER); } } } } CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter) { CRGB incrementalColor = color; incrementalColor.nscale8( howMuchBrighter); return color + incrementalColor; } CRGB makeDarker( const CRGB& color, fract8 howMuchDarker) { CRGB newcolor = color; newcolor.nscale8( 255 - howMuchDarker); return newcolor; } // For illustration purposes, there are two separate implementations // provided here for the array of 'directionFlags': // - a simple one, which uses one byte (8 bits) of RAM for each pixel, and // - a compact one, which uses just one BIT of RAM for each pixel. // Set this to 1 or 8 to select which implementation // of directionFlags is used. 1=more compact, 8=simpler. #define BITS_PER_DIRECTION_FLAG 1 #if BITS_PER_DIRECTION_FLAG == 8 // Simple implementation of the directionFlags array, // which takes up one byte (eight bits) per pixel. uint8_t directionFlags[NUM_LEDS]; bool getPixelDirection( uint16_t i) { return directionFlags[i]; } void setPixelDirection( uint16_t i, bool dir) { directionFlags[i] = dir; } #endif #if BITS_PER_DIRECTION_FLAG == 1 // Compact (but more complicated) implementation of // the directionFlags array, using just one BIT of RAM // per pixel. This requires a bunch of bit wrangling, // but conserves precious RAM. The cost is a few // cycles and about 100 bytes of flash program memory. uint8_t directionFlags[ (NUM_LEDS+7) / 8]; bool getPixelDirection( uint16_t i) { uint16_t index = i / 8; uint8_t bitNum = i & 0x07; // using Arduino 'bitRead' function; expanded code below return bitRead( directionFlags[index], bitNum); // uint8_t andMask = 1 << bitNum; // return (directionFlags[index] & andMask) != 0; } void setPixelDirection( uint16_t i, bool dir) { uint16_t index = i / 8; uint8_t bitNum = i & 0x07; // using Arduino 'bitWrite' function; expanded code below bitWrite( directionFlags[index], bitNum, dir); // uint8_t orMask = 1 << bitNum; // uint8_t andMask = 255 - orMask; // uint8_t value = directionFlags[index] & andMask; // if( dir ) { // value += orMask; // } // directionFlags[index] = value; } #endif
查找差异