/
filter-brightness-als.c
1928 lines (1582 loc) · 53.7 KB
/
filter-brightness-als.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file filter-brightness-als.c
* Ambient Light Sensor level adjusting filter module
* for display backlight, key backlight, and LED brightness
* This file implements a filter module for MCE
* <p>
* Copyright © 2007-2011 Nokia Corporation and/or its subsidiary(-ies).
* Copyright © 2012-2019 Jolla Ltd.
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
* @author Tuomo Tanskanen <ext-tuomo.1.tanskanen@nokia.com>
* @author Tapio Rantala <ext-tapio.rantala@nokia.com>
* @author Santtu Lakkala <ext-santtu.1.lakkala@nokia.com>
* @author Jukka Turunen <ext-jukka.t.turunen@nokia.com>
* @author Victor Portnov <ext-victor.portnov@nokia.com>
* @author Kalle Jokiniemi <kalle.jokiniemi@jolla.com>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filter-brightness-als.h"
#include "display.h"
#include "../mce-log.h"
#include "../mce-io.h"
#include "../mce-conf.h"
#include "../mce-setting.h"
#include "../mce-dbus.h"
#include "../mce-sensorfw.h"
#include "../mce-wakelock.h"
#include "../tklock.h"
#include <string.h>
#include <mce/dbus-names.h>
#include <gmodule.h>
/* ========================================================================= *
* CONSTANTS
* ========================================================================= */
/** Module name */
#define MODULE_NAME "filter-brightness-als"
/** Maximum number of Lux->Brightness curves that can be defined in config
*
* Using odd number means the brightness setting values 1, 50 and 100 will
* be mapped to the 1st, the middle and the last of the profiles available.
*
* Using 21 equals rouhgly: 1 automatic curve / 5 brightness setting steps.
*/
#define FBA_PROFILE_COUNT 21
/** Maximum number of steps Lux->Brightness curves can have
*
* Using 21 allows: Covering the 1-100% brightness range in < 5% steps.
*/
#define FBA_PROFILE_STEPS 21
/** Size of the median filtering window; code expects the value to be odd */
#define FBA_INPUTFLT_MEDIAN_SIZE 9
/** Duration of temporary ALS enable sessions */
#define FBA_SENSORPOLL_DURATION_MS 5000
/* ========================================================================= *
* FUNCTIONALITY
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* MISC_UTILS
* ------------------------------------------------------------------------- */
static int fba_util_imin (int a, int b);
static bool fba_util_streq (const char *s1, const char *s2);
/* ------------------------------------------------------------------------- *
* DYNAMIC_SETTINGS
* ------------------------------------------------------------------------- */
static void fba_setting_cb (GConfClient *const gcc, const guint id, GConfEntry *const entry, gpointer const data);
static void fba_setting_init (void);
static void fba_setting_quit (void);
/* ------------------------------------------------------------------------- *
* COLOR_PROFILE
* ------------------------------------------------------------------------- */
static bool fba_color_profile_exists (const char *id);
static bool fba_color_profile_set (const gchar *id);
static void fba_color_profile_init (void);
static void fba_color_profile_quit (void);
/* ------------------------------------------------------------------------- *
* SENSOR_INPUT_FILTERING
* ------------------------------------------------------------------------- */
/** Hooks that an input filter backend needs to implement */
typedef struct
{
const char *fi_name;
void (*fi_reset)(void);
int (*fi_filter)(int);
bool (*fi_stable)(void);
} fba_inputflt_t;
// INPUT_FILTER_BACKEND_DUMMY
static int fba_inputflt_dummy_filter (int add);
static bool fba_inputflt_dummy_stable (void);
static void fba_inputflt_dummy_reset (void);
// INPUT_FILTER_BACKEND_MEDIAN
static int fba_inputflt_median_filter (int add);
static bool fba_inputflt_median_stable (void);
static void fba_inputflt_median_reset (void);
// INPUT_FILTER_FRONTEND
static void fba_inputflt_reset (void);
static int fba_inputflt_filter (int lux);
static bool fba_inputflt_stable (void);
static void fba_inputflt_select (const char *name);
static void fba_inputflt_flush_on_change (void);
static void fba_inputflt_sampling_output (int lux);
static gboolean fba_inputflt_sampling_cb (gpointer aptr);
static void fba_inputflt_sampling_start (void);
static void fba_inputflt_sampling_stop (void);
static int fba_inputflt_sampling_time (void);
static void fba_inputflt_sampling_input (int lux);
static void fba_inputflt_init (void);
static void fba_inputflt_quit (void);
/* ------------------------------------------------------------------------- *
* ALS_FILTER
* ------------------------------------------------------------------------- */
/** A step in ALS ramp */
typedef struct
{
int lux; /**< upper lux limit */
int val; /**< brightness percentage to use */
} fba_als_limit_t;
/** ALS filtering state */
typedef struct
{
/** Filter name; used for locating configuration data */
const char *id;
/* Number of profiles available */
int profiles;
/** Threshold: lower lux limit */
int lux_lo;
/** Threshold: upper lux limit */
int lux_hi;
/** Threshold: active ALS profile */
int prof;
/** Latest brightness percentage result */
int val;
/** Brightness percent from lux value look up table */
fba_als_limit_t lut[FBA_PROFILE_COUNT][FBA_PROFILE_STEPS+1];
} fba_als_filter_t;
static void fba_als_filter_clear_threshold (fba_als_filter_t *self);
static bool fba_als_filter_load_profile (fba_als_filter_t *self, const char *grp, int prof);
static void fba_als_filter_reset_profiles (fba_als_filter_t *self);
static void fba_als_filter_load_profiles (fba_als_filter_t *self);
static int fba_als_filter_get_lux (fba_als_filter_t *self, int prof, int slot);
static int fba_als_filter_run (fba_als_filter_t *self, int prof, int lux);
static void fba_als_filter_init (void);
/* ------------------------------------------------------------------------- *
* DATAPIPE_TRACKING
* ------------------------------------------------------------------------- */
static gpointer fba_datapipe_display_brightness_filter (gpointer data);
static gpointer fba_datapipe_led_brightness_filter (gpointer data);
static gpointer fba_datapipe_lpm_brightness_filter (gpointer data);
static gpointer fba_datapipe_key_backlight_brightness_filter(gpointer data);
static gpointer fba_datapipe_light_sensor_poll_request_filter(gpointer data);
static void fba_datapipe_display_state_curr_trigger (gconstpointer data);
static void fba_datapipe_display_state_next_trigger (gconstpointer data);
static void fba_datapipe_execute_brightness_change (void);
static void fba_datapipe_init (void);
static void fba_datapipe_quit (void);
/* ------------------------------------------------------------------------- *
* DBUS_HANDLERS
* ------------------------------------------------------------------------- */
static void fba_dbus_send_current_color_profile (DBusMessage *method_call);
static gboolean fba_dbus_get_color_profile_cb (DBusMessage *const msg);
static gboolean fba_dbus_get_color_profiles_cb (DBusMessage *const msg);
static gboolean fba_dbus_set_color_profile_cb (DBusMessage *const msg);
static void fba_dbus_init (void);
static void fba_dbus_quit (void);
/* ------------------------------------------------------------------------- *
* SENSOR_STATUS
* ------------------------------------------------------------------------- */
static void fba_status_sensor_value_change_cb (int lux);
static bool fba_status_sensor_is_needed (void);
static void fba_status_rethink (void);
/* ------------------------------------------------------------------------- *
* SENSOR_POLL
* ------------------------------------------------------------------------- */
static gboolean fba_sensorpoll_timer_cb (gpointer aptr);
static void fba_sensorpoll_start (void);
static void fba_sensorpoll_stop (void);
static void fba_sensorpoll_rethink (void);
/* ------------------------------------------------------------------------- *
* LOAD_UNLOAD
* ------------------------------------------------------------------------- */
G_MODULE_EXPORT const gchar *g_module_check_init (GModule *module);
G_MODULE_EXPORT void g_module_unload (GModule *module);
/** Flag for: The plugin is about to be unloaded */
static bool fba_module_unload = false;
/* ========================================================================= *
* MISC_UTILS
* ========================================================================= */
/** Integer minimum helper in style of fminf()
*
* @param a integer value
* @param b integer value
*
* @return minimum of the two values
*/
static int fba_util_imin(int a, int b)
{
return (a < b) ? a : b;
}
/* Null tolerant string equality predicate
*
* @param s1 string
* @param s2 string
*
* @return true if both s1 and s2 are null or same string, false otherwise
*/
static bool fba_util_streq(const char *s1, const char *s2)
{
return (s1 && s2) ? !strcmp(s1, s2) : (s1 == s2);
}
/* ========================================================================= *
* DYNAMIC_SETTINGS
* ========================================================================= */
/** Master ALS enabled setting */
static gboolean fba_setting_als_enabled = MCE_DEFAULT_DISPLAY_ALS_ENABLED;
static guint fba_setting_als_enabled_id = 0;
/** Filter brightness through ALS setting */
static gboolean fba_setting_als_autobrightness = MCE_DEFAULT_DISPLAY_ALS_AUTOBRIGHTNESS;
static guint fba_setting_als_autobrightness_id = 0;
/** ALS is used for LID filtering setting */
static gboolean fba_setting_filter_lid_with_als = MCE_DEFAULT_TK_FILTER_LID_WITH_ALS;
static guint fba_setting_filter_lid_with_als_id = 0;
/** Input filter to use for ALS sensor - config value */
static gchar *fba_setting_als_input_filter = 0;
static guint fba_setting_als_input_filter_id = 0;
/** Sample time for ALS input filtering - config value */
static gint fba_setting_als_sample_time = MCE_DEFAULT_DISPLAY_ALS_SAMPLE_TIME;
static guint fba_setting_als_sample_time_id = 0;
/** Currently active color profile (dummy implementation) */
static gchar *fba_setting_color_profile = 0;
static guint fba_setting_color_profile_id = 0;
/** Default color profile, set in ini-files */
static gchar *fba_default_color_profile = 0;
/** GConf callback for powerkey related settings
*
* @param gcc (not used)
* @param id Connection ID from gconf_client_notify_add()
* @param entry The modified GConf entry
* @param data (not used)
*/
static void
fba_setting_cb(GConfClient *const gcc, const guint id,
GConfEntry *const entry, gpointer const data)
{
(void)gcc;
(void)data;
const GConfValue *gcv = gconf_entry_get_value(entry);
if( !gcv ) {
mce_log(LL_DEBUG, "GConf Key `%s' has been unset",
gconf_entry_get_key(entry));
goto EXIT;
}
if( id == fba_setting_als_enabled_id ) {
fba_setting_als_enabled = gconf_value_get_bool(gcv);
fba_status_rethink();
}
else if( id == fba_setting_als_autobrightness_id ) {
fba_setting_als_autobrightness = gconf_value_get_bool(gcv);
fba_status_rethink();
}
else if( id == fba_setting_filter_lid_with_als_id ) {
fba_setting_filter_lid_with_als = gconf_value_get_bool(gcv);
fba_status_rethink();
}
else if( id == fba_setting_als_input_filter_id ) {
const char *val = gconf_value_get_string(gcv);
if( !fba_util_streq(fba_setting_als_input_filter, val) ) {
g_free(fba_setting_als_input_filter);
fba_setting_als_input_filter = g_strdup(val);
fba_inputflt_select(fba_setting_als_input_filter);
}
}
else if( id == fba_setting_als_sample_time_id ) {
gint old = fba_setting_als_sample_time;
fba_setting_als_sample_time = gconf_value_get_int(gcv);
if( fba_setting_als_sample_time != old ) {
mce_log(LL_NOTICE, "fba_setting_als_sample_time: %d -> %d",
old, fba_setting_als_sample_time);
// NB: takes effect on the next sample timer restart
}
}
else if (id == fba_setting_color_profile_id) {
const gchar *val = gconf_value_get_string(gcv);
mce_log(LL_NOTICE, "fba_setting_color_profile: '%s' -> '%s'",
fba_setting_color_profile, val);
fba_color_profile_set(val);
}
else {
mce_log(LL_WARN, "Spurious GConf value received; confused!");
}
EXIT:
return;
}
/** Start tracking setting changes
*/
static void
fba_setting_init(void)
{
/* ALS enabled settings */
mce_setting_track_bool(MCE_SETTING_DISPLAY_ALS_ENABLED,
&fba_setting_als_enabled,
MCE_DEFAULT_DISPLAY_ALS_ENABLED,
fba_setting_cb,
&fba_setting_als_enabled_id);
mce_setting_track_bool(MCE_SETTING_DISPLAY_ALS_AUTOBRIGHTNESS,
&fba_setting_als_autobrightness,
MCE_DEFAULT_DISPLAY_ALS_AUTOBRIGHTNESS,
fba_setting_cb,
&fba_setting_als_autobrightness_id);
mce_setting_track_bool(MCE_SETTING_TK_FILTER_LID_WITH_ALS,
&fba_setting_filter_lid_with_als,
MCE_DEFAULT_TK_FILTER_LID_WITH_ALS,
fba_setting_cb,
&fba_setting_filter_lid_with_als_id);
/* ALS input filter setting */
mce_setting_track_string(MCE_SETTING_DISPLAY_ALS_INPUT_FILTER,
&fba_setting_als_input_filter,
MCE_DEFAULT_DISPLAY_ALS_INPUT_FILTER,
fba_setting_cb,
&fba_setting_als_input_filter_id);
/* ALS sample time setting */
mce_setting_track_int(MCE_SETTING_DISPLAY_ALS_SAMPLE_TIME,
&fba_setting_als_sample_time,
MCE_DEFAULT_DISPLAY_ALS_SAMPLE_TIME,
fba_setting_cb,
&fba_setting_als_sample_time_id);
/* Color profile setting */
mce_setting_notifier_add(MCE_SETTING_DISPLAY_PATH,
MCE_SETTING_DISPLAY_COLOR_PROFILE,
fba_setting_cb,
&fba_setting_color_profile_id);
fba_color_profile_init();
}
/** Stop tracking setting changes
*/
static void
fba_setting_quit(void)
{
mce_setting_notifier_remove(fba_setting_als_enabled_id),
fba_setting_als_enabled_id = 0;
mce_setting_notifier_remove(fba_setting_als_autobrightness_id),
fba_setting_als_autobrightness_id = 0;
mce_setting_notifier_remove(fba_setting_filter_lid_with_als_id),
fba_setting_filter_lid_with_als_id = 0;
mce_setting_notifier_remove(fba_setting_als_input_filter_id),
fba_setting_als_input_filter_id = 0;
mce_setting_notifier_remove(fba_setting_als_sample_time_id),
fba_setting_als_sample_time_id = 0;
mce_setting_notifier_remove(fba_setting_color_profile_id),
fba_setting_color_profile_id = 0;
fba_color_profile_quit();
}
/* ========================================================================= *
* COLOR_PROFILE
* ========================================================================= */
/** List of known color profiles (dummy implementation) */
static const char * const fba_color_profile_names[] =
{
COLOR_PROFILE_ID_HARDCODED,
};
/** Check if color profile is supported
*
* @param id color profile name
*
* @return true if profile is supported, false otherwise
*/
static bool
fba_color_profile_exists(const char *id)
{
if( !id || !*id )
return false;
if( !strcmp(id, COLOR_PROFILE_ID_HARDCODED) )
return true;
for( size_t i = 0; i < G_N_ELEMENTS(fba_color_profile_names); ++i ) {
if( !strcmp(fba_color_profile_names[i], id) )
return true;
}
return false;
}
/**
* Set the current color profile according to requested
*
* @param id Name of requested color profile
*
* @return true on success, false on failure
*/
static bool
fba_color_profile_set(const gchar *id)
{
// NOTE: color profile support dropped, this just a stub
/* Transform "default" request into request for whatever is
* the configured default color profile. */
if( !g_strcmp0(id, COLOR_PROFILE_ID_DEFAULT) )
id = fba_default_color_profile ?: COLOR_PROFILE_ID_HARDCODED;
/* Succes is: The requested color profile exists */
bool success = fba_color_profile_exists(id);
/* Fall back to hadrcoded if requested profile is not supported */
if( !success ) {
if( id && *id )
mce_log(LL_WARN, "%s: unsupported color profile", id);
id = COLOR_PROFILE_ID_HARDCODED;
}
/* Check if the color profile does change */
bool changed = g_strcmp0(id, fba_setting_color_profile);
/* Update the cached value */
if( changed ) {
g_free(fba_setting_color_profile),
fba_setting_color_profile = g_strdup(id);
}
/* Send a change indication if the value did change, or
* if the requested value was not accepted as-is */
if( changed || !success ) {
fba_dbus_send_current_color_profile(0);
}
/* If we were to do something about the color profile,
* it would happen here */
if( changed ) {
/* nop */
}
/* Always sync the settings cache */
mce_setting_set_string(MCE_SETTING_DISPLAY_COLOR_PROFILE,
fba_setting_color_profile);
return success;
}
/**
* Initialization of saveed color profile during boot
*/
static void
fba_color_profile_init(void)
{
/* Get the default value specified in static configuration.
* This must be done before fba_color_profile_set() is called.
*/
fba_default_color_profile =
mce_conf_get_string(MCE_CONF_COMMON_GROUP,
MCE_CONF_DEFAULT_PROFILE_ID_KEY,
NULL);
/* Apply the last value saved to dynamic settings / the default.
*/
gchar *saved_color_profile = 0;
mce_setting_get_string(MCE_SETTING_DISPLAY_COLOR_PROFILE,
&saved_color_profile);
fba_color_profile_set(saved_color_profile);
g_free(saved_color_profile);
}
static void
fba_color_profile_quit(void)
{
g_free(fba_default_color_profile),
fba_default_color_profile = 0;
g_free(fba_setting_color_profile),
fba_setting_color_profile = 0;
}
/* ========================================================================= *
* SENSOR_INPUT_FILTERING
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* INPUT_FILTER_BACKEND_DUMMY
* ------------------------------------------------------------------------- */
static int
fba_inputflt_dummy_filter(int add)
{
return add;
}
static bool
fba_inputflt_dummy_stable(void)
{
return true;
}
static void
fba_inputflt_dummy_reset(void)
{
}
/* ------------------------------------------------------------------------- *
* INPUT_FILTER_BACKEND_MEDIAN
* ------------------------------------------------------------------------- */
/** Moving window of ALS measurements */
static int fba_inputflt_median_fifo[FBA_INPUTFLT_MEDIAN_SIZE] = { };
/** Contents of fba_inputflt_median_fifo in ascending order */
static int fba_inputflt_median_stat[FBA_INPUTFLT_MEDIAN_SIZE] = { };;
static int
fba_inputflt_median_filter(int add)
{
/* Adding negative sample values mean the sensor is not
* in use and we should forget any history that exists */
if( add < 0 ) {
for( int i = 0; i < FBA_INPUTFLT_MEDIAN_SIZE; ++i )
fba_inputflt_median_fifo[i] = fba_inputflt_median_stat[i] = -1;
goto EXIT;
}
/* Value to be removed */
int rem = fba_inputflt_median_fifo[0];
/* If negative value gets shifted out, it means we do not
* have history. Initialize with the value we have */
if( rem < 0 ) {
for( int i = 0; i < FBA_INPUTFLT_MEDIAN_SIZE; ++i )
fba_inputflt_median_fifo[i] = fba_inputflt_median_stat[i] = add;
goto EXIT;
}
/* Shift the new value to the sample window fifo */
for( int i = 1; i < FBA_INPUTFLT_MEDIAN_SIZE; ++i )
fba_inputflt_median_fifo[i-1] = fba_inputflt_median_fifo[i];
fba_inputflt_median_fifo[FBA_INPUTFLT_MEDIAN_SIZE-1] = add;
/* If we shift in the same value as what was shifted out,
* the ordered statistics do not change */
if( add == rem )
goto EXIT;
/* Do one pass filtering of ordered statistics:
* Remove the value we shifted out of the fifo
* and insert the new sample to correct place. */
int src = 0, dst = 0, tmp;
int stk = fba_inputflt_median_stat[src++];
while( dst < FBA_INPUTFLT_MEDIAN_SIZE ) {
if( stk < add )
tmp = stk, stk = add, add = tmp;
if( add == rem )
rem = INT_MAX;
else
fba_inputflt_median_stat[dst++] = add;
if( src < FBA_INPUTFLT_MEDIAN_SIZE )
add = fba_inputflt_median_stat[src++];
else
add = INT_MAX;
}
EXIT:
mce_log(LL_DEBUG, "%d - %d - %d",
fba_inputflt_median_stat[0],
fba_inputflt_median_stat[FBA_INPUTFLT_MEDIAN_SIZE/2],
fba_inputflt_median_stat[FBA_INPUTFLT_MEDIAN_SIZE-1]);
/* Return median of the history window */
return fba_inputflt_median_stat[FBA_INPUTFLT_MEDIAN_SIZE/2];
}
static bool
fba_inputflt_median_stable(void)
{
/* The smallest and the largest values in the sorted
* samples buffer are equal */
int small = 0;
int large = FBA_INPUTFLT_MEDIAN_SIZE-1;
return fba_inputflt_median_stat[small] == fba_inputflt_median_stat[large];
}
static void
fba_inputflt_median_reset(void)
{
fba_inputflt_median_filter(-1);
}
/* ------------------------------------------------------------------------- *
* INPUT_FILTER_FRONTEND
* ------------------------------------------------------------------------- */
/** Array of available input filter backendss */
static const fba_inputflt_t fba_inputflt_lut[] =
{
// NOTE: "disabled" must be in the 1st slot
{
.fi_name = "disabled",
.fi_reset = fba_inputflt_dummy_reset,
.fi_filter = fba_inputflt_dummy_filter,
.fi_stable = fba_inputflt_dummy_stable,
},
{
.fi_name = "median",
.fi_reset = fba_inputflt_median_reset,
.fi_filter = fba_inputflt_median_filter,
.fi_stable = fba_inputflt_median_stable,
},
};
/** Currently used input filter backend */
static const fba_inputflt_t *fba_inputflt_cur = fba_inputflt_lut;
/** Latest Lux value fed in to the filter */
static gint fba_inputflt_input_lux = 0;
/** Latest Lux value emitted from the filter */
static gint fba_inputflt_output_lux = -1;
/** Flag for: forget history on the next als change */
static bool fba_inputflt_flush_history = true;
/** Timer ID for: ALS data sampling */
static guint fba_inputflt_sampling_id = 0;
/** Set input filter backend
*
* @param name name of the backend to use
*/
static void
fba_inputflt_select(const char *name)
{
fba_inputflt_reset();
if( !name ) {
fba_inputflt_cur = fba_inputflt_lut;
goto EXIT;
}
for( size_t i = 0; ; ++i ) {
if( i == G_N_ELEMENTS(fba_inputflt_lut) ) {
mce_log(LL_WARN, "filter '%s' is unknown", name);
fba_inputflt_cur = fba_inputflt_lut;
break;
}
if( !strcmp(fba_inputflt_lut[i].fi_name, name) ) {
fba_inputflt_cur = fba_inputflt_lut + i;
break;
}
}
EXIT:
mce_log(LL_NOTICE, "selected '%s' als filter",
fba_inputflt_cur->fi_name);
fba_inputflt_reset();
}
/** Reset history buffer
*
* Is used to clear stale history data when powering up the sensor.
*/
static void
fba_inputflt_reset(void)
{
fba_inputflt_cur->fi_reset();
}
/** Apply filtering backend for als sensor value
*
* @param add sample to shift into the filter history
*
* @return filtered value
*/
static int
fba_inputflt_filter(int lux)
{
return fba_inputflt_cur->fi_filter(lux);
}
/** Check if the whole history buffer is filled with the same value
*
* Is used to determine when the pseudo sampling timer can be stopped.
*/
static bool
fba_inputflt_stable(void)
{
return fba_inputflt_cur->fi_stable();
}
/** Request filter history to be flushed on the next ALS change
*/
static void
fba_inputflt_flush_on_change(void)
{
fba_inputflt_flush_history = true;
}
/** Get filtered ALS state and feed it to datapipes
*
* @param lux sensor reading, or -1 for no-data
*/
static void
fba_inputflt_sampling_output(int lux)
{
lux = fba_inputflt_filter(lux);
if( fba_inputflt_output_lux == lux )
goto EXIT;
mce_log(LL_DEBUG, "output: %d -> %d", fba_inputflt_output_lux, lux);
fba_inputflt_output_lux = lux;
fba_datapipe_execute_brightness_change();
/* Feed filtered sensor data to datapipe */
datapipe_exec_full(&light_sensor_filtered_pipe,
GINT_TO_POINTER(fba_inputflt_output_lux));
EXIT:
return;
}
/** Timer callback for: ALS data sampling
*
* @param aptr (unused user data pointer)
*
* @return TRUE to keep timer repeating, or FALSE to stop it
*/
static gboolean
fba_inputflt_sampling_cb(gpointer aptr)
{
(void)aptr;
if( !fba_inputflt_sampling_id )
return FALSE;
/* Drive the filter */
fba_inputflt_sampling_output(fba_inputflt_input_lux);
/* Keep timer active while changes come in */
if( !fba_inputflt_stable() )
return TRUE;
/* Stop sampling activity */
mce_log(LL_DEBUG, "stable");
fba_inputflt_sampling_id = 0;
return FALSE;
}
static int
fba_inputflt_sampling_time(void)
{
return mce_clip_int(ALS_SAMPLE_TIME_MIN,
ALS_SAMPLE_TIME_MAX,
fba_setting_als_sample_time);
}
/** Start ALS sampling timer
*/
static void
fba_inputflt_sampling_start(void)
{
// check if we need to flush history
if( fba_inputflt_flush_history ) {
fba_inputflt_flush_history = false;
mce_log(LL_DEBUG, "reset");
if( fba_inputflt_sampling_id ) {
g_source_remove(fba_inputflt_sampling_id),
fba_inputflt_sampling_id = 0;
}
fba_inputflt_reset();
}
// start collecting history
if( !fba_inputflt_sampling_id ) {
mce_log(LL_DEBUG, "start");
fba_inputflt_sampling_id = g_timeout_add(fba_inputflt_sampling_time(),
fba_inputflt_sampling_cb, 0);
fba_inputflt_sampling_output(fba_inputflt_input_lux);
}
}
/** Stop ALS sampling timer
*/
static void
fba_inputflt_sampling_stop(void)
{
if( fba_inputflt_sampling_id ) {
mce_log(LL_DEBUG, "stop");
g_source_remove(fba_inputflt_sampling_id),
fba_inputflt_sampling_id = 0;
}
fba_inputflt_sampling_output(fba_inputflt_input_lux);
}
/** Feed sensor input to filter
*
* @param lux sensor reading, or -1 for no-data
*/
static void
fba_inputflt_sampling_input(int lux)
{
if( fba_inputflt_input_lux == lux )
goto EXIT;
mce_log(LL_DEBUG, "input: %d -> %d", fba_inputflt_input_lux, lux);
fba_inputflt_input_lux = lux;
if( fba_inputflt_input_lux < 0 )
fba_inputflt_sampling_stop();
else
fba_inputflt_sampling_start();
EXIT:
return;
}
/** Initialize ALS filtering
*/
static void
fba_inputflt_init(void)
{
fba_inputflt_select(fba_setting_als_input_filter);
}
/** De-initialize ALS filtering
*/
static void
fba_inputflt_quit(void)
{
/* Stop sampling timer by feeding no-data to filter */
fba_inputflt_sampling_input(-1);
}
/* ========================================================================= *
* ALS_FILTER
* ========================================================================= */
/** ALS filtering state for display backlight brightness */
static fba_als_filter_t lut_display =
{
.id = "Display",
};
/** ALS filtering state for keypad backlight brightness */
static fba_als_filter_t lut_key =
{
.id = "Keypad",
};
/** ALS filtering state for indication led brightness */
static fba_als_filter_t lut_led =
{
.id = "Led",
};
/** ALS filtering state for low power mode display simulation */
static fba_als_filter_t lut_lpm =
{
.id = "LPM",
};
/** Remove transition thresholds from filtering state
*
* Set thresholds so that any lux value will be out of bounds.
*
* @param self ALS filtering state data
*/
static void
fba_als_filter_clear_threshold(fba_als_filter_t *self)
{
self->lux_lo = INT_MAX;
self->lux_hi = 0;
}
/** Load ALS ramp into filtering state
*
* @param self ALS filtering state data
* @param grp Configuration group name
* @param prof ALS profile id
*/
static bool
fba_als_filter_load_profile(fba_als_filter_t *self, const char *grp, int prof)
{
bool success = false;
gsize lim_cnt = 0;
gsize lev_cnt = 0;
gint *lim_val = 0;
gint *lev_val = 0;
char lim_key[64];
char lev_key[64];
snprintf(lim_key, sizeof lim_key, "LimitsProfile%d", prof);
snprintf(lev_key, sizeof lev_key, "LevelsProfile%d", prof);
lim_val = mce_conf_get_int_list(grp, lim_key, &lim_cnt);
lev_val = mce_conf_get_int_list(grp, lev_key, &lev_cnt);