-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmooth_led_brightness.c
50 lines (42 loc) · 1.15 KB
/
smooth_led_brightness.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Compile:
* gcc -lm -o brightness smooth_led_brightness.c
*
* Example:
* [username@localhost ~]$ ./brightness 10 255
* Output:
* int brightness[10] = { 0, 0, 2, 4, 8, 14, 26, 47, 83, 145};
*
*
* argv[1] - количество шагов ШИМ (по умолчанию 100)
* argv[2] - до скольки считает ШИМ (по умолчанию 255 как у Arduino)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
int pwmSteps;
int pwmCounts;
int brightness;
float R;
if (argc < 2) {
pwmSteps = 100;
pwmCounts = 255;
} else {
pwmSteps = strtol(argv[1], NULL, 10);
pwmCounts = strtol(argv[2], NULL, 10);
}
R = (pwmSteps * log10(2))/(log10(pwmCounts));
printf("\n");
printf("int brightness[%d] = {", pwmSteps*2);
for (int interval = 0; interval < pwmSteps; interval++) {
brightness = pow(2, (interval / R)) - 1;
printf("%3d, ", brightness);
}
for (int interval = pwmSteps-1; interval > 0; interval--) {
brightness = pow(2, (interval / R)) - 1;
printf("%3d, ", brightness);
}
printf("\b\b};\n\n");
return 0;
}