【问题标题】:I need help combining or translating c and JS我需要帮助结合或翻译 c 和 JS
【发布时间】:2015-10-29 20:10:19
【问题描述】:

我有一些运行良好的 JavaScript 代码,并通过发布到 Particle 微控制器来控制手表上的灯光。

我需要的部分是

 ajax(
      {
        url: particleAPIURL + 'devices/' + particleDeviceID + '/fanIO',
        method: 'post',
        data: {access_token: particleAPIKey, args: fanIO}
      });

这会打开我的风扇。

我也有一个用 c 语言控制的代码。

#include <pebble.h>



static Window *s_main_window;
static TextLayer *s_output_layer;

static DictationSession *s_dictation_session;
static char s_last_text[100];
int desk = 0;
int bright = 0;
int fan = 0;
/******************************* Dictation API ********************************/

static void dictation_session_callback(DictationSession *session, DictationSessionStatus status,
                                       char *transcription, void *context) {

    //Show the text
    /********** IMPORTANT**************************************************/


  if(status == DictationSessionStatusSuccess) {
    // Display the dictated text

        if (strcmp(transcription, "Desk light") == 0) {


        snprintf(s_last_text, sizeof(s_last_text), "It Worked");
        text_layer_set_text(s_output_layer, s_last_text);

            if(desk == 0) {
                desk = 1;
              //I NEED TO DO WHAT THE JS DOES HERE
            }

    }
        else {

            snprintf(s_last_text, sizeof(s_last_text), "I'm sorry, blank. I'm afraid I can't do that...");
        text_layer_set_text(s_output_layer, s_last_text);


        }



  }

    //ERROR
    else {
    // Display the reason for any error
    static char s_failed_buff[128];
    snprintf(s_failed_buff, sizeof(s_failed_buff), "Transcription failed.\n\nError ID:\n%d", (int)status);
    text_layer_set_text(s_output_layer, s_failed_buff);
  }
}

/************************************ App *************************************/

//Click the middle button
static void select_click_handler(ClickRecognizerRef recognizer, void *context) {



    // Start voice dictation UI
  dictation_session_start(s_dictation_session);

}



static void click_config_provider(void *context) {
  window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
}



static void window_load(Window *window) {
  Layer *window_layer = window_get_root_layer(window);
  GRect bounds = layer_get_bounds(window_layer);

  s_output_layer = text_layer_create(GRect(bounds.origin.x, (bounds.size.h - 24) / 2, bounds.size.w, bounds.size.h));
  text_layer_set_text(s_output_layer, "Press Select to get input!");
  text_layer_set_text_alignment(s_output_layer, GTextAlignmentCenter);
  layer_add_child(window_layer, text_layer_get_layer(s_output_layer));
}

//What to do when exit app
static void window_unload(Window *window) {
  text_layer_destroy(s_output_layer);
}


static void init() {
  s_main_window = window_create();
  window_set_click_config_provider(s_main_window, click_config_provider);
  window_set_window_handlers(s_main_window, (WindowHandlers) {
    .load = window_load,
    .unload = window_unload,
  });
  window_stack_push(s_main_window, true);




  // Create new dictation session

    //s_last_text is size of info you can input
  s_dictation_session = dictation_session_create(sizeof(s_last_text), dictation_session_callback, NULL);
}

static void deinit() {
  // Free the last session data
  dictation_session_destroy(s_dictation_session);

  window_destroy(s_main_window);
}

int main() {
  init();
  app_event_loop();
  deinit();
}

评论在哪里,我需要做javascript在那里做的事情,那就是打开我的灯。

有人知道我该怎么做吗? 如果需要,我可以提供更多的 js。 我没有太多的编程经验,所以我会很感激描述性的答案。

TYIA

【问题讨论】:

    标签: javascript c pebble-watch pebble-sdk


    【解决方案1】:

    C 代码在手表上运行,因此不能像您展示的 JavaScript 代码那样直接进行 http 调用。基于 watch 的代码能做的事情非常有限。

    您最好将基于手表的 C 代码 send a message 发送到手机上运行的配套应用程序,并让该配套应用程序调用您需要的 http 请求。由于配套应用程序can be written in JavaScript,您应该能够重用您现有的代码。

    【讨论】:

    • 您认为是否有可能在应用程序中使用 pebble.js 编写另一个源文件并让 C 向其发送消息?
    • pebble.js 为您提供了一种创建完整手表应用程序的方法。您使用在手机上运行的 JavaScript 编写所有代码,它会代表您生成手表应用交互。如果你需要做语音识别之类的事情,你需要编写在手表上运行的 C 代码。我建议使用 PebbleKit JS 而不是 pebble.js - 这是一个完整的示例:github.com/pebble-examples/pebblekit-js-quotes 它显示了手表上运行的 C 代码与手机上的 JavaScript 对话。
    • 您能解释一下如何使用它来解决我的问题吗?抱歉,我对编程很陌生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多