智能桌面改造

发布时间: 2022-12-06

前言

一直对改造桌面有想法,于是采购了一片8266正式开始改造。
1.为显示器加了一圈氛围灯,改写代码可实现流光溢彩的效果。
2.电脑实现远程启动,小爱同学启动(局域网网卡唤醒)。

代码

//无线局域网音乐律动 远程启动电脑
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#define BLINKER_MIOT_OUTLET
//#define BLINKER_ESP_SMARTCONFIG
#define BLINKER_WITHOUT_SSL
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <FastLED.h>
#include <Blinker.h>
char auth[] = "";
char ssid[] = "";
char pswd[] = "";

WiFiUDP Udp;
bool oState = false;

unsigned int localUdpPort = 6001;
char packetBuffer[256];
unsigned long triger_time;
BlinkerButton Button1("ButtonKey1");
BlinkerButton Button2("ButtonKey2");
BlinkerButton Button3("ButtonKey3");
BlinkerButton Button4("ButtonKey4");
BlinkerButton Button5("ButtonKey5");
BlinkerSlider Slider1("SliderKey");
BlinkerSlider Slider2("SliderKey2");
BlinkerRGB RGBWS2812("RGBKey");
int openState = 2;
unsigned int nowState = 2;
int freq_flash = 15;
uint8_t colorR = 250, colorG = 0, colorB = 0, colorW = 250;
bool wsState;
uint8_t wsMode = BLINKER_CMD_MIOT_DAY;
int brt_set = 100;

#include <Adafruit_NeoPixel.h>
#define PIN 5
#define NUMPIXELS 60
int pre_si = 0;
int drop_dot = NUMPIXELS - 1;
unsigned long drop_time;
unsigned long lift_time;
unsigned long up_time;
unsigned long down_time;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
CRGBArray<NUMPIXELS> leds;
void pixelShow()
{

pixels.setBrightness(colorW);

for (int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, colorR, colorG, colorB);

}
pixels.show();

}
void slider1_callback(int32_t value)
{

Blinker.vibrate();
BLINKER_LOG("get slider value: ", value);
freq_flash = value;
}
void slider2_callback(int32_t value)
{

Blinker.vibrate();
BLINKER_LOG("get slider value: ", value);


}
void ws2812_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{
button_clear();
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
colorR = r_value;
colorG = g_value;
colorB = b_value;
colorW = bright_value;
openState = 6;
//Text1.print("灯光模式:" , "单颜色");
}
void button_clear()
{
Button1.print("off");
Button2.print("off");
Button3.print("off");
Button4.print("off");
Button5.print("off");
Button1.color("#008000");
Button2.color("#008000");
Button3.color("#008000");
Button4.color("#008000");
Button5.color("#008000");
}


void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
BLINKER_LOG("日光模式开启");
button_clear();
Button1.color("#DC143C");
Button1.print("on");
openState = 1;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("日光模式关闭!");
button_clear();
Button1.color("#008000");
openState = 0;
}
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
BLINKER_LOG("月光模式开启");
button_clear();
Button2.print("on");
Button2.color("#DC143C");
openState = 2;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("月光模式关闭!");
button_clear();
openState = 0;
}
}
void button3_callback(const String & state)
{

BLINKER_LOG("get button state: ", state);

if (state == BLINKER_CMD_ON) {
BLINKER_LOG("温馨模式开启");
button_clear();
Button3.print("on");
Button3.color("#DC143C");
openState = 3;
brt_set = colorW;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("温馨模式关闭!");
button_clear();
openState = 0;
}

}
void button4_callback(const String & state)
{

BLINKER_LOG("get button state: ", state);

if (state == BLINKER_CMD_ON) {
BLINKER_LOG("电脑模式开启");

BlinkerMIOT.print();
pcawaking();

openState = 4;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("电脑模式关闭!");

BlinkerMIOT.print();
openState = 0;
}

}
void button5_callback(const String & state)
{

BLINKER_LOG("get button state: ", state);

if (state == BLINKER_CMD_ON) {
BLINKER_LOG("电视模式开启");

button_clear();
Button5.print("on");
Button5.color("#DC143C");
openState = 5;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("电视模式关闭!");

button_clear();
openState = 0;

}

}

