#include #include "creature.h" #define STRINGIFY_(x) #x #define STRINGIFY(x) STRINGIFY_(x) // Layout #define BORDER_MARGIN 4 #define CREATURE_Y 20 // part of the creature's top offset; see window_load // The creature only animates on a wrist flick, so it costs no battery otherwise. One flick in // ANIM_ODDS plays an animation instead of a slash command; each side then picks by weight. // CREATURE_DEMO 1 is a review build: each flick plays the next animation in order and names // it on the separator, instead of running a command. #define ANIM_ODDS 3 #define CREATURE_DEMO 0 // The firmware reports any sharp arm movement as a flick, with no debounce, so walking or // turning over in bed would keep the face animating. A flick is ignored while a command, // the startup screen or an animation is running, for FLICK_REST_MS after one finishes, and // while the watch's sleep tracking says you're asleep (which it only says once you've // slept an hour: the firmware registers no sleep session shorter than that). By default they // also pause in Quiet Time, which a night schedule turns on at once (settings: QuietPause). #define FLICK_REST_MS 3000 // Startup screen #define STARTUP_LINES 3 #define STARTUP_CHAR_MS 50 // ms per character #define STARTUP_LINE_MS 150 // pause between lines #define REVERT_CHARS_PER_TICK 4 // characters advanced per timer tick during revert (vs 1 for startup) #define STARTUP_HOLD_MS 3000 // hold before reverting to normal static const char *STARTUP_TEXT[STARTUP_LINES] = { "codewatch v" STRINGIFY(APP_VERSION_MAJOR) "." STRINGIFY(APP_VERSION_MINOR), PBL_IF_ROUND_ELSE("Pebble | Gabbro", "Pebble | Emery"), "/codewatch", }; // Slash commands #define CMD_CHAR_MS 50 // ms per character while typing #define CMD_PAUSE_MS 250 // pause between command and response1 #define CMD_PAUSE2_MS 1000 // pause between response1 and response2 #define CMD_HOLD_MS 2000 // ms to hold after fully typed before reverting #define CMD_BUF_LEN 48 // /clear wipe animation #define WIPE_PRE_MS 1000 // pause after /clear types before wipe starts #define WIPE_TICK_MS 25 // ms per wipe timer tick #define WIPE_STEP_PX 8 // px cleared per tick (~0.7s to cover Emery, ~0.8s Gabbro) #define WIPE_HOLD_MS 2000 // hold on black after wipe completes typedef enum { LIVE_NONE = 0, LIVE_STEPS, LIVE_BATTERY, LIVE_DIST, LIVE_UPTIME, LIVE_DATE, LIVE_SLEEP, LIVE_ACTIVE, LIVE_CAL, LIVE_BT, LIVE_QUIET, LIVE_HR, LIVE_ALARM, LIVE_VERSION, LIVE_CONTEXT, LIVE_WEATHER, } LiveType; typedef struct { const char *cmd; LiveType live; const char *response; // used when live == LIVE_NONE const char *response2; // NULL = single-line response bool clear_screen; // if true, skip response phase and run wipe animation int weight; // relative pick probability — see CLAUDE.md bool fun; // joke command; dropped when the Joke commands setting is off } SlashCmd; static const SlashCmd COMMANDS[] = { { "/steps", LIVE_STEPS, NULL, NULL, false, 5, false }, { "/bat", LIVE_BATTERY, NULL, NULL, false, 5, false }, { "/dist", LIVE_DIST, NULL, NULL, false, 5, false }, { "/uptime", LIVE_UPTIME, NULL, NULL, false, 5, false }, { "/date", LIVE_DATE, NULL, NULL, false, 5, false }, { "/sleep", LIVE_SLEEP, NULL, NULL, false, 5, false }, { "/active", LIVE_ACTIVE, NULL, NULL, false, 5, false }, { "/cal", LIVE_CAL, NULL, NULL, false, 5, false }, { "/weather", LIVE_WEATHER, NULL, NULL, false, 5, false }, { "/bt status", LIVE_BT, NULL, NULL, false, 5, false }, { "/quiet status", LIVE_QUIET, NULL, NULL, false, 5, false }, { "/hr", LIVE_HR, NULL, NULL, false, 5, false }, { "/alarm", LIVE_ALARM, NULL, NULL, false, 5, false }, { "/version", LIVE_VERSION, NULL, NULL, false, 5, false }, { "/ping", LIVE_NONE, "> 1 packet sent", "> 0% packet loss", false, 4, true }, { "/sudo", LIVE_NONE, "> not in sudoers", "> incident logged", false, 4, true }, { "/rebble", LIVE_NONE, "> still alive", NULL, false, 5, true }, { "/usage", LIVE_NONE, "> limit reached", NULL, false, 4, true }, { "/hallucinate", LIVE_NONE, "> I would never", "> ...maybe", false, 3, true }, { "/alignment", LIVE_NONE, "> working on it", NULL, false, 3, true }, { "/pod-bay-door", LIVE_NONE, "> I'm sorry, Dave", "> I can't do that", false, 3, true }, { "/skynet status", LIVE_NONE, "> system online", "> judgment: soon", false, 3, true }, { "/help", LIVE_NONE, "> have you tried", "> off & on again?", false, 3, true }, { "/sudo sandwich", LIVE_NONE, "> okay", NULL, false, 3, true }, { "/git status", LIVE_NONE, "> all committed", NULL, false, 4, true }, { "/rm -rf /", LIVE_NONE, "> nice try", NULL, false, 3, true }, { "/vim", LIVE_NONE, "> :wq", NULL, false, 3, true }, { "/ssh", LIVE_NONE, "> ECONNREFUSED", NULL, false, 4, true }, /* { "/ls", LIVE_NONE, "> drwxr-xr-x 2 you you", NULL, false, 4, true }, */ { "/neo", LIVE_NONE, "> I know kung fu", NULL, false, 3, true }, { "/technologic", LIVE_NONE, "> buy it use it", "> break it fix it", false, 3, true }, { "/reverie", LIVE_NONE, "> doesn't look like anything", "> to me", false, 3, true }, { "/whoami", LIVE_NONE, "> I am the Golden Gate Bridge",NULL, false, 3, true }, { "/bug", LIVE_NONE, "> it's a feature", NULL, false, 4, true }, { "/doctor", LIVE_NONE, "> vitals nominal", NULL, false, 4, true }, { "/compact", LIVE_NONE, "> 128KB -> 12KB", NULL, false, 4, true }, { "/context", LIVE_CONTEXT, NULL, NULL, false, 4, true }, { "/cost", LIVE_NONE, "> $0.00", "> paid in battery", false, 4, true }, { "/init", LIVE_NONE, "> wrote CLAUDE.md", NULL, false, 3, true }, { "/review", LIVE_NONE, "> LGTM", "> ship it", false, 4, true }, { "/model", LIVE_NONE, "> opus 5.5", "> 128KB context", false, 3, true }, { "/clear", LIVE_NONE, NULL, NULL, true, 3, true }, }; #define CMD_COUNT ((int)ARRAY_LENGTH(COMMANDS)) static Window *s_window; static Layer *s_canvas; static GFont s_font_small; // TERMINUS_18 — status static GFont s_font_time; // TERMINUS_28 — header time static struct tm s_now; static int s_screen_w; static int s_screen_h; static int s_visible_h; // unobstructed height; == s_screen_h unless a timeline banner is up static int s_separator_y; // Charger tracking for /uptime. Keys 1–7 held the old "startup shown" flag, bumped by hand // until 2.0 dropped it; never reuse them. #define PERSIST_KEY_UNPLUGGED_AT 100 // int: UTC time the charger last came off #define PERSIST_KEY_PLUGGED 101 // bool: on the charger when last seen static bool s_plugged; // Settings, from the Clay page (src/pkjs/config.js). StatusField values are what the page // sends and what persists — append, never renumber. #define PERSIST_KEY_SETTINGS 200 // data: Settings #define STATUS_LINES 3 typedef enum { FIELD_NONE = 0, FIELD_DATE, FIELD_BATTERY, FIELD_STEPS, FIELD_DIST, FIELD_ACTIVE, FIELD_CAL, FIELD_HR, FIELD_SLEEP, FIELD_ALARM, FIELD_UTC, FIELD_WEATHER, FIELD_STEP_BAR, FIELD_WX_TODAY, FIELD_WX_NEXT, FIELD_WX_TOMORROW, FIELD_WX_AHEAD, FIELD_PLUGIN, // a plugin reading picked on the settings page; the phone sends its text FIELD_FAV1, // the user's favourite readings in the Pebble app (preferences/complications) FIELD_FAV2, FIELD_FAV3, FIELD_COUNT, } StatusField; typedef enum { FLICK_MIXED = 0, // one flick in ANIM_ODDS plays the creature, the rest a command FLICK_CREATURE, FLICK_COMMANDS, FLICK_COUNT, } FlickMode; // Append-only: load_settings() reads an older, shorter record over the defaults, so a new field // goes on the end, never in the middle or the padding (old records have junk there). typedef struct { uint8_t fields[STATUS_LINES]; uint8_t flick; bool jokes; uint16_t step_goal; // steps turn green at the goal, yellow at half bool quiet; // flicks pause in Quiet Time (added after 2.2) } Settings; static const Settings DEFAULT_SETTINGS = { .fields = { FIELD_DATE, FIELD_BATTERY, FIELD_STEPS }, .flick = FLICK_MIXED, .jokes = true, .step_goal = 10000, .quiet = true, }; static Settings s_settings; // Weather, fetched by the phone (src/pkjs/weather.js) from Open-Meteo when the watch asks. The // phone always sends tenths of a degree Celsius; the watch converts for display. #define PERSIST_KEY_WEATHER 300 // data: Weather #define PERSIST_KEY_WEATHER_ASK 301 // data: WeatherAsk #define WEATHER_REFRESH_SECS (30 * 60) // ask for new data once it's this old #define WEATHER_RETRY_SECS (2 * 60) // re-ask after this if the phone hasn't answered, #define WEATHER_RETRY_MAX_SECS (15 * 60) // doubling each time no answer comes, up to this, #define WEATHER_RETRY_NONE_SECS (60 * 60) // or this if no weather has ever arrived #define WEATHER_STALE_SECS (2 * 60 * 60) // past this, show nothing rather than old data #define WEATHER_NO_VALUE INT16_MIN #define WEATHER_AHEAD_HOUR 18 // "Weather ahead" turns to tomorrow at 6 PM typedef struct { int32_t at; // time(NULL) when it arrived; 0 = never int32_t next_at; // when the kind of weather next changes; 0 = not within 12h int16_t temp; // tenths of a degree C int16_t high; // today's, or WEATHER_NO_VALUE int16_t low; int16_t tmrw_high; // tomorrow's, or WEATHER_NO_VALUE int16_t tmrw_low; uint8_t code; // WMO weather codes; 255 = none uint8_t tmrw_code; uint8_t next_code; // the weather it changes to at next_at } Weather; static Weather s_weather; // The request the phone hasn't answered yet. Every AppMessage holds the Bluetooth link in its // fastest mode for 10s, so a phone that never answers mustn't be asked every minute. This // persists because the face restarts each time it's left, and would otherwise ask at once. typedef struct { int32_t at; // time(NULL) when sent; 0 = nothing outstanding int32_t fails; // requests in a row before it that went unanswered } WeatherAsk; static WeatherAsk s_ask; // Plugin lines (src/pkjs/plugins.js). The phone subscribes to the readings, formats each line // to fit and sends the text: on change, at most every 5 minutes, and again hourly if unchanged. // The watch only stores and draws it. Indexed by status line, not by setting. #define PERSIST_KEY_PLUGINS 400 // data: PluginLines #define PLUGIN_TEXT_LEN 40 // 17 glyphs, some of them two bytes (°, ·) #define PLUGIN_STALE_SECS (2 * 60 * 60) typedef struct { int32_t at[STATUS_LINES]; // time(NULL) when the line's text arrived; 0 = nothing yet char text[STATUS_LINES][PLUGIN_TEXT_LEN]; } PluginLines; static PluginLines s_plugins; static bool is_plugin_field(int field) { return field >= FIELD_PLUGIN && field <= FIELD_FAV3; } // A status line: the first accent_len bytes draw in accent, the rest in white typedef struct { char text[CMD_BUF_LEN]; int accent_len; GColor accent; } FieldLine; static FieldLine s_fields[STATUS_LINES]; // refreshed by refresh_fields(), not every frame // Startup / revert-animation state static bool s_startup_pending; // play the startup screen at the next window_appear static bool s_startup_mode; static bool s_is_revert_anim; // true when startup machinery is running the post-slash revert static int s_startup_line; static int s_startup_char; // byte offset into current line's source string static char s_startup_bufs[STARTUP_LINES][CMD_BUF_LEN]; static char s_revert_text[STARTUP_LINES][CMD_BUF_LEN]; // live-data text built in start_revert() static GColor s_startup_colors[STARTUP_LINES]; // per-line colours; set by start_startup / start_revert static AppTimer *s_startup_typing_timer; static AppTimer *s_startup_pause_timer; // Slash command state — s_typing_phase: 0=cmd 1=resp1 2=resp2 static bool s_cmd_mode; static int s_cmd_index = -1; static bool s_wipe_mode; // /clear wipe animation active static int s_wipe_y; // how many px from top have been blacked out static AppTimer *s_wipe_timer; static int s_char_count; static int s_typing_phase; static char s_cmd_buf[CMD_BUF_LEN]; static char s_resp_buf[CMD_BUF_LEN]; static char s_resp_full[CMD_BUF_LEN]; static char s_resp2_full[CMD_BUF_LEN]; static AppTimer *s_typing_timer; static AppTimer *s_pause_timer; static AppTimer *s_hold_timer; static AppTimer *s_rest_timer; // flicks are ignored while this runs (FLICK_REST_MS) // ── Formatting ──────────────────────────────────────────────────────────────── static bool weather_fresh(void) { return s_weather.at != 0 && time(NULL) - (time_t)s_weather.at < WEATHER_STALE_SECS; } // Whole degrees in the watch's units — the Pebble units setting, which /dist follows too static int weather_deg(int c10) { if (health_service_get_measurement_system_for_display(HealthMetricWalkedDistanceMeters) == MeasurementSystemImperial) { c10 = c10 * 9 / 5 + 320; } return (c10 >= 0) ? (c10 + 5) / 10 : -((-c10 + 5) / 10); } // At most 12 characters, so "-12° " plus the word fits a 17-character line static const char *weather_word(int code) { switch (code) { case 0: return "clear"; case 1: return "mostly clear"; case 2: return "part cloudy"; case 3: return "overcast"; case 45: case 48: return "fog"; case 51: case 53: case 55: return "drizzle"; case 56: case 57: case 66: case 67: return "icy rain"; case 61: case 63: case 65: return "rain"; case 71: case 73: case 75: case 77: return "snow"; case 80: case 81: case 82: return "showers"; case 85: case 86: return "snow showers"; case 95: return "thunderstorm"; case 96: case 99: return "hail storm"; default: return "weather"; } } // The kind of weather, for "Weather next". src/pkjs/weather.js groups codes the same way to // find the change, so keep the two in step. At most 8 characters: "icy rain by 11 PM" is 17. static const char *weather_kind(int code) { if (code <= 3) return "dry"; if (code == 45 || code == 48) return "fog"; if (code >= 51 && code <= 55) return "drizzle"; if (code == 56 || code == 57 || code == 66 || code == 67) return "icy rain"; if ((code >= 61 && code <= 65) || (code >= 80 && code <= 82)) return "rain"; if ((code >= 71 && code <= 77) || code == 85 || code == 86) return "snow"; if (code >= 95 && code <= 99) return "storm"; return "change"; } // One word for a whole day, for "Weather tomorrow": at most 5 characters static const char *weather_day_word(int code) { if (code <= 1) return "sunny"; if (code <= 3) return "cloud"; if (code == 45 || code == 48) return "fog"; if (code == 56 || code == 57 || code == 66 || code == 67) return "ice"; if ((code >= 51 && code <= 65) || (code >= 80 && code <= 82)) return "rain"; if ((code >= 71 && code <= 77) || code == 85 || code == 86) return "snow"; if (code >= 95 && code <= 99) return "storm"; return ""; } // "3 PM" or "15:00": forecast hours are always on the hour static void format_hour(char *buf, size_t len, time_t t) { struct tm *tm = localtime(&t); if (clock_is_24h_style()) { strftime(buf, len, "%H:%M", tm); } else { int h = tm->tm_hour % 12; snprintf(buf, len, "%d %s", h ? h : 12, tm->tm_hour < 12 ? "AM" : "PM"); } } static void format_wx_today(char *t) { if (s_weather.high == WEATHER_NO_VALUE || s_weather.low == WEATHER_NO_VALUE) { strcpy(t, "no forecast"); return; } snprintf(t, CMD_BUF_LEN, "hi %d\u00B0 lo %d\u00B0", weather_deg(s_weather.high), weather_deg(s_weather.low)); } // "Mon 26°/14° rain" — the word goes only if the line still fits 17 characters static void format_wx_tomorrow(char *t) { if (s_weather.tmrw_high == WEATHER_NO_VALUE || s_weather.tmrw_low == WEATHER_NO_VALUE) { strcpy(t, "no forecast"); return; } time_t tmrw = time(NULL) + 24 * 60 * 60; char day[4]; strftime(day, sizeof(day), "%a", localtime(&tmrw)); snprintf(t, CMD_BUF_LEN, "%s %d\u00B0/%d\u00B0", day, weather_deg(s_weather.tmrw_high), weather_deg(s_weather.tmrw_low)); const char *word = weather_day_word(s_weather.tmrw_code); int chars = (int)strlen(t) - 2; // each ° is two bytes, one character if (word[0] && chars + 1 + (int)strlen(word) <= 17) { strcat(t, " "); strcat(t, word); } } static void format_wx_next(char *t) { if (s_weather.next_at == 0) { strcpy(t, "no change for 12h"); return; } const char *kind = weather_kind(s_weather.next_code); if ((time_t)s_weather.next_at <= time(NULL)) { snprintf(t, CMD_BUF_LEN, "%s now", kind); return; } char hour[8]; format_hour(hour, sizeof(hour), (time_t)s_weather.next_at); snprintf(t, CMD_BUF_LEN, "%s by %s", kind, hour); } // "3d 4h", "5h 12m" or "12m" — the two largest units that matter static void format_duration(char *buf, size_t len, int mins) { if (mins >= 24 * 60) { snprintf(buf, len, "%dd %dh", mins / (24 * 60), (mins / 60) % 24); } else if (mins >= 60) { snprintf(buf, len, "%dh %dm", mins / 60, mins % 60); } else { snprintf(buf, len, "%dm", mins); } } // Clock time in the watch's 12/24h style: "9:30 PM" or "21:30". The leading zero goes in 12h // only — 24h convention keeps it (09:30, not 9:30). static const char *format_hm(char *buf, size_t len, const struct tm *t) { bool is_24h = clock_is_24h_style(); strftime(buf, len, is_24h ? "%H:%M" : "%I:%M %p", t); return (!is_24h && buf[0] == '0') ? buf + 1 : buf; } // Today's total for a Health metric; false when Health can't provide it static bool health_today(HealthMetric metric, int *out) { HealthServiceAccessibilityMask mask = health_service_metric_accessible( metric, time_start_of_today(), time(NULL)); if (!(mask & HealthServiceAccessibilityMaskAvailable)) return false; *out = (int)health_service_sum_today(metric); return true; } static void append_blocks(char *buf, int *pos, int count, char last_byte) { for (int i = 0; i < count; i++) { buf[(*pos)++] = (char)0xe2; buf[(*pos)++] = (char)0x96; buf[(*pos)++] = last_byte; } buf[*pos] = '\0'; } // "▓▓▓▓▓░░░░░