-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Create Smart Battery Widget #3947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3feaf83
Create widget.js
RKBoss6 4f9aa34
Create README.md
RKBoss6 2da0831
Create metadata.json
RKBoss6 925c71a
Merge branch 'espruino:master' into Smart-Battery
RKBoss6 faa6257
Update metadata.json
RKBoss6 725d160
Update widget.js
RKBoss6 7d44e20
Add smartbatt module specifications, and links to it
RKBoss6 fbb8500
Create ChangeLog
RKBoss6 29be5f5
add icon
RKBoss6 86e0bef
Update metadata.json
RKBoss6 bddd0f7
Update widget.js
RKBoss6 c09e29b
Define widget first
RKBoss6 45df9b3
Update widget.js
RKBoss6 c06255d
Update widget.js
RKBoss6 328cc50
Fix filename
RKBoss6 751af4a
Fix no entry point for widget
RKBoss6 79dcbd7
Remove outdated update changes
RKBoss6 0875f51
Update widget.js
RKBoss6 274b518
Fix define data error
RKBoss6 ffc8d20
Fix draw define first
RKBoss6 0fbcea8
Remove unused var
RKBoss6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v0.01: New app! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Smart Battery Widget | ||
Shows battery in terms of days (21 days, 12 hours), and uses the [`smartbatt`](https://banglejs.com/apps/?id=smartbatt) module to learn from daily battery drainage and provide accurate estimations. | ||
This app was modified from `wid_a_battery_widget`, by @alainsaas | ||
|
||
When you install this widget for the first time, or clear the data, it will also install the [`smartbatt`](https://banglejs.com/apps/?id=smartbatt) module as a dependency. As it learns your battery usage for the first time the forecast will fluctate, and will not be reliable for a while. As it compunds many drainage values together, it will keep learning, and provide better predictions. | ||
The module learns by averaging all the battery drainage over a period of time, and saves it to a json, averaging it with many others, providing an accurate prediction. The module gives the best forecast when you use the watch relatively similar per day. | ||
|
||
Tap on the widget to show the battery percentage. It will go back to the days left after 3 seconds. | ||
|
||
When charging, only the percentage is shown. | ||
## Creator | ||
RKBoss6 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"id": "widsmartbatt", | ||
"name": "Smart Battery Widget", | ||
"shortName":"Smart Batt Wid", | ||
"icon": "icon.png", | ||
"version":"0.01", | ||
"type": "widget", | ||
"supports": ["BANGLEJS", "BANGLEJS2"], | ||
"readme": "README.md", | ||
"dependencies" : { "smartbatt":"module" }, | ||
"description": "Simple and slim battery widget that shows days remaining, and uses the `smartbatt` module to learm from your usage and provide accurate predictions.", | ||
"tags": "widget,battery", | ||
"provides_widgets" : ["battery"], | ||
"storage": [ | ||
{"name":"widsmartbatt.wid.js","url":"widget.js"} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
(function(){ | ||
|
||
var showPercent = false; | ||
const width = 40; | ||
const height = 24; | ||
|
||
let COLORS = { | ||
'bg': g.theme.bg, | ||
'fg': g.theme.fg, | ||
'charging': "#08f", | ||
'high': g.theme.dark ? "#fff" : "#000", | ||
'low': "#f00", | ||
}; | ||
|
||
const levelColor = (l) => { | ||
if (Bangle.isCharging()) return COLORS.charging; | ||
if (l >= 30) return COLORS.high; | ||
return COLORS.low; | ||
}; | ||
|
||
function draw() { | ||
let batt=E.getBattery(); | ||
let data = require("smartbatt").get(); | ||
let hrsLeft=data.hrsLeft; | ||
let days = hrsLeft / 24; | ||
|
||
let txt = showPercent | ||
? batt | ||
: (days >= 1 | ||
? Math.round(Math.min(days, 99)) + "d" | ||
: Math.round(hrsLeft) + "h"); | ||
if(Bangle.isCharging()) txt=E.getBattery(); | ||
let s = 29; | ||
let x = this.x, y = this.y; | ||
let xl = x + 4 + batt * (s - 12) / 100; | ||
|
||
// Drawing code follows... | ||
g.setColor(COLORS.bg); | ||
g.fillRect(x + 2, y + 5, x + s - 6, y + 18); | ||
|
||
g.setColor(levelColor(batt)); | ||
g.fillRect(x + 1, y + 3, x + s - 5, y + 4); | ||
g.fillRect(x + 1, y + 19, x + s - 5, y + 20); | ||
g.fillRect(x, y + 4, x + 1, y + 19); | ||
g.fillRect(x + s - 5, y + 4, x + s - 4, y + 19); | ||
g.fillRect(x + s - 3, y + 8, x + s - 2, y + 16); | ||
g.fillRect(x + 4, y + 15, xl, y + 16); | ||
|
||
g.setColor(COLORS.fg); | ||
g.setFontAlign(0, 0); | ||
g.setFont('6x8'); | ||
g.drawString(txt, x + 14, y + 10); | ||
|
||
|
||
} | ||
WIDGETS["widsmartbatt"] = { | ||
area: "tr", | ||
width: 30, | ||
draw: draw | ||
}; | ||
|
||
// Touch to temporarily show battery percent | ||
Bangle.on("touch", function (_btn, xy) { | ||
if (WIDGETS["back"] || !xy) return; | ||
|
||
var oversize = 5; | ||
var w = WIDGETS["widsmartbatt"]; | ||
var x = xy.x, y = xy.y; | ||
|
||
if (w.x - oversize <= x && x < w.x + width + oversize | ||
&& w.y - oversize <= y && y < w.y + height + oversize) { | ||
E.stopEventPropagation && E.stopEventPropagation(); | ||
showPercent = true; | ||
setTimeout(() => { | ||
showPercent = false; | ||
w.draw(w); | ||
}, 3000); | ||
w.draw(w); | ||
} | ||
}); | ||
|
||
// Update widget on charging state change | ||
Bangle.on('charging', function () { | ||
WIDGETS["widsmartbatt"].draw(); | ||
}); | ||
|
||
setInterval(() => WIDGETS["widsmartbatt"].draw(), 60000); | ||
|
||
|
||
})(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.