Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
sblantipodi committed Apr 16, 2020
1 parent 374a3cf commit d077c54
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 298 deletions.
189 changes: 189 additions & 0 deletions include/Ambilight.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <FastLED.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Secrets.h>
#include <Version.h>

/****************** WIFI and MQTT INFO ******************/
// MQTT server port
const int mqtt_port = 1883;
// DNS address for the microcontroller:
IPAddress mydns(192, 168, 1, 1);
// GATEWAY address for the microcontroller:
IPAddress mygateway(192, 168, 1, 1);
WiFiClient espClient;
PubSubClient client(espClient);

/****************** OTA ******************/
// SENSORNAME will be used as device network name
#define SENSORNAME "ambilight"
// Port for the OTA firmware uplaod
int OTAport = 8266;
// Static IP address for the microcontroller:
IPAddress arduinoip(192, 168, 1, 52);

/************* MQTT TOPICS (change these topics as you wish) **************************/
const char* light_state_topic = "lights/pcambilight";
const char* light_set_topic = "lights/pcambiligh/set";
const char* smartostat_climate_state_topic = "stat/smartostat/CLIMATE";
const char* cmnd_ambi_reboot = "cmnd/ambilight/reboot";
const char* stat_ambi_reboot = "stat/ambilight/reboot";

const char* on_cmd = "ON";
const char* off_cmd = "OFF";
const char* effect = "solid";
String effectString = "solid";
String oldeffectString = "solid";

/****************************************FOR JSON***************************************/
const int BUFFER_SIZE = JSON_OBJECT_SIZE(20);

/********************************* AmbiLight *************************************/
#define max_bright 255 // maximum brightness (0 - 255)
#define min_bright 50 // the minimum brightness (0 - 255)
#define bright_constant 500 // the gain constant from external light (0 - 1023)
// than the LESS constant, the "sharper" the brightness will be added
#define coef 0.9 // the filter coefficient (0.0 - 1.0), the more - the more slowly the brightness changes

int new_bright, new_bright_f;
unsigned long bright_timer, off_timer;

#define serialRate 500000
uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
bool led_state = true;

/*********************************** FastLED Defintions ********************************/
#define NUM_LEDS 95 //95
#define DATA_PIN 5 // Wemos D1 Mini Lite PIN D5
//#define CLOCK_PIN 5
#define CHIPSET WS2812
#define COLOR_ORDER GRB

byte realRed = 0;
byte realGreen = 0;
byte realBlue = 0;

byte red = 255;
byte green = 255;
byte blue = 255;
byte brightness = 255;

/******************************** GLOBALS for fade/flash *******************************/
bool stateOn = false;
bool startFade = false;
bool onbeforeflash = false;
unsigned long lastLoop = 0;
int transitionTime = 0;
int effectSpeed = 0;
bool inFade = false;
int loopCount = 0;
int stepR, stepG, stepB;
int redVal, grnVal, bluVal;

bool flash = false;
bool startFlash = false;
int flashLength = 0;
unsigned long flashStartTime = 0;
byte flashRed = red;
byte flashGreen = green;
byte flashBlue = blue;
byte flashBrightness = brightness;

/********************************** GLOBALS for EFFECTS ******************************/
//RAINBOW
uint8_t thishue = 0; // Starting hue value.
uint8_t deltahue = 10;

//CANDYCANE
CRGBPalette16 currentPalettestriped; //for Candy Cane
CRGBPalette16 gPal; //for fire

//NOISE
static uint16_t dist; // A random number for our noise generator.
uint16_t scale = 30; // Wouldn't recommend changing this on the fly, or the animation will be really blocky.
uint8_t maxChanges = 48; // Value for blending between palettes.
CRGBPalette16 targetPalette(OceanColors_p);
CRGBPalette16 currentPalette(CRGB::Black);

//TWINKLE
#define DENSITY 80
int twinklecounter = 0;

//RIPPLE
uint8_t colour; // Ripple colour is randomized.
int center = 0; // Center of the current ripple.
int step = -1; // -1 is the initializing step.
uint8_t myfade = 255; // Starting brightness.
#define maxsteps 16 // Case statement wouldn't allow a variable.
uint8_t bgcol = 0; // Background colour rotates.
int thisdelay = 20; // Standard delay value.

//DOTS
uint8_t count = 0; // Count up to 255 and then reverts to 0
uint8_t fadeval = 224; // Trail behind the LED's. Lower => faster fade.
uint8_t bpm = 30;

//LIGHTNING
uint8_t frequency = 50; // controls the interval between strikes
uint8_t flashes = 8; //the upper limit of flashes per strike
unsigned int dimmer = 1;
uint8_t ledstart; // Starting location of a flash
uint8_t ledlen;
int lightningcounter = 0;

//FUNKBOX
int idex = 0; //-LED INDEX (0 to NUM_LEDS-1
int TOP_INDEX = int(NUM_LEDS / 2);
int thissat = 255; //-FX LOOPS DELAY VAR
uint8_t thishuepolice = 0;
int antipodal_index(int i) {
int iN = i + TOP_INDEX;
if (i >= TOP_INDEX) {
iN = ( i + TOP_INDEX ) % NUM_LEDS;
}
return iN;
}

//FIRE
#define COOLING 55
#define SPARKING 120
bool gReverseDirection = false;

//BPM
uint8_t gHue = 0;
CRGB leds[NUM_LEDS];

// LED_BUILTIN vars
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 200; // interval at which to blink (milliseconds)
bool ledTriggered = false;
const int blinkTimes = 6; // 6 equals to 3 blink on and 3 off
int blinkCounter = 0;

String timedate = "OFF";

/********************************** FUNCTION DECLARATION (NEEDED BY PLATFORMIO WHILE COMPILING CPP FILES) *****************************************/
void setup_wifi();
void sendState();
void setupStripedPalette( CRGB A, CRGB AB, CRGB B, CRGB BA);
void callback(char* topic, byte* payload, unsigned int length);
bool processSmartostatClimateJson(char* message);
bool processJson(char* message);
void nonBlokingBlink();
void mqttReconnect();
bool processAmbilightRebootCmnd(char* message);
void setColor(int inR, int inG, int inB);
void checkConnection();
void setupStripedPalette( CRGB A, CRGB AB, CRGB B, CRGB BA);
void fadeall();
void Fire2012WithPalette();
void addGlitter(fract8 chanceOfGlitter);
void addGlitterColor( fract8 chanceOfGlitter, int red, int green, int blue);
void showleds();
void temp2rgb(unsigned int kelvin);
int calculateStep(int prevValue, int endValue);
int calculateVal(int step, int val, int i);
4 changes: 2 additions & 2 deletions include/Version.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// AUTO GENERATED FILE FROM version_increment.py, DO NOT EDIT THIS FILE
#ifndef VERSION
#define VERSION "2.0.35"
#define VERSION "2.0.36"
#endif
#ifndef BUILD_TIMESTAMP
#define BUILD_TIMESTAMP "2020-04-13 17:13:11.522791"
#define BUILD_TIMESTAMP "2020-04-16 18:59:16.371546"
#endif

Loading

0 comments on commit d077c54

Please sign in to comment.