this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor, cleanup

alice e57f870a 536a0f41

+89 -200
+2 -11
src/c/clock_beat.c
··· 3 3 #include <stdint.h> // For uint32_t, uint64_t 4 4 #include <time.h> // For time_t 5 5 #include <stdio.h> // For snprintf 6 + #include "text_layer_util.h" 6 7 7 8 #define HOUR_LENGTH 3600 8 9 #define DAY_LENGTH 86400 ··· 40 41 // --- Pebble UI Interface Functions --- 41 42 42 43 TextLayer* clock_beat_init(GRect bounds, Layer *window_layer) { 43 - TextLayer* layer = text_layer_create(bounds); 44 - text_layer_set_background_color(layer, GColorClear); 45 - text_layer_set_text_color(layer, GColorBlack); 46 - text_layer_set_text(layer, "@--.-"); // Initial placeholder 47 - #define BEAT_FONT FONT_KEY_GOTHIC_24_BOLD // Keep font definitions near usage 48 - text_layer_set_font(layer, fonts_get_system_font(BEAT_FONT)); 49 - text_layer_set_text_alignment(layer, GTextAlignmentCenter); 50 - layer_add_child(window_layer, text_layer_get_layer(layer)); 51 - return layer; 44 + return text_layer_util_create(bounds, window_layer, "@--.-", FONT_KEY_GOTHIC_24_BOLD); 52 45 } 53 46 54 47 void clock_beat_deinit(TextLayer *layer) { ··· 73 66 text_layer_set_text(layer, s_beat_buffer); 74 67 last_beat_time = b; 75 68 } 76 - 77 - // Removed clock_beat_get_time_string wrapper as formatting logic moved
+3 -8
src/c/clock_closest_noon.c
··· 6 6 #include <stdbool.h>// For bool 7 7 #include <limits.h> // For LONG_MAX // Not strictly needed anymore, but harmless 8 8 #include <string.h> // For strcmp, strncmp 9 + #include <stdio.h> // For snprintf 10 + #include "text_layer_util.h" 9 11 10 12 // Include the generated timezone data structure definitions and list 11 13 // This must contain TzInfo, tz_list[], and TZ_LIST_COUNT ··· 154 156 // --- Interface Functions (Pebble only) --- 155 157 156 158 TextLayer* clock_closest_noon_init(GRect bounds, Layer *window_layer) { 157 - TextLayer* layer = text_layer_create(bounds); 158 - text_layer_set_background_color(layer, GColorClear); 159 - text_layer_set_text_color(layer, GColorBlack); 160 - text_layer_set_text(layer, "Wait..."); // Initial placeholder 161 - #define CLOSEST_FONT FONT_KEY_GOTHIC_18_BOLD // Keep font definitions near usage 162 - text_layer_set_font(layer, fonts_get_system_font(CLOSEST_FONT)); 163 - text_layer_set_text_alignment(layer, GTextAlignmentCenter); 164 - layer_add_child(window_layer, text_layer_get_layer(layer)); 159 + TextLayer* layer = text_layer_util_create(bounds, window_layer, "Wait...", FONT_KEY_GOTHIC_18_BOLD); 165 160 166 161 // Initialize static vars 167 162 s_selected_city_name = "Wait...";
+18 -39
src/c/clock_decimal.c
··· 2 2 #include <pebble.h> 3 3 #include <stdio.h> 4 4 #include <time.h> 5 + #include "text_layer_util.h" 5 6 6 - #define DECIMAL_FONT FONT_KEY_GOTHIC_18_BOLD 7 + static char s_buffer[10]; // Buffer for time string HH:MM:SS.ss 7 8 8 - static char s_buffer[10]; // Buffer for time string HH:MM:SS.ss 9 + // Updates the decimal-time TextLayer by converting local time to French decimal time inline 10 + void clock_decimal_update(TextLayer *text_layer, time_t system_seconds) { 11 + // Get local hours, minutes, seconds 12 + struct tm *local_tm = localtime(&system_seconds); 13 + uint32_t seconds_today = local_tm->tm_hour * 3600 + local_tm->tm_min * 60 + local_tm->tm_sec; 9 14 10 - // Function to calculate French Revolutionary Time using integer math 11 - static void calculate_decimal_time(time_t system_seconds, int *decimal_hour, int *decimal_minute, int *decimal_second) { 12 - // Standard time has 86400 seconds per day. 13 - // Decimal time has 100000 decimal seconds per day. 14 - 15 - // Get the time structure for the current day 16 - struct tm *current_tm = localtime(&system_seconds); 17 - 18 - // Calculate standard seconds elapsed since midnight 19 - uint32_t seconds_today = current_tm->tm_hour * 3600 + current_tm->tm_min * 60 + current_tm->tm_sec; 20 - 21 - // Convert standard seconds today to decimal seconds today using 64-bit integer math 22 - // total_decimal_seconds = (seconds_today * 100000) / 86400 23 - uint64_t total_decimal_seconds = ((uint64_t)seconds_today * 100000ULL) / 86400ULL; 24 - 25 - // Calculate decimal hours, minutes, seconds using integer division and modulo 26 - *decimal_hour = (int)(total_decimal_seconds / 10000ULL); 27 - uint64_t remainder = total_decimal_seconds % 10000ULL; 28 - *decimal_minute = (int)(remainder / 100ULL); 29 - *decimal_second = (int)(remainder % 100ULL); 30 - } 15 + // Compute decimal seconds: (seconds_today * 100000) / 86400 16 + uint32_t total_decimal_seconds = (uint32_t)(((uint64_t)seconds_today * 100000ULL) / 86400ULL); 17 + uint32_t dec_hour = total_decimal_seconds / 10000; 18 + uint32_t dec_min = (total_decimal_seconds / 100) % 100; 19 + uint32_t dec_sec = total_decimal_seconds % 100; 31 20 32 - // Updates the decimal time TextLayer 33 - void clock_decimal_update(TextLayer *text_layer, time_t system_seconds) { 34 - int decimal_hour, decimal_minute, decimal_second; 35 - calculate_decimal_time(system_seconds, &decimal_hour, &decimal_minute, &decimal_second); 36 - 37 - // Format the time string (e.g., H:MM:SS) 38 - snprintf(s_buffer, sizeof(s_buffer), "%d:%02d:%02d", decimal_hour, decimal_minute, decimal_second); 39 - 40 - APP_LOG(APP_LOG_LEVEL_DEBUG, "Decimal Update: %s", s_buffer); 41 - // Display this time on the TextLayer 21 + // Format and display 22 + snprintf(s_buffer, sizeof(s_buffer), "%lu:%02lu:%02lu", 23 + (unsigned long)dec_hour, 24 + (unsigned long)dec_min, 25 + (unsigned long)dec_sec); 42 26 text_layer_set_text(text_layer, s_buffer); 43 27 } 44 28 45 29 // Initializes the decimal time TextLayer 46 30 TextLayer* clock_decimal_init(GRect bounds, Layer *window_layer) { 47 - TextLayer *text_layer = text_layer_create(bounds); 48 - text_layer_set_background_color(text_layer, GColorClear); 49 - text_layer_set_text_color(text_layer, GColorBlack); 50 - text_layer_set_font(text_layer, fonts_get_system_font(DECIMAL_FONT)); 51 - text_layer_set_text_alignment(text_layer, GTextAlignmentCenter); 52 - layer_add_child(window_layer, text_layer_get_layer(text_layer)); 31 + TextLayer *text_layer = text_layer_util_create(bounds, window_layer, "", FONT_KEY_GOTHIC_18_BOLD); 53 32 54 33 // Initialize with placeholder or current time 55 34 time_t temp = time(NULL);
+2 -8
src/c/clock_noonzone.c
··· 3 3 #include <time.h> // For time_t, struct tm, gmtime 4 4 #include <string.h> // For NULL definition (consider stddef.h?) 5 5 #include <stdio.h> // For snprintf 6 + #include "text_layer_util.h" 6 7 7 8 // --- Static variables specific to Noon Zone Clock --- 8 9 static char s_noonzone_buffer[16]; // Buffer for "NAME:MM:SS\0" ··· 56 57 // --- Pebble UI Interface Functions --- 57 58 58 59 TextLayer* clock_noonzone_init(GRect bounds, Layer *window_layer) { 59 - TextLayer* layer = text_layer_create(bounds); 60 - text_layer_set_background_color(layer, GColorClear); 61 - text_layer_set_text_color(layer, GColorBlack); 62 - text_layer_set_text(layer, "ZONE:--:--"); // Initial placeholder 63 - #define NOONZONE_FONT FONT_KEY_GOTHIC_18_BOLD // Keep font definitions near usage 64 - text_layer_set_font(layer, fonts_get_system_font(NOONZONE_FONT)); 65 - text_layer_set_text_alignment(layer, GTextAlignmentCenter); 66 - layer_add_child(window_layer, text_layer_get_layer(layer)); 60 + TextLayer* layer = text_layer_util_create(bounds, window_layer, "ZONE:--:--", FONT_KEY_GOTHIC_18_BOLD); 67 61 return layer; 68 62 } 69 63
+32 -61
src/c/clock_tid.c
··· 1 1 #include "clock_tid.h" 2 2 #include <pebble.h> 3 - #include <string.h> // For memcpy, memset 4 3 #include <stdint.h> // For uint types 5 - #include <stdio.h> // For snprintf 4 + #include "text_layer_util.h" 5 + #include <stdlib.h> // For rand() 6 6 7 - // --- Static variables specific to TID Clock --- 8 7 static const char S32_CHAR[] = "234567abcdefghijklmnopqrstuvwxyz"; 9 - #define S32_CHAR_LEN (sizeof(S32_CHAR) - 1) 10 - static uint64_t last_timestamp = 0; 11 - // Buffer needs to hold 11 (timestamp) + 2 (clock ID) + 1 (null terminator) = 14 chars 12 - static char s_tid_buffer[14]; // Buffer for TID time string "MMMSS\0" 13 - // Remove cache for the old 5-digit display (variable is now unused) 14 - // static int last_tid_time = -1; 8 + static uint64_t last_timestamp; 9 + static char s_tid_buffer[14]; 15 10 16 - // --- Static helper functions --- 11 + // Generate monotonic TID string into tid_buffer 12 + void clock_tid_get_string(char *tid_buffer, size_t tid_buffer_len, time_t seconds, uint16_t milliseconds) { 13 + if (tid_buffer_len < sizeof(s_tid_buffer)) return; 17 14 18 - static char* s32encode_c(uint64_t i, char *buffer, size_t buffer_len) { 19 - if (buffer_len == 0) return NULL; 20 - char *ptr = buffer + buffer_len; 21 - if (i == 0) { 22 - if (ptr > buffer) { *(--ptr) = S32_CHAR[0]; return ptr; } 23 - else { return NULL; } 15 + uint64_t current_micros = (uint64_t)seconds * 1000000 + (uint64_t)milliseconds * 1000; 16 + if (current_micros <= last_timestamp) { 17 + current_micros = last_timestamp + 1; 18 + } 19 + last_timestamp = current_micros; 20 + 21 + // Encode 11-char base-32 timestamp (pad with '2') 22 + size_t pos = 11; 23 + for (size_t i = 0; i < pos; ++i) { 24 + tid_buffer[i] = S32_CHAR[0]; 24 25 } 25 - while (i > 0 && ptr > buffer) { 26 - uint8_t remainder = i & 31; 27 - i >>= 5; 28 - *(--ptr) = S32_CHAR[remainder]; 26 + uint64_t v = current_micros; 27 + while (v && pos) { 28 + tid_buffer[--pos] = S32_CHAR[v & 31]; 29 + v >>= 5; 29 30 } 30 - return ptr; 31 - } 32 31 33 - static void createRaw_c(uint64_t timestamp, char *tid_buffer, size_t tid_buffer_len) { 34 - if (tid_buffer_len < 14) return; 35 - 36 - char ts_temp_buffer[11]; 37 - char* encoded_ts_start = s32encode_c(timestamp, ts_temp_buffer, sizeof(ts_temp_buffer)); 38 - size_t encoded_ts_len = encoded_ts_start ? (ts_temp_buffer + sizeof(ts_temp_buffer)) - encoded_ts_start : 0; 39 - 40 - memset(tid_buffer, S32_CHAR[0], 11); 41 - if (encoded_ts_len > 0 && encoded_ts_len <= 11) { 42 - memcpy(tid_buffer + (11 - encoded_ts_len), encoded_ts_start, encoded_ts_len); 43 - } else if (encoded_ts_len > 11) { 44 - memcpy(tid_buffer, encoded_ts_start + (encoded_ts_len - 11), 11); 32 + // Encode 2-char random clock ID (0-1023) 33 + uint16_t cid = (uint16_t)(rand() % 1024); 34 + pos = 2; 35 + for (size_t i = 0; i < pos; ++i) { 36 + tid_buffer[11 + i] = S32_CHAR[0]; 37 + } 38 + uint16_t v2 = cid; 39 + while (v2 && pos) { 40 + tid_buffer[11 + --pos] = S32_CHAR[v2 & 31]; 41 + v2 >>= 5; 45 42 } 46 43 47 - tid_buffer[11] = S32_CHAR[0]; 48 - tid_buffer[12] = S32_CHAR[0]; 49 44 tid_buffer[13] = '\0'; 50 45 } 51 46 52 - /** 53 - * Generates a TID string for the given time into the provided buffer. 54 - * Ensures monotonicity using a static variable internally. 55 - */ 56 - void clock_tid_get_string(char *tid_buffer, size_t tid_buffer_len, time_t seconds, uint16_t milliseconds) { 57 - if (tid_buffer_len < 14) return; // Ensure buffer is large enough 58 - 59 - uint64_t current_micros = (uint64_t)seconds * 1000000 + (uint64_t)milliseconds * 1000; 60 - 61 - if (current_micros <= last_timestamp) { 62 - current_micros = last_timestamp + 1; 63 - } 64 - last_timestamp = current_micros; 65 - createRaw_c(current_micros, tid_buffer, tid_buffer_len); 66 - } 67 - 68 47 // --- Pebble UI Interface Functions --- 69 48 70 49 TextLayer* clock_tid_init(GRect bounds, Layer *window_layer) { 71 - TextLayer* layer = text_layer_create(bounds); 72 - text_layer_set_background_color(layer, GColorClear); 73 - text_layer_set_text_color(layer, GColorBlack); 74 - text_layer_set_text(layer, "-----"); // Initial placeholder 75 - #define TID_FONT FONT_KEY_GOTHIC_18_BOLD // Keep font definitions near usage 76 - text_layer_set_font(layer, fonts_get_system_font(TID_FONT)); 77 - text_layer_set_text_alignment(layer, GTextAlignmentCenter); 78 - layer_add_child(window_layer, text_layer_get_layer(layer)); 79 - return layer; 50 + return text_layer_util_create(bounds, window_layer, "-----", FONT_KEY_GOTHIC_18_BOLD); 80 51 } 81 52 82 53 void clock_tid_deinit(TextLayer *layer) {
+19
src/c/text_layer_util.h
··· 1 + #pragma once 2 + 3 + #include <pebble.h> 4 + 5 + // A helper to create a TextLayer configured with standard styling and add it to a parent layer. 6 + // bounds: frame of the TextLayer 7 + // parent: parent Layer to add this TextLayer to 8 + // init_text: initial text to display 9 + // font_key: key of the system font, e.g. FONT_KEY_GOTHIC_24_BOLD 10 + static inline TextLayer* text_layer_util_create(GRect bounds, Layer* parent, const char* init_text, const char* font_key) { 11 + TextLayer* layer = text_layer_create(bounds); 12 + text_layer_set_background_color(layer, GColorClear); 13 + text_layer_set_text_color(layer, GColorBlack); 14 + text_layer_set_text(layer, init_text); 15 + text_layer_set_font(layer, fonts_get_system_font(font_key)); 16 + text_layer_set_text_alignment(layer, GTextAlignmentCenter); 17 + layer_add_child(parent, text_layer_get_layer(layer)); 18 + return layer; 19 + }
+13 -73
src/c/watchface.c
··· 2 2 #include <stdlib.h> 3 3 #include <time.h> 4 4 5 - // Enable logging - Removed incorrect define, APP_LOG is in pebble.h 6 - 7 - // --- Clock Module Includes --- 5 + // Clock modules 8 6 #include "clock_beat.h" 9 - // #include "clock_noonzone.h" 10 7 #include "clock_closest_noon.h" 11 8 #include "clock_tid.h" 12 9 #include "clock_decimal.h" 13 10 14 11 // --- Window and Layer Globals --- 15 12 static Window *s_main_window; 16 - static TextLayer *s_beat_layer; // Layer for Swatch Beat Time 17 - static TextLayer *s_tid_layer; // Layer for TID Time 18 - // static TextLayer *s_noonzone_layer; // Layer for Noon Zone Time // Removed 19 - static TextLayer *s_closest_noon_layer; // Layer for Closest-to-Noon TZ 20 - static TextLayer *s_decimal_layer; // Layer for Decimal Time 13 + static TextLayer *s_beat_layer; 14 + static TextLayer *s_tid_layer; 15 + static TextLayer *s_closest_noon_layer; 16 + static TextLayer *s_decimal_layer; 21 17 22 18 // --- Pebble Window Management --- 23 19 24 20 // Handles updates from the TickTimerService 25 21 static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { 26 - // Get time once for both updates 27 22 time_t seconds; 28 23 uint16_t milliseconds; 29 24 time_ms(&seconds, &milliseconds); ··· 31 26 // Update both time displays 32 27 clock_beat_update(s_beat_layer, seconds); 33 28 clock_tid_update(s_tid_layer, seconds, milliseconds); 34 - // clock_noonzone_update(s_noonzone_layer, seconds); // Removed 35 29 clock_closest_noon_update(s_closest_noon_layer, seconds); 36 30 clock_decimal_update(s_decimal_layer, seconds); 37 31 } ··· 39 33 static void main_window_load(Window *window) { 40 34 Layer *window_layer = window_get_root_layer(window); 41 35 GRect bounds = layer_get_bounds(window_layer); 42 - APP_LOG(APP_LOG_LEVEL_DEBUG, "Bounds: H=%d, W=%d", bounds.size.h, bounds.size.w); 43 - 44 - // Define layout constants for 4 layers 45 - const int num_layers = 4; 46 - const int16_t v_padding = 2; // Slightly more padding maybe 47 - // Calculate available height for layers after removing padding 48 - const int16_t total_available_h = bounds.size.h - (num_layers - 1) * v_padding; 49 - // Distribute height equally among layers 50 - const int16_t layer_h = total_available_h / num_layers; 51 - APP_LOG(APP_LOG_LEVEL_DEBUG, "Total Available H: %d, Layer H: %d", total_available_h, layer_h); 52 - // Verify calculation result 53 - if (layer_h <= 0) { 54 - APP_LOG(APP_LOG_LEVEL_ERROR, "Error: Calculated Layer Height <= 0"); 55 - // Handle error or default to a minimal height 56 - // layer_h = 10; // Example fallback 57 - } 58 - 59 - // Font sizes (adjust as needed - removed noonzone) 60 - #define BEAT_FONT FONT_KEY_GOTHIC_24_BOLD 61 - // #define NOONZONE_FONT FONT_KEY_GOTHIC_18_BOLD // Removed 62 - #define CLOSEST_FONT FONT_KEY_GOTHIC_18_BOLD 63 - #define TID_FONT FONT_KEY_GOTHIC_18_BOLD 64 - #define DECIMAL_FONT FONT_KEY_GOTHIC_18_BOLD 65 - 66 - // Calculate Y positions based on new layout 67 - // Beat: Top 68 - int16_t beat_y = 0; 69 - 70 - // TID: Bottom 71 - int16_t tid_y = bounds.size.h - layer_h; 36 + const int16_t total_h = bounds.size.h; 37 + const int16_t layer_h = total_h / 4; 72 38 73 - // Closest Noon: Above TID 74 - int16_t closest_y = tid_y - layer_h - v_padding; 75 - 76 - // Decimal: Halfway between bottom of Beat and top of Closest Noon 77 - // Bottom of Beat = beat_y + layer_h = layer_h 78 - // Top of Closest Noon = closest_y 79 - // Midpoint = (bottom_of_beat + top_of_closest_noon) / 2 80 - // Adjust for decimal layer height: Midpoint - layer_h / 2 81 - int16_t decimal_midpoint = (layer_h + closest_y) / 2; 82 - int16_t decimal_y = decimal_midpoint - (layer_h / 2); 83 - 84 - APP_LOG(APP_LOG_LEVEL_DEBUG, "Y Positions: Beat=%d, Decimal=%d, Closest=%d, TID=%d", beat_y, decimal_y, closest_y, tid_y); 85 - 86 - // Create Layers in new order 87 - // Beat Time TextLayer (Top) 88 - s_beat_layer = clock_beat_init(GRect(0, beat_y, bounds.size.w, layer_h), window_layer); 89 - 90 - // Decimal Time TextLayer (Middle) 91 - s_decimal_layer = clock_decimal_init(GRect(0, decimal_y, bounds.size.w, layer_h), window_layer); 92 - 93 - // Closest Noon Time TextLayer (Middle-Bottom) 94 - s_closest_noon_layer = clock_closest_noon_init(GRect(0, closest_y, bounds.size.w, layer_h), window_layer); 95 - 96 - // TID TextLayer (Bottom) 97 - s_tid_layer = clock_tid_init(GRect(0, tid_y, bounds.size.w, layer_h), window_layer); 98 - 99 - // // Create Noon Zone Time TextLayer // Removed 100 - // s_noonzone_layer = clock_noonzone_init(GRect(0, noonzone_y, bounds.size.w, layer_h), window_layer); 101 - 39 + // Create layers 40 + s_beat_layer = clock_beat_init(GRect(0, 0, bounds.size.w, layer_h), window_layer); 41 + s_decimal_layer = clock_decimal_init(GRect(0, layer_h, bounds.size.w, layer_h), window_layer); 42 + s_closest_noon_layer = clock_closest_noon_init(GRect(0, 2 * layer_h, bounds.size.w, layer_h), window_layer); 43 + s_tid_layer = clock_tid_init(GRect(0, 3 * layer_h, bounds.size.w, layer_h), window_layer); 102 44 } 103 45 104 46 static void main_window_unload(Window *window) { 105 47 // Destroy TextLayers 106 48 clock_beat_deinit(s_beat_layer); 107 - // clock_noonzone_deinit(s_noonzone_layer); // Removed 108 49 clock_closest_noon_deinit(s_closest_noon_layer); 109 50 clock_tid_deinit(s_tid_layer); 110 - clock_decimal_deinit(s_decimal_layer); 51 + clock_decimal_deinit(s_decimal_layer); 111 52 } 112 53 113 54 static void init() { 114 - // Seed random number generator (used by closest noon clock) 115 55 srand(time(NULL)); 116 56 117 57 // Create main Window element and assign to pointer