/*
目前测试下来基本正常,就是色温好像没啥改变,有空再看一下!
看了许多帖子改了一下!
抛砖引玉吧!
按照图片的名字做几个按键和一个选色盘即可
我用的8266-12的板子。
*/
#define BLINKER_WIFI //WIFI库
#define BLINKER_MIOT_LIGHT //小爱控制颜色库
#define BLINKER_MIOT_MULTI_OUTLET //小爱控制亮度库
#include <Blinker.h> //点灯库
char auth[] = "***********";
char ssid[] = "***********";
char pswd[] = "**********";
//WS2812B库
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
BlinkerRGB RGB1("RGBKey");
uint8_t colorR, colorG, colorB, colorW;
uint32_t colorT;
#define PIN 15 //单片机输出口
#define NUMPIXELS 16 //灯珠数量
//#define numPixels NUMPIXELS
#define BRIGHTNESS 128 //亮度初始值0-255之间
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//------------------------------------------------------------
void pixelShow()//声明灯带的颜色和亮度
{
pixels.setBrightness(colorW);
for(int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, colorR, colorG, colorB);
}
pixels.show();
}
//-------------------------------------------------------------
void miotPowerState(const String & state)//小爱开关状态
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
//digitalWrite(LED_BUILTIN, HIGH);
yuedumoshi();
//pixels.setBrightness(colorW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
//digitalWrite(LED_BUILTIN, LOW);
yuedumoshi();
pixels.setBrightness(0);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
//-------------------------------------------------------------手动选色盘
void RGB1_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value) {
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
pixels.setBrightness(bright_value);
for(int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, r_value, g_value, b_value);
}
pixels.show();
}
//---------------------------------------------------------
uint32_t Wheel(byte WheelPos){
if(WheelPos < 85){
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if(WheelPos < 170){
WheelPos -= 85;
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else{
WheelPos -= 170;return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
//-------------------------------------------------小爱控制颜色
void miotColor(int32_t color)
{
BLINKER_LOG("need set color: ", color);
colorR = color >> 16 & 0xFF;
colorG = color >> 8 & 0xFF;
colorB = color & 0xFF;
BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
pixels.clear();
pixels.setBrightness(colorW);
for(int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, colorR, colorG, colorB);
}
pixels.show();
BlinkerMIOT.color(color);
BlinkerMIOT.print();
}
//----------------------------------小爱控制亮度
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
colorW = bright.toInt();
BLINKER_LOG("now set brightness: ", colorW);
pixelShow();
BlinkerMIOT.brightness(colorW);
BlinkerMIOT.print();
}
//-----------------------------------小爱色温
void miotColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);
colorT = colorTemp;
BlinkerMIOT.colorTemp(colorTemp);
BlinkerMIOT.print();
}
//---------------------------------小爱自定义模式
void miotMode(uint8_t mode)
{
BLINKER_LOG("need set mode: ", mode);
if (mode == BLINKER_CMD_MIOT_DAY) {//日光
// Your mode function
for(int i = 0; i <pixels.numPixels(); i++){
pixels.setPixelColor(i, 0xffcc33);
pixels.setBrightness(200);
}
}
else if (mode == BLINKER_CMD_MIOT_NIGHT) {//月光
// Your mode function
pulseWhite(10);//呼吸灯
}
else if (mode == BLINKER_CMD_MIOT_COLOR) {//彩光
// Your mode function
whiteOverRainbow(20,75,5);//调用霹雳游侠
}
else if (mode == BLINKER_CMD_MIOT_WARMTH) {//温馨
// Your mode function
rainbow(10);
}
else if (mode == BLINKER_CMD_MIOT_TV) {//电视
// Your mode function
// rainbow(10);
for(int i = 0; i <pixels.numPixels(); i++){
pixels.setPixelColor(i, Wheel(((i * 256 /pixels.numPixels()) + 20) & 255));
}
pixels.show();
pixels.setBrightness(BRIGHTNESS);
}
else if (mode == BLINKER_CMD_MIOT_READING) {//阅读
// Your mode function
yuedumoshi();
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {//电脑
// Your mode function
for(int i = 0; i <pixels.numPixels(); i++){
pixels.setPixelColor(i, Wheel(((i * 256 /pixels.numPixels()) + 20) & 255));
}
pixels.show();
pixels.setBrightness(BRIGHTNESS);
}
uint8_t wsMode = mode;
BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
}
//------------------------------小爱外接接口数据函数
/*void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_PM25_NUMBER :
BLINKER_LOG("MIOT Query PM25");
BlinkerMIOT.pm25(20);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_HUMI_NUMBER :
BLINKER_LOG("MIOT Query HUMI");
BlinkerMIOT.humi(20);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_TEMP_NUMBER :
BLINKER_LOG("MIOT Query TEMP");
BlinkerMIOT.temp(20);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_TIME_NUMBER :
BLINKER_LOG("MIOT Query Time");
BlinkerMIOT.time(millis());
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.temp(20);
BlinkerMIOT.humi(20);
BlinkerMIOT.pm25(20);
BlinkerMIOT.co2(20);
BlinkerMIOT.print();
break;
}
}*/
//--------------------------------按键返回事件
BlinkerButton Button1("donggan");
void Button1_callback(const String & state) {
if (state == "on") {
rainbow(10);
for(int i = 0; i <pixels.numPixels(); i++){
pixels.setPixelColor(i, Wheel(((i * 256 /pixels.numPixels()) + 20) & 255));
}
pixels.show();
pixels.setBrightness(BRIGHTNESS);
}
}
BlinkerButton Button2("yuedu");
void Button2_callback(const String & state) {
if (state == "on") {
for(int i = 0; i <pixels.numPixels(); i++){
pixels.setPixelColor(i, 0xffcc33);
pixels.setBrightness(200);
}
}
if (state == "press") {
pulseWhite(10);
//pixels.setBrightness(0);
}
if (state == "pressup") {
//pixels.setBrightness(0);
pulseWhite(10);
}
}
BlinkerButton Button3("off");
void Button3_callback(const String & state) {
if (state == "on") {
pixels.setBrightness(0);
}
}
BlinkerButton Button4("youxia");
void Button4_callback(const String & state) {
if (state == "on") {
whiteOverRainbow(20,75,5);//调用霹雳游侠
}
/* if (state == "pressup") {
pixels.setBrightness(0);
}*/
if (state == "press") {
pixels.setBrightness(0);
}
}
//------------------------------设置初始化
void setup(){
Blinker.begin(auth,ssid,pswd);
colorW = 168;
pixels.begin();
//pixels.show();
RGB1.attach(RGB1_callback);
Button1.attach(Button1_callback);
Button2.attach(Button2_callback);
Button3.attach(Button3_callback);
Button4.attach(Button4_callback);
BlinkerMIOT.attachColor(miotColor);//小爱定义色彩回调函数
BlinkerMIOT.attachBrightness(miotBright);//小爱亮度控制回调函数
BlinkerMIOT.attachPowerState(miotPowerState);//小爱开关状态回调函数
BlinkerMIOT.attachColorTemperature(miotColoTemp);//小爱色温回调函数
BlinkerMIOT.attachMode(miotMode);//小爱自定义功能回调函数
}
//------------------------循环执行
void loop(){
pixels.show();
Blinker.run();
}
//-------------------------------呼吸效果
void pulseWhite(uint8_t wait) {
for(int j = 0; j < 10 ; j++){
for(int j = 0; j < 256 ; j++){
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i,pixels.Color(j,j,j ) );
}
delay(wait);
pixels.show();
}
for(int j = 255; j >= 0 ; j--){
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i,pixels.Color(j,j,j ) );
}
delay(wait);
pixels.show();
}
}
}
//-----------------------------------极光模式
void rainbow(uint8_t wait){
/* uint16_t i, j;
for(j=0; j<256; j++){
for(i=0; i<pixels.numPixels(); i++){
pixels.setPixelColor(i, Wheel((i+j) & 255));
}
pixels.show();
delay(wait);
}*/
uint16_t i, j,k;//k为循环多少次,i为第几个灯珠,j为每个灯珠的色彩亮度值
for(k=0;k<8;k++){
for(j=0; j<256* k; j++) { // 5 cycles of all colors on wheel
for(i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel(((i * 256 /pixels.numPixels()) + j) & 255));
}
pixels.show();
delay(wait);
}
}
}
//-------------------------------------------霹雳游侠
void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) {
if(whiteLength >=pixels.numPixels()) whiteLength =pixels.numPixels() - 1;
int head = whiteLength - 1;
int tail = 0;
uint16_t loops = 28;//循环次数
int loopNum = 0;//点亮的灯珠数
static unsigned long lastTime = 0;
while(true){
for(int j=0; j<256; j++) {
for(uint16_t i=0; i<pixels.numPixels(); i++) {
if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){
pixels.setPixelColor(i,pixels.Color(0,0,0, 255 ) );
//pixels.setPixelColor(i, Wheel(((i * 256 /pixels.numPixels()) + j) & 255));
}
else{
pixels.setPixelColor(i, Wheel(((i * 256 /pixels.numPixels()) + j) & 255));
}
}
if(millis() - lastTime > whiteSpeed) {
head++;
tail++;
if(head ==pixels.numPixels()){
loopNum++;
}
lastTime = millis();
}
if(loopNum == loops) return;
head%=pixels.numPixels();
tail%=pixels.numPixels();
pixels.show();
delay(wait);
}
}
}
//-------------------------------------------------阅读模式
void yuedumoshi()
{
for(int i = 0; i <pixels.numPixels(); i++){
pixels.setPixelColor(i, 0xffcc33);
pixels.setBrightness(200);
}
}
|