Pebble 用の非常に単純なアーチェリー フィールド スコア追跡アプリを作成していますが、セッション間の整数値の保存と取得に問題があります。保存と取得の動作は正常に機能していますが、奇妙な理由で 1 つの値のみが取得され、2 番目の変数に入力されるため、両方が同じになります。
それを理解しようとしている間、私のデバッグログを含む私の完全なコードを貼り付けます。ウィンドウをアンロードしてもすべての値は正しいのですが、次のウィンドウのロード時に値を取得すると、1 になるようです... ?
/* TODO
- Show time bar along top of screen
*/
#include <pebble.h>
uint32_t keyTotal,keyTargets;
static Window *window;
static TextLayer *text_layer_SCORE,*text_layer_TARGETS,*text_layer_TOTAL,*text_layer_TITLE;
int score,targets,total;
char targetText[]="Targets: ";
char scoreText[]="Score: ";
char totalText[]="TOTAL: ";
char currentScore[]="Score: xxxx";
char currentTotal[]="TOTAL: xxxx";
char currentTargets[]="Targets: xxx";
static void logValues(char *string) {
APP_LOG(APP_LOG_LEVEL_INFO, string);
APP_LOG(APP_LOG_LEVEL_DEBUG, "----- SCORE: %i", score);
APP_LOG(APP_LOG_LEVEL_DEBUG, "----- TOTAL: %i", total);
APP_LOG(APP_LOG_LEVEL_DEBUG, "----- TARGETS: %i", targets);
}
static void updateScore() {
snprintf(currentScore,sizeof(currentScore),"%s %d",scoreText,score);
text_layer_set_text(text_layer_SCORE, currentScore);
logValues("----- AFTER UPDATE SCORE");
}
static void updateTotal() {
snprintf(currentTotal,sizeof(currentTotal),"%s %d",totalText,total);
text_layer_set_text(text_layer_TOTAL, currentTotal);
logValues("----- UPDATE TOTAL");
}
static void updateTargets() {
snprintf(currentTargets,sizeof(currentTargets),"%s %d",targetText,targets);
text_layer_set_text(text_layer_TARGETS, currentTargets);
logValues("----- AFTER UPDATE TARGETS");
}
static void updateAll() {
updateTotal();
updateTargets();
updateScore();
logValues("----- AFTER UPDATE ALL");
}
static void persistValues() {
persist_write_int(keyTotal, total);
persist_write_int(keyTargets, targets);
logValues("----- AFTER PERSIST VALUES");
}
static void resetValues() {
score = 0;
total = 0;
targets = 0;
persist_write_int(keyTargets, 0);
persist_write_int(keyTotal, 0);
logValues("----- AFTER RESET VALUES");
}
static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
total = total + score;
++targets;
updateAll();
logValues("----- AFTER SELECT PRESSED");
}
static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
// text_layer_set_text(text_layer, "Up");
score++;
updateScore();
logValues("----- AFTER UP PRESSED");
}
static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
// text_layer_set_text(text_layer, "Down");
score--;
updateScore();
logValues("----- AFTER DOWN PRESSED");
}
static void up_long_click_handler(ClickRecognizerRef recognizer, void *context) {
//Window *window = (Window *)context;
}
static void up_long_click_release_handler(ClickRecognizerRef recognizer, void *context) {
//Window *window = (Window *)
resetValues();
updateAll();
}
static void click_config_provider(void *context) {
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
window_long_click_subscribe(BUTTON_ID_UP, 3000, up_long_click_handler, up_long_click_release_handler);
}
static void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
logValues("----- START OF WINDOW LOAD");
score = 0;
// Get previous total (if it exists)
if(persist_exists(keyTotal)) {
total = persist_read_int(keyTotal);
} else {
total = 0;
//persist_write_int(keyTotal, total);
APP_LOG(APP_LOG_LEVEL_DEBUG, "----- PERSIST TOTAL NOT FOUND, SETTING: %i", total);
}
// Get previous targets (if it exists)
if(persist_exists(keyTargets)) {
targets = persist_read_int(keyTargets);
} else {
targets = 0;
//persist_write_int(keyTargets, targets);
APP_LOG(APP_LOG_LEVEL_DEBUG, "----- PERSIST TARGETS NOT FOUND, SETTING: %i", targets);
}
APP_LOG(APP_LOG_LEVEL_DEBUG, "----- PERSIST END, TOTAL IS CURRENTLY: %i", total);
APP_LOG(APP_LOG_LEVEL_DEBUG, "----- PERSIST END, TOTAL IS CURRENTLY: %i", total);
window_set_click_config_provider(window, (ClickConfigProvider) click_config_provider);
text_layer_TITLE = text_layer_create(GRect(0, 0, bounds.size.w, bounds.size.h));
text_layer_SCORE = text_layer_create(GRect(0, 40, bounds.size.w, 35));
text_layer_TARGETS = text_layer_create(GRect(0, 80, bounds.size.w, 35));
text_layer_TOTAL = text_layer_create(GRect(0,120, bounds.size.w, 50));
text_layer_set_text(text_layer_TITLE, "ScoreTracker");
text_layer_set_text(text_layer_SCORE, currentScore);
text_layer_set_text(text_layer_TARGETS, currentTargets);
text_layer_set_text(text_layer_TOTAL, currentTotal);
updateAll();
text_layer_set_text_color(text_layer_TOTAL, GColorWhite);
text_layer_set_text_color(text_layer_TARGETS, GColorWhite);
text_layer_set_text_color(text_layer_SCORE, GColorBlack);
text_layer_set_background_color(text_layer_TITLE, GColorGreen);
text_layer_set_background_color(text_layer_TOTAL, GColorBlack);
text_layer_set_background_color(text_layer_SCORE, GColorLightGray);
text_layer_set_background_color(text_layer_TARGETS, GColorBlue);
text_layer_set_text_alignment(text_layer_TITLE, GTextAlignmentCenter);
text_layer_set_text_alignment(text_layer_SCORE, GTextAlignmentLeft);
text_layer_set_text_alignment(text_layer_TARGETS, GTextAlignmentLeft);
text_layer_set_text_alignment(text_layer_TOTAL, GTextAlignmentCenter);
// Fonts
text_layer_set_font(text_layer_TITLE, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_font(text_layer_SCORE, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
text_layer_set_font(text_layer_TARGETS, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
text_layer_set_font(text_layer_TOTAL, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
layer_add_child(window_layer, text_layer_get_layer(text_layer_TITLE));
layer_add_child(window_layer, text_layer_get_layer(text_layer_SCORE));
layer_add_child(window_layer, text_layer_get_layer(text_layer_TARGETS));
layer_add_child(window_layer, text_layer_get_layer(text_layer_TOTAL));
logValues("----- END OF WINDOW LOAD");
}
static void window_unload(Window *window) {
text_layer_destroy(text_layer_SCORE);
text_layer_destroy(text_layer_TARGETS);
text_layer_destroy(text_layer_TOTAL);
text_layer_destroy(text_layer_TITLE);
persistValues();
logValues("----- END OF WINDOW UNLOAD");
}
static void init(void) {
window = window_create();
window_set_click_config_provider(window, click_config_provider);
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
const bool animated = true;
window_stack_push(window, animated);
logValues("----- END OF INIT");
}
static void deinit(void) {
window_destroy(window);
}
int main(void) {
init();
APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window);
app_event_loop();
deinit();
}
上記のアプリ コードを実行し、スコアを変更し、アプリを終了してリロードしたときのログ出力を次に示します。
[INFO] ocess_manager.c:419: Heap Usage for App <ScoreTrack: Total Size <62480B> Used <628B> Still allocated <0B>
[INFO] button_click.c:19: ----- START OF WINDOW LOAD
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[DEBUG] button_click.c:126: ----- PERSIST TOTAL NOT FOUND, SETTING: 0
[DEBUG] button_click.c:135: ----- PERSIST TARGETS NOT FOUND, SETTING: 0
[DEBUG] button_click.c:138: ----- PERSIST END, TOTAL IS CURRENTLY: 0
[DEBUG] button_click.c:139: ----- PERSIST END, TARGETS IS CURRENTLY: 0
[INFO] button_click.c:19: ----- UPDATE TOTAL
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UPDATE TARGETS
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UPDATE ALL
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- END OF WINDOW LOAD
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- END OF INIT
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[DEBUG] button_click.c:213: Done initializing, pushed window: 0x20020c0c
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 1
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UP PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 1
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 2
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UP PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 2
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 3
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UP PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 3
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- AFTER UP PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[INFO] button_click.c:19: ----- UPDATE TOTAL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 4
[DEBUG] button_click.c:22: ----- TARGETS: 1
[INFO] button_click.c:19: ----- AFTER UPDATE TARGETS
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 4
[DEBUG] button_click.c:22: ----- TARGETS: 1
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 4
[DEBUG] button_click.c:22: ----- TARGETS: 1
[INFO] button_click.c:19: ----- AFTER UPDATE ALL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 4
[DEBUG] button_click.c:22: ----- TARGETS: 1
[INFO] button_click.c:19: ----- AFTER SELECT PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 4
[DEBUG] button_click.c:22: ----- TARGETS: 1
[INFO] button_click.c:19: ----- UPDATE TOTAL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 8
[DEBUG] button_click.c:22: ----- TARGETS: 2
[INFO] button_click.c:19: ----- AFTER UPDATE TARGETS
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 8
[DEBUG] button_click.c:22: ----- TARGETS: 2
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 8
[DEBUG] button_click.c:22: ----- TARGETS: 2
[INFO] button_click.c:19: ----- AFTER UPDATE ALL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 8
[DEBUG] button_click.c:22: ----- TARGETS: 2
[INFO] button_click.c:19: ----- AFTER SELECT PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 8
[DEBUG] button_click.c:22: ----- TARGETS: 2
[INFO] button_click.c:19: ----- UPDATE TOTAL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 12
[DEBUG] button_click.c:22: ----- TARGETS: 3
[INFO] button_click.c:19: ----- AFTER UPDATE TARGETS
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 12
[DEBUG] button_click.c:22: ----- TARGETS: 3
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 12
[DEBUG] button_click.c:22: ----- TARGETS: 3
[INFO] button_click.c:19: ----- AFTER UPDATE ALL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 12
[DEBUG] button_click.c:22: ----- TARGETS: 3
[INFO] button_click.c:19: ----- AFTER SELECT PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 12
[DEBUG] button_click.c:22: ----- TARGETS: 3
[INFO] button_click.c:19: ----- UPDATE TOTAL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 16
[DEBUG] button_click.c:22: ----- TARGETS: 4
[INFO] button_click.c:19: ----- AFTER UPDATE TARGETS
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 16
[DEBUG] button_click.c:22: ----- TARGETS: 4
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 16
[DEBUG] button_click.c:22: ----- TARGETS: 4
[INFO] button_click.c:19: ----- AFTER UPDATE ALL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 16
[DEBUG] button_click.c:22: ----- TARGETS: 4
[INFO] button_click.c:19: ----- AFTER SELECT PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 16
[DEBUG] button_click.c:22: ----- TARGETS: 4
[INFO] button_click.c:19: ----- UPDATE TOTAL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 20
[DEBUG] button_click.c:22: ----- TARGETS: 5
[INFO] button_click.c:19: ----- AFTER UPDATE TARGETS
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 20
[DEBUG] button_click.c:22: ----- TARGETS: 5
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 20
[DEBUG] button_click.c:22: ----- TARGETS: 5
[INFO] button_click.c:19: ----- AFTER UPDATE ALL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 20
[DEBUG] button_click.c:22: ----- TARGETS: 5
[INFO] button_click.c:19: ----- AFTER SELECT PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 20
[DEBUG] button_click.c:22: ----- TARGETS: 5
[INFO] button_click.c:19: ----- UPDATE TOTAL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 24
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- AFTER UPDATE TARGETS
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 24
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 24
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- AFTER UPDATE ALL
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 24
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- AFTER SELECT PRESSED
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 24
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- AFTER PERSIST VALUES
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 24
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- END OF WINDOW UNLOAD
[DEBUG] button_click.c:20: ----- SCORE: 4
[DEBUG] button_click.c:21: ----- TOTAL: 24
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] ocess_manager.c:419: Heap Usage for App <ScoreTrack: Total Size <62480B> Used <628B> Still allocated <0B>
[INFO] button_click.c:19: ----- START OF WINDOW LOAD
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 0
[DEBUG] button_click.c:22: ----- TARGETS: 0
[DEBUG] button_click.c:138: ----- PERSIST END, TOTAL IS CURRENTLY: 6
[DEBUG] button_click.c:139: ----- PERSIST END, TARGETS IS CURRENTLY: 6
[INFO] button_click.c:19: ----- UPDATE TOTAL
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 6
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- AFTER UPDATE TARGETS
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 6
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- AFTER UPDATE SCORE
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 6
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- AFTER UPDATE ALL
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 6
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- END OF WINDOW LOAD
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 6
[DEBUG] button_click.c:22: ----- TARGETS: 6
[INFO] button_click.c:19: ----- END OF INIT
[DEBUG] button_click.c:20: ----- SCORE: 0
[DEBUG] button_click.c:21: ----- TOTAL: 6
[DEBUG] button_click.c:22: ----- TARGETS: 6
[DEBUG] button_click.c:213: Done initializing, pushed window: 0x20020c0c
これらの値を保存および取得する代わりにデータ構造を使用しようとしましたが、これがどのように行われるかを理解することはできません。Pebble 開発サイトの例は、空白を埋めるのに十分なほどパディングされていません。このようなことについてはまだ趣味のコーダーにすぎないので、ここで完全に見逃している基本的なことがたくさんあるでしょう。どんな助けでも大歓迎です!