merge js changes

changes by Willy-JL
spi module by jamisonderek
This commit is contained in:
MX 2024-10-28 22:21:42 +03:00
parent e7239f6e8b
commit 6b6d98da2a
No known key found for this signature in database
GPG Key ID: 7CCC66B7DBDD1C83
13 changed files with 385 additions and 22 deletions

View File

@ -193,3 +193,11 @@ App(
requires=["js_app"],
sources=["modules/js_i2c.c"],
)
App(
appid="js_spi",
apptype=FlipperAppType.PLUGIN,
entry_point="js_spi_ep",
requires=["js_app"],
sources=["modules/js_spi.c"],
)

View File

@ -198,12 +198,18 @@ static void js_require(struct mjs* mjs) {
static void js_parse_int(struct mjs* mjs) {
const char* str;
JS_FETCH_ARGS_OR_RETURN(mjs, JS_AT_LEAST, JS_ARG_STR(&str));
int32_t base = 10;
if(mjs_nargs(mjs) == 1) {
JS_FETCH_ARGS_OR_RETURN(mjs, JS_EXACTLY, JS_ARG_STR(&str));
} else {
JS_FETCH_ARGS_OR_RETURN(mjs, JS_EXACTLY, JS_ARG_STR(&str), JS_ARG_INT32(&base));
if(mjs_nargs(mjs) >= 2) {
mjs_val_t base_arg = mjs_arg(mjs, 1);
if(!mjs_is_number(base_arg)) {
mjs_prepend_errorf(mjs, MJS_BAD_ARGS_ERROR, "Base must be a number");
mjs_return(mjs, MJS_UNDEFINED);
}
base = mjs_get_int(mjs, base_arg);
}
int32_t num;
if(strint_to_int32(str, NULL, &num, base) != StrintParseNoError) {
num = 0;

View File

@ -8,6 +8,7 @@
typedef struct {
uint8_t* buffer;
size_t buffer_size;
size_t default_data_size;
FuriString* header;
FuriSemaphore* input_semaphore;
JsEventLoopContract contract;
@ -38,7 +39,14 @@ static bool
len_assign(struct mjs* mjs, ByteInput* input, JsViewPropValue value, JsByteKbContext* context) {
UNUSED(mjs);
UNUSED(input);
context->buffer_size = (size_t)(value.number);
size_t new_buffer_size = value.number;
if(new_buffer_size < context->default_data_size) {
// Avoid confusing parameters from user
mjs_prepend_errorf(
mjs, MJS_BAD_ARGS_ERROR, "length must be larger than defaultData length");
return false;
}
context->buffer_size = new_buffer_size;
context->buffer = realloc(context->buffer, context->buffer_size); //-V701
byte_input_set_result_callback(
input,
@ -57,16 +65,24 @@ static bool default_data_assign(
JsByteKbContext* context) {
UNUSED(mjs);
mjs_val_t array_buf = value.array;
mjs_val_t array_buf = value.term;
if(mjs_is_data_view(array_buf)) {
array_buf = mjs_dataview_get_buf(mjs, array_buf);
}
size_t default_data_len = 0;
char* default_data = mjs_array_buf_get_ptr(mjs, array_buf, &default_data_len);
memcpy(
context->buffer,
(uint8_t*)default_data,
MIN((size_t)context->buffer_size, default_data_len));
char* default_data = mjs_array_buf_get_ptr(mjs, array_buf, &context->default_data_size);
if(context->buffer_size < context->default_data_size) {
// Ensure buffer is large enough for defaultData
context->buffer_size = context->default_data_size;
context->buffer = realloc(context->buffer, context->buffer_size); //-V701
}
memcpy(context->buffer, (uint8_t*)default_data, context->default_data_size);
if(context->buffer_size > context->default_data_size) {
// Reset previous data after defaultData
memset(
context->buffer + context->default_data_size,
0x00,
context->buffer_size - context->default_data_size);
}
byte_input_set_result_callback(
input,

View File

@ -216,14 +216,14 @@ static bool
expected_type = "array";
break;
}
c_value = (JsViewPropValue){.array = value};
c_value = (JsViewPropValue){.term = value};
} break;
case JsViewPropTypeTypedArr: {
if(!mjs_is_typed_array(value)) {
expected_type = "typed_array";
break;
}
c_value = (JsViewPropValue){.array = value};
c_value = (JsViewPropValue){.term = value};
} break;
case JsViewPropTypeBool: {
if(!mjs_is_boolean(value)) {

View File

@ -16,8 +16,8 @@ typedef enum {
typedef union {
const char* string;
int32_t number;
mjs_val_t array;
bool boolean;
mjs_val_t term;
} JsViewPropValue;
/**

View File

@ -33,9 +33,9 @@ static bool
static bool items_assign(struct mjs* mjs, Submenu* submenu, JsViewPropValue value, void* context) {
UNUSED(mjs);
submenu_reset(submenu);
size_t len = mjs_array_length(mjs, value.array);
size_t len = mjs_array_length(mjs, value.term);
for(size_t i = 0; i < len; i++) {
mjs_val_t item = mjs_array_get(mjs, value.array, i);
mjs_val_t item = mjs_array_get(mjs, value.term, i);
if(!mjs_is_string(item)) return false;
submenu_add_item(submenu, mjs_get_string(mjs, &item, NULL), i, choose_callback, context);
}

View File

@ -8,6 +8,7 @@
typedef struct {
char* buffer;
size_t buffer_size;
size_t default_text_size;
FuriString* header;
bool default_text_clear;
FuriSemaphore* input_semaphore;
@ -49,7 +50,14 @@ static bool max_len_assign(
JsViewPropValue value,
JsKbdContext* context) {
UNUSED(mjs);
context->buffer_size = (size_t)(value.number + 1);
size_t new_buffer_size = value.number + 1;
if(new_buffer_size < context->default_text_size) {
// Avoid confusing parameters from user
mjs_prepend_errorf(
mjs, MJS_BAD_ARGS_ERROR, "maxLength must be larger than defaultText length");
return false;
}
context->buffer_size = new_buffer_size;
context->buffer = realloc(context->buffer, context->buffer_size); //-V701
text_input_set_result_callback(
input,
@ -70,6 +78,13 @@ static bool default_text_assign(
UNUSED(input);
if(value.string) {
context->default_text_size = strlen(value.string) + 1;
if(context->buffer_size < context->default_text_size) {
// Ensure buffer is large enough for defaultData
context->buffer_size = context->default_text_size;
context->buffer = realloc(context->buffer, context->buffer_size); //-V701
}
// Also trim excess previous data with strlcpy()
strlcpy(context->buffer, value.string, context->buffer_size);
text_input_set_result_callback(
input,

View File

@ -308,7 +308,7 @@ void js_math_trunc(struct mjs* mjs) {
static void* js_math_create(struct mjs* mjs, mjs_val_t* object, JsModules* modules) {
UNUSED(modules);
mjs_val_t math_obj = mjs_mk_object(mjs);
mjs_set(mjs, math_obj, "is_equal", ~0, MJS_MK_FN(js_math_is_equal));
mjs_set(mjs, math_obj, "isEqual", ~0, MJS_MK_FN(js_math_is_equal));
mjs_set(mjs, math_obj, "abs", ~0, MJS_MK_FN(js_math_abs));
mjs_set(mjs, math_obj, "acos", ~0, MJS_MK_FN(js_math_acos));
mjs_set(mjs, math_obj, "acosh", ~0, MJS_MK_FN(js_math_acosh));

View File

@ -0,0 +1,283 @@
#include "../js_modules.h"
#include <furi_hal_spi.h>
typedef struct {
bool acquired_bus;
} JsSpiInst;
static JsSpiInst* get_this_ctx(struct mjs* mjs) {
mjs_val_t obj_inst = mjs_get(mjs, mjs_get_this(mjs), INST_PROP_NAME, ~0);
JsSpiInst* spi = mjs_get_ptr(mjs, obj_inst);
furi_assert(spi);
return spi;
}
static void ret_bad_args(struct mjs* mjs, const char* error) {
mjs_prepend_errorf(mjs, MJS_BAD_ARGS_ERROR, "%s", error);
mjs_return(mjs, MJS_UNDEFINED);
}
static bool check_arg_count_range(struct mjs* mjs, size_t min_count, size_t max_count) {
size_t num_args = mjs_nargs(mjs);
if(num_args < min_count || num_args > max_count) {
ret_bad_args(mjs, "Wrong argument count");
return false;
}
return true;
}
static void js_spi_acquire(struct mjs* mjs) {
if(!check_arg_count_range(mjs, 0, 0)) return;
JsSpiInst* spi = get_this_ctx(mjs);
if(!spi->acquired_bus) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
spi->acquired_bus = true;
}
mjs_return(mjs, MJS_UNDEFINED);
}
static void js_spi_release(struct mjs* mjs) {
if(!check_arg_count_range(mjs, 0, 0)) return;
JsSpiInst* spi = get_this_ctx(mjs);
if(spi->acquired_bus) {
furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
spi->acquired_bus = false;
}
mjs_return(mjs, MJS_UNDEFINED);
}
static bool js_spi_is_acquired(struct mjs* mjs) {
JsSpiInst* spi = get_this_ctx(mjs);
return spi->acquired_bus;
}
static void js_spi_write(struct mjs* mjs) {
if(!check_arg_count_range(mjs, 1, 2)) return;
mjs_val_t tx_buf_arg = mjs_arg(mjs, 0);
bool tx_buf_was_allocated = false;
uint8_t* tx_buf = NULL;
size_t tx_len = 0;
if(mjs_is_array(tx_buf_arg)) {
tx_len = mjs_array_length(mjs, tx_buf_arg);
if(tx_len == 0) {
ret_bad_args(mjs, "Data array must not be empty");
return;
}
tx_buf = malloc(tx_len);
tx_buf_was_allocated = true;
for(size_t i = 0; i < tx_len; i++) {
mjs_val_t val = mjs_array_get(mjs, tx_buf_arg, i);
if(!mjs_is_number(val)) {
ret_bad_args(mjs, "Data array must contain only numbers");
free(tx_buf);
return;
}
uint32_t byte_val = mjs_get_int32(mjs, val);
if(byte_val > 0xFF) {
ret_bad_args(mjs, "Data array values must be 0-255");
free(tx_buf);
return;
}
tx_buf[i] = byte_val;
}
} else if(mjs_is_typed_array(tx_buf_arg)) {
mjs_val_t array_buf = tx_buf_arg;
if(mjs_is_data_view(tx_buf_arg)) {
array_buf = mjs_dataview_get_buf(mjs, tx_buf_arg);
}
tx_buf = (uint8_t*)mjs_array_buf_get_ptr(mjs, array_buf, &tx_len);
if(tx_len == 0) {
ret_bad_args(mjs, "Data array must not be empty");
return;
}
} else {
ret_bad_args(mjs, "Data must be an array, arraybuf or dataview");
return;
}
uint32_t timeout = 1;
if(mjs_nargs(mjs) > 1) { // Timeout is optional argument
mjs_val_t timeout_arg = mjs_arg(mjs, 1);
if(!mjs_is_number(timeout_arg)) {
ret_bad_args(mjs, "Timeout must be a number");
if(tx_buf_was_allocated) free(tx_buf);
return;
}
timeout = mjs_get_int32(mjs, timeout_arg);
}
if(!js_spi_is_acquired(mjs)) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
}
bool result = furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, tx_buf, tx_len, timeout);
if(!js_spi_is_acquired(mjs)) {
furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
}
if(tx_buf_was_allocated) free(tx_buf);
mjs_return(mjs, mjs_mk_boolean(mjs, result));
}
static void js_spi_read(struct mjs* mjs) {
if(!check_arg_count_range(mjs, 1, 2)) return;
mjs_val_t rx_len_arg = mjs_arg(mjs, 0);
if(!mjs_is_number(rx_len_arg)) {
ret_bad_args(mjs, "Length must be a number");
return;
}
size_t rx_len = mjs_get_int32(mjs, rx_len_arg);
if(rx_len == 0) {
ret_bad_args(mjs, "Length must not zero");
return;
}
uint8_t* rx_buf = malloc(rx_len);
uint32_t timeout = 1;
if(mjs_nargs(mjs) > 1) { // Timeout is optional argument
mjs_val_t timeout_arg = mjs_arg(mjs, 1);
if(!mjs_is_number(timeout_arg)) {
ret_bad_args(mjs, "Timeout must be a number");
free(rx_buf);
return;
}
timeout = mjs_get_int32(mjs, timeout_arg);
}
if(!js_spi_is_acquired(mjs)) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
}
bool result = furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, rx_buf, rx_len, timeout);
if(!js_spi_is_acquired(mjs)) {
furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
}
mjs_val_t ret = MJS_UNDEFINED;
if(result) {
ret = mjs_mk_array_buf(mjs, (char*)rx_buf, rx_len);
}
free(rx_buf);
mjs_return(mjs, ret);
}
static void js_spi_write_read(struct mjs* mjs) {
if(!check_arg_count_range(mjs, 1, 2)) return;
mjs_val_t tx_buf_arg = mjs_arg(mjs, 0);
bool tx_buf_was_allocated = false;
uint8_t* tx_buf = NULL;
size_t data_len = 0;
if(mjs_is_array(tx_buf_arg)) {
data_len = mjs_array_length(mjs, tx_buf_arg);
if(data_len == 0) {
ret_bad_args(mjs, "Data array must not be empty");
return;
}
tx_buf = malloc(data_len);
tx_buf_was_allocated = true;
for(size_t i = 0; i < data_len; i++) {
mjs_val_t val = mjs_array_get(mjs, tx_buf_arg, i);
if(!mjs_is_number(val)) {
ret_bad_args(mjs, "Data array must contain only numbers");
free(tx_buf);
return;
}
uint32_t byte_val = mjs_get_int32(mjs, val);
if(byte_val > 0xFF) {
ret_bad_args(mjs, "Data array values must be 0-255");
free(tx_buf);
return;
}
tx_buf[i] = byte_val;
}
} else if(mjs_is_typed_array(tx_buf_arg)) {
mjs_val_t array_buf = tx_buf_arg;
if(mjs_is_data_view(tx_buf_arg)) {
array_buf = mjs_dataview_get_buf(mjs, tx_buf_arg);
}
tx_buf = (uint8_t*)mjs_array_buf_get_ptr(mjs, array_buf, &data_len);
if(data_len == 0) {
ret_bad_args(mjs, "Data array must not be empty");
return;
}
} else {
ret_bad_args(mjs, "Data must be an array, arraybuf or dataview");
return;
}
uint8_t* rx_buf = malloc(data_len); // RX and TX are same length for SPI writeRead.
uint32_t timeout = 1;
if(mjs_nargs(mjs) > 1) { // Timeout is optional argument
mjs_val_t timeout_arg = mjs_arg(mjs, 1);
if(!mjs_is_number(timeout_arg)) {
ret_bad_args(mjs, "Timeout must be a number");
if(tx_buf_was_allocated) free(tx_buf);
free(rx_buf);
return;
}
timeout = mjs_get_int32(mjs, timeout_arg);
}
if(!js_spi_is_acquired(mjs)) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
}
bool result =
furi_hal_spi_bus_trx(&furi_hal_spi_bus_handle_external, tx_buf, rx_buf, data_len, timeout);
if(!js_spi_is_acquired(mjs)) {
furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
}
mjs_val_t ret = MJS_UNDEFINED;
if(result) {
ret = mjs_mk_array_buf(mjs, (char*)rx_buf, data_len);
}
if(tx_buf_was_allocated) free(tx_buf);
free(rx_buf);
mjs_return(mjs, ret);
}
static void* js_spi_create(struct mjs* mjs, mjs_val_t* object, JsModules* modules) {
UNUSED(modules);
JsSpiInst* spi = (JsSpiInst*)malloc(sizeof(JsSpiInst));
spi->acquired_bus = false;
mjs_val_t spi_obj = mjs_mk_object(mjs);
mjs_set(mjs, spi_obj, INST_PROP_NAME, ~0, mjs_mk_foreign(mjs, spi));
mjs_set(mjs, spi_obj, "acquire", ~0, MJS_MK_FN(js_spi_acquire));
mjs_set(mjs, spi_obj, "release", ~0, MJS_MK_FN(js_spi_release));
mjs_set(mjs, spi_obj, "write", ~0, MJS_MK_FN(js_spi_write));
mjs_set(mjs, spi_obj, "read", ~0, MJS_MK_FN(js_spi_read));
mjs_set(mjs, spi_obj, "writeRead", ~0, MJS_MK_FN(js_spi_write_read));
*object = spi_obj;
furi_hal_spi_bus_handle_init(&furi_hal_spi_bus_handle_external);
return (void*)spi;
}
static void js_spi_destroy(void* inst) {
JsSpiInst* spi = (JsSpiInst*)inst;
if(spi->acquired_bus) {
furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
}
free(spi);
furi_hal_spi_bus_handle_deinit(&furi_hal_spi_bus_handle_external);
}
static const JsModuleDescriptor js_spi_desc = {
"spi",
js_spi_create,
js_spi_destroy,
NULL,
};
static const FlipperAppPluginDescriptor spi_plugin_descriptor = {
.appid = PLUGIN_APP_ID,
.ep_api_version = PLUGIN_API_VERSION,
.entry_point = &js_spi_desc,
};
const FlipperAppPluginDescriptor* js_spi_ep(void) {
return &spi_plugin_descriptor;
}

View File

@ -40,7 +40,7 @@ export type KeyCode = MainKey | ModifierKey | number;
*
* @param settings USB device settings. Omit to select default parameters
*/
export declare function setup(settings?: { vid: number, pid: number, mfrName?: string, prodName?: string, layoutPath: string }): void;
export declare function setup(settings?: { vid: number, pid: number, mfrName?: string, prodName?: string, layoutPath?: string }): void;
/**
* @brief Tells whether the virtual USB HID device has successfully connected

View File

@ -31,8 +31,10 @@ declare const __filename: string;
/**
* @brief Reads a JS value from a file
*
* Reads a file at the specified path, interprets it as a JS value and returns
* the last value pushed on the stack.
* Reads a file at the specified path and runs it as JS, returning the last evaluated value.
*
* The result is cached and this filepath will not re-evaluated on future
* load() calls for this session.
*
* @param path The path to the file
* @param scope An object to use as global scope while running this file

View File

@ -1,3 +1,4 @@
export function isEqual(a: number, b: number, tolerance: number): boolean;
export function abs(n: number): number;
export function acos(n: number): number;
export function acosh(n: number): number;
@ -12,6 +13,7 @@ export function clz32(n: number): number;
export function cos(n: number): number;
export function exp(n: number): number;
export function floor(n: number): number;
export function log(n: number): number;
export function max(n: number, m: number): number;
export function min(n: number, m: number): number;
export function pow(n: number, m: number): number;
@ -21,4 +23,5 @@ export function sin(n: number): number;
export function sqrt(n: number): number;
export function trunc(n: number): number;
declare const PI: number;
declare const E: number;
declare const EPSILON: number;

View File

@ -0,0 +1,30 @@
/**
* @brief Acquire SPI bus
*/
export declare function acquire(): void;
/**
* @brief Release SPI bus
*/
export declare function release(): void;
/**
* @brief Write data to SPI bus and return success status
* @param data The data to write
* @param timeout Timeout in milliseconds
*/
export declare function write(data: number[] | ArrayBuffer, timeout?: number): boolean;
/**
* @brief Read data from SPI bus or return undefined on failure
* @param length How many bytes to read
* @param timeout Timeout in milliseconds
*/
export declare function read(length: number, timeout?: number): ArrayBuffer | undefined;
/**
* @brief Write and read data on SPI bus or return undefined on failure
* @param data The data to write, its length also indicates how many bytes will be read
* @param timeout Timeout in milliseconds
*/
export declare function writeRead(data: number[] | ArrayBuffer, timeout?: number): ArrayBuffer | undefined;