Skip to content

Instantly share code, notes, and snippets.

@mcourteaux
Created November 5, 2016 21:22
Show Gist options
  • Save mcourteaux/4aa4230c5f64a63b2ba6a80cc29bcfea to your computer and use it in GitHub Desktop.
Save mcourteaux/4aa4230c5f64a63b2ba6a80cc29bcfea to your computer and use it in GitHub Desktop.
MacBook Pro 8,1 Disco Lights
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
struct light_dev {
virtual void set_brighness(float fraction) = 0;
};
struct led_dev : light_dev {
led_dev(std::string folder) : folder(folder) {
std::ifstream mfs(folder + "/max_brightness");
mfs >> max;
mfs.close();
std::cout << folder << " max=" << max << std::endl;
}
std::string folder;
int max;
virtual void set_brighness(float fraction) override {
std::ofstream ofs(folder + "/brightness");
ofs << (int)std::round(fraction * max) << std::endl;
ofs.close();
}
};
struct spdif_dev : light_dev {
int status;
virtual void set_brighness(float fraction) override {
int s = (int)std::round(fraction);
if (s != status) {
status = s;
std::string str = status ? "on" : "off";
system(("amixer set IEC958,16 " + str).c_str());
}
}
};
int main() {
using namespace std::chrono_literals;
std::vector<light_dev*> lights = {
new led_dev("/sys/class/leds/smc::kbd_backlight"),
new led_dev("/sys/class/leds/input6::capslock"),
new led_dev("/sys/class/backlight/intel_backlight"), new spdif_dev()};
std::vector<double> time_factors = {2.0, 8.0, 0.1, 10};
auto start = std::chrono::system_clock::now();
while (true) {
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> dur = (end - start);
double time = dur.count();
for (int i = 0; i < lights.size(); ++i) {
float s = std::sin(time * 5.0f * time_factors[i]);
s *= s;
lights[i]->set_brighness(s);
}
std::this_thread::sleep_for(50ms);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment