summaryrefslogtreecommitdiffstats
path: root/apps/main_clock/main.cpp
blob: b4f469462d277b585297f2ff236369982b464044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include "pico/stdlib.h"

#include "main.hpp"
// For time format:
#include "../../globals.hpp"

// TODO in this app
// - 

// Time as string
// Adapted from pico-sdk/scr/common/pico_util/datetime.c
void app_main_clock::time_as_str(char *buf, uint buf_size, const datetime_t *t) {
    int hour;
    if (g_user.time_format) // 24 hour format
        hour = t->hour;
    else { // 12 hour format
        hour = t->hour % 12;
        if (hour == 0) hour = 12;
    }
    snprintf(buf,
            buf_size,
            "%02d:%02d ",
            hour, t->min, t->sec);
};

void app_main_clock::date_as_str(char *buf, uint buf_size, const datetime_t *t) {
    static const char *DATETIME_DOWS[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    snprintf(buf,
            buf_size,
            "%02d-%02d-%02d %s",
            t->year,
            t->month,
            t->day,
            DATETIME_DOWS[t->dotw]);
};

void app_main_clock::show_datetime(Api *app_api) {
    char datetime_buf[256];
    char *datetime_str = &datetime_buf[0];
    datetime_t t;
    app_api->datetime_get(&t);

    // time
    time_as_str(datetime_str, sizeof(datetime_buf), &t);
    app_api->display_write_string(0,25,3, datetime_str, FONT_12x16, 0, 1);

    snprintf(datetime_str, sizeof(datetime_buf), "%02d", t.sec);
    app_api->display_write_string(0,87,4, datetime_str, FONT_8x8, 0, 1);

    // for 12 hour time
    if (!g_user.time_format) {
        bool is_pm = !(t.hour < 12);
        const char *indicator = is_pm? "PM" : "AM";
        app_api->display_write_string(0,98,2, indicator, FONT_8x8, 0, 1);
    }

    // date
    date_as_str(datetime_str, sizeof(datetime_buf), &t);
    app_api->gui_footer_text((std::string)datetime_str);
}

BaseApp::AppReturnValues app_main_clock::render(Api *app_api) {
    app_api->gui_header_text("Test clock", 17);
    show_datetime(app_api);

    return AppReturnValues::OK;
}

BaseApp::AppReturnValues app_main_clock::btn_pressed(Api *app_api, uint gpio, unsigned long delta) {
    return AppReturnValues::OK;
}

app_main_clock::app_main_clock(Api *app_api) {
    app_api->performance_set(Api::perf_modes::LOW_POWER);
}

BaseApp::AppReturnValues app_main_clock::bgrefresh(Api *app_api, bool in_foreground) {
    return AppReturnValues::OK;
}

app_main_clock::~app_main_clock() {
}
ass="p">(const char *hex, const char *prefix) { char *argv[] = { "gzip", "-n", NULL }; return write_compressed_tar_archive(hex, prefix, argv); } static int write_tar_bzip2_archive(const char *hex, const char *prefix) { char *argv[] = { "bzip2", NULL }; return write_compressed_tar_archive(hex, prefix, argv); } static int write_tar_xz_archive(const char *hex, const char *prefix) { char *argv[] = { "xz", NULL }; return write_compressed_tar_archive(hex, prefix, argv); } const struct cgit_snapshot_format cgit_snapshot_formats[] = { { ".zip", "application/x-zip", write_zip_archive, 0x01 }, { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 }, { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 }, { ".tar", "application/x-tar", write_tar_archive, 0x08 }, { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 }, { NULL } }; static const struct cgit_snapshot_format *get_format(const char *filename) { const struct cgit_snapshot_format *fmt; for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) { if (!suffixcmp(filename, fmt->suffix)) return fmt; } return NULL; } static int make_snapshot(const struct cgit_snapshot_format *format, const char *hex, const char *prefix, const char *filename) { unsigned char sha1[20]; if (get_sha1(hex, sha1)) { cgit_print_error("Bad object id: %s", hex); return 1; } if (!lookup_commit_reference(sha1)) { cgit_print_error("Not a commit reference: %s", hex); return 1; } ctx.page.mimetype = xstrdup(format->mimetype); ctx.page.filename = xstrdup(filename); cgit_print_http_headers(); format->write_func(hex, prefix); return 0; } /* Try to guess the requested revision from the requested snapshot name. * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become * "cgit-0.7.2". If this is a valid commit object name we've got a winner. * Otherwise, if the snapshot name has a prefix matching the result from * repo_basename(), we strip the basename and any following '-' and '_' * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once * more. If this still isn't a valid commit object name, we check if pre- * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" -> * "v0.7.2") gives us something valid. */ static const char *get_ref_from_filename(const char *url, const char *filename, const struct cgit_snapshot_format *format) { const char *reponame; unsigned char sha1[20]; struct strbuf snapshot = STRBUF_INIT; int result = 1; strbuf_addstr(&snapshot, filename); strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix)); if (get_sha1(snapshot.buf, sha1) == 0) goto out; reponame = cgit_repobasename(url); if (prefixcmp(snapshot.buf, reponame) == 0) { const char *new_start = snapshot.buf; new_start += strlen(reponame); while (new_start && (*new_start == '-' || *new_start == '_')) new_start++; strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0); } if (get_sha1(snapshot.buf, sha1) == 0) goto out; strbuf_insert(&snapshot, 0, "v", 1); if (get_sha1(snapshot.buf, sha1) == 0) goto out; strbuf_splice(&snapshot, 0, 1, "V", 1); if (get_sha1(snapshot.buf, sha1) == 0) goto out; result = 0; strbuf_release(&snapshot); out: return result ? strbuf_detach(&snapshot, NULL) : NULL; } __attribute__((format (printf, 1, 2))) static void show_error(char *fmt, ...) { va_list ap; ctx.page.mimetype = "text/html"; cgit_print_http_headers(); cgit_print_docstart(); cgit_print_pageheader(); va_start(ap, fmt); cgit_vprint_error(fmt, ap); va_end(ap); cgit_print_docend(); } void cgit_print_snapshot(const char *head, const char *hex, const char *filename, int snapshots, int dwim) { const struct cgit_snapshot_format* f; char *prefix = NULL; if (!filename) { show_error("No snapshot name specified"); return; } f = get_format(filename); if (!f) { show_error("Unsupported snapshot format: %s", filename); return; } if (!hex && dwim) { hex = get_ref_from_filename(ctx.repo->url, filename, f); if (hex == NULL) { html_status(404, "Not found", 0); return; } prefix = xstrdup(filename); prefix[strlen(filename) - strlen(f->suffix)] = '\0'; } if (!hex) hex = head; if (!prefix) prefix = xstrdup(cgit_repobasename(ctx.repo->url)); make_snapshot(f, hex, prefix, filename); free(prefix); }