diff --git a/ntag215.js b/ntag215.js index f4c9dfa..0a97a0e 100644 --- a/ntag215.js +++ b/ntag215.js @@ -24,6 +24,11 @@ const POWER_OFF_TIME = 5000; */ const ENABLE_LOG = false; +/** + * Target console device that is set when calling {@link fastMode}. + */ +const FAST_MODE_CONSOLE = null; + /** * The name of the script. */ @@ -508,8 +513,8 @@ function initialize() { * The string indicated by {@link FAST_MODE_STRING} will be sent when {@link fastRx} is ready to start receiving data. */ function fastMode() { - // Move the console to Serial1 so it doesn't do anything over Bluetooth. - Serial1.setConsole(); + // Move the console to the specified device so it doesn't do anything over Bluetooth. + E.setConsole(FAST_MODE_CONSOLE); // Attach fastRx to the bluetooth data received event. _Bluetooth.on(_Data, fastRx); @@ -534,6 +539,9 @@ function onFastModeDisconnect() { // Remove event listener NRF.removeListener(_Disconnect, onFastModeDisconnect); + + // Restore Bluetooth console + E.setConsole("Bluetooth"); } /** @@ -679,7 +687,7 @@ function fastRx(data) { }); _Bluetooth.write(data); - }, 0) + }, 0); return; diff --git a/puck-ntag215-manager/src/espruino.ts b/puck-ntag215-manager/src/espruino.ts index 93c456e..dd70b5d 100644 --- a/puck-ntag215-manager/src/espruino.ts +++ b/puck-ntag215-manager/src/espruino.ts @@ -73,6 +73,7 @@ export interface SemVer { export interface GetCodeOptions { saveToFlash?: boolean, + enableDebug?: boolean, board?: string, enableLed1?: boolean, enableLed2?: boolean, @@ -142,41 +143,48 @@ export async function checkLed(ledNumber: number): Promise { export function getCode(options: GetCodeOptions = {}): Promise { return new Promise((resolve, reject) => { const { - saveToFlash = false, board = undefined, enableLed1, enableLed2, enableLed3 + saveToFlash = false, enableDebug = false, board = undefined, enableLed1, enableLed2, enableLed3 } = options let code = $("#code").text() as string code = code.replace( /(const SAVE_TO_FLASH = )(true|false);/, - `$1${saveToFlash};`) - - if (board) { - code = code.replace( - /(const BOARD = )(process\.env\.BOARD);/, - `$1${JSON.stringify(board)};` - ) - } - - if (enableLed1 != null) { - code = code.replace( - /(const ENABLE_LED1 = )(this\.LED1 != null);/, - `$1${enableLed1};` - ) - } - - if (enableLed2 != null) { - code = code.replace( - /(const ENABLE_LED2 = )(this\.LED2 != null);/, - `$1${enableLed2};` - ) - } - - if (enableLed3 != null) { - code = code.replace( - /(const ENABLE_LED3 = )(this\.LED3 != null);/, - `$1${enableLed3};` - ) - } + `$1${saveToFlash};` + ).replace( + /(const ENABLE_LOG = )(true|false);/, + `$1${enableDebug};` + ).replace( + /(const FAST_MODE_CONSOLE = )([^;]+);/, + `$1${enableDebug ? '"Serial1"' : null};` + ); + + if (board) { + code = code.replace( + /(const BOARD = )(process\.env\.BOARD);/, + `$1${JSON.stringify(board)};` + ) + } + + if (enableLed1 != null) { + code = code.replace( + /(const ENABLE_LED1 = )(this\.LED1 != null);/, + `$1${enableLed1};` + ) + } + + if (enableLed2 != null) { + code = code.replace( + /(const ENABLE_LED2 = )(this\.LED2 != null);/, + `$1${enableLed2};` + ) + } + + if (enableLed3 != null) { + code = code.replace( + /(const ENABLE_LED3 = )(this\.LED3 != null);/, + `$1${enableLed3};` + ) + } Espruino.callProcessor("transformForEspruino", code, resolve) }) diff --git a/puck-ntag215-manager/src/index.ts b/puck-ntag215-manager/src/index.ts index 47d47f0..20c8b73 100644 --- a/puck-ntag215-manager/src/index.ts +++ b/puck-ntag215-manager/src/index.ts @@ -358,6 +358,15 @@ $(() => { preventClose: true }) + const debugModalResult = await showModal({ + title: "Enable Debug Mode?", + message: modalMessages(ModalMessageType.DebugMode), + htmlEscapeBody: false, + buttons: ModalButtonTypes.YesNo, + dialog: true, + preventClose: true + }) + await showModal({ title: "Please Wait", message: "Uploading script file, please wait.", @@ -366,6 +375,7 @@ $(() => { await EspruinoHelper.writeCode({ saveToFlash: modalResult === ModalResult.ButtonYes, + enableDebug: debugModalResult === ModalResult.ButtonYes, board, enableLed1, enableLed2, diff --git a/puck-ntag215-manager/src/modalMessages.ts b/puck-ntag215-manager/src/modalMessages.ts index 18d2fa6..4396fe2 100644 --- a/puck-ntag215-manager/src/modalMessages.ts +++ b/puck-ntag215-manager/src/modalMessages.ts @@ -2,6 +2,7 @@ const template = require("./templates/modal-messages.pug") export enum ModalMessageType { SaveToFlash = "save-to-flash", + DebugMode = "debug-mode", DfuInstructions = "dfu-instructions", FirmwareUpdate = "firmware-update" } diff --git a/puck-ntag215-manager/src/puck.ts b/puck-ntag215-manager/src/puck.ts index 1bdd48b..b16d95f 100644 --- a/puck-ntag215-manager/src/puck.ts +++ b/puck-ntag215-manager/src/puck.ts @@ -141,13 +141,12 @@ export class Puck { private initFastMode(timeout: number = 0): Promise { return new Promise(async (resolve, reject) => { - const instance = this var errorTimer: NodeJS.Timeout = undefined function finishName(this: BluetoothRemoteGATTCharacteristic, ev: CharacteristicEvent) { var text = new TextDecoder().decode(ev.target.value) - if (text == "DTM_PUCK_FAST") { + if (text.includes("DTM_PUCK_FAST")) { this.removeEventListener("characteristicvaluechanged", finishName) if (errorTimer) { diff --git a/puck-ntag215-manager/src/templates/modal-messages.pug b/puck-ntag215-manager/src/templates/modal-messages.pug index 8f23e2c..39b97fd 100644 --- a/puck-ntag215-manager/src/templates/modal-messages.pug +++ b/puck-ntag215-manager/src/templates/modal-messages.pug @@ -4,6 +4,10 @@ case kind p If this feature is not enabled, the tags stored on the puck will be lost when the battery dies or if it is removed. p This may reduce the life of the puck due to the additional writes to the flash storage. + when "debug-mode" + p Do you want to enable debug mode? + p This will enable log output and use "Serial1" as a console when fast mode is enabled. + when "dfu-instructions" p To enter DFU mode, please remove the battery from your Puck.js and re-insert it while holding the power button until the LED indicator turns green.