@@ -12,10 +12,6 @@ const Config = imports.misc.config;
1212/** Settings
1313 */
1414
15- const HISTORY_DEPTH = 5 ;
16- const MEASURE_PERIOD = 1000 ;
17- const FORCE_SYNC_PERIOD = 5000 ;
18-
1915const BAT_STATUS = "/sys/class/power_supply/BAT0/status" ;
2016const POWER_NOW = "/sys/class/power_supply/BAT0/power_now" ;
2117
@@ -31,14 +27,18 @@ var TPIndicator = GObject.registerClass(
3127 _init ( ) {
3228 super . _init ( ) ;
3329
30+ this . settings = ExtensionUtils . getSettings ( 'org.gnome.shell.extensions.tp_wattmeter' ) ;
31+
32+ // to detect changes FIXME: a better way?
33+ this . last_period_val = this . settings . get_double ( 'period-sec' ) ;
34+
3435 this . readings = [ ] ;
3536 this . last_value = 0.0 ;
3637 this . tm_measure = null ;
37- this . tm_force_sync = null ;
3838 }
3939
4040 _getBatteryStatus ( ) {
41- const pct = this . _proxy . Percentage ;
41+ const pct = this . _proxy . Percentage . toFixed ( 0 ) ;
4242 const power = this . last_value . toFixed ( 1 ) ;
4343 const status = this . _read_file ( BAT_STATUS , '???' ) ;
4444
@@ -71,39 +71,40 @@ var TPIndicator = GObject.registerClass(
7171 const power = parseFloat ( this . _read_file ( POWER_NOW ) , 0 ) / 1000000 ;
7272 this . readings . push ( power )
7373
74- if ( this . readings . length >= HISTORY_DEPTH ) {
75- let avg = this . readings . reduce ( ( acc , elem ) => acc + elem , 0.0 ) / this . readings . length ;
76- this . last_value = avg ;
77-
78- while ( this . readings . length ) { this . readings . pop ( ) ; } ;
79- this . readings . push ( avg ) ;
80- }
81- return true ;
82- }
83-
84- _force_sync ( ) {
85- this . _sync ( ) ;
74+ const period_now = this . settings . get_double ( 'period-sec' ) ;
75+ if ( period_now . toFixed ( 1 ) != this . last_period_val . toFixed ( 1 ) ) {
76+ // period changed, re-spawn
77+ this . _spawn ( ) ;
78+ this . last_period_val = period_now ;
79+ } ;
80+
81+ const avg_of = this . settings . get_int ( 'avg-of' ) ;
82+ if ( this . readings . length >= avg_of ) {
83+ this . last_value = this . readings . reduce ( ( acc , elem ) => acc + elem , 0.0 ) / this . readings . length ; // simple mean
84+ this . _sync ( ) ; // update battery widget now!
85+ this . readings . length = 0 ; // fastest way to clear array?
86+ }
8687 return true ;
8788 }
8889
8990 _spawn ( ) {
91+ if ( this . tm_measure !== null ) {
92+ GLib . source_remove ( this . tm_measure ) ;
93+ }
9094 this . tm_measure = GLib . timeout_add (
9195 GLib . PRIORITY_DEFAULT ,
92- MEASURE_PERIOD ,
93- Lang . bind ( this , this . _measure ) ) ;
94- this . tm_force_sync = GLib . timeout_add (
95- GLib . PRIORITY_DEFAULT ,
96- FORCE_SYNC_PERIOD ,
97- Lang . bind ( this , this . _force_sync ) ) ;
96+ this . settings . get_double ( 'period-sec' ) * 1000 ,
97+ Lang . bind ( this , this . _measure )
98+ ) ;
9899 }
99100
100101 _stop ( ) {
101102 GLib . source_remove ( this . tm_measure ) ;
102- GLib . source_remove ( this . tm_force_sync ) ;
103103 }
104104 }
105105) ;
106106
107+
107108/** Extension
108109 */
109110
@@ -114,12 +115,12 @@ class TPWattMeter {
114115
115116 this . aggregateMenu = Panel . statusArea [ 'aggregateMenu' ] ;
116117 this . originalIndicator = this . aggregateMenu . _power ;
117- this . aggregateMenu . _indicators . replace_child ( this . originalIndicator . indicators , this . customIndicator . indicators ) ;
118+ this . aggregateMenu . _indicators . replace_child ( this . originalIndicator , this . customIndicator ) ;
118119 }
119120
120121 destroy ( arg ) {
121122 this . customIndicator . _stop ( ) ;
122- this . aggregateMenu . _indicators . replace_child ( this . customIndicator . indicators , this . originalIndicator . indicators ) ;
123+ this . aggregateMenu . _indicators . replace_child ( this . customIndicator , this . originalIndicator ) ;
123124 this . customIndicator = null ;
124125 }
125126}
@@ -132,7 +133,7 @@ let tp_wattmeter;
132133
133134
134135function enable ( ) {
135- tp_wattmeter = new TPWattMeter ( ) ; //tp_reader, tp_indicator);
136+ tp_wattmeter = new TPWattMeter ( ) ;
136137}
137138
138139function disable ( ) {
0 commit comments