summaryrefslogtreecommitdiffstats
path: root/watch-library/simulator/watch/watch_rtc.c
blob: 107ae56ceab91a34ac3562f4b6af2e32265ff90f (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * MIT License
 *
 * Copyright (c) 2020 Joey Castillo
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "watch_rtc.h"
#include "watch_main_loop.h"

#include <emscripten.h>
#include <emscripten/html5.h>

static double time_offset = 0;
static long tick_callbacks[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };

static long alarm_interval_id = -1;
static long alarm_timeout_id = -1;
static double alarm_interval;
ext_irq_cb_t alarm_callback;
ext_irq_cb_t btn_alarm_callback;
ext_irq_cb_t a2_callback;
ext_irq_cb_t a4_callback;

bool _watch_rtc_is_enabled(void) {
    return true;
}

void _watch_rtc_init(void) {
}

void watch_rtc_set_date_time(watch_date_time date_time) {
    time_offset = EM_ASM_DOUBLE({
        const year = 2020 + (($0 >> 26) & 0x3f);
        const month = ($0 >> 22) & 0xf;
        const day = ($0 >> 17) & 0x1f;
        const hour = ($0 >> 12) & 0x1f;
        const minute = ($0 >> 6) & 0x3f;
        const second = $0 & 0x3f;
        const date = new Date(year, month - 1, day, hour, minute, second);
        return date - Date.now();
    }, date_time.reg);
}

watch_date_time watch_rtc_get_date_time(void) {
    watch_date_time retval;
    retval.reg = EM_ASM_INT({
        const date = new Date(Date.now() + $0);
        return date.getSeconds() |
            (date.getMinutes() << 6) |
            (date.getHours() << 12) |
            (date.getDate() << 17) |
            ((date.getMonth() + 1) << 22) |
            ((date.getFullYear() - 2020) << 26);
    }, time_offset);
    return retval;
}

void watch_rtc_register_tick_callback(ext_irq_cb_t callback) {
    watch_rtc_register_periodic_callback(callback, 1);
}

void watch_rtc_disable_tick_callback(void) {
    watch_rtc_disable_periodic_callback(1);
}

static void watch_invoke_periodic_callback(void *userData) {
    ext_irq_cb_t callback = userData;
    callback();
    resume_main_loop();
}

void watch_rtc_register_periodic_callback(ext_irq_cb_t callback, uint8_t frequency) {
    // we told them, it has to be a power of 2.
    if (__builtin_popcount(frequency) != 1) return;

    // this left-justifies the period in a 32-bit integer.
    uint32_t tmp = frequency << 24;
    // now we can count the leading zeroes to get the value we need.
    // 0x01 (1 Hz) will have 7 leading zeros for PER7. 0xF0 (128 Hz) will have no leading zeroes for PER0.
    uint8_t per_n = __builtin_clz(tmp);

    // this also maps nicely to an index for our list of tick callbacks.
    double interval = 1000 / frequency; // in msec

    if (tick_callbacks[per_n] != -1) emscripten_clear_interval(tick_callbacks[per_n]);
    tick_callbacks[per_n] = emscripten_set_interval(watch_invoke_periodic_callback, interval, (void *)callback);
}

void watch_rtc_disable_periodic_callback(uint8_t frequency) {
    if (__builtin_popcount(frequency) != 1) return;
    uint8_t per_n = __builtin_clz(frequency << 24);
    if (tick_callbacks[per_n] != -1) {
        emscripten_clear_interval(tick_callbacks[per_n]);
        tick_callbacks[per_n] = -1;
    }
}

void watch_rtc_disable_matching_periodic_callbacks(uint8_t mask) {
    for (int i = 0; i < 8; i++) {
        if (tick_callbacks[i] != -1 && (mask & (1 << (7 - i))) != 0) {
            emscripten_clear_interval(tick_callbacks[i]);
            tick_callbacks[i] = -1;
        }
    }
}

void watch_rtc_disable_all_periodic_callbacks(void) {
    watch_rtc_disable_matching_periodic_callbacks(0xFF);
}

static void watch_invoke_alarm_interval_callback(void *userData) {
    if (alarm_callback) alarm_callback();
}

static void watch_invoke_alarm_callback(void *userData) {
    if (alarm_callback) alarm_callback();
    alarm_interval_id = emscripten_set_interval(watch_invoke_alarm_interval_callback, alarm_interval, NULL);
}

void watch_rtc_register_alarm_callback(ext_irq_cb_t callback, watch_date_time alarm_time, watch_rtc_alarm_match mask) {
    watch_rtc_disable_alarm_callback();

    switch (mask) {
        case ALARM_MATCH_DISABLED:
            return;
        case ALARM_MATCH_SS:
            alarm_interval = 60 * 1000;
            break;
        case ALARM_MATCH_MMSS:
            alarm_interval = 60 * 60 * 1000;
            break;
        case ALARM_MATCH_HHMMSS:
            alarm_interval = 60 * 60 * 60 * 1000;
            break;
    }

    double timeout = EM_ASM_DOUBLE({
        const now = Date.now();
        const date = new Date(now + $0);

        const hour = ($1 >> 12) & 0x1f;
        const minute = ($1 >> 6) & 0x3f;
        const second = $1 & 0x3f;

        if ($2 == 1) { // SS
            if (second < date.getSeconds()) date.setMinutes(date.getMinutes() + 1);
            date.setSeconds(second);
        } else if ($2 == 2) { // MMSS
            if (second < date.getSeconds()) date.setMinutes(date.getMinutes() + 1);
            if (minute < date.getMinutes()) date.setHours(date.getHours() + 1);
            date.setMinutes(minute, second);
        } else if ($2 == 3) { // HHMMSS
            if (second < date.getSeconds()) date.setMinutes(date.getMinutes() + 1);
            if (minute < date.getMinutes()) date.setHours(date.getHours() + 1);
            if (hour < date.getHours()) date.setDate(date.getDate() + 1);
            date.setHours(hour, minute, second);
        } else {
            throw 'Invalid alarm match mask';
        }

        return date - now;
    }, time_offset, alarm_time.reg, mask);

    alarm_callback = callback;
    alarm_timeout_id = emscripten_set_timeout(watch_invoke_alarm_callback, timeout, NULL);
}

void watch_rtc_disable_alarm_callback(void) {
    alarm_callback = NULL;
    alarm_interval = 0;

    if (alarm_timeout_id != -1) {
        emscripten_clear_timeout(alarm_timeout_id);
        alarm_timeout_id = -1;
    }

    if (alarm_interval_id != -1) {
        emscripten_clear_interval(alarm_interval_id);
        alarm_interval_id = -1;
    }
}