nRF52: Add E.getBattery as a more global battery percentage function, deprecate Puck.getBatteryPercentage

This commit is contained in:
Gordon Williams 2018-04-17 12:47:49 +01:00
parent 7080fd2b15
commit 2eb2c519bd
8 changed files with 42 additions and 21 deletions

View File

@ -23,6 +23,7 @@
setWatch(..., {edge:"rising",debounce:25}) is now default for built-in buttons
Pixl.js: add Pixl.menu function for easy menus, build in graphical_menu.js
Fix regression in MDBT42Q advertised name
nRF52: Add E.getBattery as a more global battery percentage function, deprecate `Puck.getBatteryPercentage`
1v96 : ESP8266: no callback if SSID is not available (fix #1297)
ESP8266: esp8266 wifi getStatus doesn't show savedMode (fix #752)

View File

@ -44,19 +44,14 @@ Class containing utility functions for [Pixl.js](http://www.espruino.com/Pixl.js
"type" : "staticmethod",
"class" : "Pixl",
"name" : "getBatteryPercentage",
"generate" : "jswrap_pixljs_getBatteryPercentage",
"generate" : "jswrap_espruino_getBattery",
"return" : ["int", "A percentage between 0 and 100" ]
}
DEPRECATED - Please use `E.getBattery()` instead.
Return an approximate battery percentage remaining based on
a normal CR2032 battery (2.8 - 2.2v)
*/
int jswrap_pixljs_getBatteryPercentage() {
JsVarFloat v = jswrap_nrf_bluetooth_getBattery();
int pc = (v-2.2)*100/0.6;
if (pc>100) pc=100;
if (pc<0) pc=0;
return pc;
}
/*JSON{
"type" : "staticmethod",

View File

@ -13,7 +13,6 @@
*/
#include "jspin.h"
int jswrap_pixljs_getBatteryPercentage();
JsVar *jswrap_pixljs_menu(JsVar *menu);
void jswrap_pixljs_lcdw(JsVarInt c);
void jswrap_pixljs_setContrast(JsVarFloat c);

View File

@ -537,21 +537,14 @@ JsVarFloat jswrap_puck_light() {
"type" : "staticmethod",
"class" : "Puck",
"name" : "getBatteryPercentage",
"generate" : "jswrap_puck_getBatteryPercentage",
"generate" : "jswrap_espruino_getBattery",
"return" : ["int", "A percentage between 0 and 100" ]
}
DEPRECATED - Please use `E.getBattery()` instead.
Return an approximate battery percentage remaining based on
a normal CR2032 battery (2.8 - 2.2v)
a normal CR2032 battery (2.8 - 2.2v).
*/
int jswrap_puck_getBatteryPercentage() {
JsVarFloat v = jswrap_nrf_bluetooth_getBattery();
int pc = (v-2.2)*100/0.6;
if (pc>100) pc=100;
if (pc<0) pc=0;
return pc;
}
static bool selftest_check_pin(Pin pin) {

View File

@ -20,7 +20,6 @@ JsVarInt jswrap_puck_magTemp();
void jswrap_puck_IR(JsVar *data, Pin cathode, Pin anode);
int jswrap_puck_capSense(Pin tx, Pin rx);
JsVarFloat jswrap_puck_light();
int jswrap_puck_getBatteryPercentage();
bool jswrap_puck_selfTest();
void jswrap_puck_init();

View File

@ -182,6 +182,9 @@ def get_jsondata(is_for_document, parseArgs = True, board = False):
if ("#ifdef" in jsondata) or ("#ifndef" in jsondata):
sys.stderr.write( "'#ifdef' where 'ifdef' should be used in " + jsonstring + " - "+str(sys.exc_info()[0]) + "\n" )
exit(1)
if ("if" in jsondata):
sys.stderr.write( "'if' where '#if' should be used in " + jsonstring + " - "+str(sys.exc_info()[0]) + "\n" )
exit(1)
if ("#if" in jsondata):
expr = jsondata["#if"]
for defn in defines:

View File

@ -1454,3 +1454,32 @@ bool jswrap_espruino_sendUSBHID(JsVar *arr) {
}
#endif
/*JSON{
"type" : "staticmethod",
"#if" : "defined(PUCKJS) || defined(PIXLJS)",
"class" : "E",
"name" : "getBattery",
"generate" : "jswrap_espruino_getBattery",
"return" : ["int","A percentage between 0 and 100"]
}
In devices that come with batteries, this function returns
the battery charge percentage as an integer between 0 and 100.
**Note:** this is an estimation only, based on battery voltage.
The temperature of the battery (as well as the load being drawn
from it at the time `E.getBattery` is called) will affect the
readings.
*/
JsVarInt jswrap_espruino_getBattery() {
#if defined(PUCKJS) || defined(PIXLJS)
JsVarFloat v = jshReadVRef();
int pc = (v-2.2)*100/0.6;
if (pc>100) pc=100;
if (pc<0) pc=0;
return pc;
#else
return 0;
#endif
}

View File

@ -54,3 +54,5 @@ void jswrap_espruino_asm(JsVar *callspec, JsVar *args);
void jswrap_espruino_setUSBHID(JsVar *arr);
bool jswrap_espruino_sendUSBHID(JsVar *arr);
JsVarInt jswrap_espruino_getBattery();