Gm,
Ik heb het volgende issue. Via een JS haal ik een waarde op uit een API. Middels een dictionary schrijf ik deze code weg. Nu wil ik in C# de waarde weer ophalen en er vervolgens mee aan de slag.
NB: de reden dat ik zowel JS als C gebruik is dat het om een Watchapp gaat en beiden standaards daarin gehanteerd worden.
Zo schrijf ik de code weg in JS:
//Construct a key-value dictionary
var dict = {"KEY_LOCATION" : location, "KEY_TEMPERATURE": temperature};
//Send data to watch for display
Pebble.sendAppMessage(dict);;
In deze code wil ik de waarde terug laten komen als s_num_drinks
#include "pebble.h"
#define REPEAT_INTERVAL_MS 50
// This is a custom defined key for saving our count field
#define NUM_DRINKS_PKEY 1
// You can define defaults for values in persistent storage
#define NUM_DRINKS_DEFAULT 0
static Window *s_main_window;
static ActionBarLayer *s_action_bar;
static TextLayer *s_header_layer, *s_body_layer, *s_label_layer;
static GBitmap *s_icon_plus, *s_icon_minus;
static int s_num_drinks = NUM_DRINKS_DEFAULT;
static void update_text() {
static char s_body_text[18];
snprintf(s_body_text, sizeof(s_body_text), "%u Bottles", s_num_drinks);
text_layer_set_text(s_body_layer, s_body_text);
}
static void increment_click_handler(ClickRecognizerRef recognizer, void *context) {
s_num_drinks++;
update_text();
}
static void decrement_click_handler(ClickRecognizerRef recognizer, void *context) {
if (s_num_drinks <= 0) {
// Keep the counter at zero
return;
}
s_num_drinks--;
update_text();
}
static void click_config_provider(void *context) {
window_single_repeating_click_subscribe(BUTTON_ID_UP, REPEAT_INTERVAL_MS, increment_click_handler);
window_single_repeating_click_subscribe(BUTTON_ID_DOWN, REPEAT_INTERVAL_MS, decrement_click_handler);
}
static void main_window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
s_action_bar = action_bar_layer_create();
action_bar_layer_add_to_window(s_action_bar, window);
action_bar_layer_set_click_config_provider(s_action_bar, click_config_provider);
action_bar_layer_set_icon(s_action_bar, BUTTON_ID_UP, s_icon_plus);
action_bar_layer_set_icon(s_action_bar, BUTTON_ID_DOWN, s_icon_minus);
int width = layer_get_frame(window_layer).size.w - ACTION_BAR_WIDTH - 3;
s_header_layer = text_layer_create(GRect(4, 0, width, 60));
text_layer_set_font(s_header_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24));
text_layer_set_background_color(s_header_layer, GColorClear);
text_layer_set_text(s_header_layer, "Drink Counter");
layer_add_child(window_layer, text_layer_get_layer(s_header_layer));
s_body_layer = text_layer_create(GRect(4, 44, width, 60));
text_layer_set_font(s_body_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_background_color(s_body_layer, GColorClear);
layer_add_child(window_layer, text_layer_get_layer(s_body_layer));
s_label_layer = text_layer_create(GRect(4, 44 + 28, width, 60));
text_layer_set_font(s_label_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18));
text_layer_set_background_color(s_label_layer, GColorClear);
text_layer_set_text(s_label_layer, "of drinks on the wall");
layer_add_child(window_layer, text_layer_get_layer(s_label_layer));
update_text();
}
static void main_window_unload(Window *window) {
text_layer_destroy(s_header_layer);
text_layer_destroy(s_body_layer);
text_layer_destroy(s_label_layer);
action_bar_layer_destroy(s_action_bar);
}
static void init() {
s_icon_plus = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ACTION_ICON_PLUS);
s_icon_minus = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ACTION_ICON_MINUS);
// Get the count from persistent storage for use if it exists, otherwise use the default
s_num_drinks = persist_exists(NUM_DRINKS_PKEY) ? persist_read_int(NUM_DRINKS_PKEY) : NUM_DRINKS_DEFAULT;
s_main_window = window_create();
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload,
});
window_stack_push(s_main_window, true);
}
static void deinit() {
// Save the count into persistent storage on app exit
persist_write_int(NUM_DRINKS_PKEY, s_num_drinks);
window_destroy(s_main_window);
gbitmap_destroy(s_icon_plus);
gbitmap_destroy(s_icon_minus);
}
int main(void) {
init();
app_event_loop();
deinit();
}
Ik heb het volgende issue. Via een JS haal ik een waarde op uit een API. Middels een dictionary schrijf ik deze code weg. Nu wil ik in C# de waarde weer ophalen en er vervolgens mee aan de slag.
NB: de reden dat ik zowel JS als C gebruik is dat het om een Watchapp gaat en beiden standaards daarin gehanteerd worden.
Zo schrijf ik de code weg in JS:
//Construct a key-value dictionary
var dict = {"KEY_LOCATION" : location, "KEY_TEMPERATURE": temperature};
//Send data to watch for display
Pebble.sendAppMessage(dict);;
In deze code wil ik de waarde terug laten komen als s_num_drinks
#include "pebble.h"
#define REPEAT_INTERVAL_MS 50
// This is a custom defined key for saving our count field
#define NUM_DRINKS_PKEY 1
// You can define defaults for values in persistent storage
#define NUM_DRINKS_DEFAULT 0
static Window *s_main_window;
static ActionBarLayer *s_action_bar;
static TextLayer *s_header_layer, *s_body_layer, *s_label_layer;
static GBitmap *s_icon_plus, *s_icon_minus;
static int s_num_drinks = NUM_DRINKS_DEFAULT;
static void update_text() {
static char s_body_text[18];
snprintf(s_body_text, sizeof(s_body_text), "%u Bottles", s_num_drinks);
text_layer_set_text(s_body_layer, s_body_text);
}
static void increment_click_handler(ClickRecognizerRef recognizer, void *context) {
s_num_drinks++;
update_text();
}
static void decrement_click_handler(ClickRecognizerRef recognizer, void *context) {
if (s_num_drinks <= 0) {
// Keep the counter at zero
return;
}
s_num_drinks--;
update_text();
}
static void click_config_provider(void *context) {
window_single_repeating_click_subscribe(BUTTON_ID_UP, REPEAT_INTERVAL_MS, increment_click_handler);
window_single_repeating_click_subscribe(BUTTON_ID_DOWN, REPEAT_INTERVAL_MS, decrement_click_handler);
}
static void main_window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
s_action_bar = action_bar_layer_create();
action_bar_layer_add_to_window(s_action_bar, window);
action_bar_layer_set_click_config_provider(s_action_bar, click_config_provider);
action_bar_layer_set_icon(s_action_bar, BUTTON_ID_UP, s_icon_plus);
action_bar_layer_set_icon(s_action_bar, BUTTON_ID_DOWN, s_icon_minus);
int width = layer_get_frame(window_layer).size.w - ACTION_BAR_WIDTH - 3;
s_header_layer = text_layer_create(GRect(4, 0, width, 60));
text_layer_set_font(s_header_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24));
text_layer_set_background_color(s_header_layer, GColorClear);
text_layer_set_text(s_header_layer, "Drink Counter");
layer_add_child(window_layer, text_layer_get_layer(s_header_layer));
s_body_layer = text_layer_create(GRect(4, 44, width, 60));
text_layer_set_font(s_body_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_background_color(s_body_layer, GColorClear);
layer_add_child(window_layer, text_layer_get_layer(s_body_layer));
s_label_layer = text_layer_create(GRect(4, 44 + 28, width, 60));
text_layer_set_font(s_label_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18));
text_layer_set_background_color(s_label_layer, GColorClear);
text_layer_set_text(s_label_layer, "of drinks on the wall");
layer_add_child(window_layer, text_layer_get_layer(s_label_layer));
update_text();
}
static void main_window_unload(Window *window) {
text_layer_destroy(s_header_layer);
text_layer_destroy(s_body_layer);
text_layer_destroy(s_label_layer);
action_bar_layer_destroy(s_action_bar);
}
static void init() {
s_icon_plus = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ACTION_ICON_PLUS);
s_icon_minus = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ACTION_ICON_MINUS);
// Get the count from persistent storage for use if it exists, otherwise use the default
s_num_drinks = persist_exists(NUM_DRINKS_PKEY) ? persist_read_int(NUM_DRINKS_PKEY) : NUM_DRINKS_DEFAULT;
s_main_window = window_create();
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload,
});
window_stack_push(s_main_window, true);
}
static void deinit() {
// Save the count into persistent storage on app exit
persist_write_int(NUM_DRINKS_PKEY, s_num_drinks);
window_destroy(s_main_window);
gbitmap_destroy(s_icon_plus);
gbitmap_destroy(s_icon_minus);
}
int main(void) {
init();
app_event_loop();
deinit();
}