Added OTA-Update (with effect), spiral-effect, larson-scabber. Source indented

This commit is contained in:
starcalc 2017-02-27 17:51:18 +01:00
parent 6538a559a0
commit 920070745d
1 changed files with 485 additions and 269 deletions

View File

@ -1,11 +1,12 @@
#include <Homie.h>
// homie lib from: https://github.com/marvinroger/homie-esp8266/
#include <Adafruit_NeoPixel.h>
#include <ArduinoOTA.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 2 //data pin for ws2812
#define PIN D1 //data pin for ws2812 (pixelprojektor @ ctdo: PIN 2)
#define NUMPIXELS 64
@ -23,6 +24,9 @@ int smoothing=80; //0 to 100. 100=no change (ultrasmooth), 0=no smoothing.
int strength = 50; //how much pixels to apply color to
#define EFFECT_SPIRAL 2
#define EFFECT_RANDOMFADE 3
#define EFFECT_CHASE 4
#define EFFECT_RADAR 5
#define EFFECT_LARSON 6
int fadespeedmax = 5; //1 to 255
int iconCountStart = 0; //for percentage calculation
@ -30,30 +34,32 @@ int iconCountdown=0; //0=off
uint8_t iconchar = 0; //last displayed char
int32_t iconcolor = 0; //last icon color
uint16_t Index; // current step within the pattern
// int Index = 0; // Step for Effect (e.g. chase)
// int state = 0; // Direction for Larson Scanner (spiral)
enum direction { FORWARD, REVERSE }; // Direction
direction Direction; // direction to run the pattern
/* the values in this array are a 8x8 bitmap font for ascii characters */
static uint64_t font[128] = {
/************************************************************************
* font.c
* Copyright (C) Lisa Milne 2014 <lisa@ltmnet.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
font.c
Copyright (C) Lisa Milne 2014 <lisa@ltmnet.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
************************************************************************/
0x7E7E7E7E7E7E0000, /* NUL */
0x7E7E7E7E7E7E0000, /* SOH */
@ -215,6 +221,50 @@ int numToPos(int num){ //convert pixel number to actual 8x8 matrix position
return xyToPos(x, y);
}
int numToSpiralPos(int num) { // convert pixel number to actual 8x8 matrix position in a spiral
int findx = 7;
int findy = 0;
int stepsize = 7; // initial value (0..7)
int stepnumber = 0; // each "step" should be used twice
int count = -1;
int dir = 1; // direction: 0 = incX, 1=incY, 2=decX, 3=decY
if (num < 8) {
return num; // trivial
}
for (int i = 8; i <= num; i++)
{
count++;
if (count == stepsize) {
count = 0;
// Change direction
dir++;
stepnumber++;
if (stepnumber == 2) {
stepsize -= 1;
stepnumber = 0;
}
if (dir == 4) {
dir = 0;
}
}
switch (dir) {
case 0:
findx++;
break;
case 1:
findy++;
break;
case 2:
findx--;
break;
case 3:
findy--;
break;
}
}
return xyToPos(findx, findy);
}
uint32_t wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
@ -400,15 +450,132 @@ void bufferClear()
}
}
void led_spiral()
void led_chase()
{
wheelPos++;
for (int i=0; i < strip.numPixels(); i++) {
//strip.setPixelColor(i,wheel((wheelPos+i*5)%255));
Index += 1;
if (Index > 255) {
Index = 1;
}
// for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, wheel( (i + Index) % 255)); //turn every third pixel on
}
strip.show();
delay(49);
for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
void led_radar()
{
// "Sweep" in cirles...
// line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle)));
}
// Calculate 50% dimmed version of a color (used by led_larson())
uint32_t DimColor(uint32_t color)
{
// Shift R, G and B components one bit to the right
uint32_t Dimcolor = strip.Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
return Dimcolor;
}
// Returns the Red component of a 32-bit color
uint8_t Red(uint32_t color)
{
return (color >> 16) & 0xFF;
}
// Returns the Green component of a 32-bit color
uint8_t Green(uint32_t color)
{
return (color >> 8) & 0xFF;
}
// Returns the Blue component of a 32-bit color
uint8_t Blue(uint32_t color)
{
return color & 0xFF;
}
void Increment()
{
if (Direction == FORWARD)
{
Index++;
if (Index >= (strip.numPixels() - 1) * 2)
{
Index = 0;
}
}
else // Direction == REVERSE
{
--Index;
if (Index <= 0)
{
Index = (strip.numPixels() - 1) * 2 - 1;
}
}
}
void led_larson()
{
int EyeSize = 5;
uint32_t rgb[3] = {0};
wheelPos++;
if (wheelPos >= 256) {
wheelPos = 0;
}
int Color1 = wheel(wheelPos);
for (int i = 0; i < strip.numPixels(); i++)
{
int realpos = numToSpiralPos(i);
if (i == Index) // Scan Pixel to the right
{
strip.setPixelColor(realpos, Color1);
}
else if (i == ((strip.numPixels() - 1) * 2) - Index) // Scan Pixel to the left
{
strip.setPixelColor(realpos, Color1);
}
else // Fading tail
{
strip.setPixelColor(realpos, DimColor(strip.getPixelColor(realpos)));
}
}
strip.show();
Increment();
}
void led_spiral()
{
int every = 4;
wheelPos++;
int qp = Index % every;
Index++;
if (Index >= strip.numPixels()-1) {
Index = 0;
}
int q = Index % every;
for (uint16_t i = 0; i < strip.numPixels(); i = i + every) {
strip.setPixelColor(numToSpiralPos(i + q), wheel( (i + Index * 4) % 255)); //turn every "every" pixel on
}
for (uint16_t i = 0; i < strip.numPixels(); i = i + every) {
strip.setPixelColor(numToSpiralPos(i + qp), 0); //turn every "every" pixel off
}
strip.show();
}
void led_randomfade()
@ -522,6 +689,7 @@ bool effectHandler(const HomieRange& range, const String& value) {
strip.show();
} else if (command.equals("spiral")) {
effect = EFFECT_SPIRAL;
Index = 0;
bufferClear();
showBuffer();
strip.show();
@ -541,11 +709,24 @@ bool effectHandler(const HomieRange& range, const String& value) {
} else if (command.equals("randombuffer")) {
set_randomBuffer(); //set random
showBuffer();
} else if (command.equals("chase")) {
effect = EFFECT_CHASE;
bufferClear();
showBuffer();
strip.show();
} else if (command.equals("radar")) {
effect = EFFECT_RADAR;
Index = 0;
bufferClear();
showBuffer();
strip.show();
} else if (command.equals("larson")) {
effect = EFFECT_LARSON;
Index = 0;
bufferClear();
showBuffer();
strip.show();
}
return true;
}
@ -628,28 +809,56 @@ void setup() {
Serial << "homie setup" << endl;
Homie.setup();
led_fill(strip.Color(0, 0, 0));
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("pixelprojektor");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"ctdo2342");
ArduinoOTA.onStart([]() {
Serial.println("Start");
led_fill(strip.Color(0, 0, 0)); // Clear
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
strip.setPixelColor(numToPos((progress / (total / strip.numPixels()))), strip.Color(255, 255, 255));
strip.show();
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
// led_fill(strip.Color(0, 0, 0));
// Initialer Effekt
//effect = EFFECT_CHASE;
//effect = EFFECT_SPIRAL;
effect = EFFECT_LARSON;
Serial << "Setup finished" << endl;
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
long currentMillis = millis();
if (lastMillis + fpsdelay < currentMillis) {
if (iconCountdown > 0) { //icon effect active
iconCountdown--;
led_icon(iconchar, iconcolor);
} else {
switch (effect) {
case EFFECT_SMOOTH:
led_movingPoint();
@ -661,10 +870,17 @@ void loop() {
case EFFECT_RANDOMFADE:
led_randomfade();
break;
case EFFECT_CHASE:
led_chase();
break;
case EFFECT_RADAR:
led_radar();
break;
case EFFECT_LARSON:
led_larson();
break;
}
}
lastMillis = currentMillis;
}