Skip to content

Commit

Permalink
philipsWho mod
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyDonahue committed May 27, 2024
1 parent 2f6f0d2 commit dc97fbc
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
142 changes: 142 additions & 0 deletions usermods/philipswho/usermod_v2_philipswho.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#pragma once

#include "wled.h"

/*
* PhilipsWho Usermod class that reads information
* from WLED and lights LED's as a result.
* LED 1: wifi connection status
* LED 2: WLED activity status
* Contact: [email protected]
*/
class PhilipsWho : public Usermod {
private:
// strings to reduce flash memory usage (used more than twice)
static const char PhilipsWho::_name[] PROGMEM = "PhilipsWho";
static const char PhilipsWho::_enabled[] PROGMEM = "enabled";

public:
//How frequently to check for wifi
const unsigned long poll_frequency_wifi = 5000; // every 5 second

//Time when wifi was last checked
unsigned long last_checked_wifi = 0;

//How long the Activity LED remains on when activity detected
unsigned long activity_led_on_period = 1000;

//Default Pin Values
int activity_led_pin = 13;
int wifi_status_led_pin = 14;

//Enabled Bool
bool enabled = false;

//The current time since ESP32 Started (in ms)
unsigned long current_ms = 0L;

//Time (in ms) when onStateChange was called
unsigned long state_change_fired_ms = 0;

/*
* Check if wifi is connected
* Light the LED appropriately
*/
void check_wifi() {
if (WLED_CONNECTED) {
digitalWrite(wifi_status_led_pin, HIGH);
}else{
digitalWrite(wifi_status_led_pin, LOW);
}
}

/**
* Enable/Disable the usermod
*/
void enable(bool enable) { enabled = enable; }

/**
* Get usermod enabled/disabled state
*/
bool isEnabled() { return enabled; }

/*
* Callback function called when any change happens in WLED
* This includes light and configuration changes
*/
void onStateChange(uint8_t mode){
digitalWrite(activity_led_pin, HIGH);
state_change_fired_ms = millis();
}

/*
* Add settings to the configuration file
*/
void addToConfig(JsonObject& root)
{
JsonObject top = root.createNestedObject(FPSTR(_name));
top["activity_led_pin"] = activity_led_pin;
top["activity_led_on_period"] = activity_led_on_period;
top["wifi_status_led_pin"] = wifi_status_led_pin;
top["enabled"] = enabled;
}

/*
* Read Settings from Configuration File
*/
bool readFromConfig(JsonObject& root)
{
JsonObject top = root[FPSTR(_name)];
bool configComplete = !top.isNull();
configComplete &= getJsonValue(top["activity_led_pin"], activity_led_pin, 13);
configComplete &= getJsonValue(top["activity_led_on_period"], activity_led_on_period, 1000);
configComplete &= getJsonValue(top["wifi_status_led_pin"], wifi_status_led_pin, 14);
configComplete &= getJsonValue(top["enabled"], enabled, false);

// Turn off LED's if disabling
if(!enabled){
digitalWrite(wifi_status_led_pin, LOW);
digitalWrite(activity_led_pin, LOW);
}

return configComplete;
}

/*
* Initial Setup - Called once on boot
*/
void setup() {
Serial.println("PhilipsWho - Setup");
pinMode(wifi_status_led_pin, OUTPUT);
pinMode(activity_led_pin, OUTPUT);
digitalWrite(wifi_status_led_pin, LOW);
digitalWrite(activity_led_pin, LOW);
}

/*
* Main Loop - Called continuously while ESP32 is running
*/
void loop() {
//If disabled, do nothing
if (!enabled) return;

//Get the current time in MS
current_ms = millis();

//Do we need to check for wifi yet?
if(current_ms - last_checked_wifi >= poll_frequency_wifi){
last_checked_wifi = current_ms;
check_wifi();
}

if(current_ms - state_change_fired_ms >= activity_led_on_period){
digitalWrite(activity_led_pin, LOW);
}

//Check for millis overflow - reset back to 0
if(current_ms < last_checked_wifi){
last_checked_wifi = 0;
state_change_fired_ms = 0;
}
}
};
11 changes: 10 additions & 1 deletion wled00/usermods_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,19 @@
#ifdef USERMOD_STAIRCASE_WIPE
#include "../usermods/stairway_wipe_basic/stairway-wipe-usermod-v2.h"
#endif

#ifdef USERMOD_PHILIPSWHO
#include "../usermods/philipswho/usermod_v2_philipswho.h"
#endif

void registerUsermods()
{
/*
* Add your usermod class name here
* || || ||
* \/ \/ \/
*/
//usermods.add(new MyExampleUsermod());

#ifdef USERMOD_BATTERY
usermods.add(new UsermodBattery());
#endif
Expand Down Expand Up @@ -387,4 +392,8 @@ void registerUsermods()
#ifdef USERMOD_STAIRCASE_WIPE
usermods.add(new StairwayWipeUsermod());
#endif

#ifdef USERMOD_PHILIPSWHO
usermods.add(new PhilipsWho());
#endif
}

0 comments on commit dc97fbc

Please sign in to comment.