Skip to content

Commit

Permalink
JS: Add extra SDK feature strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Nov 2, 2024
1 parent 48aceff commit 67d9f5b
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Script cannot work without blebeacon module so check before
checkSdkFeatures(["blebeacon"]);

let blebeacon = require("blebeacon");

// Stop if previous background beacon is active
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Script cannot work without i2c module so check before
checkSdkFeatures(["i2c"]);

// Connect an 24C32N EEPROM to the I2C bus of the board. SDA=pin 15, SCL=pin 16, VCC=pin 9, GND=pin 8.
let i2c = require("i2c");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Script cannot work without spi module so check before
checkSdkFeatures(["spi"]);

// Connect a w25q32 SPI device to the Flipper Zero.
// D1=pin 2 (MOSI), SLK=pin 5 (SCK), GND=pin 8 (GND), D0=pin 3 (MISO), CS=pin 4 (CS), VCC=pin 9 (3V3)
let spi = require("spi");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ storage.remove(path);
print("Done")

// You don't need to close the file after each operation, this is just to show some different ways to use the API
// There's also many more functions and options, check type definitions in firmware repo
// There's also many more functions and options, check type definitions in firmware repo

// There is also virtual API, which is not available in all firmwares
// Allows you to mount disk images (like mass storage) and read/modify their contents
// If you want to use this as an optional feature, not essential to your script:
// if (doesSdkSupport(["storage-virtual"])) {
// storage.virtualInit("/ext/disk_image.img");
// storage.virtualMount();
// // Read/modify data from /mnt filesystem
// storage.virtualQuit();
// }
// If instead virtual API is essential to your script, put this near beginning of script:
// checkSdkFeatures(["storage-virtual"]);
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Script cannot work without subghz module so check before
checkSdkFeatures(["subghz"]);

let subghz = require("subghz");
subghz.setup();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
// Script cannot work without usbdisk module so check before
checkSdkFeatures(["usbdisk"]);

let usbdisk = require("usbdisk");
// print("Creating image...");
// usbdisk.createImage("/ext/apps_data/mass_storage/128MB.img", 128 * 1024 * 1024);
let storage = require("storage");

let imagePath = "/ext/apps_data/mass_storage/128MB.img";
let imageSize = 128 * 1024 * 1024;

let imageExisted = storage.fileExists(imagePath);
if (imageExisted) {
print("Disk image '128MB' already exists");
} else {
// CreateImage isn't necessary to overall function, check when its used not at script start
if (doesSdkSupport(["usbdisk-createimage"])) {
print("Creating disk image '128MB'...");
usbdisk.createImage(imagePath, imageSize);
} else {
die("Disk image '128MB' not present, can't auto-create");
}
}

print("Starting UsbDisk...");
usbdisk.start("/ext/apps_data/mass_storage/128MB.img");

print("Started, waiting until ejected...");
while (!usbdisk.wasEjected()) {
delay(1000);
}

print("Ejected, stopping UsbDisk...");
usbdisk.stop();

if (!imageExisted) {
print("Removing disk image...");
storage.remove(imagePath);
}

print("Done");
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Script cannot work without widget module so check before
checkSdkFeatures(["widget"]);

let widget = require("widget");

let demo_seconds = 30;
Expand Down Expand Up @@ -34,8 +37,12 @@ widget.addDot(102, 44);
widget.addDot(104, 43);

// add an icon (x, y, icon)
widget.addIcon(100, 50, "ButtonUp_7x4");
widget.addIcon(100, 55, "ButtonDown_7x4");
// not available in all firmwares, but not essential for this script's
// functionality, so we just check at runtime and use it if it is available
if (doesSdkSupport(["widget-addicon"])) {
widget.addIcon(100, 50, "ButtonUp_7x4");
widget.addIcon(100, 55, "ButtonDown_7x4");
}

// add a glyph (x, y, glyph)
widget.addGlyph(115, 50, "#".charCodeAt(0));
Expand Down
14 changes: 14 additions & 0 deletions applications/system/js_app/js_modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,20 @@ void js_check_sdk_compatibility(struct mjs* mjs) {

static const char* extra_features[] = {
"baseline", // dummy "feature"

// extra modules
"blebeacon",
"i2c",
"spi",
"subghz",
"usbdisk",
"vgm",
"widget",

// extra features
"storage-virtual",
"usbdisk-createimage",
"widget-addicon",
};

/**
Expand Down
6 changes: 6 additions & 0 deletions applications/system/js_app/packages/fz-sdk/i2c/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* I2C bus communication
* @version Available with JS feature `i2c`
* @module
*/

/**
* @brief Check if there is an I2C device ready on the bus
* @param address The device address to check
Expand Down
6 changes: 6 additions & 0 deletions applications/system/js_app/packages/fz-sdk/spi/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* SPI bus communication
* @version Available with JS feature `spi`
* @module
*/

/**
* @brief Acquire SPI bus
*/
Expand Down
3 changes: 3 additions & 0 deletions applications/system/js_app/packages/fz-sdk/storage/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,18 @@ export declare function isSubpathOf(parentPath: string, childPath: string): bool
* Initialize virtual mount api with disk image at given path.
* Errors on failure.
* @param parentPath Path to disk image file
* @version Available with JS feature `storage-virtual`
*/
export declare function virtualInit(path: string): void;
/**
* Mount selected disk image at /mnt path.
* Errors on failure.
* @version Available with JS feature `storage-virtual`
*/
export declare function virtualMount(): void;
/**
* Deinitialize virtual mount api.
* Errors on failure.
* @version Available with JS feature `storage-virtual`
*/
export declare function virtualQuit(): void;

0 comments on commit 67d9f5b

Please sign in to comment.