void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);

Blinker.vibrate();

uint32_t BlinkerTime = millis();

Blinker.print("millis", BlinkerTime);
}
uint32_t getColor()
{

uint32_t color = colorR << 16 | colorG << 8 | colorB;

return color;
}
//小爱同学
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);

if (state == BLINKER_CMD_ON) {

BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
wsState = true;
if (colorW == 0) colorW = 255;
openState = 6;
}
else if (state == BLINKER_CMD_OFF) {
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
wsState = false;
openState = 0;
}
}

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);
openState = 6;
BlinkerMIOT.color(color);
BlinkerMIOT.print();
}

void miotMode(uint8_t mode)
{
BLINKER_LOG("need set mode: ", mode);

if (mode == BLINKER_CMD_MIOT_DAY) {
button_clear();
Button1.print("on");
Button1.color("#DC143C");
openState = 1;

}
else if (mode == BLINKER_CMD_MIOT_NIGHT) {
button_clear();
Button2.print("on");
Button2.color("#DC143C");
openState = 2;
}
else if (mode == BLINKER_CMD_MIOT_COLOR) {
button_clear();
openState = 6;

}
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
button_clear();
Button3.print("on");
Button3.color("#DC143C");
openState = 3;
}
else if (mode == BLINKER_CMD_MIOT_TV) {
button_clear();
Button5.print("on");
Button5.color("#DC143C");
openState = 5;
}
else if (mode == BLINKER_CMD_MIOT_READING) {
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
pcawaking();
button_clear();
Button1.print("on");
Button1.color("#DC143C");
openState = 4;
}

wsMode = mode;

BlinkerMIOT.mode(mode);
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();
openState = 6;
}

void miotColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);

BlinkerMIOT.colorTemp(colorTemp);
BlinkerMIOT.print();
}

void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);

switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.color(0);
BlinkerMIOT.mode(0);
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_COLOR_NUMBER :
BLINKER_LOG("MIOT Query Color");
BlinkerMIOT.color(0);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_MODE_NUMBER :
BLINKER_LOG("MIOT Query Mode");
BlinkerMIOT.mode(0);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_COLORTEMP_NUMBER :
BLINKER_LOG("MIOT Query ColorTemperature");
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER :
BLINKER_LOG("MIOT Query Brightness");
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.color(0);
BlinkerMIOT.mode(0);
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
}
}
// 开机
void pcawaking()
{
int i=0;
char mac[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
char pac[102];
char * Address = "192.168.0.255";
int Port = 3333;
for(i=0;i<6;i++)
{
pac[i]=0xFF;
}
for(i=6;i<102;i+=6)
{
memcpy(pac+i,mac,6);
}
Udp.beginPacket(Address, Port);
Udp.write((byte*)pac, 102);
Udp.endPacket();
}
bool active()
{
Blinker.run();
if (openState != nowState)
{
nowState = openState;
Serial.println("Operating in new mode " + String(openState));
return true;
}
if (openState == nowState)
{
//Serial.println("Operating in same mode " + String(openState));
return false;

}
}

uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
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));
}
if (active())
{
break;
}
pixels.show();
delay(freq_flash);
}
}

void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, c);
pixels.show();
delay(wait);
if (active())
{
break;
}
}
}

void fadeall() {
for (int i = 0; i < NUMPIXELS; i++) {
leds[i].nscale8(250);
}
}


