胶水语言可以让我们职责更加明确,可以类前后端开发模式,后端负责--硬件的适配,方法的导出;前端负责--业务逻辑的的开发。
本篇文章作者来介绍JS在嵌入式设备上的如何使用,我们通过一下几点说明:
在使用JerryScript引擎实现C端与JavaScript端交互时,许哟啊弄清楚几个概念:
类型 | 说明 |
---|---|
object | 对象,JerryScript是基于对象编程的,所以我们导出的方法都是对象的基础上的 |
property | 特性,如导出的参数,函数等,都属于特性,归属于某个对象的特性 |
value | 值,在JerryScript中,所有的对象,特性,参数,方法,都会转为一个32位的数值,然后我们通过这个数值便可以访问对应内容 |
typedef jerry_value_t (*jerry_external_handler_t) (const jerry_value_t function_obj,
const jerry_value_t this_val,
const jerry_value_t args_p[],
const jerry_length_t args_count);
static jerry_value_t log_handler(const jerry_value_t function_obj,
const jerry_value_t this_val,
const jerry_value_t args[],
const jerry_length_t argCount)
{
jerry_size_t log_len = 0;
jerry_value_t str_value = jerry_value_to_string(args[0]);
log_len = jerry_string_to_utf8_char_buffer(str_value, log_buff, (LOG_BUFF_LEN - 1));
log_buff[log_len] = '';
jerry_release_value(str_value);
rt_kprintf("%srn", (const char *)log_buff);
return jerry_create_undefined();
}
static void js_func_init(void)
{
jerry_value_t global_object = jerry_get_global_object();
jerry_value_t func = jerry_create_external_function(log_handler);
jerry_value_t property_name_val = jerry_create_string_from_utf8 ((const jerry_char_t *)"log");
jerry_value_t result_val = jerry_set_property (global_object, property_name_val, func);
jerry_release_value (property_name_val);
jerry_release_value (result_val);
jerry_release_value(func);
}
var hello = 123;
log("youyeetoo: " + hello);
#define E_VALUE 123
#define E_STRING "youyeetoo"
static void js_num_param_init(void)
{
jerry_value_t global_object = jerry_get_global_object();
jerry_value_t num_val = jerry_create_number(123);
jerry_value_t property_name_val = jerry_create_string_from_utf8 ((const jerry_char_t *)"E_VALUE");
jerry_value_t result_val = jerry_set_property (global_object, property_name_val, num_val);
jerry_release_value (property_name_val);
jerry_release_value (result_val);
jerry_release_value(num_val);
}
static void js_str_param_init(void)
{
jerry_value_t global_object = jerry_get_global_object();
jerry_value_t str_val = jerry_create_string("good");
jerry_value_t property_name_val = jerry_create_string_from_utf8 ((const jerry_char_t *)"E_STRING");
jerry_value_t result_val = jerry_set_property (global_object, property_name_val, str_val);
jerry_release_value (property_name_val);
jerry_release_value (result_val);
jerry_release_value(str_val);
}
log("youyeetoo: " + E_VALUE);
log("youyeetoo: " + E_STRING);
#define LED1_PIN GET_PIN(E, 3)
#define LED2_PIN GET_PIN(D, 7)
#define LED3_PIN GET_PIN(G, 3)
#define LED4_PIN GET_PIN(A, 5)
static uint16_t led_list[] = {LED1_PIN, LED2_PIN, LED3_PIN, LED4_PIN};
static jerry_value_t led_handler(const jerry_value_t function_obj,
const jerry_value_t this_val,
const jerry_value_t args[],
const jerry_length_t argCount)
{
jerry_value_t led = jerry_get_number_value(args[0]);
jerry_value_t level = jerry_get_number_value(args[1]);
rt_pin_write(led_list[led], level);
return jerry_create_undefined();
}
static void js_led_func_init(void)
{
jerry_value_t global_object = jerry_get_global_object();
jerry_value_t func = jerry_create_external_function(led_handler);
jerry_value_t property_name_val = jerry_create_string_from_utf8 ((const jerry_char_t *)"led");
jerry_value_t result_val = jerry_set_property (global_object, property_name_val, func);
jerry_release_value (property_name_val);
jerry_release_value (result_val);
jerry_release_value(func);
}
log("youyeetoo: " + E_VALUE);
log("youyeetoo: " + E_STRING);
led(0, 1);
led(1, 1);
led(2, 1);
led(3, 1);
全部0条评论
快来发表一下你的评论吧 !