Преглед изворни кода

Added command argument for smoothing time

Added command line argument '-t' to specify the length of time (in
milliseconds) over which to interpolate brightness changes. The time
between each brightness step is still unconfigurable at 10ms.
Nicola Sorace пре 3 година
родитељ
комит
0d6c4b5489
2 измењених фајлова са 31 додато и 9 уклоњено
  1. 30
    9
      src/light.c
  2. 1
    0
      src/light.h

+ 30
- 9
src/light.c Прегледај датотеку

@@ -17,16 +17,19 @@
17 17
 
18 18
 /* Static helper functions for this file only, prefix with _ */
19 19
 
20
-// Changes brightness smoothly from start value to end value
21
-static void _fade_value(light_device_target_t *target, uint64_t start_value, uint64_t end_value)
20
+// Changes brightness smoothly from start_value to end_value
21
+static void _fade_value(light_context_t *ctx, uint64_t start_value, uint64_t end_value)
22 22
 {
23
-    int steps = 20;
23
+    light_device_target_t *target = ctx->run_params.device_target;
24
+
25
+    useconds_t step_time = 10e3; // Microseconds per step
26
+    int steps = (ctx->run_params.smooth_ms * 1e3) / step_time;
24 27
     uint64_t step_size = (uint64_t) ( ((float)end_value - (float)start_value) / (float)steps );
25 28
 
26 29
     for(int i=1; i<steps-1; i++)
27 30
     {
28 31
       target->set_value(target, start_value + i * step_size);
29
-      usleep(10000);
32
+      usleep(step_time);
30 33
     }
31 34
 }
32 35
 
@@ -216,6 +219,7 @@ static void _light_print_usage()
216 219
         "Options:\n"
217 220
         "  -r          Interpret input and output values in raw mode (ignored for -T)\n"
218 221
         "  -s          Specify device target path to use, use -L to list available\n"
222
+        "  -t          Specify milliseconds taken to smoothly transition brightness (default 0)\n"
219 223
         "  -v          Specify the verbosity level (default 0)\n"
220 224
         "                 0: Values only\n"
221 225
         "                 1: Values, Errors.\n"
@@ -245,6 +249,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
245 249
 {
246 250
     int32_t curr_arg = -1;
247 251
     int32_t log_level = 0;
252
+    int32_t smooth_ms = 0;
248 253
     
249 254
     char ctrl_name[NAME_MAX];
250 255
     bool need_value = false;
@@ -253,7 +258,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
253 258
     bool specified_target = false;
254 259
     snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
255 260
     
256
-    while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:s:r")) != -1)
261
+    while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:t:s:r")) != -1)
257 262
     {
258 263
         switch(curr_arg)
259 264
         {
@@ -276,6 +281,22 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
276 281
                 
277 282
                 light_loglevel = (light_loglevel_t)log_level;
278 283
                 break;
284
+            case 't':
285
+                if(sscanf(optarg, "%i", &smooth_ms) != 1)
286
+                {
287
+                    fprintf(stderr, "-t argument is not an integer.\n\n");
288
+                    _light_print_usage();
289
+                    return false;
290
+                }
291
+                if(smooth_ms < 0)
292
+                {
293
+                    fprintf(stderr, "-t argument can't be negative.\n\n");
294
+                    _light_print_usage();
295
+                    return false;
296
+                }
297
+
298
+                ctx->run_params.smooth_ms = smooth_ms;
299
+                break;
279 300
             case 's':
280 301
                 snprintf(ctrl_name, sizeof(ctrl_name), "%s", optarg);
281 302
                 specified_target = true;
@@ -742,7 +763,7 @@ bool light_cmd_set_brightness(light_context_t *ctx)
742 763
 
743 764
     uint64_t start_value;
744 765
     target->get_value(target, &start_value);
745
-    _fade_value(target, start_value, value);
766
+    _fade_value(ctx, start_value, value);
746 767
     
747 768
     if(!target->set_value(target, value))
748 769
     {
@@ -916,7 +937,7 @@ bool light_cmd_add_brightness(light_context_t *ctx)
916 937
     
917 938
     uint64_t start_value;
918 939
     target->get_value(target, &start_value);
919
-    _fade_value(target, start_value, value);
940
+    _fade_value(ctx, start_value, value);
920 941
 
921 942
     if(!target->set_value(target, value))
922 943
     {
@@ -960,7 +981,7 @@ bool light_cmd_sub_brightness(light_context_t *ctx)
960 981
 
961 982
     uint64_t start_value;
962 983
     target->get_value(target, &start_value);
963
-    _fade_value(target, start_value, value);
984
+    _fade_value(ctx, start_value, value);
964 985
 
965 986
     if(!target->set_value(target, value))
966 987
     {
@@ -1019,7 +1040,7 @@ bool light_cmd_mul_brightness(light_context_t *ctx)
1019 1040
 
1020 1041
     uint64_t start_value;
1021 1042
     target->get_value(target, &start_value);
1022
-    _fade_value(target, start_value, value);
1043
+    _fade_value(ctx, start_value, value);
1023 1044
 
1024 1045
     if(!target->set_value(target, value))
1025 1046
     {

+ 1
- 0
src/light.h Прегледај датотеку

@@ -78,6 +78,7 @@ struct _light_context_t
78 78
         float                   float_value; // The input value as a float
79 79
         bool                    raw_mode; // Whether or not we use raw or percentage mode
80 80
         light_device_target_t   *device_target; // The device target to act on
81
+        int32_t                 smooth_ms; // Smoothing time in milliseconds
81 82
     } run_params;
82 83
 
83 84
     struct