@@ -36,11 +36,23 @@ void simple_calculator_face_setup(movement_settings_t *settings, uint8_t watch_f
36
36
}
37
37
}
38
38
39
+ static void reset_to_zero (calculator_number_t * number ) {
40
+ number -> negative = false;
41
+ number -> hundredths = 0 ;
42
+ number -> tenths = 0 ;
43
+ number -> ones = 0 ;
44
+ number -> tens = 0 ;
45
+ number -> hundreds = 0 ;
46
+ number -> thousands = 0 ;
47
+ }
48
+
39
49
void simple_calculator_face_activate (movement_settings_t * settings , void * context ) {
40
50
(void ) settings ;
41
51
simple_calculator_state_t * state = (simple_calculator_state_t * )context ;
42
52
state -> placeholder = PLACEHOLDER_ONES ;
43
53
state -> mode = MODE_ENTERING_FIRST_NUM ;
54
+ reset_to_zero (& state -> second_num );
55
+ reset_to_zero (& state -> result );
44
56
movement_request_tick_frequency (4 );
45
57
}
46
58
@@ -71,12 +83,18 @@ static float convert_to_float(calculator_number_t number) {
71
83
72
84
// Round to nearest hundredth
73
85
result = roundf (result * 100 ) / 100 ;
86
+
87
+ // Handle negative numbers
88
+ if (number .negative ) result = - result ;
89
+ //printf("convert_to_float results = %f\n", result); // For debugging
90
+
74
91
return result ;
75
92
}
76
93
77
94
static char * update_display_number (calculator_number_t * number , char * display_string , uint8_t which_num ) {
78
95
char sign = ' ' ;
79
96
if (number -> negative ) sign = '-' ;
97
+
80
98
sprintf (display_string , "CA%d%c%d%d%d%d%d%d" ,
81
99
which_num ,
82
100
sign ,
@@ -115,23 +133,24 @@ static void set_operation(simple_calculator_state_t *state) {
115
133
116
134
static void cycle_operation (simple_calculator_state_t * state ) {
117
135
state -> operation = (state -> operation + 1 ) % OPERATIONS_COUNT ; // Assuming there are 6 operations
118
- //printf("Current operation: %d\n", state->operation); // For debugging
119
136
}
120
137
121
138
122
139
static calculator_number_t convert_to_string (float number ) {
123
140
calculator_number_t result ;
124
141
125
142
// Handle negative numbers
126
- bool is_negative = (number < 0 );
127
- if (is_negative ) {
143
+ if (number < 0 ) {
128
144
number = - number ;
129
145
result .negative = true;
130
- }
146
+ } else result . negative = false;
131
147
148
+ // Get each digit from each placeholder
132
149
int int_part = (int )number ;
150
+
133
151
float decimal_part_float = ((number - int_part ) * 100 ); // two decimal places
134
152
//printf("decimal_part_float = %f\n", decimal_part_float); //For debugging
153
+
135
154
int decimal_part = round (decimal_part_float );
136
155
//printf("decimal_part = %d\n", decimal_part); //For debugging
137
156
@@ -146,22 +165,15 @@ static calculator_number_t convert_to_string(float number) {
146
165
return result ;
147
166
}
148
167
149
- static void reset_to_zero (calculator_number_t * number ) {
150
- number -> negative = false;
151
- number -> hundredths = 0 ;
152
- number -> tenths = 0 ;
153
- number -> ones = 0 ;
154
- number -> tens = 0 ;
155
- number -> hundreds = 0 ;
156
- number -> thousands = 0 ;
157
- }
158
-
168
+ // This is the main function for setting the first_num and second_num
169
+ // WISH: there must be a way to pass less to this function?
159
170
static void set_number (calculator_number_t * number , calculator_placeholder_t placeholder , char * display_string , char * temp_display_string , movement_event_t event , uint8_t which_num ) {
171
+
172
+ // Create the display index
160
173
uint8_t display_index ;
161
- // Update display string with current number
174
+
175
+ // Update display string with current number and copy into temp string
162
176
update_display_number (number , display_string , which_num );
163
-
164
- // Copy the updated display string to a temporary buffer
165
177
strcpy (temp_display_string , display_string );
166
178
167
179
// Determine the display index based on the placeholder
@@ -179,14 +191,13 @@ static void set_number(calculator_number_t *number, calculator_placeholder_t pla
179
191
}
180
192
181
193
static void view_results (simple_calculator_state_t * state , char * display_string ) {
182
- float first_num_float , second_num_float , result_float = 0.0f ; // For arithmetic operations
183
- // Convert the numbers to float
194
+
195
+ // Initialize float variables to do the math
196
+ float first_num_float , second_num_float , result_float = 0.0f ;
197
+
198
+ // Convert the passed numbers to floats
184
199
first_num_float = convert_to_float (state -> first_num );
185
- if (state -> first_num .negative ) first_num_float = first_num_float * -1 ;
186
- //printf("first_num_float = %f\n", first_num_float); // For debugging // For debugging
187
200
second_num_float = convert_to_float (state -> second_num );
188
- if (state -> second_num .negative ) second_num_float = second_num_float * -1 ;
189
- //printf("second_num_float = %f\n", second_num_float); // For debugging
190
201
191
202
// Perform the calculation based on the selected operation
192
203
switch (state -> operation ) {
@@ -216,36 +227,45 @@ static void view_results(simple_calculator_state_t *state, char *display_string)
216
227
}
217
228
break ;
218
229
case OP_POWER :
219
- result_float = powf (first_num_float , second_num_float ); // Power operation
230
+ result_float = powf (first_num_float , second_num_float );
220
231
break ;
221
232
default :
222
233
result_float = 0.0f ;
223
234
break ;
224
235
}
225
236
237
+ // Be sure the result can fit on the watch display, else error
226
238
if (result_float > 9999.99 || result_float < -9999.99 ) {
227
239
state -> mode = MODE_ERROR ;
228
240
return ;
229
241
}
230
242
231
243
result_float = roundf (result_float * 100.0f ) / 100.0f ; // Might not be needed
244
+
232
245
//printf("result as float = %f\n", result_float); // For debugging
233
246
234
247
// Convert the float result to a string
248
+ // This isn't strictly necessary, but allows easily reusing the result as
249
+ // the next calculation's first_num
235
250
state -> result = convert_to_string (result_float );
236
251
237
252
// Update the display with the result
238
253
update_display_number (& state -> result , display_string , 3 );
254
+
255
+ //printf("display_string = %s\n", display_string); // For debugging
256
+
239
257
watch_display_string (display_string , 0 );
240
258
}
241
259
260
+ // Used both when returning from errors and when long pressing MODE
242
261
static void reset_all (simple_calculator_state_t * state ) {
243
262
reset_to_zero (& state -> first_num );
244
263
reset_to_zero (& state -> second_num );
245
264
state -> mode = MODE_ENTERING_FIRST_NUM ;
246
265
state -> operation = OP_ADD ;
247
266
state -> placeholder = PLACEHOLDER_ONES ;
248
267
}
268
+
249
269
bool simple_calculator_face_loop (movement_event_t event , movement_settings_t * settings , void * context ) {
250
270
simple_calculator_state_t * state = (simple_calculator_state_t * )context ;
251
271
char display_string [10 ];
@@ -258,6 +278,7 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
258
278
case EVENT_TICK :
259
279
switch (state -> mode ) {
260
280
case MODE_ENTERING_FIRST_NUM :
281
+ // See the WISH for this function above
261
282
set_number (& state -> first_num ,
262
283
state -> placeholder ,
263
284
display_string ,
@@ -274,8 +295,8 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
274
295
// If doing a square root calculation, skip to results
275
296
if (state -> operation == OP_ROOT ) {
276
297
state -> mode = MODE_VIEW_RESULTS ;
277
- // otherwise, set the second number
278
298
} else {
299
+ // See the WISH for this function above
279
300
set_number (& state -> second_num ,
280
301
state -> placeholder ,
281
302
display_string ,
@@ -288,6 +309,7 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
288
309
case MODE_VIEW_RESULTS :
289
310
view_results (state , display_string );
290
311
break ;
312
+
291
313
case MODE_ERROR :
292
314
watch_display_string ("CA Error " , 0 );
293
315
break ;
@@ -340,16 +362,21 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
340
362
// Increment the digit in the current placeholder
341
363
increment_placeholder (& state -> first_num , state -> placeholder );
342
364
update_display_number (& state -> first_num , display_string , 1 );
365
+
366
+ //printf("display_string = %s\n", display_string); // For debugging
367
+
343
368
break ;
344
369
case MODE_CHOOSING :
345
370
// Confirm and select the current operation
346
- //printf("Selected operation: %d\n", state->operation); // For debugging
347
371
state -> mode = MODE_ENTERING_SECOND_NUM ;
348
372
break ;
349
373
case MODE_ENTERING_SECOND_NUM :
350
374
// Increment the digit in the current placeholder
351
375
increment_placeholder (& state -> second_num , state -> placeholder );
352
376
update_display_number (& state -> second_num , display_string , 2 );
377
+
378
+ //printf("display_string = %s\n", display_string); // For debugging
379
+
353
380
break ;
354
381
case MODE_ERROR :
355
382
reset_all (state );
@@ -391,30 +418,30 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
391
418
state -> first_num .thousands == 0 ) {
392
419
movement_move_to_next_face ();
393
420
} else {
421
+ // Reset the placeholder and proceed to the next MODE
394
422
state -> placeholder = PLACEHOLDER_ONES ;
395
423
state -> mode = (state -> mode + 1 ) % 4 ;
424
+ // When looping back to MODE_ENTERING_FIRST_NUM, reuse the
425
+ // previous calculation's results as the next calculation's
426
+ // first_num; also reset other numbers
396
427
if (state -> mode == MODE_ENTERING_FIRST_NUM ) {
397
428
state -> first_num = state -> result ;
398
429
reset_to_zero (& state -> second_num );
430
+ reset_to_zero (& state -> result );
399
431
}
400
- //printf("Current mode: %d\n", state->mode); // For debugging
401
432
}
402
433
break ;
403
434
404
435
case EVENT_MODE_LONG_PRESS :
436
+ // Move to next face if first number is 0
405
437
if (state -> first_num .hundredths == 0 &&
406
438
state -> first_num .tenths == 0 &&
407
439
state -> first_num .ones == 0 &&
408
440
state -> first_num .tens == 0 &&
409
441
state -> first_num .hundreds == 0 &&
410
- state -> first_num .thousands == 0 &&
411
- state -> second_num .hundredths == 0 &&
412
- state -> second_num .tenths == 0 &&
413
- state -> second_num .ones == 0 &&
414
- state -> second_num .tens == 0 &&
415
- state -> second_num .hundreds == 0 &&
416
- state -> second_num .thousands == 0 ) {
442
+ state -> first_num .thousands == 0 ) {
417
443
movement_move_to_face (0 );
444
+ // otherwise, start over
418
445
} else {
419
446
reset_all (state );
420
447
}
0 commit comments