From fc82d3b13bc49c49de145802731f2b44d5293ef4 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 20 Apr 2023 13:06:29 +0100 Subject: [PATCH] Update typescript & remove "use strict"s --- apps/clkinfostopw/clkinfo.ts | 4 +- apps/clkinfostopw/settings.ts | 4 +- apps/drained/app.js | 1 - apps/drained/boot.js | 1 - apps/drained/settings.js | 1 - apps/drained/settings.ts | 4 +- typescript/types/funcs.d.ts | 2 + typescript/types/main.d.ts | 120 +++++++++++++++++++++++++--------- 8 files changed, 97 insertions(+), 40 deletions(-) create mode 100644 typescript/types/funcs.d.ts diff --git a/apps/clkinfostopw/clkinfo.ts b/apps/clkinfostopw/clkinfo.ts index 6051e106e..e5361644d 100644 --- a/apps/clkinfostopw/clkinfo.ts +++ b/apps/clkinfostopw/clkinfo.ts @@ -1,4 +1,4 @@ -((): ClockInfo.Menu => { +(() => { let durationOnPause = "---"; let redrawInterval: number | undefined; let startTime: number | undefined; @@ -74,4 +74,4 @@ } ] }; -}) +}) satisfies ClockInfoFunc diff --git a/apps/clkinfostopw/settings.ts b/apps/clkinfostopw/settings.ts index 3d7ff5c05..a9a35f245 100644 --- a/apps/clkinfostopw/settings.ts +++ b/apps/clkinfostopw/settings.ts @@ -6,7 +6,7 @@ type StopWatchSettings = { format: StopWatchFormat, }; -((back: () => void) => { +(back => { const SETTINGS_FILE = "clkinfostopw.setting.json"; const storage = require("Storage"); @@ -31,4 +31,4 @@ type StopWatchSettings = { }, }, }); -}) +}) satisfies SettingsFunc diff --git a/apps/drained/app.js b/apps/drained/app.js index 000727e19..f9ae04605 100644 --- a/apps/drained/app.js +++ b/apps/drained/app.js @@ -1,4 +1,3 @@ -"use strict"; var app = "drained"; if (typeof drainedInterval !== "undefined") drainedInterval = clearInterval(drainedInterval); diff --git a/apps/drained/boot.js b/apps/drained/boot.js index 639b39ef3..97f405123 100644 --- a/apps/drained/boot.js +++ b/apps/drained/boot.js @@ -1,4 +1,3 @@ -"use strict"; { var _a = require("Storage").readJSON("drained.setting.json", true) || {}, _b = _a.battery, threshold_1 = _b === void 0 ? 5 : _b, _c = _a.interval, interval = _c === void 0 ? 10 : _c, _d = _a.disableBoot, disableBoot_1 = _d === void 0 ? false : _d; drainedInterval = setInterval(function () { diff --git a/apps/drained/settings.js b/apps/drained/settings.js index 310570455..fe4ab9c4b 100644 --- a/apps/drained/settings.js +++ b/apps/drained/settings.js @@ -1,4 +1,3 @@ -"use strict"; (function (back) { var _a, _b, _c, _d; var SETTINGS_FILE = "drained.setting.json"; diff --git a/apps/drained/settings.ts b/apps/drained/settings.ts index 5b7523fc9..c79e6605c 100644 --- a/apps/drained/settings.ts +++ b/apps/drained/settings.ts @@ -5,7 +5,7 @@ type DrainedSettings = { disableBoot?: ShortBoolean, }; -((back: () => void) => { +(back => { const SETTINGS_FILE = "drained.setting.json"; const storage = require("Storage") @@ -64,4 +64,4 @@ type DrainedSettings = { }, }, }); -}) +}) satisfies SettingsFunc diff --git a/typescript/types/funcs.d.ts b/typescript/types/funcs.d.ts new file mode 100644 index 000000000..81c4646ea --- /dev/null +++ b/typescript/types/funcs.d.ts @@ -0,0 +1,2 @@ +type SettingsFunc = (back: () => void) => void; +type ClockInfoFunc = () => ClockInfo.Menu; diff --git a/typescript/types/main.d.ts b/typescript/types/main.d.ts index 806725535..29118b7c6 100644 --- a/typescript/types/main.d.ts +++ b/typescript/types/main.d.ts @@ -304,6 +304,12 @@ type VariableSizeInformation = { more?: VariableSizeInformation; }; +type PipeOptions = { + chunkSize?: number, + end?: boolean, + complete?: () => void, +}; + // CLASSES @@ -802,6 +808,29 @@ declare class NRF { */ static on(event: "security", callback: (status: any) => void): void; + /** + * Called when Bluetooth advertising starts or stops on Espruino + * @param {string} event - The event to listen to. + * @param {(isAdvertising: boolean) => void} callback - A function that is executed when the event occurs. Its arguments are: + * * `isAdvertising` Whether we are advertising or not + * @url http://www.espruino.com/Reference#l_NRF_advertising + */ + static on(event: "advertising", callback: (isAdvertising: boolean) => void): void; + + /** + * Called during the bonding process to update on status + * `status` is one of: + * * `"request"` - Bonding has been requested in code via `NRF.startBonding` + * * `"start"` - The bonding procedure has started + * * `"success"` - The bonding procedure has succeeded (`NRF.startBonding`'s promise resolves) + * * `"fail"` - The bonding procedure has failed (`NRF.startBonding`'s promise rejects) + * @param {string} event - The event to listen to. + * @param {(status: any) => void} callback - A function that is executed when the event occurs. Its arguments are: + * * `status` One of `'request'/'start'/'success'/'fail'` + * @url http://www.espruino.com/Reference#l_NRF_bond + */ + static on(event: "bond", callback: (status: any) => void): void; + /** * Called with a single byte value when Espruino is set up as a HID device and the * computer it is connected to sends a HID report back to Espruino. This is usually @@ -993,15 +1022,17 @@ declare class NRF { * `options` is an object, which can contain: * ``` * { - * name: "Hello" // The name of the device - * showName: true/false // include full name, or nothing - * discoverable: true/false // general discoverable, or limited - default is limited - * connectable: true/false // whether device is connectable - default is true - * scannable : true/false // whether device can be scanned for scan response packets - default is true - * interval: 600 // Advertising interval in msec, between 20 and 10000 (default is 375ms) - * manufacturer: 0x0590 // IF sending manufacturer data, this is the manufacturer ID - * manufacturerData: [...] // IF sending manufacturer data, this is an array of data - * phy: "1mbps/2mbps/coded" // (NRF52840 only) use the long-range coded phy for transmission (1mbps default) + * name: "Hello" // The name of the device + * showName: true/false // include full name, or nothing + * discoverable: true/false // general discoverable, or limited - default is limited + * connectable: true/false // whether device is connectable - default is true + * scannable : true/false // whether device can be scanned for scan response packets - default is true + * whenConnected : true/false // keep advertising when connected (nRF52 only) + * // switches to advertising as non-connectable when it is connected + * interval: 600 // Advertising interval in msec, between 20 and 10000 (default is 375ms) + * manufacturer: 0x0590 // IF sending manufacturer data, this is the manufacturer ID + * manufacturerData: [...] // IF sending manufacturer data, this is an array of data + * phy: "1mbps/2mbps/coded" // (NRF52840 only) use the long-range coded phy for transmission (1mbps default) * } * ``` * Setting `connectable` and `scannable` to false gives the lowest power @@ -2282,7 +2313,7 @@ declare class Socket { * end : call the 'end' function on the destination when the source is finished * @url http://www.espruino.com/Reference#l_Socket_pipe */ - pipe(destination: any, options?: any): void; + pipe(destination: any, options?: PipeOptions): void /** * This function writes the `data` argument as a string. Data that is passed in @@ -2477,7 +2508,7 @@ declare class httpSRq { * end : call the 'end' function on the destination when the source is finished * @url http://www.espruino.com/Reference#l_httpSRq_pipe */ - pipe(destination: any, options?: any): void; + pipe(dest: any, options?: PipeOptions): void } /** @@ -2700,7 +2731,7 @@ declare class httpCRs { * end : call the 'end' function on the destination when the source is finished * @url http://www.espruino.com/Reference#l_httpCRs_pipe */ - pipe(destination: any, options?: any): void; + pipe(destination: any, options?: PipeOptions): void } /** @@ -2914,20 +2945,11 @@ declare class Microbit { } -/** - * This is the File object - it allows you to stream data to and from files (As - * opposed to the `require('fs').readFile(..)` style functions that read an entire - * file). - * To create a File object, you must type ```var fd = - * E.openFile('filepath','mode')``` - see [E.openFile](#l_E_openFile) for more - * information. - * **Note:** If you want to remove an SD card after you have started using it, you - * *must* call `E.unmountSD()` or you may cause damage to the card. - * @url http://www.espruino.com/Reference#File - */ -declare class File { +interface FileConstructor { +} +interface File { /** * Close an open file. * @url http://www.espruino.com/Reference#l_File_close @@ -2984,9 +3006,22 @@ declare class File { * end : call the 'end' function on the destination when the source is finished * @url http://www.espruino.com/Reference#l_File_pipe */ - pipe(destination: any, options?: any): void; + pipe(destination: any, options?: PipeOptions): void } +/** + * This is the File object - it allows you to stream data to and from files (As + * opposed to the `require('fs').readFile(..)` style functions that read an entire + * file). + * To create a File object, you must type ```var fd = + * E.openFile('filepath','mode')``` - see [E.openFile](#l_E_openFile) for more + * information. + * **Note:** If you want to remove an SD card after you have started using it, you + * *must* call `E.unmountSD()` or you may cause damage to the card. + * @url http://www.espruino.com/Reference#File + */ +declare const File: FileConstructor + /** * Class containing [Puck.js's](http://www.puck-js.com) utility functions. * @url http://www.espruino.com/Reference#Puck @@ -3527,7 +3562,7 @@ declare class Bangle { * @param {string} event - The event to listen to. * @param {(button: number, xy: any) => void} callback - A function that is executed when the event occurs. Its arguments are: * * `button` `1` for left, `2` for right - * * `xy` Object of form `{x,y}` containing touch coordinates (if the device supports full touch). Clipped to 0..175 (LCD pixel coordinates) on firmware 2v13 and later. + * * `xy` Object of form `{x,y,type}` containing touch coordinates (if the device supports full touch). Clipped to 0..175 (LCD pixel coordinates) on firmware 2v13 and later.`type` is only available on Bangle.js 2 and is an integer, either 0 for swift touches or 2 for longer ones. * @url http://www.espruino.com/Reference#l_Bangle_touch */ static on(event: "touch", callback: TouchCallback): void; @@ -7984,7 +8019,7 @@ declare class E { * end : call the 'end' function on the destination when the source is finished * @url http://www.espruino.com/Reference#l_E_pipe */ - static pipe(source: any, destination: any, options?: { chunkSize?: number, end?: boolean, complete?: () => void }): void + static pipe(source: any, destination: any, options?: PipeOptions): void /** * Create an ArrayBuffer from the given string. This is done via a reference, not a @@ -8621,6 +8656,12 @@ declare class E { /** * This class provides a software-defined OneWire master. It is designed to be * similar to Arduino's OneWire library. + * **Note:** OneWire commands are very timing-sensitive, and on nRF52 devices + * (Bluetooth LE Espruino boards) the bluetooth stack can get in the way. Before + * version 2v18 of Espruino OneWire could be unreliable, but as of firmware 2v18 + * Espruino now schedules OneWire accesses with the bluetooth stack to ensure it doesn't interfere. + * OneWire is now reliable but some functions such as `OneWire.search` can now take + * a while to execute (around 1 second). * @url http://www.espruino.com/Reference#OneWire */ declare class OneWire { @@ -9680,6 +9721,19 @@ declare class StorageFile { * @url http://www.espruino.com/Reference#l_StorageFile_erase */ erase(): void; + + /** + * Pipe this file to a stream (an object with a 'write' method) + * + * @param {any} destination - The destination file/stream that will receive content from the source. + * @param {any} [options] + * [optional] An object `{ chunkSize : int=32, end : bool=true, complete : function }` + * chunkSize : The amount of data to pipe from source to destination at a time + * complete : a function to call when the pipe activity is complete + * end : call the 'end' function on the destination when the source is finished + * @url http://www.espruino.com/Reference#l_StorageFile_pipe + */ + pipe(destination: any, options?: PipeOptions): void } interface processConstructor { @@ -10014,7 +10068,7 @@ declare class Serial { * end : call the 'end' function on the destination when the source is finished * @url http://www.espruino.com/Reference#l_Serial_pipe */ - pipe(destination: any, options?: any): void; + pipe(destination: any, options?: PipeOptions): void } interface StringConstructor { @@ -12004,6 +12058,10 @@ declare module "neopixel" { * white). These are still supported but the array of data supplied must still be a * multiple of 3 bytes long. Just round the size up - it won't cause any problems. * * On some platforms like STM32, pins capable of hardware SPI MOSI are required. + * * On STM32, `neopixel.write` chooses a hardware SPI device to output the signal on + * and uses that. However in order to avoid spikes in the output, if that hardware device is *already + * initialised* it will not be re-initialised. This means that if the SPI device was already in use, + * you may have to use `SPIx.setup({baud:3200000, mosi:the_pin})` to force it to be re-setup on the pin. * * Espruino devices tend to have 3.3v IO, while WS2812/etc run off of 5v. Many * WS2812 will only register a logic '1' at 70% of their input voltage - so if * powering them off 5v you will not be able to send them data reliably. You can @@ -12928,7 +12986,7 @@ declare module "fs" { * end : call the 'end' function on the destination when the source is finished * @url http://www.espruino.com/Reference#l_fs_pipe */ - function pipe(source: any, destination: any, options?: any): void; + function pipe(destination: any, options?: PipeOptions): void } /** @@ -13285,7 +13343,7 @@ declare module "Storage" { /** * The Flash Storage system is journaling. To make the most of the limited write - * cycles of Flash memory, Espruino marks deleted/replaced files as garbage and + * cycles of Flash memory, Espruino marks deleted/replaced files as garbage/trash files and * moves on to a fresh part of flash memory. Espruino only fully erases those files * when it is running low on flash, or when `compact` is called. * `compact` may fail if there isn't enough RAM free on the stack to use as swap @@ -13324,7 +13382,7 @@ declare module "Storage" { * fileBytes // How many bytes of allocated files do we have? * fileCount // How many allocated files do we have? * trashBytes // How many bytes of trash files do we have? - * trashCount // How many trash files do we have? + * trashCount // How many trash files do we have? (can be cleared with .compact) * } * ``` * @returns {any} An object containing info about the current Storage system