Skip to content
This repository has been archived by the owner on Apr 27, 2020. It is now read-only.

Commit

Permalink
Minor fixes (again)
Browse files Browse the repository at this point in the history
Loads of minor fixes, again.

Changed some variable names to better fit style already being used

Changed if statements to have braces to fit style already being used

App no longer freaks out if the user changes the breath speed during the breathing session

A bit more documentation
  • Loading branch information
aaronhktan committed Dec 6, 2016
1 parent 6c073f2 commit e24d394
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 54 deletions.
5 changes: 2 additions & 3 deletions src/c/appglance.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void appglance_update_app_glance(AppGlanceReloadSession *session, size_t limit,

// Add the slice, and check the result
const AppGlanceResult result = app_glance_add_slice(session, entry);
if (result != APP_GLANCE_RESULT_SUCCESS) {
APP_LOG(APP_LOG_LEVEL_ERROR, "AppGlance Error: %d", result);
}
if (result != APP_GLANCE_RESULT_SUCCESS)
APP_LOG(APP_LOG_LEVEL_ERROR, "AppGlance Error: %d", result);
}
86 changes: 44 additions & 42 deletions src/c/breathe_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@

static Window *s_main_window;
static Layer *s_circle_layer, *s_inside_text_layer, *s_upper_text_layer, *s_lower_text_layer;
static AppTimer *s_animation_completed_timer, *animationTimer, *s_hide_lower_text_layer, *s_click_provider_timer, *s_interrupt_timer;
static AppTimer *s_animation_completed_timer, *animationTimer, *s_hide_lower_text_layer, *s_click_provider_timer, *s_interrupt_timer, *s_update_hr_timer, *s_main_timer;
static GRect bounds;
static uint8_t s_radius_final, s_radius = 0;
static int s_min_to_breathe = 1, s_min_breathed_today = 0, s_times_played = 0, s_breath_duration, s_breaths_per_minute, s_current_radius;
static bool s_animation_completed = false, s_animating = false;
static bool s_animation_completed = false, s_animating = false, s_main_done;
static GPoint s_center;
static char s_min_to_breathe_text[3] = "1", s_instruct_text[27], s_min_text[25], s_min_today[25], s_greet_text[27], s_start_time[11], s_end_time[11];
static time_t t;
static time_t s_start_stamp;

static AppTimer *s_update_hr_timer, *s_main_timer;
static bool s_main_done;

