mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-22 12:33:11 +00:00
more js stuff
https://github.com/Next-Flip/Momentum-Firmware/commits/js-backport-of-backport/
This commit is contained in:
parent
f37799e0f4
commit
cd46d8ca07
4 changed files with 36 additions and 29 deletions
|
@ -2,7 +2,7 @@ let storage = require("storage");
|
|||
|
||||
print("script has __dirpath of" + __dirpath);
|
||||
print("script has __filepath of" + __filepath);
|
||||
if (storage.exists(__dirpath + "/math.js")) {
|
||||
if (storage.fileExists(__dirpath + "/math.js")) {
|
||||
print("math.js exist here.");
|
||||
} else {
|
||||
print("math.js does not exist here.");
|
||||
|
|
|
@ -1,43 +1,29 @@
|
|||
let storage = require("storage");
|
||||
let path = "/ext/storage.test";
|
||||
|
||||
function arraybuf_to_string(arraybuf) {
|
||||
let string = "";
|
||||
let data_view = Uint8Array(arraybuf);
|
||||
for (let i = 0; i < data_view.length; i++) {
|
||||
string += chr(data_view[i]);
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
print("File exists:", storage.exists(path));
|
||||
print("File exists:", storage.fileExists(path));
|
||||
|
||||
print("Writing...");
|
||||
// write(path, data, offset)
|
||||
// If offset is specified, the file is not cleared, content is kept and data is written at specified offset
|
||||
// Takes both strings and array buffers
|
||||
storage.write(path, "Hello ");
|
||||
let file = storage.openFile(path, "w", "create_always");
|
||||
file.write("Hello ");
|
||||
file.close();
|
||||
|
||||
print("File exists:", storage.exists(path));
|
||||
print("File exists:", storage.fileExists(path));
|
||||
|
||||
// Append will create the file even if it doesnt exist!
|
||||
// Takes both strings and array buffers
|
||||
storage.append(path, "World!");
|
||||
file = storage.openFile(path, "w", "open_append");
|
||||
file.write("World!");
|
||||
file.close();
|
||||
|
||||
print("Reading...");
|
||||
// read(path, size, offset)
|
||||
// If no size specified, total filesize is used
|
||||
// If offset is specified, size is capped at (filesize - offset)
|
||||
let data = storage.read(path);
|
||||
// read returns an array buffer, to allow proper usage of raw binary data
|
||||
print(arraybuf_to_string(data));
|
||||
file = storage.openFile(path, "r", "open_existing");
|
||||
let text = file.read("ascii", 128);
|
||||
file.close();
|
||||
print(text);
|
||||
|
||||
print("Removing...")
|
||||
storage.remove(path);
|
||||
|
||||
print("Done")
|
||||
|
||||
// There's also:
|
||||
// storage.copy(old_path, new_path);
|
||||
// storage.move(old_path, new_path);
|
||||
// storage.mkdir(path);
|
||||
// You don't need to close the file after each operation, this is just to show some different ways to use
|
||||
// There's also many more functions and options, check types docs in firmware repo
|
21
applications/system/js_app/types/global.d.ts
vendored
21
applications/system/js_app/types/global.d.ts
vendored
|
@ -56,6 +56,15 @@ declare const __filepath: string;
|
|||
*/
|
||||
declare function load(path: string): any;
|
||||
|
||||
/**
|
||||
* @brief Return 1-byte string whose ASCII code is the integer `n`
|
||||
*
|
||||
* If `n` is not numeric or outside of `0-255` range, `null` is returned
|
||||
*
|
||||
* @param n The ASCII code to convert to string
|
||||
*/
|
||||
declare function chr(n: number): string | null;
|
||||
|
||||
/**
|
||||
* @brief mJS Foreign Pointer type
|
||||
*
|
||||
|
@ -189,6 +198,18 @@ declare class String {
|
|||
* See `charCodeAt`
|
||||
*/
|
||||
at(index: number): number;
|
||||
/**
|
||||
* @brief Return index of first occurence of substr within the string or `-1` if not found
|
||||
* @param substr The string to search for
|
||||
* @param fromIndex The index to start searching from
|
||||
*/
|
||||
indexOf(substr: string, fromIndex?: number): number;
|
||||
/**
|
||||
* @brief Return a substring between two indices
|
||||
* @param start The index to start substring at
|
||||
* @param end The index to end substring at
|
||||
*/
|
||||
slice(start: number, end?: number): string;
|
||||
}
|
||||
|
||||
declare class Boolean { }
|
||||
|
|
Loading…
Reference in a new issue