summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2019-07-19 23:39:28 -0600
committerAaron Ball <nullspoon@oper.io>2019-07-19 23:41:44 -0600
commitec5df5fd9d2403b610356377e29e77a272d1809d (patch)
treef4b2365b9107453dfc69ad2b941906660a876e7b /src
parent4021b5cd0ae053afcacedbbe1b4554665819abb8 (diff)
downloadkbd-fade-ec5df5fd9d2403b610356377e29e77a272d1809d.tar.gz
kbd-fade-ec5df5fd9d2403b610356377e29e77a272d1809d.tar.xz
Refactor to use modulus for color channel switching
Added macros for colormax and step size. Updated load_current_state. Added comments. Wrote channel_fade function which fades between two color channels. Also wrote rgb_fade which is the loop function around all the channels.
Diffstat (limited to 'src')
-rw-r--r--src/main.c140
1 files changed, 78 insertions, 62 deletions
diff --git a/src/main.c b/src/main.c
index 0d035b4..788adf8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -20,103 +20,119 @@
#include <unistd.h>
#define COLORFILE "/sys/class/leds/system76::kbd_backlight/color_left"
+#define COLORMAX 255
+#define COLORSTEP 5
-struct rgb {
- int r;
- int g;
- int b;
-};
-
-
-void writecolor(struct rgb color, int usec_wait) {
+void writecolor(int r, int g, int b) {
FILE* fd;
fd = fopen(COLORFILE, "w");
- fprintf(fd, "%02X%02X%02X\n", color.r, color.g, color.b);
+ fprintf(fd, "%02X%02X%02X\n", r, g, b);
fclose(fd);
- usleep(usec_wait);
}
-void load_current_state(struct rgb* srgb) {
+/**
+ * load_current_state:
+ * Loads the current color state from the keyboard's sys file at
+ * /sys/class/leds/*kbd_backlight/..
+ *
+ * @r Pointer to red int buffer
+ * @g Pointer to green int buffer
+ * @b Pointer to blue int buffer
+ */
+int load_current_state(int* r, int* g, int* b) {
char chan[3];
FILE* fd;
chan[3] = '\0';
fd = fopen(COLORFILE, "r");
+ if(!fd) {
+ *r = 0;
+ *g = 0;
+ *b = 0;
+ return 2;
+ }
chan[0] = fgetc(fd);
chan[1] = fgetc(fd);
- srgb->r = atoi(chan);
+ *r = atoi(chan);
chan[0] = fgetc(fd);
chan[1] = fgetc(fd);
- srgb->g = atoi(chan);
+ *g = atoi(chan);
chan[0] = fgetc(fd);
chan[1] = fgetc(fd);
- srgb->b = atoi(chan);
+ *b = atoi(chan);
fclose(fd);
+ return 0;
}
-int main(int argc, char* argv[]) {
- int step = 4;
- int wait = 10000;
+/**
+ * channel_fade:
+ * Fades one color channel into another in one step.
+ *
+ * @a Pointer to A color channel buffer
+ * @b Pointer to B color channel buffer
+ */
+void channel_fade(int* a, int* b) {
+ // Increase channel B with A maxed
+ if(*a <= COLORMAX) {
+ if(COLORMAX - *a >= COLORSTEP)
+ *a = *a + COLORSTEP;
+ else
+ *a = COLORMAX;
+ }
- struct rgb color;
- load_current_state(&color);
+ if(*b > 0) {
+ if(*b >= COLORSTEP)
+ *b = *b - COLORSTEP;
+ else
+ *b = 0;
+ }
+}
- // Set wait time between steps
- if(argc >= 2)
- wait = atoi(argv[1]) * 1000;
- // Step size
- if(argc == 3)
- step = atoi(argv[2]);
+/**
+ * rgb_fade:
+ * Infinite loop tha handles color fading
+ *
+ * @r Initialization value for red channel
+ * @b Initialization value for blue channel
+ * @g Initialization value for green channel
+ * @wait Wait time in milliseconds between steps
+ */
+void rgb_fade(int r, int g, int b, int wait) {
+ int i = 0;
+ int highest = 0;
+ int colors[3] = {r, g, b};
+ int size = 3;
while(1) {
- // Increase green
- while(color.g < 256) {
- writecolor(color, wait);
- color.g += step;
+ while(colors[i % size] < COLORMAX) {
+ channel_fade(&colors[i % size], &colors[(i + 1) % size]);
+ writecolor(colors[0], colors[1], colors[2]);
+ //printf("%02X %02X %02X\n", colors[0], colors[1], colors[2]);
+ usleep(wait * 1000);
}
- color.g = 255;
+ i++;
+ }
+}
- // Decrease red
- while(color.r > 0) {
- writecolor(color, wait);
- color.r -= step;
- }
- color.r = 0;
- // Increase blue
- while(color.b < 256) {
- writecolor(color, wait);
- color.b += step;
- }
- color.b = 255;
-
- // Decrease green
- while(color.g > 0) {
- writecolor(color, wait);
- color.g -= step;
- }
- color.g = 0;
+int main(int argc, char* argv[]) {
+ int wait = 500;
+ int r = 0;
+ int g = 0;
+ int b = 0;
- // Increase red
- while(color.r < 256) {
- writecolor(color, wait);
- color.r += step;
- }
- color.r = 255;
+ //Set wait time between steps if specified
+ if(argc == 2)
+ wait = atoi(argv[1]);
- // Decrease blue
- while(color.b > 0) {
- writecolor(color, wait);
- color.b -= step;
- }
- color.b = 0;
- }
+ load_current_state(&r, &g, &b);
+ rgb_fade(r, g, b, wait);
}

Generated by cgit