void cylon() {
static uint8_t hue = 0;


for (int i = 0; i < NUMPIXELS; i++) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeall();
if (active())
{
break;
}
delay(freq_flash);
}
for (int i = (NUMPIXELS) - 1; i >= 0; i--) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeall();
if (active())
{
break;
}
delay(freq_flash);
}
}
//局域网
void musicWrite2()
{
int si;
int delta = int(255 / NUMPIXELS );
String tmpString;
int packetSize = Udp.parsePacket();
if (packetSize) {

int len = Udp.read(packetBuffer, 256);
if (len > 0) {
packetBuffer[len] = 0;

si = uint8_t(packetBuffer[0]);
}
//si = tmpString.toInt();
Serial.println(si);
if (si > drop_dot and si < NUMPIXELS) {

if ((si + 3) >= NUMPIXELS)
{
drop_dot = NUMPIXELS - 1;
}
else {
drop_dot = si + 3;
}

}
if (si > pre_si)
{

for (int j = pre_si - 1; j < si + 1; j++)
{

while ((millis() - up_time ) < int(60 / (si - pre_si) + 2))
{
if ((drop_dot > si + 3) && (millis() - drop_time > 15))
{
pixels.setPixelColor(drop_dot + 1, pixels.Color(0, 0, 0));
pixels.setPixelColor(drop_dot, pixels.Color(colorR, colorG, colorB));//adjust color with APP color palette;
pixels.show();
drop_time = millis();
drop_dot--;
}
int j_fade = int(delta * j);
pixels.setPixelColor(j, Wheel(j_fade & 255)); //fade from BLUE-RED
pixels.show();
}
up_time = millis();

}
}
if (si < pre_si)
{

for (int k = pre_si + 1; k > si - 1; k--)
{

while ((millis() - down_time) < int(60 / (pre_si - si) + 2))
{

if ((drop_dot > si + 3) && (millis() - drop_time > 15))
{
pixels.setPixelColor(drop_dot + 1, pixels.Color(0, 0, 0));
pixels.setPixelColor(drop_dot, pixels.Color(colorR, colorG, colorB));//adjust color with APP color palette;
pixels.show();
drop_time = millis();
drop_dot--;
}


pixels.setPixelColor(k, pixels.Color(0, 0, 0));//
pixels.show();
}

down_time = millis();
}

}
pre_si = si;
Udp.stop();
Udp.begin(localUdpPort);

}


}
void pride()
{
static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;

uint8_t sat8 = beatsin88( 87, 220, 250);
uint8_t brightdepth = beatsin88( 341, 96, 224);
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);

uint16_t hue16 = sHue16;//gHue * 256;
uint16_t hueinc16 = beatsin88(113, 1, 3000);

uint16_t ms = millis();
uint16_t deltams = ms - sLastMillis ;
sLastMillis = ms;
sPseudotime += deltams * msmultiplier;
sHue16 += deltams * beatsin88( 400, 5, 9);
uint16_t brightnesstheta16 = sPseudotime;

for ( uint16_t i = 0 ; i < NUMPIXELS; i++) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;

brightnesstheta16 += brightnessthetainc16;
uint16_t b16 = sin16( brightnesstheta16 ) + 32768;

uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);

CRGB newcolor = CHSV( hue8, sat8, bri8);

uint16_t pixelnumber = i;
pixelnumber = (NUMPIXELS - 1) - pixelnumber;

nblend( leds[pixelnumber], newcolor, 64);
}
FastLED.show();
}


void breath()
{
wsState = true;

for (int brt = 1; brt < brt_set + 1; brt++) {
colorW = brt;
pixelShow();
if (active())
{
break;
}
delay(freq_flash);
}
for (int brt = brt_set; brt > 0 ; brt--) {
colorW = brt;
pixelShow();
if (active())
{
break;
}
delay(freq_flash);
}

}

void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
LEDS.addLeds<WS2812, PIN, RGB>(leds, NUMPIXELS);
Blinker.begin(auth, ssid, pswd);
pixels.begin();
Udp.begin(localUdpPort);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachColor(miotColor);
BlinkerMIOT.attachMode(miotMode);
BlinkerMIOT.attachBrightness(miotBright);
BlinkerMIOT.attachColorTemperature(miotColoTemp);
BlinkerMIOT.attachQuery(miotQuery);
pinMode(14, OUTPUT);
digitalWrite(14, HIGH);
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
Slider1.attach(slider1_callback);
Slider2.attach(slider2_callback);
RGBWS2812.attach(ws2812_callback);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
pixels.setBrightness(250);
button_clear();

}

void mode_1()
{
if (colorW == 0)
{
colorW = 150;
}
cylon();
}

请在下方留下您的评论.加入TG吹水群