pebblisp/src/calc.c

121 lines
3.7 KiB
C
Raw Normal View History

2016-04-18 04:25:36 -04:00
#include <stdio.h>
#include <limits.h>
#include "fixed.h"
#include "calc.h"
#include "pebblisp.h"
static inline char* getToken(int n) {
const int length = sizeof(tokens) / sizeof(tokens[0]);
return tokens[n % length];
}
static int8_t selected_token = 1; //Currently selected button, starts on '5'
static struct Environment env;
static void updateText()
{
strcpy(temptext, mytext);
2020-05-03 17:00:45 -04:00
2016-04-18 04:25:36 -04:00
if(getToken(selected_token)[0] == ' ') {
strcat(temptext, "_");
} else {
strcat(temptext, getToken(selected_token));
}
text_layer_set_text(s_input_text_layer, temptext);
}
2020-05-03 17:00:45 -04:00
// Button handler
2016-04-18 04:25:36 -04:00
static void up_down_handler(ClickRecognizerRef recognizer, void *context){
2020-05-03 17:00:45 -04:00
// Change current token
2016-04-18 04:25:36 -04:00
selected_token += (click_recognizer_get_button_id(recognizer) == BUTTON_ID_DOWN) ? 1 : -1;
2020-05-03 17:00:45 -04:00
// If selected token is outside of range, wrap around
2016-04-18 04:25:36 -04:00
selected_token = selected_token < 0 ? TOKEN_END : selected_token > TOKEN_END ? 0 : selected_token;
updateText();
}
2020-05-03 21:24:36 -04:00
static void back_handler(ClickRecognizerRef recognizer, void *context) {
if(mytext[0] == '\0') {
window_stack_remove(window_stack_get_top_window(), true);
} else {
int i = 0;
while(mytext[++i] != '\0') { ; }
mytext[i-1] = '\0';
updateText();
}
}
2016-04-18 04:25:36 -04:00
// Adds the current string to the main string
static void enter(){
strcat(mytext, getToken(selected_token));
selected_token = 0;
updateText();
}
//Calculate result, display it and reset
static void calculate(){
2020-05-03 17:00:45 -04:00
Object obj = parseEval(mytext, &env);
2020-05-03 21:24:36 -04:00
snprintf(resulttext, MAX_LENGTH, "R:%d", obj.number);
2020-05-03 17:00:45 -04:00
selected_token = 0;
//strcpy(mytext, HAHA);
2020-05-03 21:24:36 -04:00
text_layer_set_text(s_result_text_layer, resulttext);
2016-04-18 04:25:36 -04:00
}
// Button press handler
static void select_handler(ClickRecognizerRef recognizer, void *context){
if(selected_token == sizeof(tokens) / sizeof(tokens[0]) - 1)
calculate();
else
enter();
}
static void click_config_provider(void *context) {
// Register click handlers
window_single_repeating_click_subscribe(BUTTON_ID_UP, 100, up_down_handler);
window_single_repeating_click_subscribe(BUTTON_ID_DOWN, 100, up_down_handler);
window_single_click_subscribe(BUTTON_ID_SELECT, select_handler);
2020-05-03 21:24:36 -04:00
window_single_click_subscribe(BUTTON_ID_BACK, back_handler);
2016-04-18 04:25:36 -04:00
}
static void init(void) {
// Create a window and get information about the window
s_window = window_create();
2020-05-03 21:24:36 -04:00
// Layer *window_layer = window_get_root_layer(s_window);
2016-04-18 04:25:36 -04:00
// Register click config provider
window_set_click_config_provider(s_window, click_config_provider);
// Input text layer setup
2020-05-03 21:24:36 -04:00
GRect text_bounds = GRect(6, 6, 132, 127);
2016-04-18 04:25:36 -04:00
s_input_text_layer = text_layer_create(text_bounds);
text_layer_set_text(s_input_text_layer, getToken(1));
2020-05-03 21:24:36 -04:00
text_layer_set_font(s_input_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
2016-04-18 04:25:36 -04:00
layer_add_child(window_get_root_layer(s_window), text_layer_get_layer(s_input_text_layer));
2020-05-03 21:24:36 -04:00
// Result text layer setup
GRect result_bounds = GRect(6, 128, 132, 132);
s_result_text_layer = text_layer_create(result_bounds);
text_layer_set_text(s_result_text_layer, "R: ");
text_layer_set_font(s_result_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_text_alignment(s_result_text_layer, GTextAlignmentRight);
layer_add_child(window_get_root_layer(s_window), text_layer_get_layer(s_result_text_layer));
2016-04-18 04:25:36 -04:00
// Push the window, setting the window animation to 'true'
window_stack_push(s_window, true);
env = defaultEnv();
}
static void deinit(void) {
// Destroy the text layer
text_layer_destroy(s_input_text_layer);
// Destroy the window
window_destroy(s_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}