// ******************************************************************************************* Layer Update Procedures
// Updates circle
static void canvas_update_proc(Layer *s_drawing_layer, GContext *ctx) {
Expand All @@ -44,7 +41,7 @@ static void inside_text_layer_update_proc(Layer *s_inside_text_layer, GContext *

// Draws text at top of screen
static void upper_text_layer_update_proc(Layer *s_inside_text_layer, GContext *ctx) {
graphics_draw_upper_text(ctx, bounds, s_animating, settings_get_displayText(), settings_get_textColor(), s_greet_text);
graphics_draw_upper_text(ctx, bounds, s_animating, settings_get_heartRateVariation(), settings_get_displayText(), settings_get_textColor(), s_greet_text);
}

// Draws text at bottom of screen
Expand Down Expand Up @@ -204,8 +201,10 @@ static void animation_end_callback(void *data) {
layer_set_hidden(s_upper_text_layer, false);
layer_set_hidden(s_lower_text_layer, false);

// restore the HR sample period
if (settings_get_heartRateVariation()) data_set_heart_rate_period(0);
// Restore the HR sample period
if (settings_get_heartRateVariation()) {
data_set_heart_rate_period(0);
}
}

// Last out animation
Expand Down Expand Up @@ -291,12 +290,12 @@ static void main_animation() {
case 0: ; // This is vibrations only on inhale
// Vibrations! (play for 0, rest for 1500, play for 25, rest for 25, etc.)
static uint32_t *segments;
switch(s_breaths_per_minute) {
switch(s_breath_duration) {
case 4: ; // 15000 milliseconds long, with an empty statement after a label before a declaration
static const uint32_t four_segments[49] = {0, 2500, 25, 50, 25, 50, 25, 65, 25, 65, 25, 75, 25, 75, 25, 80, 25, 80, 25, 100, 25, 100, 25, 150, 25, 150, 25, 175, 25, 175, 25, 225, 25, 225, 25, 275, 25, 275, 25, 375, 25, 375, 25, 450, 25, 450, 25, 500, 25};
segment_length = ARRAY_LENGTH(four_segments);
segments = (uint32_t *) calloc(segment_length, sizeof(uint32_t));
memcpy(segments, four_segments, sizeof(four_segments));
segment_length = ARRAY_LENGTH(four_segments); // Get size of array
segments = (uint32_t *) calloc(segment_length, sizeof(uint32_t)); // Allocate memory equal to size of array to segments and return pointer to segments array
memcpy(segments, four_segments, sizeof(four_segments)); // Copy each value of array into segments array
break;
case 5: ; // 12000 milliseconds long
static const uint32_t five_segments[44] = {0, 2000, 25, 50, 25, 50, 25, 65, 25, 65, 25, 75, 25, 75, 25, 80, 25, 80, 25, 100, 25, 100, 25, 150, 25, 150, 25, 175, 25, 175, 25, 225, 25, 225, 25, 275, 25, 275, 25, 375, 25, 375, 25};
Expand Down Expand Up @@ -334,7 +333,7 @@ static void main_animation() {
break;
case 1: ; // This is vibrations on inhale and exhale
static uint32_t *segments_both;
switch(s_breaths_per_minute) {
switch(s_breath_duration) {
case 4: ; // 15000 milliseconds long, with an empty statement after a label before a declaration
static const uint32_t four_segments[96] = {0, 2500, 25, 50, 25, 50, 25, 65, 25, 65, 25, 75, 25, 75, 25, 80, 25, 80, 25, 100, 25, 100, 25, 150, 25, 150, 25, 175, 25, 175, 25, 225, 25, 225, 25, 275, 25, 275, 25, 375, 25, 450, 25, 450, 25, 500, 25, 2500, 25, 50, 25, 50, 25, 65, 25, 65, 25, 75, 25, 75, 25, 80, 25, 80, 25, 100, 25, 100, 25, 150, 25, 150, 25, 175, 25, 175, 25, 225, 25, 225, 25, 275, 25, 275, 25, 375, 25, 375, 25, 450, 25, 450, 25, 500, 25};
segment_length = ARRAY_LENGTH(four_segments);
Expand Down Expand Up @@ -381,12 +380,10 @@ static void main_animation() {
free(segments_both);
break;
default: ; // This is simple vibrations (double tap)
// 1000 delay for animation, 50 play, 100 stop, 50 play, rest for breath duration and delay and subtract (50 + 100 + 50), and vibrate again.
//const uint32_t segments_simple[] = {0, 1000, 50, 100, 50, settings_get_breathDuration() + 1000 - 200, 50, 100, 50, settings_get_breathDuration() - 300};
int vibOn, vibOff;
vibOn = 40+s_breath_duration/1000;
vibOff = s_breath_duration/10*2;
const uint32_t segments_simple[] = {0, 1000, vibOn, vibOff, vibOn, settings_get_breathDuration() + 1000 - (vibOff+vibOn*2), vibOn, vibOff, vibOn};
int vib_on, vib_off; // Declare variables for length of vibration on and rest time between vibrations
vib_on = 40 + s_breath_duration / 1000;
vib_off = s_breath_duration / 10 * 2;
const uint32_t segments_simple[] = {0, 1000, vib_on, vib_off, vib_on, s_breath_duration + 1000 - (vib_off + vib_on * 2), vib_on, vib_off, vib_on};
VibePattern vibes_simple = {
.durations = segments_simple,
.num_segments = ARRAY_LENGTH(segments_simple),
Expand All @@ -400,21 +397,23 @@ static void main_animation() {
// Schedules next animation if the number of times played is less than 7 times the number of minutes (seven breaths per minute)
static void main_animation_callback () {

// Update the breathDuration if in variable HR mode, but only if it's higher.
// Update the breathDuration if in variable HR mode, but only if it's higher. (Which means that the breaths are slowing down and people are relaxing! Wow!)
if (settings_get_heartRateVariation()) {
int newDuration = settings_get_breathDuration();
if (newDuration > s_breath_duration) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "Old duration %d, new %d", s_breath_duration, newDuration);
s_breath_duration = newDuration;
int new_duration = settings_get_breathDuration();
if (new_duration > s_breath_duration) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "Old duration %d, new %d", s_breath_duration, new_duration);
s_breath_duration = new_duration;
}
} else {
s_breath_duration = settings_get_breathDuration(); // Check for changes in the breath duration; it may have been changed in the settings
}

// If we are animating and main timer isn't done yet
if (s_animating && !s_main_done) {
animationTimer = app_timer_register(2 * s_breath_duration + 2000, main_animation_callback, NULL);
if (!layer_get_hidden(s_upper_text_layer) || !layer_get_hidden(s_lower_text_layer)) {
animationTimer = app_timer_register(2 * s_breath_duration + 2 * D_BREATH_HOLD_TIME, main_animation_callback, NULL); // Run this method again after duration of breaths and delays
if (!layer_get_hidden(s_upper_text_layer) || !layer_get_hidden(s_lower_text_layer)) { // The text layers aren't hidden; this keep if HRV is enabled
if (settings_get_heartRateVariation() && s_times_played > 1) layer_set_hidden(s_upper_text_layer, false); // For the HR text
else layer_set_hidden(s_upper_text_layer, true);
else layer_set_hidden(s_upper_text_layer, true); // HRV isn't enabled, so hide the text layers
layer_set_hidden(s_lower_text_layer, true);
}
main_animation();
Expand All @@ -430,30 +429,32 @@ static void main_done_callback(void *context) {
}

// Update HeartRate in the top slot during a session
static void heartrate_update_callback(void *context) {
static void heart_rate_update_callback(void *context) {
if (s_animating) {
data_update_heart_rate_buffer();
snprintf(s_greet_text, sizeof(s_greet_text), data_get_current_heart_rate_buffer());
data_update_heart_rate_buffer(); // Update heart rate
snprintf(s_greet_text, sizeof(s_greet_text), data_get_current_heart_rate_buffer()); // Show heart rate upon redraw of s_upper_text_layer
layer_set_hidden(s_upper_text_layer, false);

// Re-schedule the timer
s_update_hr_timer = app_timer_register(s_breath_duration, heartrate_update_callback, NULL);
s_update_hr_timer = app_timer_register(s_breath_duration, heart_rate_update_callback, NULL);
}
}

// Shows instructions to exhale; first hides the top text and then shows the bottom text
static void first_breath_out_callback(void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "first_breath_out_callback");

// Intro done, start main timer
s_main_timer = app_timer_register(s_min_to_breathe*60000, main_done_callback, NULL);
// Intro done, start timer for when to stop breathing session
s_main_timer = app_timer_register(s_min_to_breathe * MILLISECONDS_PER_MINUTE, main_done_callback, NULL);

snprintf(s_min_today, sizeof(s_min_today), localize_get_exhale_text());
layer_set_hidden(s_upper_text_layer, true);
layer_set_hidden(s_lower_text_layer, false);

// Start HR update timer
if (settings_get_heartRateVariation()) s_update_hr_timer = app_timer_register(s_breath_duration*2, heartrate_update_callback, NULL);
// Start HR update timer which shows the heart rate in the upper text field after the first breath
if (settings_get_heartRateVariation()) {
s_update_hr_timer = app_timer_register(s_breath_duration*2, heart_rate_update_callback, NULL);
}
}

// Shows instructions to inhale
Expand Down Expand Up @@ -603,15 +604,17 @@ static void back_click_handler(ClickRecognizerRef recognizer, void *context) {
static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
// Only does things if things are not animating
if (!s_animating) {
s_animating = true;
s_times_played = 0;
s_breaths_per_minute = settings_get_breathsPerMinute();
s_breath_duration = settings_get_breathDuration();
s_animating = true;
s_animation_completed = false;

s_main_done = false;
// kick the HR to high gear
if (settings_get_heartRateVariation()) data_set_heart_rate_period(1);

// Kick the HR to high gear (update every one second!)
if (settings_get_heartRateVariation()) {
data_set_heart_rate_period(1);
}

// Hides all text layers
layer_set_hidden(s_inside_text_layer, true);
Expand All @@ -628,7 +631,6 @@ static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
animationTimer = app_timer_register(6000, main_animation_callback, NULL);

// Schedules the last out animation (circle expand) after min * duration of 7 breaths + duration of first circle contraction
APP_LOG(APP_LOG_LEVEL_DEBUG, "The timer is set to fire at %d.", s_min_to_breathe * s_breaths_per_minute * 2 * (s_breath_duration + 1000) + 7000);
APP_LOG(APP_LOG_LEVEL_DEBUG, "The number of minutes to breath is %d.", s_min_to_breathe);
APP_LOG(APP_LOG_LEVEL_DEBUG, "The number of breaths per minute is %d.", s_breaths_per_minute);
APP_LOG(APP_LOG_LEVEL_DEBUG, "The duration per breath is %d.", s_breath_duration);
Expand All @@ -642,11 +644,11 @@ static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
}

static void click_config_provider(void *context) {
ButtonId id_up = BUTTON_ID_UP; // The Up button
ButtonId id_up = BUTTON_ID_UP; // The Up button
ButtonId id_down = BUTTON_ID_DOWN; // The Down button
ButtonId id_select = BUTTON_ID_SELECT; // The Select button
ButtonId id_back = BUTTON_ID_BACK;
window_single_click_subscribe(id_up, up_click_handler);
window_single_click_subscribe(id_up, up_click_handler);
window_single_click_subscribe(id_down, down_click_handler);
window_single_click_subscribe(id_back, back_click_handler);
window_single_click_subscribe(id_select, select_click_handler);
Expand Down
6 changes: 5 additions & 1 deletion src/c/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ void data_set_current_heart_rate(int value) {
//APP_LOG(APP_LOG_LEVEL_DEBUG, "Raw BPM: %d, avg BPM: %d", value, (int)s_current_heart_rate);
}

// Sets heart rate sample period to get fresh data
void data_set_heart_rate_period(int val){
#if PBL_API_EXISTS(health_service_set_heart_rate_sample_period)
health_service_set_heart_rate_sample_period(val); // Sets heart rate sample period to 5 seconds to get fresh data
health_service_set_heart_rate_sample_period(val);
#endif
}

Expand Down Expand Up @@ -131,6 +132,7 @@ void data_write_last_duration_data(int last_duration) {
persist_write_int(LAST_DURATION_KEY, last_duration);
}

// Get last duration from persistent storage
int data_read_last_duration_data() {
int last_duration = 0;
if (persist_exists(LAST_DURATION_KEY)) {
Expand All @@ -143,10 +145,12 @@ int data_read_last_duration_data() {
return last_duration;
}

// Store Wakeup id
void data_write_wakeup_time_data(char* wakeup_time) {
persist_write_string(WAKEUP_TIME_KEY, wakeup_time);
}

// Wakeup time as a string
char * data_read_wakeup_time_data() {
static char wakeup_time[9];
if (persist_exists(WAKEUP_TIME_KEY)) {
Expand Down
2 changes: 1 addition & 1 deletion src/c/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static GPath *s_up_triangle, *s_down_triangle;
#endif

// Method for updating the upper text layer
void graphics_draw_upper_text(GContext *ctx, GRect bounds, bool is_animating, int display_text, GColor textColor, char *greet_text) {
void graphics_draw_upper_text(GContext *ctx, GRect bounds, bool is_animating, bool heart_rate_variation, int display_text, GColor textColor, char *greet_text) {
#if defined(PBL_HEALTH)
const char *steps_buffer = data_get_current_steps_buffer(); // Pebble Health exists; fetch the number of steps walked today
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/c/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define FONT_KEY FONT_KEY_GOTHIC_14_BOLD
#endif

void graphics_draw_upper_text(GContext *ctx, GRect bounds, bool is_animating, int displayText, GColor textColor, char *);
void graphics_draw_upper_text(GContext *ctx, GRect bounds, bool is_animating, bool heart_rate_variation, int displayText, GColor textColor, char *);

void graphics_draw_lower_text(GContext *ctx, GRect bounds, bool is_animating, GColor textColor, char *);

Expand Down
4 changes: 3 additions & 1 deletion src/c/health.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ static void health_handler(HealthEventType event, void *context) {
void health_init() {
health_service_events_subscribe(health_handler, NULL);
#if PBL_API_EXISTS(health_service_set_heart_rate_sample_period)
if (settings_get_heartRateVariation()) health_service_set_heart_rate_sample_period(5); // Sets heart rate sample period to 5 seconds to get fresh data
if (settings_get_heartRateVariation()) {
health_service_set_heart_rate_sample_period(5); // Sets heart rate sample period to 5 seconds to get fresh data
}
#endif
}
10 changes: 5 additions & 5 deletions src/c/reminder_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ static void reminder_window_load(Window *window) {

layer_add_child(window_layer, s_canvas_layer);

// Only vibrate when the watch isn't in Quiet Time
if (!quiet_time_is_active()){
vibes_double_pulse();
}

window_set_background_color(s_reminder_window, PBL_IF_COLOR_ELSE(random_color, GColorWhite));

app_timer_register(DELTA, next_frame_handler, NULL);
Expand Down Expand Up @@ -207,4 +202,9 @@ void reminder_window_push() {
window_set_click_config_provider(s_reminder_window, click_config_provider);

window_stack_push(s_reminder_window, true);

// Only vibrate when the watch isn't in Quiet Time
if (!quiet_time_is_active()){
vibes_double_pulse();
}
}

0 comments on commit e24d394

Please sign in to comment.