Skip to content

Commit

Permalink
Merge #36 - Update fastMode
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTheMan827 committed Jan 13, 2025
2 parents 5d12291 + 9c57bfb commit 25cbc41
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 35 deletions.
14 changes: 11 additions & 3 deletions ntag215.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand All @@ -534,6 +539,9 @@ function onFastModeDisconnect() {

// Remove event listener
NRF.removeListener(_Disconnect, onFastModeDisconnect);

// Restore Bluetooth console
E.setConsole("Bluetooth");
}

/**
Expand Down Expand Up @@ -679,7 +687,7 @@ function fastRx(data) {
});

_Bluetooth.write(data);
}, 0)
}, 0);

return;

Expand Down
68 changes: 38 additions & 30 deletions puck-ntag215-manager/src/espruino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface SemVer {

export interface GetCodeOptions {
saveToFlash?: boolean,
enableDebug?: boolean,
board?: string,
enableLed1?: boolean,
enableLed2?: boolean,
Expand Down Expand Up @@ -142,41 +143,48 @@ export async function checkLed(ledNumber: number): Promise<boolean> {
export function getCode(options: GetCodeOptions = {}): Promise<string> {
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)
})
Expand Down
10 changes: 10 additions & 0 deletions puck-ntag215-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -366,6 +375,7 @@ $(() => {

await EspruinoHelper.writeCode({
saveToFlash: modalResult === ModalResult.ButtonYes,
enableDebug: debugModalResult === ModalResult.ButtonYes,
board,
enableLed1,
enableLed2,
Expand Down
1 change: 1 addition & 0 deletions puck-ntag215-manager/src/modalMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
3 changes: 1 addition & 2 deletions puck-ntag215-manager/src/puck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,12 @@ export class Puck {

private initFastMode(timeout: number = 0): Promise<void> {
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) {
Expand Down
4 changes: 4 additions & 0 deletions puck-ntag215-manager/src/templates/modal-messages.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 25cbc41

Please sign in to comment.