简体   繁体   中英

I need help combining or translating c and JS

I have some code in JavaScript that functions well and controls my lights from my watch by posting to a Particle micro controller.

The part that I need is

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

Which turns on my fan.

I also have a code in c which does voice control.

#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();
}

Where the comment is, I need to do what the javascript does there, which is turn on my light.

Does anyone know how I can do this? I can provide more of the js if needed. I don't have too much experience programming, so I would appreciate descriptive answers.

TYIA

The C code runs on the watch, and as such can't directly make http calls like the one the JavaScript code you show makes. The watch-based code is very limited in what it can do.

Your best bet is for your watch based C code to send a message to a companion app running on the phone, and have that companion app invoke the http request you require. Since companion apps can be written in JavaScript you should be able to re-used your existing code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM