mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
Format
This commit is contained in:
@@ -94,5 +94,3 @@ App(
|
||||
requires=["js_app"],
|
||||
sources=["modules/js_keyboard.c"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -9,10 +9,9 @@ typedef struct {
|
||||
GapExtraBeaconConfig beacon_config;
|
||||
} JSblebeaconInst;
|
||||
|
||||
|
||||
struct OUI_MAP_ENTRY {
|
||||
const char *brand;
|
||||
const char *oui;
|
||||
const char* brand;
|
||||
const char* oui;
|
||||
};
|
||||
|
||||
struct OUI_MAP_ENTRY OUI_MAP[] = {
|
||||
@@ -36,41 +35,39 @@ struct OUI_MAP_ENTRY OUI_MAP[] = {
|
||||
|
||||
#define OUI_MAP_SIZE (sizeof(OUI_MAP) / sizeof(OUI_MAP[0]))
|
||||
|
||||
|
||||
int rand_range(int min, int max) {
|
||||
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
|
||||
}
|
||||
|
||||
void byte_to_hex(char *output, unsigned char byte) {
|
||||
void byte_to_hex(char* output, unsigned char byte) {
|
||||
static const char hex_chars[] = "0123456789ABCDEF";
|
||||
output[0] = hex_chars[byte >> 4];
|
||||
output[1] = hex_chars[byte & 0x0F];
|
||||
}
|
||||
|
||||
static char* generate_mac_address(const char *brand) {
|
||||
char *mac_address = (char*)malloc(18 * sizeof(char));
|
||||
if (mac_address == NULL) {
|
||||
static char* generate_mac_address(const char* brand) {
|
||||
char* mac_address = (char*)malloc(18 * sizeof(char));
|
||||
if(mac_address == NULL) {
|
||||
FURI_LOG_D("BLE", "Memory allocation failed.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *oui = NULL;
|
||||
for (unsigned int i = 0; i < OUI_MAP_SIZE; ++i) {
|
||||
if (strcmp(brand, OUI_MAP[i].brand) == 0) {
|
||||
const char* oui = NULL;
|
||||
for(unsigned int i = 0; i < OUI_MAP_SIZE; ++i) {
|
||||
if(strcmp(brand, OUI_MAP[i].brand) == 0) {
|
||||
oui = OUI_MAP[i].oui;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (oui == NULL) {
|
||||
if(oui == NULL) {
|
||||
FURI_LOG_D("BLE", "Brand not found.\n");
|
||||
free(mac_address);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char last_bytes[6];
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for(int i = 0; i < 3; ++i) {
|
||||
unsigned char byte = rand_range(0x00, 0xFF);
|
||||
byte_to_hex(&last_bytes[i * 2], byte);
|
||||
}
|
||||
@@ -114,9 +111,9 @@ static bool get_str_arg(struct mjs* mjs, size_t index, char** value) {
|
||||
ret_bad_args(mjs, "Bad string argument");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
*value = (char*)malloc(str_len + 1);
|
||||
if (!*value) {
|
||||
if(!*value) {
|
||||
ret_bad_args(mjs, "Memory allocation failed");
|
||||
return false;
|
||||
}
|
||||
@@ -126,24 +123,23 @@ static bool get_str_arg(struct mjs* mjs, size_t index, char** value) {
|
||||
}
|
||||
|
||||
static uint8_t hex_char_to_uint(char c) {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return 10 + c - 'a';
|
||||
if (c >= 'A' && c <= 'F') return 10 + c - 'A';
|
||||
if(c >= '0' && c <= '9') return c - '0';
|
||||
if(c >= 'a' && c <= 'f') return 10 + c - 'a';
|
||||
if(c >= 'A' && c <= 'F') return 10 + c - 'A';
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t* macstr_to_uint8(const char* macstr) {
|
||||
if (strlen(macstr) != 17) return NULL;
|
||||
if(strlen(macstr) != 17) return NULL;
|
||||
|
||||
uint8_t* mac_bytes = (uint8_t*)malloc(6 * sizeof(uint8_t));
|
||||
if (!mac_bytes) return NULL;
|
||||
|
||||
for (size_t i = 0, j = 0; i < 17; i += 3, ++j) {
|
||||
if(!mac_bytes) return NULL;
|
||||
|
||||
for(size_t i = 0, j = 0; i < 17; i += 3, ++j) {
|
||||
mac_bytes[j] = (hex_char_to_uint(macstr[i]) << 4) | hex_char_to_uint(macstr[i + 1]);
|
||||
|
||||
if (i < 15 && macstr[i + 2] != ':' && macstr[i + 2] != '-') {
|
||||
free(mac_bytes);
|
||||
if(i < 15 && macstr[i + 2] != ':' && macstr[i + 2] != '-') {
|
||||
free(mac_bytes);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -153,43 +149,42 @@ static uint8_t* macstr_to_uint8(const char* macstr) {
|
||||
|
||||
static uint8_t* hexstr_to_uint8(const char* hexstr, size_t* out_length) {
|
||||
size_t len = strlen(hexstr);
|
||||
if (len % 2 != 0) return NULL;
|
||||
if(len % 2 != 0) return NULL;
|
||||
|
||||
if (len > EXTRA_BEACON_MAX_DATA_SIZE + 1) return NULL;
|
||||
if(len > EXTRA_BEACON_MAX_DATA_SIZE + 1) return NULL;
|
||||
|
||||
*out_length = len / 2;
|
||||
uint8_t* bytes = (uint8_t*)malloc(*out_length);
|
||||
if (!bytes) return NULL;
|
||||
if(!bytes) return NULL;
|
||||
|
||||
for (size_t i = 0; i < *out_length; ++i) {
|
||||
for(size_t i = 0; i < *out_length; ++i) {
|
||||
bytes[i] = (hex_char_to_uint(hexstr[i * 2]) << 4) | hex_char_to_uint(hexstr[i * 2 + 1]);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static void js_blebeacon_set_data(struct mjs *mjs) {
|
||||
static void js_blebeacon_set_data(struct mjs* mjs) {
|
||||
FURI_LOG_D("BLE", "Setting data");
|
||||
if (!check_arg_count(mjs, 1)) return;
|
||||
if(!check_arg_count(mjs, 1)) return;
|
||||
JSblebeaconInst* inst = get_this_ctx(mjs);
|
||||
if (!inst) {
|
||||
if(!inst) {
|
||||
FURI_LOG_D("BLE", "Beacon instance is null");
|
||||
ret_bad_args(mjs, "Beacon instance is null");
|
||||
return;
|
||||
}
|
||||
|
||||
if (inst->data) {
|
||||
if(inst->data) {
|
||||
FURI_LOG_D("BLE", "Freeing existing data");
|
||||
free(inst->data);
|
||||
inst->data = NULL;
|
||||
inst->data = NULL;
|
||||
}
|
||||
|
||||
|
||||
if (!get_str_arg(mjs, 0, &(inst->data))) return; // get_str_arg now modifies inst->data directly
|
||||
if(!get_str_arg(mjs, 0, &(inst->data))) return; // get_str_arg now modifies inst->data directly
|
||||
|
||||
size_t data_len = 0;
|
||||
uint8_t* beacon_data = hexstr_to_uint8(inst->data, &data_len);
|
||||
if (!beacon_data) {
|
||||
if(!beacon_data) {
|
||||
FURI_LOG_D("BLE", "Failed to convert data to hex");
|
||||
ret_bad_args(mjs, "Failed to convert data to hex");
|
||||
return;
|
||||
@@ -201,10 +196,9 @@ static void js_blebeacon_set_data(struct mjs *mjs) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
|
||||
static void js_blebeacon_generate_mac(struct mjs *mjs)
|
||||
{
|
||||
static void js_blebeacon_generate_mac(struct mjs* mjs) {
|
||||
char* company = "";
|
||||
if (!get_str_arg(mjs, 0, &company)) return;
|
||||
if(!get_str_arg(mjs, 0, &company)) return;
|
||||
|
||||
char* mac = generate_mac_address(company);
|
||||
|
||||
@@ -213,17 +207,16 @@ static void js_blebeacon_generate_mac(struct mjs *mjs)
|
||||
return mjs_return(mjs, js_mac_address);
|
||||
}
|
||||
|
||||
static void js_blebeacon_set_mac(struct mjs *mjs) {
|
||||
static void js_blebeacon_set_mac(struct mjs* mjs) {
|
||||
FURI_LOG_D("BLE", "Setting Mac");
|
||||
if (!check_arg_count(mjs, 1))
|
||||
{
|
||||
if(!check_arg_count(mjs, 1)) {
|
||||
ret_bad_args(mjs, "Bad args");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
JSblebeaconInst* inst = get_this_ctx(mjs);
|
||||
char* mac_addr = "";
|
||||
if (!get_str_arg(mjs, 0, &mac_addr)) return;
|
||||
if(!get_str_arg(mjs, 0, &mac_addr)) return;
|
||||
inst->mac_addr = mac_addr;
|
||||
inst->beacon_config.min_adv_interval_ms = 50;
|
||||
inst->beacon_config.max_adv_interval_ms = 150;
|
||||
@@ -234,7 +227,7 @@ static void js_blebeacon_set_mac(struct mjs *mjs) {
|
||||
inst->beacon_config.address_type = GapAddressTypePublic;
|
||||
|
||||
uint8_t* mac = macstr_to_uint8(mac_addr);
|
||||
if (mac) {
|
||||
if(mac) {
|
||||
memcpy(inst->beacon_config.address, mac, 6);
|
||||
furi_hal_bt_extra_beacon_set_config(&inst->beacon_config);
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
@@ -245,21 +238,19 @@ static void js_blebeacon_set_mac(struct mjs *mjs) {
|
||||
}
|
||||
}
|
||||
|
||||
static void js_blebeacon_send(struct mjs *mjs) {
|
||||
|
||||
static void js_blebeacon_send(struct mjs* mjs) {
|
||||
furi_hal_bt_extra_beacon_start();
|
||||
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
|
||||
static void js_blebeacon_stop(struct mjs *mjs) {
|
||||
|
||||
static void js_blebeacon_stop(struct mjs* mjs) {
|
||||
furi_hal_bt_extra_beacon_stop();
|
||||
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
|
||||
static void* js_blebeacon_create(struct mjs *mjs, mjs_val_t* object) {
|
||||
static void* js_blebeacon_create(struct mjs* mjs, mjs_val_t* object) {
|
||||
JSblebeaconInst* inst = malloc(sizeof(JSblebeaconInst));
|
||||
mjs_val_t blebeacon_obj = mjs_mk_object(mjs);
|
||||
mjs_set(mjs, blebeacon_obj, INST_PROP_NAME, ~0, mjs_mk_foreign(mjs, inst));
|
||||
@@ -272,9 +263,9 @@ static void* js_blebeacon_create(struct mjs *mjs, mjs_val_t* object) {
|
||||
return inst;
|
||||
}
|
||||
|
||||
static void js_blebeacon_destroy(void *ptr) {
|
||||
static void js_blebeacon_destroy(void* ptr) {
|
||||
JSblebeaconInst* inst = (JSblebeaconInst*)ptr;
|
||||
if (inst) {
|
||||
if(inst) {
|
||||
free(inst->data);
|
||||
free(inst->mac_addr);
|
||||
free(inst);
|
||||
|
||||
@@ -20,7 +20,7 @@ static void ret_bad_args(struct mjs* mjs, const char* error) {
|
||||
|
||||
char nibble_to_hex_character(uint8_t nibble) {
|
||||
nibble &= 0xF;
|
||||
if (nibble < 10) {
|
||||
if(nibble < 10) {
|
||||
return '0' + nibble;
|
||||
} else {
|
||||
return 'A' + (nibble - 10);
|
||||
@@ -29,12 +29,12 @@ char nibble_to_hex_character(uint8_t nibble) {
|
||||
|
||||
char* bytes_to_hex_string(uint8_t* bytes, size_t num_bytes) {
|
||||
char* hex_string = (char*)malloc(num_bytes * 2 + 1);
|
||||
if (hex_string == NULL) {
|
||||
if(hex_string == NULL) {
|
||||
FURI_LOG_D("LOG", "Memory allocation failed\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < num_bytes; ++i) {
|
||||
for(size_t i = 0; i < num_bytes; ++i) {
|
||||
hex_string[i * 2] = nibble_to_hex_character(bytes[i] >> 4);
|
||||
hex_string[i * 2 + 1] = nibble_to_hex_character(bytes[i] & 0xF);
|
||||
}
|
||||
@@ -74,20 +74,17 @@ static JSkeyboardInst* get_this_ctx(struct mjs* mjs) {
|
||||
return storage;
|
||||
}
|
||||
|
||||
void text_input_callback(void* context)
|
||||
{
|
||||
void text_input_callback(void* context) {
|
||||
JSkeyboardInst* keyboardinst = (JSkeyboardInst*)context;
|
||||
view_dispatcher_stop(keyboardinst->view_dispatcher);
|
||||
}
|
||||
|
||||
void byte_input_callback(void* context)
|
||||
{
|
||||
void byte_input_callback(void* context) {
|
||||
JSkeyboardInst* keyboardinst = (JSkeyboardInst*)context;
|
||||
view_dispatcher_stop(keyboardinst->view_dispatcher);
|
||||
}
|
||||
|
||||
static void js_keyboard_text(struct mjs *mjs) {
|
||||
|
||||
static void js_keyboard_text(struct mjs* mjs) {
|
||||
JSkeyboardInst* keyboardinst = get_this_ctx(mjs);
|
||||
|
||||
int MaxInputLength;
|
||||
@@ -100,18 +97,24 @@ static void js_keyboard_text(struct mjs *mjs) {
|
||||
mjs_val_t bool_obj = mjs_arg(mjs, 2);
|
||||
ShouldSelect = (int)mjs_get_bool(mjs, bool_obj);
|
||||
|
||||
if (keyboardinst->textinput && keyboardinst->view_dispatcher)
|
||||
{
|
||||
if (strlen(defaultText) > 0)
|
||||
{
|
||||
if(keyboardinst->textinput && keyboardinst->view_dispatcher) {
|
||||
if(strlen(defaultText) > 0) {
|
||||
text_input_set_header_text(keyboardinst->textinput, defaultText);
|
||||
}
|
||||
|
||||
view_dispatcher_attach_to_gui(
|
||||
keyboardinst->view_dispatcher, furi_record_open(RECORD_GUI), ViewDispatcherTypeFullscreen);
|
||||
keyboardinst->view_dispatcher,
|
||||
furi_record_open(RECORD_GUI),
|
||||
ViewDispatcherTypeFullscreen);
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
text_input_set_result_callback(keyboardinst->textinput, text_input_callback, keyboardinst, keyboardinst->data, MaxInputLength, ShouldSelect);
|
||||
text_input_set_result_callback(
|
||||
keyboardinst->textinput,
|
||||
text_input_callback,
|
||||
keyboardinst,
|
||||
keyboardinst->data,
|
||||
MaxInputLength,
|
||||
ShouldSelect);
|
||||
|
||||
view_dispatcher_switch_to_view(keyboardinst->view_dispatcher, 0);
|
||||
|
||||
@@ -123,10 +126,7 @@ static void js_keyboard_text(struct mjs *mjs) {
|
||||
mjs_return(mjs, mjs_mk_string(mjs, keyboardinst->data, strlen(keyboardinst->data), 1));
|
||||
}
|
||||
|
||||
static void js_keyboard_byte(struct mjs *mjs) {
|
||||
|
||||
|
||||
|
||||
static void js_keyboard_byte(struct mjs* mjs) {
|
||||
JSkeyboardInst* keyboardinst = get_this_ctx(mjs);
|
||||
|
||||
int MaxInputLength;
|
||||
@@ -135,18 +135,24 @@ static void js_keyboard_byte(struct mjs *mjs) {
|
||||
const char* defaultText;
|
||||
get_str_arg(mjs, 1, &defaultText);
|
||||
|
||||
if (keyboardinst->byteinputview && keyboardinst->view_dispatcher)
|
||||
{
|
||||
if (strlen(defaultText) > 0)
|
||||
{
|
||||
if(keyboardinst->byteinputview && keyboardinst->view_dispatcher) {
|
||||
if(strlen(defaultText) > 0) {
|
||||
byte_input_set_header_text(keyboardinst->byteinputview, defaultText);
|
||||
}
|
||||
|
||||
view_dispatcher_attach_to_gui(
|
||||
keyboardinst->view_dispatcher, furi_record_open(RECORD_GUI), ViewDispatcherTypeFullscreen);
|
||||
keyboardinst->view_dispatcher,
|
||||
furi_record_open(RECORD_GUI),
|
||||
ViewDispatcherTypeFullscreen);
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
byte_input_set_result_callback(keyboardinst->byteinputview, byte_input_callback, NULL, keyboardinst, keyboardinst->byteinput, 10);
|
||||
byte_input_set_result_callback(
|
||||
keyboardinst->byteinputview,
|
||||
byte_input_callback,
|
||||
NULL,
|
||||
keyboardinst,
|
||||
keyboardinst->byteinput,
|
||||
10);
|
||||
|
||||
view_dispatcher_switch_to_view(keyboardinst->view_dispatcher, 1);
|
||||
|
||||
@@ -160,7 +166,6 @@ static void js_keyboard_byte(struct mjs *mjs) {
|
||||
FURI_LOG_D("SHIT", "DID THING 2");
|
||||
}
|
||||
|
||||
|
||||
static void* js_keyboard_create(struct mjs* mjs, mjs_val_t* object) {
|
||||
JSkeyboardInst* keyboardinst = malloc(sizeof(JSkeyboardInst));
|
||||
mjs_val_t keyboard_obj = mjs_mk_object(mjs);
|
||||
@@ -173,8 +178,10 @@ static void* js_keyboard_create(struct mjs* mjs, mjs_val_t* object) {
|
||||
keyboardinst->data = malloc(100);
|
||||
keyboardinst->byteinput = malloc(100);
|
||||
view_dispatcher_enable_queue(keyboardinst->view_dispatcher);
|
||||
view_dispatcher_add_view(keyboardinst->view_dispatcher, 0, text_input_get_view(keyboardinst->textinput));
|
||||
view_dispatcher_add_view(keyboardinst->view_dispatcher, 1, byte_input_get_view(keyboardinst->byteinputview));
|
||||
view_dispatcher_add_view(
|
||||
keyboardinst->view_dispatcher, 0, text_input_get_view(keyboardinst->textinput));
|
||||
view_dispatcher_add_view(
|
||||
keyboardinst->view_dispatcher, 1, byte_input_get_view(keyboardinst->byteinputview));
|
||||
*object = keyboard_obj;
|
||||
return keyboardinst;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ static bool check_arg_count(struct mjs* mjs, size_t count) {
|
||||
}
|
||||
|
||||
void mjs_abs(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -27,11 +27,11 @@ void mjs_abs(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_acos(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
if (x < -1 || x > 1) {
|
||||
if(x < -1 || x > 1) {
|
||||
ret_bad_args(mjs, "Invalid input value for Math.acos");
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
@@ -39,11 +39,11 @@ void mjs_acos(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_acosh(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
if (x < 1) {
|
||||
if(x < 1) {
|
||||
ret_bad_args(mjs, "Invalid input value for Math.acosh");
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
@@ -51,7 +51,7 @@ void mjs_acosh(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_asin(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -59,7 +59,7 @@ void mjs_asin(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_asinh(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -67,7 +67,7 @@ void mjs_asinh(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_atan(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -75,7 +75,8 @@ void mjs_atan(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_atan2(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 2) || !mjs_is_number(mjs_arg(mjs, 0)) || !mjs_is_number(mjs_arg(mjs, 1))) {
|
||||
if(!check_arg_count(mjs, 2) || !mjs_is_number(mjs_arg(mjs, 0)) ||
|
||||
!mjs_is_number(mjs_arg(mjs, 1))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double y = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -84,11 +85,11 @@ void mjs_atan2(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_atanh(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
if (x <= -1 || x >= 1) {
|
||||
if(x <= -1 || x >= 1) {
|
||||
ret_bad_args(mjs, "Invalid input value for Math.atanh");
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
@@ -96,7 +97,7 @@ void mjs_atanh(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_cbrt(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -104,7 +105,7 @@ void mjs_cbrt(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_ceil(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -112,12 +113,12 @@ void mjs_ceil(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_clz32(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
unsigned int x = (unsigned int)mjs_get_int(mjs, mjs_arg(mjs, 0));
|
||||
int count = 0;
|
||||
while (x) {
|
||||
while(x) {
|
||||
x >>= 1;
|
||||
count++;
|
||||
}
|
||||
@@ -125,7 +126,7 @@ void mjs_clz32(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_cos(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -133,13 +134,13 @@ void mjs_cos(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_exp(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
double result = 1;
|
||||
double term = 1;
|
||||
for (int i = 1; i < 100; i++) {
|
||||
for(int i = 1; i < 100; i++) {
|
||||
term *= x / i;
|
||||
result += term;
|
||||
}
|
||||
@@ -147,7 +148,7 @@ void mjs_exp(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_floor(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -155,16 +156,16 @@ void mjs_floor(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_log(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
if (x <= 0) {
|
||||
if(x <= 0) {
|
||||
ret_bad_args(mjs, "Invalid input value for Math.log");
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double result = 0;
|
||||
while (x >= MJS_E) {
|
||||
while(x >= MJS_E) {
|
||||
x /= MJS_E;
|
||||
result++;
|
||||
}
|
||||
@@ -172,7 +173,8 @@ void mjs_log(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_max(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 2) || !mjs_is_number(mjs_arg(mjs, 0)) || !mjs_is_number(mjs_arg(mjs, 1))) {
|
||||
if(!check_arg_count(mjs, 2) || !mjs_is_number(mjs_arg(mjs, 0)) ||
|
||||
!mjs_is_number(mjs_arg(mjs, 1))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -181,7 +183,8 @@ void mjs_max(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_min(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 2) || !mjs_is_number(mjs_arg(mjs, 0)) || !mjs_is_number(mjs_arg(mjs, 1))) {
|
||||
if(!check_arg_count(mjs, 2) || !mjs_is_number(mjs_arg(mjs, 0)) ||
|
||||
!mjs_is_number(mjs_arg(mjs, 1))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -190,20 +193,21 @@ void mjs_min(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_pow(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 2) || !mjs_is_number(mjs_arg(mjs, 0)) || !mjs_is_number(mjs_arg(mjs, 1))) {
|
||||
if(!check_arg_count(mjs, 2) || !mjs_is_number(mjs_arg(mjs, 0)) ||
|
||||
!mjs_is_number(mjs_arg(mjs, 1))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double base = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
double exponent = mjs_get_double(mjs, mjs_arg(mjs, 1));
|
||||
double result = 1;
|
||||
for (int i = 0; i < exponent; i++) {
|
||||
for(int i = 0; i < exponent; i++) {
|
||||
result *= base;
|
||||
}
|
||||
mjs_return(mjs, mjs_mk_number(mjs, result));
|
||||
}
|
||||
|
||||
void mjs_random(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 0)) {
|
||||
if(!check_arg_count(mjs, 0)) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
const uint32_t random_val = furi_hal_random_get();
|
||||
@@ -212,7 +216,7 @@ void mjs_random(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_sign(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
@@ -220,13 +224,13 @@ void mjs_sign(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_sin(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
double result = x;
|
||||
double term = x;
|
||||
for (int i = 1; i < 10; i++) {
|
||||
for(int i = 1; i < 10; i++) {
|
||||
term *= -x * x / ((2 * i) * (2 * i + 1));
|
||||
result += term;
|
||||
}
|
||||
@@ -234,31 +238,30 @@ void mjs_sin(struct mjs* mjs) {
|
||||
}
|
||||
|
||||
void mjs_sqrt(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
if (x < 0) {
|
||||
if(x < 0) {
|
||||
ret_bad_args(mjs, "Invalid input value for Math.sqrt");
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double result = 1;
|
||||
while (result * result < x) {
|
||||
while(result * result < x) {
|
||||
result += (double)0.001;
|
||||
}
|
||||
mjs_return(mjs, mjs_mk_number(mjs, result));
|
||||
}
|
||||
|
||||
void mjs_trunc(struct mjs* mjs) {
|
||||
if (!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
if(!check_arg_count(mjs, 1) || !mjs_is_number(mjs_arg(mjs, 0))) {
|
||||
mjs_return(mjs, MJS_UNDEFINED);
|
||||
}
|
||||
double x = mjs_get_double(mjs, mjs_arg(mjs, 0));
|
||||
mjs_return(mjs, mjs_mk_number(mjs, x < 0 ? ceil(x) : floor(x)));
|
||||
}
|
||||
|
||||
|
||||
static void* js_math_create(struct mjs *mjs, mjs_val_t* object) {
|
||||
static void* js_math_create(struct mjs* mjs, mjs_val_t* object) {
|
||||
mjs_val_t math_obj = mjs_mk_object(mjs);
|
||||
mjs_set(mjs, math_obj, "abs", ~0, MJS_MK_FN(mjs_abs));
|
||||
mjs_set(mjs, math_obj, "acos", ~0, MJS_MK_FN(mjs_acos));
|
||||
@@ -286,9 +289,9 @@ static void* js_math_create(struct mjs *mjs, mjs_val_t* object) {
|
||||
mjs_set(mjs, math_obj, "PI", ~0, mjs_mk_number(mjs, MJS_PI));
|
||||
*object = math_obj;
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
static void js_math_destroy(void *ptr) {
|
||||
static void js_math_destroy(void* ptr) {
|
||||
UNUSED(ptr);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user