summaryrefslogtreecommitdiffstats
path: root/main/siderial.patch
blob: 2a2b8944551c7239ab4b629dfbb0a1611883c313 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
diff --git a/movement/alt_fw/jmm.h b/movement/alt_fw/jmm.h
index 5c2cee7..061d058 100644
--- a/movement/alt_fw/jmm.h
+++ b/movement/alt_fw/jmm.h
@@ -12,6 +12,7 @@ const watch_face_t watch_faces[] = {
     world_clock_face,
     metric_face,
     totp_face_lfs,
+    siderial_face,
     moon_phase_face,
     sunrise_sunset_face,
     decimal_time_face,
diff --git a/movement/make/Makefile b/movement/make/Makefile
index e131bbb..7110be2 100644
--- a/movement/make/Makefile
+++ b/movement/make/Makefile
@@ -121,6 +121,7 @@ SRCS += \
   ../watch_faces/clock/decimal_time_face.c \
   ../watch_faces/clock/wyoscan_face.c \
   ../watch_faces/complication/metric_face.c \
+  ../watch_faces/complication/siderial_face.c \
 # New watch faces go above this line.
 
 # Leave this line at the bottom of the file; it has all the targets for making your project.
diff --git a/movement/movement_faces.h b/movement/movement_faces.h
index 94316ce..c605743 100644
--- a/movement/movement_faces.h
+++ b/movement/movement_faces.h
@@ -96,6 +96,7 @@
 #include "decimal_time_face.h"
 #include "wyoscan_face.h"
 #include "metric_face.h"
+#include "siderial_face.h"
 // New includes go above this line.
 
 #endif // MOVEMENT_FACES_H_
diff --git a/movement/watch_faces/complication/siderial_face.c b/movement/watch_faces/complication/siderial_face.c
new file mode 100644
index 0000000..b60f4a9
--- /dev/null
+++ b/movement/watch_faces/complication/siderial_face.c
@@ -0,0 +1,175 @@
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include "watch.h"
+#include "watch_utility.h"
+#include "vsop87a_micro.h"      // smaller size, less accurate
+#include "vsop87a_milli.h"
+#include "astrolib.h"
+#include "siderial_face.h"
+
+#define TICK_MAX 14400
+
+
+static uint32_t
+date_time_to_ds (watch_date_time dt)
+{
+    uint32_t ret;
+    ret = dt.unit.hour;
+    ret *= 60;
+    ret += dt.unit.minute;
+    ret *= 60;
+    ret += dt.unit.second;
+    return ret;
+}
+
+static void
+_siderial_face_recalculate (movement_settings_t * settings,
+                            siderial_state_t * state)
+{
+    watch_date_time date_time = watch_rtc_get_date_time ();
+    movement_location_t movement_location;
+
+    double jd, days;
+    uint32_t timestamp = watch_utility_date_time_to_unix_time (date_time,
+                                                               movement_timezone_offsets
+                                                               [settings->bit.
+                                                                time_zone] *
+                                                               60);
+
+    state->ref_ds = date_time_to_ds (date_time);
+
+    date_time = watch_utility_date_time_from_unix_time (timestamp, 0);
+    jd = astro_convert_date_to_julian_date (date_time.unit.year +
+                                            WATCH_RTC_REFERENCE_YEAR,
+                                            date_time.unit.month,
+                                            date_time.unit.day,
+                                            date_time.unit.hour,
+                                            date_time.unit.minute,
+                                            date_time.unit.second);
+
+    jd -= .5;
+
+    if (state->lst)
+      {
+          movement_location = (movement_location_t) watch_get_backup_data (1);
+          jd += ((double) movement_location.bit.longitude) / 36000.;
+      }
+
+
+    state->ref_st = (uint32_t) (modf (jd, &days) * 86400. + .5);
+    state->ticks = 0;
+}
+
+static void
+_siderial_face_update (movement_event_t event, movement_settings_t * settings,
+                       siderial_state_t * state, int show_secs)
+{
+    watch_date_time date_time = watch_rtc_get_date_time ();
+    uint32_t ds = date_time_to_ds (date_time);
+    char buf[14];
+    int sh, sm, ss;
+
+    (void) event;
+
+    if (state->ticks >= TICK_MAX)
+        _siderial_face_recalculate (settings, state);
+
+
+    ss = state->ref_st + (int) (.5 +
+                                (86164.0905 / 86400.) * (double) (ds -
+                                                                  state->ref_ds));
+    if (ss > 86400.)
+        ss -= 86400.;
+#if 0 //The compiler is too dumb to realize the bounds so generates a Werror for the sprintf
+    sm = ss / 60;
+    ss -= sm * 60;
+    sh = sm / 60;
+    sm -= sh * 60;
+#else
+    sm = ss / 60;
+    ss %=  60;
+    sh = sm / 60;
+    sm %= 60;
+    sh %= 64; 
+#endif
+
+    sprintf (buf, "ST %c%02d%02d%02d", state->lst ? 'L' : ' ', sh, sm, ss);
+
+    if (!show_secs)
+      {
+          buf[8] = ' ';
+          buf[9] = ' ';
+      }
+
+    watch_display_string (buf, 0);
+}
+
+void
+siderial_face_setup (movement_settings_t * settings, uint8_t watch_face_index,
+                     void **context_ptr)
+{
+    (void) settings;
+    (void) watch_face_index;
+    if (*context_ptr == NULL)
+      {
+          *context_ptr = malloc (sizeof (siderial_state_t));
+          memset (*context_ptr, 0, sizeof (siderial_state_t));
+      }
+}
+
+void
+siderial_face_activate (movement_settings_t * settings, void *context)
+{
+    siderial_state_t *state = (siderial_state_t *) context;
+    (void) settings;
+    movement_request_tick_frequency (2);
+    state->ticks = TICK_MAX;
+}
+
+bool
+siderial_face_loop (movement_event_t event, movement_settings_t * settings,
+                    void *context)
+{
+    (void) settings;
+    siderial_state_t *state = (siderial_state_t *) context;
+
+    switch (event.event_type)
+      {
+      case EVENT_ACTIVATE:
+          watch_set_colon ();
+          watch_set_indicator (WATCH_INDICATOR_24H);
+          /*fall through */
+      case EVENT_TICK:
+          state->ticks++;
+          _siderial_face_update (event, settings, state, 1);
+          break;
+      case EVENT_LOW_ENERGY_UPDATE:
+          state->ticks += 60;
+          _siderial_face_update (event, settings, state, 0);
+          break;
+      case EVENT_ALARM_BUTTON_UP:
+          state->lst ^= 1;
+          state->ticks = TICK_MAX;
+          _siderial_face_update (event, settings, state, 1);
+          break;
+      case EVENT_ALARM_LONG_PRESS:
+          break;
+      case EVENT_TIMEOUT:
+          state->ticks = TICK_MAX;
+          break;
+      default:
+          movement_default_loop_handler (event, settings);
+          break;
+      }
+
+    return true;
+}
+
+void
+siderial_face_resign (movement_settings_t * settings, void *context)
+{
+    (void) settings;
+    siderial_state_t *state = (siderial_state_t *) context;
+    state->ticks = TICK_MAX;
+}
diff --git a/movement/watch_faces/complication/siderial_face.h b/movement/watch_faces/complication/siderial_face.h
new file mode 100644
index 0000000..a4ae67f
--- /dev/null
+++ b/movement/watch_faces/complication/siderial_face.h
@@ -0,0 +1,50 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 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.
+ */
+
+#ifndef SIDERIAL_FACE_H_
+#define SIDERIAL_FACE_H_
+
+#include "movement.h"
+
+typedef struct {
+    int lst;
+    uint32_t ticks;
+    uint32_t ref_ds;
+    uint32_t ref_st;
+} siderial_state_t;
+
+void siderial_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
+void siderial_face_activate(movement_settings_t *settings, void *context);
+bool siderial_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
+void siderial_face_resign(movement_settings_t *settings, void *context);
+
+#define siderial_face ((const watch_face_t){ \
+    siderial_face_setup, \
+    siderial_face_activate, \
+    siderial_face_loop, \
+    siderial_face_resign, \
+    NULL, \
+})
+
+#endif // SIDERIAL_FACE_H_