Skip to content

Commit 1c53873

Browse files
committed
forked for my laptop
1 parent 8faaaf5 commit 1c53873

File tree

6 files changed

+44
-50
lines changed

6 files changed

+44
-50
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
*.zip
1+
*.swo
2+
*.swp
3+
*.zip

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
# Wattmeter
2-
Simple extension to show current power consumption in gnome.
1+
# TP Wattmeter
32

4-
Tested with Dell XPS 13 9360
3+
Forked https://bitbucket.org/blackBriar/wattmeter
54

5+
Simple extension to show current power consumption in Gnome.
6+
7+
Differs from original wattmeter -- it reads directly from `/sys/class/power_supply/BAT0/power_now`.
8+
9+
Tested with ThinkPad Carbon X1 Gen4

extension.js

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,39 @@ const Shell = imports.gi.Shell;
99

1010
const Clutter = imports.gi.Clutter
1111

12-
const VOLTAGE_NOW = "/sys/class/power_supply/BAT0/voltage_now";
13-
const CURRENT_NOW = "/sys/class/power_supply/BAT0/current_now";
14-
const STATUS = "/sys/class/power_supply/BAT0/status";
12+
const POWER_NOW = "/sys/class/power_supply/BAT0/power_now";
1513
const WINDOW_SIZE = 100;
1614

1715
let meta;
18-
let wattmeter;
16+
let tp_wattmeter;
1917
let label;
2018
let interval;
2119

22-
var WattMeter = class WattMeter extends PanelMenu.Button {
20+
var TPWattMeter = class TPWattMeter extends PanelMenu.Button {
2321
constructor(meta) {
2422
super();
2523
this.meta = meta;
2624
}
2725
_init() {
2826
super._init(St.Align.START);
2927
this.mainBox = null;
30-
this.buttonText = new St.Label({ text: _("(...)"), y_align: Clutter.ActorAlign.CENTER });
28+
this.buttonText = new St.Label({
29+
text: _("?W"),
30+
y_align: Clutter.ActorAlign.CENTER,
31+
style_class: 'tp_wattmeter_lbl',
32+
});
3133
this.actor.add_actor(this.buttonText);
3234
this.powerWindows = [];
33-
this.lastStatus = 'unknown';
35+
this.lastStatus = '?W';
3436
}
3537

3638
_measure() {
37-
this.lastStatus = getStatus().trim();
38-
if (this.lastStatus !== 'Discharging') {
39+
const power = getPower();
40+
if (power < 0) {
3941
this.powerWindows = [];
4042
return true;
4143
}
42-
const current = getCurrent();
43-
const voltage = getVoltage();
44-
if (current < 0 || voltage < 0) {
45-
this.powerWindows = [];
46-
return true;
47-
}
48-
const power = current * voltage;
44+
4945
this.powerWindows.push(power);
5046
if (this.powerWindows.length >= WINDOW_SIZE) {
5147
this.powerWindows.shift();
@@ -60,17 +56,17 @@ var WattMeter = class WattMeter extends PanelMenu.Button {
6056
power_text = this.lastStatus != null ? this.lastStatus : 'N/A';
6157
} else {
6258
let avg = this.powerWindows.reduce((acc, elem) => acc + elem, 0.0) / this.powerWindows.length;
63-
power_text = avg.toFixed(2) + ' W'
59+
power_text = avg.toFixed(2) + 'W'
6460
}
6561

6662
temp.set_text(power_text);
6763
return true;
6864
}
6965

7066
_enable() {
71-
this.measure = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 50,
67+
this.measure = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 250,
7268
Lang.bind(this, this._measure));
73-
this.interval = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000,
69+
this.interval = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 5000,
7470
Lang.bind(this, this._refresh));
7571
}
7672

@@ -84,24 +80,15 @@ const Config = imports.misc.config;
8480
let shellMinorVersion = parseInt(Config.PACKAGE_VERSION.split('.')[1]);
8581

8682
if (shellMinorVersion > 30) {
87-
WattMeter = GObject.registerClass(
88-
{ GTypeName: 'WattMeter' },
89-
WattMeter
83+
TPWattMeter = GObject.registerClass(
84+
{ GTypeName: 'TPWattMeter' },
85+
TPWattMeter
9086
);
9187
}
9288

93-
function getStatus() {
94-
return readFileSafely(STATUS, "Unknown");
95-
}
96-
97-
function getVoltage() {
98-
const voltage = parseFloat(readFileSafely(VOLTAGE_NOW, -1));
99-
return voltage === -1 ? voltage : voltage / 1000000;
100-
}
101-
102-
function getCurrent() {
103-
const current = parseFloat(readFileSafely(CURRENT_NOW, -1));
104-
return current === -1 ? current : current / 1000000;
89+
function getPower() {
90+
const power = parseFloat(readFileSafely(POWER_NOW), -1);
91+
return power === -1 ? power : power / 1000000;
10592
}
10693

10794
function readFileSafely(filePath, defaultValue) {
@@ -128,13 +115,13 @@ function init(metadata) {
128115
}
129116

130117
function enable() {
131-
wattmeter = new WattMeter(meta);
132-
wattmeter._enable();
133-
Main.panel.addToStatusArea('wattmeter', wattmeter);
118+
tp_wattmeter = new TPWattMeter(meta);
119+
tp_wattmeter._enable();
120+
Main.panel.addToStatusArea('tp_wattmeter', tp_wattmeter, -1); //, 'right');
134121
}
135122

136123
function disable() {
137-
wattmeter._disable();
138-
wattmeter.destroy();
139-
wattmeter = null;
124+
tp_wattmeter._disable();
125+
tp_wattmeter.destroy();
126+
tp_wattmeter = null;
140127
}

metadata.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "wattmeter",
3-
"uuid": "wattmeter@bb",
4-
"url": "https://bitbucket.org/blackBriar/wattmeter",
5-
"description": "Shows battery power consumption",
2+
"name": "tp_wattmeter",
3+
"uuid": "tp_wattmeter@gistart",
4+
"url": "https://github.com/gistart/tp_wattmeter",
5+
"description": "Shows battery power consumption of ThinkPad laptops",
66
"shell-version": [
77
"3.30.0"
88
]
9-
}
9+
}

pack-extension.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22

3-
zip -j wattmeter.zip extension.js metadata.json
3+
zip -j tp_wattmeter.zip extension.js metadata.json stylesheet.css

stylesheet.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#panel .tp_wattmeter_lbl { font-size: 0.8em; }

0 commit comments

Comments
 (0)