diff options
author | Aaron Ball <nullspoon@oper.io> | 2019-10-28 19:16:53 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2019-10-28 19:16:53 -0600 |
commit | 19d88f4971f5ec0f3d5518e340e1c0ca08f6d42b (patch) | |
tree | 2833b747cd24de5b37191a14ec7efe9d7c56bcc1 | |
parent | 037736c60d3361b1acae57ed1e8913ba0ac4c9b7 (diff) | |
download | luminous-19d88f4971f5ec0f3d5518e340e1c0ca08f6d42b.tar.gz luminous-19d88f4971f5ec0f3d5518e340e1c0ca08f6d42b.tar.xz |
Implement simpler fading process
This elinimates the need for a `fadeup` and `fadedown` function, which
required a significant amount of duplicate code.
These functions are now superceeded by the `fade` function, which
determines direction by the direction of variance between current level
and destination level. A step count is calculated and once reduced to
zero, the loop exits without needing to constantly compare brightness
levels.
This also cleans up the brightness writing process by checking if
opening occurred successfully once, and then flushing the buffer after
write, rather than opening and closing the file within the loop to flush
the write buffer.
-rw-r--r-- | src/main.c | 114 |
1 files changed, 42 insertions, 72 deletions
@@ -26,13 +26,13 @@ struct props { - int lvl; - int step; - int rate; - int max; - int cur; - char filebrightness[512]; - char filemax_bright[512]; + int lvl; // Destination brightness + int step; // Step size + int rate; // Time in milliseconds between steps + int max; // Maximum brightness of screen device + int cur; // Current brightness prior to fade operation + char filebrightness[512]; // Path to the sysfs ent for brightness + char filemax_bright[512]; // Path to the sysfs ent for max brightness }; @@ -49,7 +49,7 @@ void props_new(struct props* p) { /** * fgeti: - * @path + * @path Path to the file from which to read the integer * * Reads integer from the specified file. If the file does not contain only an * integer, returns -1. @@ -71,18 +71,21 @@ int fgeti(char* path) { /** - * fadeup: + * fade: * @props Props struct containing runtime parameters * - * Fades brightness down, incremented by props->step every props->rate - * milliseconds + * Fades brightness up or down, based on positive (fade up) or negative value + * (fade down) contained in `step` property. Rate of fade is controled by + * `props->rate`. * * returns: Success (0) or failure (-1) */ -int fadeup(struct props* props) { +int fade(struct props* props) { struct timespec tim, tim2; int sec; // Number of seconds to sleep int msec; // Number of milliseconds to sleep (less than 1 second) + int direction = 0; // Direction of fade. Positive for up, negative for down + int opcount = 0; sec = props->rate / 1000; msec = props->rate - (sec * 1000); @@ -90,72 +93,41 @@ int fadeup(struct props* props) { tim.tv_sec = sec; tim.tv_nsec = 1000000 * msec; - while(props->cur < props->lvl) { - nanosleep(&tim, &tim2); + // Determine direction + if(props->lvl > props->cur) { + // Dest brightness is higher than current + direction = 1; + opcount = (props->lvl - props->cur) / props->step; + } else if(props->lvl < props->cur) { + // Dest brightness is lower than current + direction = -1; + opcount = (props->cur - props->lvl) / props->step; + } - FILE* fd = fopen(props->filebrightness, "w"); - if(! fd) { - if(errno == EACCES) { - fprintf(stderr, "Permission denied: %s\n", props->filebrightness); - } else { - fprintf(stderr, "Could not open file %s\n", props->filebrightness); - } - return -1; + FILE* fd = fopen(props->filebrightness, "w"); + if(! fd) { + if(errno == EACCES) { + fprintf(stderr, "Permission denied: %s\n", props->filebrightness); + } else { + fprintf(stderr, "Could not open file %s\n", props->filebrightness); } - - // Write the new brightness to the brightness file - fprintf(fd, "%d\n", props->cur); - fclose(fd); - - if(props->cur >= props->max) - break; - props->cur += props->step; + return -1; } - return 0; -} - - -/** - * fadedown: - * @props Props struct containing runtime parameters - * - * Fades brightness down, decremented by props->step every props->rate - * milliseconds - * - * returns: Success (0) or failure (-1) - */ -int fadedown(struct props* props) { - struct timespec tim, tim2; - int sec; // Number of seconds to sleep - int msec; // Number of milliseconds to sleep (less than 1 second) - - sec = props->rate / 1000; - msec = props->rate - (sec * 1000); - - tim.tv_sec = sec; - tim.tv_nsec = 1000000 * msec; - while(props->cur > props->lvl) { + while(opcount > 0) { nanosleep(&tim, &tim2); - FILE* fd = fopen(props->filebrightness, "w"); - if(! fd) { - if(errno == EACCES) { - fprintf(stderr, "Permission denied: %s\n", props->filebrightness); - } else { - fprintf(stderr, "Could not open file %s\n", props->filebrightness); - } - return -1; - } - // Write the new brightness to the brightness file fprintf(fd, "%d\n", props->cur); - fclose(fd); - if(props->cur <= 0) - break; - props->cur -= props->step; + // Flush the toil...er...buffer! + fflush(fd); + + props->cur += props->step * direction; + opcount--; } + + fclose(fd); return 0; } @@ -301,10 +273,8 @@ int main(int argc, char* argv[]) { // Determine if we should fade up or fade down if(p.lvl < 0) { printf("%d\n", p.cur); - } else if(p.lvl > p.cur) { - r = fadeup(&p); - } else if (p.lvl < p.cur) { - r = fadedown(&p); + } else { + r = fade(&p); } if(r < 0) { |