diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 1536585a..f17dded9 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -21,6 +21,7 @@ "brick.Button.pauseUntil": "Waits until the event is raised", "brick.Button.pauseUntil|param|ev": "the event to wait for", "brick.Button.wasPressed": "See if the button was pressed again since the last time you checked.", + "brick.batteryLevel": "Returns the current battery level", "brick.buttonDown": "Down button on the EV3 Brick.", "brick.buttonEnter": "Enter button on the EV3 Brick.", "brick.buttonLeft": "Left button on the EV3 Brick.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 84c9a753..05166172 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -28,6 +28,7 @@ "brick.Button.onEvent|block": "on %button|%event", "brick.Button.pauseUntil|block": "pause until %button|%event", "brick.Button.wasPressed|block": "%button|was pressed", + "brick.batteryLevel|block": "battery level", "brick.buttonDown|block": "down", "brick.buttonEnter|block": "enter", "brick.buttonLeft|block": "left", @@ -89,6 +90,7 @@ "{id:group}Buttons": "Buttons", "{id:group}Chassis": "Chassis", "{id:group}Light": "Light", + "{id:group}More": "More", "{id:group}Motion": "Motion", "{id:group}Screen": "Screen", "{id:group}Sensors": "Sensors" diff --git a/libs/core/battery.ts b/libs/core/battery.ts new file mode 100644 index 00000000..66d478b2 --- /dev/null +++ b/libs/core/battery.ts @@ -0,0 +1,12 @@ + +namespace brick { + /** + * Returns the current battery level + */ + //% blockId=brickBatteryLevel block="battery level" + //% group="More" + export function batteryLevel(): number { + const info = sensors.internal.getBatteryInfo(); + return info.current; + } +} \ No newline at end of file diff --git a/libs/core/input.ts b/libs/core/input.ts index 894c8602..88931bc3 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -89,6 +89,14 @@ namespace sensors.internal { //serial.writeLine("UART " + port + " / " + mode + " - " + info) } + export function getBatteryInfo(): { temp: number; current: number } { + init(); + return { + temp: analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.BatteryTemp), + current: analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.BatteryCurrent) + } + } + function detectDevices() { let conns = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS) let numChanged = 0 diff --git a/libs/core/pxt.json b/libs/core/pxt.json index 30ec9d16..d4260e94 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -16,6 +16,7 @@ "png.cpp", "screen.cpp", "screen.ts", + "battery.ts", "output.cpp", "output.ts", "core.ts", diff --git a/sim/state/analog.ts b/sim/state/analog.ts index f39cb865..9f9ea664 100644 --- a/sim/state/analog.ts +++ b/sim/state/analog.ts @@ -24,13 +24,14 @@ namespace pxsim { } export class EV3AnalogState { - constructor() { let data = new Uint8Array(5172) MMapMethods.register("/dev/lms_analog", { data, beforeMemRead: () => { //console.log("analog before read"); + data[AnalogOff.BatteryTemp] = 21; // TODO simulate this + data[AnalogOff.BatteryCurrent] = 100; // TODO simulate this const inputNodes = ev3board().getInputNodes(); for (let port = 0; port < DAL.NUM_INPUTS; port++) { const node = inputNodes[port];