1

对 pebble SDK 和 C 的全新和幼稚,但在其他语言方面有很多经验。这意味着当有人很容易地为我解决这个问题时,我可能会感到尴尬,但是......我在一个基于教程的表盘项目中有两个文件......我正在提取不同的数据,但它的工作原理就像入门教程,直到代码更新其中一个文本层...什么都没有。这让我感到困惑,因为它应该放在那里的数据在控制台日志中看起来很好,所以它肯定会被忽略。无论如何,这是C

include 
#define KEY_MONTHMILES 0
#define KEY_MONTHELEVATION 1
#define KEY_TOTALMILES 2
#define KEY_TOTALELEVATION 3

static Window *s_main_window;
static TextLayer *s_time_layer;
static BitmapLayer *s_background_layer;
static GBitmap *s_background_bitmap;
static TextLayer *s_weather_layer;

static void update_time() {
    // Get a tm structure
    time_t temp = time(NULL); 
    struct tm *tick_time = localtime(&temp);

    // Create a long-lived buffer
    static char buffer[] = "00:00";

    // Write the current hours and minutes into the buffer
    if(clock_is_24h_style() == true) {
        // Use 24 hour format
        strftime(buffer, sizeof("00:00"), "%H:%M", tick_time);
    } else {
        // Use 12 hour format
        strftime(buffer, sizeof("00:00"), "%I:%M", tick_time);
    }

    // Display this time on the TextLayer
    text_layer_set_text(s_time_layer, buffer);
}

static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
    update_time();
}

static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
    // Store incoming information
    static char monthlyMiles_buffer[128];
    static char monthlyElevation_buffer[128];
    static char yearlyMiles_buffer[128];
    static char yearlyElevation_buffer[128];

    // Read first item
    Tuple *t = dict_read_first(iterator);

    // For all items
    while(t != NULL) {
        // Which key was received?
        switch(t->key) {
            case KEY_MONTHMILES:
                snprintf(monthlyMiles_buffer, sizeof(monthlyMiles_buffer), "%d", (int)t->value->int32);
                break;
            case KEY_MONTHELEVATION:
                snprintf(monthlyElevation_buffer, sizeof(monthlyElevation_buffer), "%d", (int)t->value->int32);
                break;
            case KEY_TOTALMILES:
                snprintf(yearlyMiles_buffer, sizeof(yearlyMiles_buffer), "%d", (int)t->value->int32);
                break;
            case KEY_TOTALELEVATION:
                snprintf(yearlyElevation_buffer, sizeof(yearlyElevation_buffer), "%d", (int)t->value->int32);
                break;
            default:
                APP_LOG(APP_LOG_LEVEL_ERROR, "Key %d not recognized!", (int)t->key);
                break;
        }

        // Look for next item
        t = dict_read_next(iterator);
    }
    text_layer_set_text(s_weather_layer, monthlyMiles_buffer);
}

static void inbox_dropped_callback(AppMessageResult reason, void *context) {
    APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped!");
}

static void outbox_failed_callback(DictionaryIterator *iterator, AppMessageResult reason, void *context) {
    APP_LOG(APP_LOG_LEVEL_ERROR, "Outbox send failed!");
}

static void outbox_sent_callback(DictionaryIterator *iterator, void *context) {
    APP_LOG(APP_LOG_LEVEL_INFO, "Outbox send success!");
}

static void main_window_load(Window *window) {
    // Create GBitmap, then set to created BitmapLayer
    s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_BG_IMAGE);
    s_background_layer = bitmap_layer_create(GRect(0, 0, 144, 168));
    bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
    layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_background_layer)); 

    // Create time TextLayer
    s_time_layer = text_layer_create(GRect(0, 60, 144, 50));
    text_layer_set_background_color(s_time_layer, GColorClear);
    text_layer_set_text_color(s_time_layer, GColorWhite);
    layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer));

    // Improve the layout to be more like a watchface
    text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
    text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);

    // Make sure the time is displayed from the start
    update_time();

    // Create temperature Layer
    s_weather_layer = text_layer_create(GRect(0, 130, 144, 25));
    text_layer_set_background_color(s_weather_layer, GColorClear);
    text_layer_set_text_color(s_weather_layer, GColorWhite);
    text_layer_set_text_alignment(s_weather_layer, GTextAlignmentCenter);
    text_layer_set_text(s_weather_layer, "Loading...");
    text_layer_set_font(s_weather_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14));
    layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_weather_layer));
}

static void main_window_unload(Window *window) {
    // Destroy TextLayer
    text_layer_destroy(s_time_layer);

    // Destroy GBitmap
    gbitmap_destroy(s_background_bitmap);

    // Destroy BitmapLayer
    bitmap_layer_destroy(s_background_layer);

    // Destroy weather elements
    text_layer_destroy(s_weather_layer);
}

static void init() {
    // Create main Window element and assign to pointer
    s_main_window = window_create();

    // Set handlers to manage the elements inside the Window
    window_set_window_handlers(s_main_window, (WindowHandlers) {
        .load = main_window_load,
        .unload = main_window_unload
    });

    // Show the Window on the watch, with animated=true
    window_stack_push(s_main_window, true);

    // Register with TickTimerService
    tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);

    // Register callbacks
    app_message_register_inbox_received(inbox_received_callback);
    app_message_register_inbox_dropped(inbox_dropped_callback);
    app_message_register_outbox_failed(outbox_failed_callback);
    app_message_register_outbox_sent(outbox_sent_callback);

    // Open AppMessage
    app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
}

static void deinit() {
    // Destroy Window
    window_destroy(s_main_window);
}

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

这是Javascript

var xhrRequest = function (url, type, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function () {
        callback(this.responseText);
    };
    xhr.open(type, url);
    xhr.send();
};

function getStats() {
    // Construct URL
    var url = "http://crin.co.uk/stravaWatchface/getStats.php";

    // Send request to crin.co.uk
    xhrRequest(url, 'GET', 
    function(responseText) {
        // responseText contains a JSON object with stat info
        var json = JSON.parse(responseText);

        // Monthly miles
        var monthMiles = json.mM;
        console.log("Monthly total miles is: " + monthMiles);

        // Monthly Elevation
        var monthElevation = json.mE; 
        console.log("Monthly total elevation gain is: " + monthElevation);

        // Total Miles
        var totalMiles = json.tM; 
        console.log("All-time total miles is: " + totalMiles);

        // Total Elevation
        var totalElevation = json.tE; 
        console.log("All-time total elevation gain is: " + totalElevation);

        // Assemble dictionary using our keys
        var dictionary = {
            "KEY_MONTHMILES": monthMiles,
            "KEY_MONTHELEVATION": monthElevation,
            "KEY_TOTALMILES": totalMiles,
            "KEY_TOTALELEVATION": totalElevation
        };

        // Send to Pebble
        Pebble.sendAppMessage(dictionary,
        function(e) {
            console.log("Stats info sent to Pebble successfully!");
        },
        function(e) {
            console.log("Error sending stats info to Pebble!");
        }
        );
    } 
    );
}

// Listen for when the watchface is opened
Pebble.addEventListener('ready', 
function(e) {
    console.log("PebbleKit JS ready!");

    // Get the initial weather
    getStats();
}
);

// Listen for when an AppMessage is received
Pebble.addEventListener('appmessage',
function(e) {
    console.log("AppMessage received!");
    getStats();
} 
);

有人发现任何明显的东西吗?它编译和安装很好,但是这一行......

text_layer_set_text(s_weather_layer, monthlyMiles_buffer);

正在用空格而不是应有的值更新表盘。

请帮助我为此浪费了一个下午。

4

0 回答 0