From 199eef7681b0208528deb894633ecd811ec65e51 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 21 May 2020 17:57:24 +0100 Subject: [PATCH] Fix saving on Pebble. Break funcs into '...' menu Bump to 0.2 --- appinfo.json | 2 +- src/calc.c | 48 ++++++++++++++++++++++++++++++++++++++---------- src/calc.h | 11 ++++++++++- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/appinfo.json b/appinfo.json index 2a8e1b8..6529956 100644 --- a/appinfo.json +++ b/appinfo.json @@ -25,7 +25,7 @@ "basalt" ], "uuid": "70ec170a-8e1b-11ea-bc55-0242ac130003", - "versionLabel": "0.1", + "versionLabel": "0.2", "watchapp": { "watchface": false } diff --git a/src/calc.c b/src/calc.c index bfa560a..0038de6 100644 --- a/src/calc.c +++ b/src/calc.c @@ -3,11 +3,17 @@ #include "calc.h" static inline int8_t tokenCount() { - return sizeof(tokens) / sizeof(tokens[0]); + if(!using_func_tokens) + return sizeof(tokens) / sizeof(tokens[0]); + + return sizeof(func_tokens) / sizeof(func_tokens[0]); } static inline char* getToken(int8_t n) { - return tokens[n % tokenCount()]; + if(!using_func_tokens) + return tokens[n % tokenCount()]; + + return func_tokens[n % tokenCount()]; } // Currently selected button, starts on '(' @@ -48,14 +54,24 @@ static void up_down_handler(ClickRecognizerRef recognizer, void *context){ updateText(); } +static void backspace() +{ + int8_t i = 0; + while(mytext[++i] != '\0') { ; } + mytext[i-1] = '\0'; + updateText(); +} + // Backspace if possible, otherwise close the app static void back_handler(ClickRecognizerRef recognizer, void *context) { - if(mytext[0] == '\0') { - window_stack_remove(window_stack_get_top_window(), true); + if(!using_func_tokens) { + if(mytext[0] == '\0') { + window_stack_remove(window_stack_get_top_window(), true); + } else { + backspace(); + } } else { - int8_t i = 0; - while(mytext[++i] != '\0') { ; } - mytext[i-1] = '\0'; + using_func_tokens = 0; updateText(); } } @@ -74,19 +90,29 @@ static void calculate(){ stringObj(temp, &obj); snprintf(resulttext, RESULT_LENGTH, "R:%s", temp); - selected_token = 0; text_layer_set_text(s_result_text_layer, resulttext); } // Button press handler static void select_handler(ClickRecognizerRef recognizer, void *context){ - if(selected_token == sizeof(tokens) / sizeof(tokens[0]) - 1) + if(!using_func_tokens && selected_token == tokenCount() - 1) { calculate(); - else + } else if(!using_func_tokens && selected_token == tokenCount() - 2) { + using_func_tokens = 1; + selected_token = 0; + updateText(); + } else { enter(); + } } static void long_select_handler(ClickRecognizerRef recognizer, void *context){ + int8_t i = 0; + while(temptext[++i] != '\0') { ; } + for(unsigned j = 0; j < strlen(getToken(selected_token)); j++) { + temptext[i-(1 + j)] = '\0'; + } + persist_write_string(current_code, temptext); window_stack_pop(true); } @@ -240,6 +266,7 @@ void custom_unload(Window *window) Object add_window(Object obj1, Object obj2, struct Environment *env) { + printf("ADD_WINDOW\n"); if(obj1.type == TYPE_STRING) { strcpy(header, obj1.string); } @@ -261,6 +288,7 @@ static struct Environment pebbleEnv() { struct Environment e = defaultEnv(); // Needs two args addFunc("window", &add_window, &e); + parseEval("(def w (fn (a) (window a 1)))", &e); return e; } diff --git a/src/calc.h b/src/calc.h index 4df83ab..91db198 100644 --- a/src/calc.h +++ b/src/calc.h @@ -48,12 +48,21 @@ char *tokens[] = { "7","8","9", "0", "a", "b", "c", "d", "e", "= ", "< ", "> ", - "spent ", "window", "\"", "cat", "map", "fn", "def", "if", "\n", + "...", END_PHRASE }; +char *func_tokens[] = { + "spent ", "window ", + "max ", "min ", + "sq ", "cube ", "exp " + +}; + +int using_func_tokens = 0; + void code_window_load(Window *window); void code_window_unload(Window *window);