Created
October 31, 2021 18:59
-
-
Save grantstephens/b16a3e88423135df2b93e54890397b2d to your computer and use it in GitHub Desktop.
Starlight esphome code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "esphome.h" | |
#include "Wire.h" | |
#include "Adafruit_GFX.h" | |
#include "Adafruit_IS31FL3731.h" | |
Adafruit_IS31FL3731 ledmatrix = Adafruit_IS31FL3731(); | |
struct led { | |
uint8_t x; | |
uint8_t y; | |
uint8_t brightness; | |
}; | |
led all[] = { | |
led {x: 0,y: 0,brightness: 0},led {x: 0,y: 1,brightness: 1},led {x: 0,y: 2,brightness: 1},led {x: 0,y: 3,brightness: 1},led {x: 0,y: 4,brightness: 1},led {x: 0,y: 5,brightness: 1},led {x: 0,y: 6,brightness: 1},led {x: 0,y: 7,brightness: 1},led {x: 0,y: 8,brightness: 1},led {x: 1,y: 0,brightness: 1}, | |
led {x: 1,y: 1,brightness: 1},led {x: 1,y: 2,brightness: 1},led {x: 1,y: 3,brightness: 1},led {x: 1,y: 4,brightness: 1},led {x: 1,y: 5,brightness: 1},led {x: 1,y: 6,brightness: 1},led {x: 1,y: 7,brightness: 1},led {x: 1,y: 8,brightness: 1},led {x: 2,y: 0,brightness: 1}, | |
led {x: 2,y: 1,brightness: 1},led {x: 2,y: 2,brightness: 1},led {x: 2,y: 3,brightness: 1},led {x: 2,y: 4,brightness: 1},led {x: 2,y: 5,brightness: 1},led {x: 2,y: 6,brightness: 1},led {x: 2,y: 7,brightness: 1},led {x: 2,y: 8,brightness: 1},led {x: 3,y: 0,brightness: 1}, | |
led {x: 3,y: 1,brightness: 1},led {x: 3,y: 2,brightness: 1},led {x: 3,y: 3,brightness: 1},led {x: 3,y: 4,brightness: 1},led {x: 3,y: 5,brightness: 1},led {x: 3,y: 6,brightness: 1},led {x: 3,y: 7,brightness: 1},led {x: 3,y: 8,brightness: 1},led {x: 4,y: 0,brightness: 1}, | |
led {x: 4,y: 1,brightness: 1},led {x: 4,y: 2,brightness: 1},led {x: 4,y: 3,brightness: 1},led {x: 4,y: 4,brightness: 1},led {x: 4,y: 5,brightness: 1},led {x: 4,y: 6,brightness: 1},led {x: 4,y: 7,brightness: 1},led {x: 4,y: 8,brightness: 1},led {x: 5,y: 0,brightness: 1}, | |
led {x: 5,y: 1,brightness: 1},led {x: 5,y: 2,brightness: 1},led {x: 5,y: 3,brightness: 1},led {x: 5,y: 4,brightness: 1},led {x: 5,y: 5,brightness: 1},led {x: 5,y: 6,brightness: 1},led {x: 5,y: 7,brightness: 1},led {x: 5,y: 8,brightness: 1},led {x: 6,y: 0,brightness: 1}, | |
led {x: 6,y: 1,brightness: 1},led {x: 6,y: 2,brightness: 1},led {x: 6,y: 3,brightness: 1},led {x: 6,y: 4,brightness: 1},led {x: 6,y: 5,brightness: 1},led {x: 6,y: 6,brightness: 1},led {x: 6,y: 7,brightness: 1},led {x: 6,y: 8,brightness: 1},led {x: 7,y: 0,brightness: 1}, | |
led {x: 7,y: 1,brightness: 1},led {x: 7,y: 2,brightness: 1},led {x: 7,y: 3,brightness: 1},led {x: 7,y: 4,brightness: 1},led {x: 7,y: 5,brightness: 1},led {x: 7,y: 6,brightness: 1},led {x: 7,y: 7,brightness: 1},led {x: 7,y: 8,brightness: 1},led {x: 8,y: 0,brightness: 1}, | |
led {x: 8,y: 1,brightness: 1},led {x: 8,y: 2,brightness: 1},led {x: 8,y: 3,brightness: 1},led {x: 8,y: 4,brightness: 1},led {x: 8,y: 5,brightness: 1},led {x: 8,y: 6,brightness: 1},led {x: 8,y: 7,brightness: 1},led {x: 8,y: 8,brightness: 1},led {x: 15,y: 8,brightness: 1}, | |
led {x: 14,y: 8,brightness: 1},led {x: 13,y: 8,brightness: 1},led {x: 12,y: 8,brightness: 1},led {x: 11,y: 8,brightness: 1},led {x: 10,y: 8,brightness: 1},led {x: 9,y: 8,brightness: 1},led {x: 8,y: 8,brightness: 1},led {x: 15,y: 7,brightness: 1}, | |
led {x: 14,y: 7,brightness: 1},led {x: 13,y: 7,brightness: 1},led {x: 12,y: 7,brightness: 1},led {x: 11,y: 7,brightness: 1},led {x: 10,y: 7,brightness: 1},led {x: 9,y: 7,brightness: 1},led {x: 8,y: 7,brightness: 1},led {x: 15,y: 6,brightness: 1}, | |
led {x: 14,y: 6,brightness: 1},led {x: 13,y: 6,brightness: 1},led {x: 12,y: 6,brightness: 1},led {x: 11,y: 6,brightness: 1},led {x: 10,y: 6,brightness: 1},led {x: 9,y: 6,brightness: 1},led {x: 8,y: 6,brightness: 1}, | |
}; | |
led on[48]; | |
led off[48]; | |
led sw; | |
bool trans = false; | |
uint8_t curOn = 0; | |
uint8_t curOff = 0; | |
int16_t diff; | |
uint8_t dBrightness = 127; | |
bool bChange = false; | |
int bChangeIndex = 0; | |
uint8_t cChange = 0; | |
float oBrightness = 0; | |
uint8_t loops = 200; | |
class StarlightOutput: public Component, public LightOutput { | |
public: void setup() override { | |
if (!ledmatrix.begin()) { | |
ESP_LOGE("starlight", "IS31 not found"); | |
while (1); | |
} | |
ESP_LOGI("starlight", "IS31 found"); | |
ledmatrix.clear(); | |
//Randomly light them up | |
randomize(all); | |
for (int i = 0; i < 48; i++) { | |
on[i] = all[i]; | |
on[i].brightness = dBrightness; | |
ledmatrix.drawPixel(on[i].x, on[i].y, on[i].brightness); | |
off[i] = all[48 + i]; | |
off[i].brightness = 0; | |
ledmatrix.drawPixel(off[i].x, off[i].y, off[i].brightness); | |
} | |
} | |
LightTraits get_traits() override { | |
auto traits = LightTraits(); | |
traits.set_supported_color_modes({ | |
light::ColorMode::BRIGHTNESS | |
}); | |
return traits; | |
} | |
public: void loop() override { | |
loops++; | |
if (bChange) { | |
diff = on[bChangeIndex].brightness - dBrightness; | |
if (abs(diff) < 2) { | |
on[bChangeIndex].brightness = dBrightness; | |
} else { | |
on[bChangeIndex].brightness = on[bChangeIndex].brightness - round(0.5 * diff); | |
} | |
off[bChangeIndex].brightness = 0; | |
ledmatrix.drawPixel(on[bChangeIndex].x, on[bChangeIndex].y, on[bChangeIndex].brightness); | |
ledmatrix.drawPixel(off[bChangeIndex].x, off[bChangeIndex].y, off[bChangeIndex].brightness); | |
if (on[bChangeIndex].brightness == dBrightness) { | |
bChangeIndex++; | |
} | |
if (bChangeIndex == 48) { | |
bChange = false; | |
} | |
} else if (!trans && loops > 50) { | |
curOn = random(48); | |
curOff = random(48); | |
while (curOff == curOn) { | |
curOff = random(48); | |
} | |
swap( & on[curOn], & off[curOff]); | |
trans = true; | |
loops = 0; | |
ESP_LOGD("starlight", "Setting switches : %d, %d", curOn, curOff); | |
} else if (trans) { | |
diff = dBrightness - on[curOn].brightness; | |
on[curOn].brightness = on[curOn].brightness + max(round(diff*0.1),1.0); | |
if (on[curOn].brightness >= dBrightness) { | |
on[curOn].brightness = dBrightness; | |
} | |
ledmatrix.drawPixel(on[curOn].x, on[curOn].y, on[curOn].brightness); | |
off[curOff].brightness *= 0.9; | |
if (off[curOff].brightness <= 1) { | |
off[curOff].brightness = 0; | |
} | |
ledmatrix.drawPixel(off[curOff].x, off[curOff].y, off[curOff].brightness); | |
if (on[curOn].brightness == dBrightness && off[curOff].brightness == 0) { | |
trans = false; | |
} | |
} | |
} | |
void write_state(LightState * state) override { | |
state -> current_values_as_brightness( & oBrightness); | |
uint8_t newBright = uint8_t(255 * oBrightness); | |
if (newBright != dBrightness){ | |
dBrightness = newBright; | |
bChange = true; | |
randomize(on); | |
bChangeIndex = 0; | |
} | |
ESP_LOGD("starlight", "The brightness is: %lf, %d", oBrightness, dBrightness); | |
} | |
void randomize(led arr[]) { | |
int n = sizeof(arr) / sizeof(arr[0]); | |
for (int i = n - 1; i > 0; i--) { | |
long j = random(0, n); | |
swap( & arr[i], & arr[j]); | |
} | |
} | |
void swap(led * a, led * b) { | |
led temp = * a; | |
* a = * b; | |
* b = temp; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
esphome: | |
name: starlight | |
platform: ESP32 | |
board: lolin_d32_pro | |
includes: | |
- starlight.h | |
libraries: | |
- "Adafruit IS31FL3731 Library" | |
- "Adafruit GFX Library" | |
- "Adafruit BusIO" | |
- "SPI" | |
wifi: !include wifi.yaml | |
captive_portal: | |
logger: | |
api: | |
ota: | |
light: | |
- platform: custom | |
lambda: |- | |
auto light_out = new StarlightOutput(); | |
App.register_component(light_out); | |
return {light_out}; | |
lights: | |
- name: "Starlight" | |
id: starlight | |
default_transition_length: 0s | |
gamma_correct: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment