summaryrefslogtreecommitdiffstats
path: root/movement/watch_faces/complication/rpn_calculator_face.c
blob: 9271a1015e2f7bafd23d3d4f59d78c50edcea640 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * MIT License
 *
 * Copyright (c) 2022 Niclas Hoyer
 *
 * 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 <stdlib.h>
#include <string.h>
#include <math.h>
#include "rpn_calculator_face.h"

static void draw_number(char *buf, float num) {
    float f = fmodf(num, 1.0) * 100;
    sprintf(buf, "CA  %4d%02d", ((int)num) % 10000, (int) f);
}

static void draw_op(char *buf, rpn_calculator_op_t op) {
    switch (op) {
        case rpn_calculator_op_add:
            sprintf(buf, "CA     Add");
        break;
        case rpn_calculator_op_sub:
            sprintf(buf, "CA     sub");
        break;
        case rpn_calculator_op_mul:
            sprintf(buf, "CA    n&ul");
        break;
        case rpn_calculator_op_div:
            sprintf(buf, "CA     div");
        break;
        case rpn_calculator_op_pow:
            sprintf(buf, "CA     pow");
        break;
        case rpn_calculator_op_sqrt:
            sprintf(buf, "CA    sqrt");
        break;
        case rpn_calculator_op_pi:
            sprintf(buf, "CA      pi");
            break;
        default:
            break;
    }
}

static void printf_stack(rpn_calculator_state_t *state) {
    printf("Stack: [%f, %f, %f, %f], top: %d\n",
        state->stack[0],
        state->stack[1],
        state->stack[2],
        state->stack[3],
        state->top
    );
}

static void next_op(rpn_calculator_state_t *state) {
    state->op += 1;
    state->op = state->op % RPN_CALCULATOR_MAX_OPS;
}

// increase a digit of a floating point number
// FIXME: this converts the number to string and back, there might
// be better ways to do this
static float inc_digit(float num, uint8_t position) {
    char buf[8];
    if (position > 5) {
        return 0.0;
    }
    // inverse position (rtl)
    position = 5 - position;
    float f = fmodf(num, 1.0) * 100;
    sprintf(buf, "%04d%02d", ((int)num) % 10000, (int) f);
    uint8_t digit = buf[position] - '0';
    digit = (digit + 1) % 10;
    buf[position] = digit + '0';
    printf("buf: %s\n", buf);
    return strtof(buf, NULL) / 100.0;
}

static void stack_push(rpn_calculator_state_t *state, float f) {
    printf_stack(state);
    printf("push: %f\n", f);
    state->top++;
    if (state->top >= RPN_CALCULATOR_STACK_SIZE) {
        // FIXME: implement this using a circular buffer?
        for (int i=0; i<RPN_CALCULATOR_STACK_SIZE-1; i++) {
            state->stack[i] = state->stack[i+1];
        }
    }
    state->stack[state->top] = f;
}

static float stack_peek(rpn_calculator_state_t *state) {
    if (state->top > -1) {
        return state->stack[state->top];
    }
    return 0;
}

static float stack_pop(rpn_calculator_state_t *state) {
    printf_stack(state);
    float f = stack_peek(state);
    state->stack[state->top] = 0;
    printf("pop: %f\n", f);
    if (state->top > -1) {
        state->top--;
    } else {
        state->top = -1; // empty
    }
    return f;
}

static void run_op(rpn_calculator_state_t *state) {
    printf_stack(state);
    bool op_found = false;
    // ops without parameters
    switch (state->op)  {
        case rpn_calculator_op_pi:
            stack_push(state, M_PI);
            op_found = true;
            break;
        default:
            break;
    }
    if (op_found) {
        state->mode = rpn_calculator_waiting;
        return;
    }

    // ops with one parameter
    if (state->top < 0) {
        state->mode = rpn_calculator_err;
        return;
    }
    float right = stack_pop(state);
    printf("right: %f\n", right);
    switch (state->op)  {
        case rpn_calculator_op_sqrt:
            stack_push(state, sqrt(right));
            op_found = true;
            break;
        default:
            break;
    }
    if (op_found) {
        state->mode = rpn_calculator_waiting;
        return;
    }

    // ops with two parameters
    if (state->top < 0) {
        // no parameter left -> error
        state->mode = rpn_calculator_err;
        return;
    }
    float left = stack_pop(state);
    printf("left: %f\n", left);
    switch (state->op)  {
        case rpn_calculator_op_add:
            stack_push(state, left + right);
            op_found = true;
            break;
        case rpn_calculator_op_sub:
            stack_push(state, left - right);
            op_found = true;
            break;
        case rpn_calculator_op_mul:
            stack_push(state, left * right);
            op_found = true;
            break;
        case rpn_calculator_op_div:
            stack_push(state, left / right);
            op_found = true;
            break;
        case rpn_calculator_op_pow:
            stack_push(state, powf(left, right));
            op_found = true;
            break;
        default:
            break;
    }
    if (op_found) {
        state->mode = rpn_calculator_waiting;
        return;
    }
    state->mode = rpn_calculator_err;
}

static void draw(rpn_calculator_state_t *state, uint8_t subsecond) {
    char buf[16];
    switch (state->mode) {
        case rpn_calculator_err:
            sprintf(buf, "CA   err  ");
            break;
        case rpn_calculator_number:
            draw_number(buf, stack_peek(state));
            uint8_t i = 4 + (5 - state->selection);
            if (buf[i] == ' ') {
                buf[i] = '0';
            }
            if (subsecond % 2) {
                buf[i] = ' ';
            }
            break;
        case rpn_calculator_waiting:
            printf_stack(state);
            draw_number(buf, stack_peek(state));
            break;
        case rpn_calculator_op:
            draw_op(buf, state->op);
            break;
        default:
            break;
    }
    watch_display_string(buf, 0);
}

void rpn_calculator_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(rpn_calculator_state_t));
        memset(*context_ptr, 0, sizeof(rpn_calculator_state_t));
        // Do any one-time tasks in here; the inside of this conditional happens only at boot.
        rpn_calculator_state_t *state = *context_ptr;
        state->top = -1;
    }
    // Do any pin or peripheral setup here; this will be called whenever the watch wakes from deep sleep.
}

void rpn_calculator_face_activate(movement_settings_t *settings, void *context) {
    (void) settings;
    (void) context;

    // Handle any tasks related to your watch face coming on screen.
}

bool rpn_calculator_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
    (void) settings;

    rpn_calculator_state_t *state = (rpn_calculator_state_t *)context;

    switch (event.event_type) {
        case EVENT_ACTIVATE:
            draw(state, event.subsecond);
            break;
        case EVENT_TICK:
            if (state->mode == rpn_calculator_number) {
                draw(state, event.subsecond);
            }
            break;
        case EVENT_MODE_BUTTON_UP:
            switch (state->mode) {
                case rpn_calculator_number:
                    state->mode = rpn_calculator_waiting;
                    draw(state, event.subsecond);
                    movement_request_tick_frequency(1);
                    break;
                default:
                    state->mode = rpn_calculator_waiting;
                    movement_request_tick_frequency(1);
                    movement_move_to_next_face();
                break;
            }
            break;
        case EVENT_LIGHT_BUTTON_UP:
            switch (state->mode) {
                case rpn_calculator_waiting:
                    state->mode = rpn_calculator_op;
                    draw(state, event.subsecond);
                    break;
                case rpn_calculator_number:
                    state->selection = (state->selection + 1) % 6;
                    draw(state, event.subsecond);
                    break;
                case rpn_calculator_op:
                    next_op(state);
                    draw(state, event.subsecond);
                    break;
                default:
                    movement_illuminate_led();
                    break;
            }
            break;
        case EVENT_ALARM_BUTTON_UP:
            switch (state->mode) {
                case rpn_calculator_waiting:
                    state->mode = rpn_calculator_number;
                    state->selection = 2;
                    stack_push(state, 0);
                    draw(state, event.subsecond);
                    movement_request_tick_frequency(4);
                    break;
                case rpn_calculator_number:
                    state->stack[state->top] = inc_digit(state->stack[state->top], state->selection);
                    printf_stack(state);
                    draw(state, event.subsecond);
                    break;
                case rpn_calculator_err:
                    state->mode = rpn_calculator_waiting;
                    draw(state, event.subsecond);
                    break;
                case rpn_calculator_op:
                    run_op(state);
                    draw(state, event.subsecond);
                    break;
                default:
                    break;
            }
            break;
        case EVENT_TIMEOUT:
            state->mode = rpn_calculator_waiting;
            movement_request_tick_frequency(1);
            movement_move_to_face(0);
            break;
        case EVENT_LOW_ENERGY_UPDATE:
            break;
        default:
            break;
    }

    // return true if the watch can enter standby mode. If you are PWM'ing an LED or buzzing the buzzer here,
    // you should return false since the PWM driver does not operate in standby mode.
    return true;
}

void rpn_calculator_face_resign(movement_settings_t *settings, void *context) {
    (void) settings;
    (void) context;

    // handle any cleanup before your watch face goes off-screen.
}