Skip to content

Commit b774900

Browse files
committed
finally squashed the bug
1 parent 12b1432 commit b774900

File tree

1 file changed

+61
-34
lines changed

1 file changed

+61
-34
lines changed

movement/watch_faces/complication/simple_calculator_face.c

+61-34
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@ void simple_calculator_face_setup(movement_settings_t *settings, uint8_t watch_f
3636
}
3737
}
3838

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+
3949
void simple_calculator_face_activate(movement_settings_t *settings, void *context) {
4050
(void) settings;
4151
simple_calculator_state_t *state = (simple_calculator_state_t *)context;
4252
state->placeholder = PLACEHOLDER_ONES;
4353
state->mode = MODE_ENTERING_FIRST_NUM;
54+
reset_to_zero(&state->second_num);
55+
reset_to_zero(&state->result);
4456
movement_request_tick_frequency(4);
4557
}
4658

@@ -71,12 +83,18 @@ static float convert_to_float(calculator_number_t number) {
7183

7284
// Round to nearest hundredth
7385
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+
7491
return result;
7592
}
7693

7794
static char* update_display_number(calculator_number_t *number, char *display_string, uint8_t which_num) {
7895
char sign = ' ';
7996
if (number->negative) sign = '-';
97+
8098
sprintf(display_string, "CA%d%c%d%d%d%d%d%d",
8199
which_num,
82100
sign,
@@ -115,23 +133,24 @@ static void set_operation(simple_calculator_state_t *state) {
115133

116134
static void cycle_operation(simple_calculator_state_t *state) {
117135
state->operation = (state->operation + 1) % OPERATIONS_COUNT; // Assuming there are 6 operations
118-
//printf("Current operation: %d\n", state->operation); // For debugging
119136
}
120137

121138

122139
static calculator_number_t convert_to_string(float number) {
123140
calculator_number_t result;
124141

125142
// Handle negative numbers
126-
bool is_negative = (number < 0);
127-
if (is_negative) {
143+
if (number < 0) {
128144
number = -number;
129145
result.negative = true;
130-
}
146+
} else result.negative = false;
131147

148+
// Get each digit from each placeholder
132149
int int_part = (int)number;
150+
133151
float decimal_part_float = ((number - int_part) * 100); // two decimal places
134152
//printf("decimal_part_float = %f\n", decimal_part_float); //For debugging
153+
135154
int decimal_part = round(decimal_part_float);
136155
//printf("decimal_part = %d\n", decimal_part); //For debugging
137156

@@ -146,22 +165,15 @@ static calculator_number_t convert_to_string(float number) {
146165
return result;
147166
}
148167

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?
159170
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
160173
uint8_t display_index;
161-
// Update display string with current number
174+
175+
// Update display string with current number and copy into temp string
162176
update_display_number(number, display_string, which_num);
163-
164-
// Copy the updated display string to a temporary buffer
165177
strcpy(temp_display_string, display_string);
166178

167179
// Determine the display index based on the placeholder
@@ -179,14 +191,13 @@ static void set_number(calculator_number_t *number, calculator_placeholder_t pla
179191
}
180192

181193
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
184199
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
187200
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
190201

191202
// Perform the calculation based on the selected operation
192203
switch (state->operation) {
@@ -216,36 +227,45 @@ static void view_results(simple_calculator_state_t *state, char *display_string)
216227
}
217228
break;
218229
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);
220231
break;
221232
default:
222233
result_float = 0.0f;
223234
break;
224235
}
225236

237+
// Be sure the result can fit on the watch display, else error
226238
if (result_float > 9999.99 || result_float < -9999.99) {
227239
state->mode = MODE_ERROR;
228240
return;
229241
}
230242

231243
result_float = roundf(result_float * 100.0f) / 100.0f; // Might not be needed
244+
232245
//printf("result as float = %f\n", result_float); // For debugging
233246

234247
// 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
235250
state->result = convert_to_string(result_float);
236251

237252
// Update the display with the result
238253
update_display_number(&state->result, display_string, 3);
254+
255+
//printf("display_string = %s\n", display_string); // For debugging
256+
239257
watch_display_string(display_string, 0);
240258
}
241259

260+
// Used both when returning from errors and when long pressing MODE
242261
static void reset_all(simple_calculator_state_t *state) {
243262
reset_to_zero(&state->first_num);
244263
reset_to_zero(&state->second_num);
245264
state->mode = MODE_ENTERING_FIRST_NUM;
246265
state->operation = OP_ADD;
247266
state->placeholder = PLACEHOLDER_ONES;
248267
}
268+
249269
bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
250270
simple_calculator_state_t *state = (simple_calculator_state_t *)context;
251271
char display_string[10];
@@ -258,6 +278,7 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
258278
case EVENT_TICK:
259279
switch (state->mode) {
260280
case MODE_ENTERING_FIRST_NUM:
281+
// See the WISH for this function above
261282
set_number(&state->first_num,
262283
state->placeholder,
263284
display_string,
@@ -274,8 +295,8 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
274295
// If doing a square root calculation, skip to results
275296
if (state->operation == OP_ROOT) {
276297
state->mode = MODE_VIEW_RESULTS;
277-
// otherwise, set the second number
278298
} else {
299+
// See the WISH for this function above
279300
set_number(&state->second_num,
280301
state->placeholder,
281302
display_string,
@@ -288,6 +309,7 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
288309
case MODE_VIEW_RESULTS:
289310
view_results(state, display_string);
290311
break;
312+
291313
case MODE_ERROR:
292314
watch_display_string("CA Error ", 0);
293315
break;
@@ -340,16 +362,21 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
340362
// Increment the digit in the current placeholder
341363
increment_placeholder(&state->first_num, state->placeholder);
342364
update_display_number(&state->first_num, display_string, 1);
365+
366+
//printf("display_string = %s\n", display_string); // For debugging
367+
343368
break;
344369
case MODE_CHOOSING:
345370
// Confirm and select the current operation
346-
//printf("Selected operation: %d\n", state->operation); // For debugging
347371
state->mode = MODE_ENTERING_SECOND_NUM;
348372
break;
349373
case MODE_ENTERING_SECOND_NUM:
350374
// Increment the digit in the current placeholder
351375
increment_placeholder(&state->second_num, state->placeholder);
352376
update_display_number(&state->second_num, display_string, 2);
377+
378+
//printf("display_string = %s\n", display_string); // For debugging
379+
353380
break;
354381
case MODE_ERROR:
355382
reset_all(state);
@@ -391,30 +418,30 @@ bool simple_calculator_face_loop(movement_event_t event, movement_settings_t *se
391418
state->first_num.thousands == 0) {
392419
movement_move_to_next_face();
393420
} else {
421+
// Reset the placeholder and proceed to the next MODE
394422
state->placeholder = PLACEHOLDER_ONES;
395423
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
396427
if (state->mode == MODE_ENTERING_FIRST_NUM) {
397428
state->first_num = state->result;
398429
reset_to_zero(&state->second_num);
430+
reset_to_zero(&state->result);
399431
}
400-
//printf("Current mode: %d\n", state->mode); // For debugging
401432
}
402433
break;
403434

404435
case EVENT_MODE_LONG_PRESS:
436+
// Move to next face if first number is 0
405437
if (state->first_num.hundredths == 0 &&
406438
state->first_num.tenths == 0 &&
407439
state->first_num.ones== 0 &&
408440
state->first_num.tens == 0 &&
409441
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) {
417443
movement_move_to_face(0);
444+
// otherwise, start over
418445
} else {
419446
reset_all(state);
420447
}

0 commit comments

Comments
 (0)