Browse Source

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 4 years ago
parent
commit
0d6c4b5489
2 changed files with 31 additions and 9 deletions
  1. 30
    9
      src/light.c
  2. 1
    0
      src/light.h

+ 30
- 9
src/light.c View File

17
 
17
 
18
 /* Static helper functions for this file only, prefix with _ */
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
     uint64_t step_size = (uint64_t) ( ((float)end_value - (float)start_value) / (float)steps );
27
     uint64_t step_size = (uint64_t) ( ((float)end_value - (float)start_value) / (float)steps );
25
 
28
 
26
     for(int i=1; i<steps-1; i++)
29
     for(int i=1; i<steps-1; i++)
27
     {
30
     {
28
       target->set_value(target, start_value + i * step_size);
31
       target->set_value(target, start_value + i * step_size);
29
-      usleep(10000);
32
+      usleep(step_time);
30
     }
33
     }
31
 }
34
 }
32
 
35
 
216
         "Options:\n"
219
         "Options:\n"
217
         "  -r          Interpret input and output values in raw mode (ignored for -T)\n"
220
         "  -r          Interpret input and output values in raw mode (ignored for -T)\n"
218
         "  -s          Specify device target path to use, use -L to list available\n"
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
         "  -v          Specify the verbosity level (default 0)\n"
223
         "  -v          Specify the verbosity level (default 0)\n"
220
         "                 0: Values only\n"
224
         "                 0: Values only\n"
221
         "                 1: Values, Errors.\n"
225
         "                 1: Values, Errors.\n"
245
 {
249
 {
246
     int32_t curr_arg = -1;
250
     int32_t curr_arg = -1;
247
     int32_t log_level = 0;
251
     int32_t log_level = 0;
252
+    int32_t smooth_ms = 0;
248
     
253
     
249
     char ctrl_name[NAME_MAX];
254
     char ctrl_name[NAME_MAX];
250
     bool need_value = false;
255
     bool need_value = false;
253
     bool specified_target = false;
258
     bool specified_target = false;
254
     snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
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
         switch(curr_arg)
263
         switch(curr_arg)
259
         {
264
         {
276
                 
281
                 
277
                 light_loglevel = (light_loglevel_t)log_level;
282
                 light_loglevel = (light_loglevel_t)log_level;
278
                 break;
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
             case 's':
300
             case 's':
280
                 snprintf(ctrl_name, sizeof(ctrl_name), "%s", optarg);
301
                 snprintf(ctrl_name, sizeof(ctrl_name), "%s", optarg);
281
                 specified_target = true;
302
                 specified_target = true;
742
 
763
 
743
     uint64_t start_value;
764
     uint64_t start_value;
744
     target->get_value(target, &start_value);
765
     target->get_value(target, &start_value);
745
-    _fade_value(target, start_value, value);
766
+    _fade_value(ctx, start_value, value);
746
     
767
     
747
     if(!target->set_value(target, value))
768
     if(!target->set_value(target, value))
748
     {
769
     {
916
     
937
     
917
     uint64_t start_value;
938
     uint64_t start_value;
918
     target->get_value(target, &start_value);
939
     target->get_value(target, &start_value);
919
-    _fade_value(target, start_value, value);
940
+    _fade_value(ctx, start_value, value);
920
 
941
 
921
     if(!target->set_value(target, value))
942
     if(!target->set_value(target, value))
922
     {
943
     {
960
 
981
 
961
     uint64_t start_value;
982
     uint64_t start_value;
962
     target->get_value(target, &start_value);
983
     target->get_value(target, &start_value);
963
-    _fade_value(target, start_value, value);
984
+    _fade_value(ctx, start_value, value);
964
 
985
 
965
     if(!target->set_value(target, value))
986
     if(!target->set_value(target, value))
966
     {
987
     {
1019
 
1040
 
1020
     uint64_t start_value;
1041
     uint64_t start_value;
1021
     target->get_value(target, &start_value);
1042
     target->get_value(target, &start_value);
1022
-    _fade_value(target, start_value, value);
1043
+    _fade_value(ctx, start_value, value);
1023
 
1044
 
1024
     if(!target->set_value(target, value))
1045
     if(!target->set_value(target, value))
1025
     {
1046
     {

+ 1
- 0
src/light.h View File

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