Skip to content

Commit

Permalink
Implement settings as a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
cguldner committed Dec 7, 2016
1 parent 36af3a2 commit a347fdb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Cody",
"dependencies": {
"pebble-clay": "^1.0.2"
"pebble-clay": "^1.0.4"
},
"keywords": [],
"name": "metronome",
Expand Down
21 changes: 11 additions & 10 deletions src/c/auxiliary.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* Updates the bpm by the amount specified
*/
void update_bpm(int amount) {
int new_bpm = bpm + amount;
int new_bpm = settings.bpm + amount;
if(new_bpm < 1 || new_bpm > 300)
return;
bpm = new_bpm;
text_layer_set_text(bpm_text_layer, int_to_str(bpm));
text_layer_set_text(tempo_text_layer, get_tempo_marking(bpm));
settings.bpm = new_bpm;
text_layer_set_text(bpm_text_layer, int_to_str(settings.bpm));
text_layer_set_text(tempo_text_layer, get_tempo_marking(settings.bpm));
}

/**
Expand Down Expand Up @@ -106,7 +106,8 @@ char * get_tempo_marking(int bpm) {
/**
* Changes the tempo marking to either the next or previous based on the dir given, either 1 or -1
*/
int change_tempo_marking(int dir) {
int change_tempo_marking(int dir) {
int bpm = settings.bpm;
switch(bpm) {
case 1 ... 20:
bpm = dir==1 ? 21 : 1;
Expand Down Expand Up @@ -153,16 +154,16 @@ int change_tempo_marking(int dir) {
* @param toggle - a pointer to a boolean, 0 to reset
*/
void toggle_colors(int *toggle) {
GColor fg_color_con = GColorFromHEX(fg_color);
GColor bg_color_con = GColorFromHEX(bg_color);
GColor fg_color_con = GColorFromHEX(settings.fg_color);
GColor bg_color_con = GColorFromHEX(settings.bg_color);
if(*toggle) {
window_set_background_color(window, fg_color_con);
text_layer_set_background_color(bpm_text_layer, fg_color_con);
text_layer_set_text_color(bpm_text_layer, bg_color_con);
text_layer_set_background_color(tempo_text_layer, fg_color_con);
text_layer_set_text_color(tempo_text_layer, bg_color_con);

meter_color = bg_color;
meter_color = settings.bg_color;
}
// Value of 0 resets the colors
else {
Expand All @@ -172,7 +173,7 @@ void toggle_colors(int *toggle) {
text_layer_set_background_color(tempo_text_layer, bg_color_con);
text_layer_set_text_color(tempo_text_layer, fg_color_con);

meter_color = fg_color;
meter_color = settings.fg_color;
}
// Update the color of the metronome arm
layer_mark_dirty(s_path_layer);
Expand All @@ -186,7 +187,7 @@ void toggle_meter_arm() {
// Centers the text on the round watches, looks weird if off center
int action_bar_w = ACTION_BAR_WIDTH==30 ? ACTION_BAR_WIDTH : 0;

if(meter_arm) {
if(settings.meter_arm) {
layer_set_hidden(s_path_layer, false);

// Move to make room for the meter arm
Expand Down
119 changes: 66 additions & 53 deletions src/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,32 @@
#include "main.h"
#include "meter_arm.h"

// For use with persisting data
static const int BPM_KEY = 0;
static const int FG_KEY = 1;
static const int BG_KEY = 2;
static const int VIBE_KEY = 3;
static const int FLASH_KEY = 4;
static const int METERARM_KEY = 5;
// For use with persisting data with Clay
static const int SETTINGS_KEY = 7;

static AppTimer *metro_timer = NULL;
// Insert length of vibration
static uint32_t vibe_pat[1];
static int toggle_num = 0;

void metro_loop_handler(void *data) {
toggle_num = !toggle_num;
// Check if flash is enabled in the settings
if(flashing) {

if(settings.flashing) {
toggle_colors(&toggle_num);
}

VibePattern pat = {
.durations = vibe_pat,
.num_segments = ARRAY_LENGTH(vibe_pat),
.durations = settings.vibe_pat,
.num_segments = ARRAY_LENGTH(settings.vibe_pat),
};
vibes_enqueue_custom_pattern(pat);
animate_meter_arm(&toggle_num, convert_bpm(bpm));
animate_meter_arm(&toggle_num, convert_bpm(settings.bpm));

metro_timer = app_timer_register(convert_bpm(bpm), metro_loop_handler, &data);
metro_timer = app_timer_register(convert_bpm(settings.bpm), metro_loop_handler, &data);
}

void click_config_provider(void *context) {
window_single_repeating_click_subscribe(BUTTON_ID_UP, 75, up_bpm_click_handler);
//window_multi_click_subscribe(BUTTON_ID_UP, 2, 2, 100, true, up_tempo_click_handler);
window_single_repeating_click_subscribe(BUTTON_ID_DOWN, 75, down_bpm_click_handler);
//window_multi_click_subscribe(BUTTON_ID_DOWN, 2, 2, 100, true, down_tempo_click_handler);

window_single_click_subscribe(BUTTON_ID_SELECT, select_play_click_handler);
window_long_click_subscribe(BUTTON_ID_SELECT, 400, select_long_click_handler, NULL);
Expand All @@ -60,15 +51,15 @@ void up_tempo_click_handler(ClickRecognizerRef recognizer, void *context) {
action_bar_layer_destroy(aux_action_bar);
aux_action_bar = NULL;

bpm = change_tempo_marking(1);
text_layer_set_text(bpm_text_layer, int_to_str(bpm));
text_layer_set_text(tempo_text_layer, get_tempo_marking(bpm));
settings.bpm = change_tempo_marking(1);
text_layer_set_text(bpm_text_layer, int_to_str(settings.bpm));
text_layer_set_text(tempo_text_layer, get_tempo_marking(settings.bpm));
}

void select_play_click_handler(ClickRecognizerRef recognizer, void *context) {
if(metro_timer == NULL) {
action_bar_layer_set_icon_animated(prim_action_bar, BUTTON_ID_SELECT, gbitmap_create_with_resource(RESOURCE_ID_PAUSE_METRO), true);
metro_timer = app_timer_register(convert_bpm(bpm), (AppTimerCallback) metro_loop_handler, (void *)(&toggle_num));
metro_timer = app_timer_register(convert_bpm(settings.bpm), (AppTimerCallback) metro_loop_handler, (void *)(&toggle_num));
}
else {
action_bar_layer_set_icon_animated(prim_action_bar, BUTTON_ID_SELECT, gbitmap_create_with_resource(RESOURCE_ID_START_METRO), true);
Expand All @@ -94,8 +85,8 @@ void select_long_click_handler(ClickRecognizerRef recognizer, void *context) {
// Don't vibrate if metronome is going
if(metro_timer == NULL) {
VibePattern pat = {
.durations = vibe_pat,
.num_segments = ARRAY_LENGTH(vibe_pat),
.durations = settings.vibe_pat,
.num_segments = ARRAY_LENGTH(settings.vibe_pat),
};
vibes_enqueue_custom_pattern(pat);
}
Expand Down Expand Up @@ -131,9 +122,9 @@ void down_tempo_click_handler(ClickRecognizerRef recognizer, void *context) {
action_bar_layer_destroy(aux_action_bar);
aux_action_bar = NULL;

bpm = change_tempo_marking(-1);
text_layer_set_text(bpm_text_layer, int_to_str(bpm));
text_layer_set_text(tempo_text_layer, get_tempo_marking(bpm));
settings.bpm = change_tempo_marking(-1);
text_layer_set_text(bpm_text_layer, int_to_str(settings.bpm));
text_layer_set_text(tempo_text_layer, get_tempo_marking(settings.bpm));
}

void window_load(Window *window) {
Expand All @@ -146,12 +137,12 @@ void window_load(Window *window) {
// Use a system font in a TextLayer
text_layer_set_font(bpm_text_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_MEDIUM_NUMBERS));
// Set the text to the BPM number as a string
text_layer_set_text(bpm_text_layer, int_to_str(bpm));
text_layer_set_text(bpm_text_layer, int_to_str(settings.bpm));

tempo_text_layer = text_layer_create(GRect(0, bounds.size.h/2 + 35, bounds.size.w - action_bar_w, 45));
text_layer_set_font(tempo_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
// Set the text to the tempo marking the BPM currently falls under
text_layer_set_text(tempo_text_layer, get_tempo_marking(bpm));
text_layer_set_text(tempo_text_layer, get_tempo_marking(settings.bpm));

text_layer_set_text_alignment(bpm_text_layer, GTextAlignmentCenter);
text_layer_set_text_alignment(tempo_text_layer, GTextAlignmentCenter);
Expand All @@ -167,7 +158,7 @@ void window_load(Window *window) {
action_bar_layer_set_icon_animated(prim_action_bar, BUTTON_ID_SELECT, gbitmap_create_with_resource(RESOURCE_ID_START_METRO), true);
action_bar_layer_set_icon_animated(prim_action_bar, BUTTON_ID_DOWN, gbitmap_create_with_resource(RESOURCE_ID_SUBTRACT_BEATS), true);

meter_color = fg_color;
meter_color = settings.fg_color;

create_meter_arm();

Expand All @@ -182,40 +173,43 @@ void window_unload(Window *window) {
void prv_inbox_received_handler(DictionaryIterator *iter, void *context) {
// Read color preferences
Tuple *fg_color_t = dict_find(iter, MESSAGE_KEY_ForegroundColor);
if(fg_color_t) fg_color = fg_color_t->value->int32;
if(fg_color_t) settings.fg_color = fg_color_t->value->int32;

Tuple *bg_color_t = dict_find(iter, MESSAGE_KEY_BackgroundColor);
if(bg_color_t) bg_color = bg_color_t->value->int32;
if(bg_color_t) settings.bg_color = bg_color_t->value->int32;

Tuple *flashing_t = dict_find(iter, MESSAGE_KEY_Flashing);
if(flashing_t) flashing = flashing_t->value->int32 == 1;
if(flashing_t) settings.flashing = flashing_t->value->int32 == 1;

Tuple *meter_arm_t = dict_find(iter, MESSAGE_KEY_MeterArm);
if(meter_arm_t) meter_arm = meter_arm_t->value->int32 == 1;
if(meter_arm_t) settings.meter_arm = meter_arm_t->value->int32 == 1;

Tuple *vibes_t = dict_find(iter, MESSAGE_KEY_VibeLength);
if(vibes_t) vibe_pat[0] = vibes_t->value->int32;
Tuple *vibes_t = dict_find(iter, MESSAGE_KEY_VibeLength);
if(vibes_t) settings.vibe_pat[0] = vibes_t->value->int32;

toggle_num = 0;
toggle_colors(&toggle_num);

toggle_meter_arm();
}

void init(void) {
// Pass the corresponding GPathInfo to initialize a GPath
s_meter_path = gpath_create(&METER_ARM_POINTS);
void prv_save_settings() {
// Store the bpm and preferences
/*persist_write_int(BPM_KEY, bpm);
persist_write_int(FG_KEY, fg_color);
persist_write_int(BG_KEY, bg_color);
persist_write_int(VIBE_KEY, vibe_pat[0]);
persist_write_bool(FLASH_KEY, flashing);
persist_write_bool(METERARM_KEY, meter_arm);*/

window = window_create();
window_set_click_config_provider(window, click_config_provider);
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});

persist_write_data(SETTINGS_KEY, &settings, sizeof(settings));
}

void prv_load_settings() {
// Reads the preferences from storage; app config doesn't get called on load
// Otherwise set defaults
if(persist_exists(BPM_KEY)) bpm = (int)persist_read_int(BPM_KEY);
/*if(persist_exists(BPM_KEY)) bpm = (int)persist_read_int(BPM_KEY);
else bpm = 120;
if(persist_exists(FG_KEY)) fg_color = (int)persist_read_int(FG_KEY);
else fg_color = PBL_IF_BW_ELSE(0x000000, 0x00FF55);
Expand All @@ -226,7 +220,32 @@ void init(void) {
if(persist_exists(FLASH_KEY)) flashing = (bool)persist_read_bool(FLASH_KEY);
else flashing = false;
if(persist_exists(METERARM_KEY)) meter_arm = (bool)persist_read_bool(METERARM_KEY);
else meter_arm = true;
else meter_arm = true;*/


// Load the default settings
settings.bpm = 120;
settings.fg_color = PBL_IF_BW_ELSE(0x000000, 0x00FF55);
settings.bg_color = PBL_IF_BW_ELSE(0xffffff, 0x000055);
settings.vibe_pat[0] = 50;
settings.flashing = false;
settings.meter_arm = true;
// Read settings from persistent storage, if they exist
persist_read_data(SETTINGS_KEY, &settings, sizeof(settings));
}

void init(void) {
// Pass the corresponding GPathInfo to initialize a GPath
s_meter_path = gpath_create(&METER_ARM_POINTS);

window = window_create();
window_set_click_config_provider(window, click_config_provider);
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});

prv_load_settings();

// Open AppMessage connection
app_message_register_inbox_received(prv_inbox_received_handler);
Expand All @@ -237,14 +256,8 @@ void init(void) {

void deinit(void) {
app_timer_cancel(metro_timer);
// Store the bpm and preferences
persist_write_int(BPM_KEY, bpm);
persist_write_int(FG_KEY, fg_color);
persist_write_int(BG_KEY, bg_color);
persist_write_int(VIBE_KEY, vibe_pat[0]);
persist_write_bool(FLASH_KEY, flashing);
persist_write_bool(METERARM_KEY, meter_arm);
// Destroy main Window
prv_save_settings();
window_destroy(window);

gpath_destroy(s_meter_path);
Expand Down
17 changes: 13 additions & 4 deletions src/c/main.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <pebble.h>


Window *window;

// The actual value of the metronome
Expand All @@ -10,10 +11,16 @@ TextLayer *bpm_text_layer,
ActionBarLayer *prim_action_bar, *aux_action_bar;

// Variables set by the Clay configurator
int fg_color, bg_color;
bool flashing;
int bpm;
bool meter_arm;
//int fg_color, bg_color, bpm;
//bool flashing, meter_arm;

// Define our settings struct
typedef struct ClaySettings {
int fg_color, bg_color, bpm;
bool flashing, meter_arm;
uint32_t vibe_pat[1];
} ClaySettings;
ClaySettings settings;

void update_bpm(int amount);

Expand All @@ -36,6 +43,8 @@ void window_load(Window *window);
void window_unload(Window *window);

void prv_inbox_received_handler(DictionaryIterator *iter, void *context);
void prv_save_settings();
void prv_load_settings();

void init(void);
void deinit(void);
Expand Down
6 changes: 0 additions & 6 deletions src/c/meter_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ void forward_animate_update(Animation *animation, const AnimationProgress progre
int progress_percent = ((int)progress * 100) / ANIMATION_NORMALIZED_MAX;
int delta = angle_bounds*2*progress_percent/100 - (s_path_angle+angle_bounds);

//APP_LOG(APP_LOG_LEVEL_INFO, "Animation progress: %d", progress_percent);
//APP_LOG(APP_LOG_LEVEL_INFO, "Amount moving: %d, Amount left: %d", delta, 40 + -s_path_angle);

path_angle_add(delta);
layer_mark_dirty(s_path_layer);
}
Expand All @@ -95,9 +92,6 @@ void backward_animate_update(Animation *animation, const AnimationProgress progr
int progress_percent = ((int)progress * 100) / ANIMATION_NORMALIZED_MAX;
int delta = -angle_bounds*2*progress_percent/100 - (s_path_angle-angle_bounds);

//APP_LOG(APP_LOG_LEVEL_INFO, "Animation progress: %d", progress_percent);
//APP_LOG(APP_LOG_LEVEL_INFO, "Amount moving: %d, Amount left: %d", delta, 40 + -s_path_angle);

path_angle_add(delta);
layer_mark_dirty(s_path_layer);
}
Expand Down

0 comments on commit a347fdb

Please sign in to comment.