164 lines
5.0 KiB
C
164 lines
5.0 KiB
C
|
#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 bool operator_entered = false; //Has the operator been entered yet
|
||
|
// static bool num1_is_ans = false; //Is the previous result in num1? Used to allow further calculations on result.
|
||
|
// static char num1[MAX_LENGTH] = ""; //First operand
|
||
|
// static char num2[MAX_LENGTH] = ""; //Second operand
|
||
|
// static char result_text[MAX_LENGTH] = ""; //Results text layer buffer string
|
||
|
// static uint8_t operator = 0; //Operator, where 0 is +, 1 is -, 2 is *, 3 is /, 4 is ^
|
||
|
static struct Environment env;
|
||
|
|
||
|
static void updateText()
|
||
|
{
|
||
|
strcpy(temptext, mytext);
|
||
|
if(getToken(selected_token)[0] == ' ') {
|
||
|
strcat(temptext, "_");
|
||
|
} else {
|
||
|
strcat(temptext, getToken(selected_token));
|
||
|
}
|
||
|
|
||
|
text_layer_set_text(s_input_text_layer, temptext);
|
||
|
}
|
||
|
|
||
|
//Up or Down button handler
|
||
|
static void up_down_handler(ClickRecognizerRef recognizer, void *context){
|
||
|
//Move selected button down if down is pressed, and up if up is pressed
|
||
|
selected_token += (click_recognizer_get_button_id(recognizer) == BUTTON_ID_DOWN) ? 1 : -1;
|
||
|
//If selected button is outside button range, wrap around
|
||
|
selected_token = selected_token < 0 ? TOKEN_END : selected_token > TOKEN_END ? 0 : selected_token;
|
||
|
|
||
|
updateText();
|
||
|
}
|
||
|
|
||
|
// Adds the current string to the main string
|
||
|
static void enter(){
|
||
|
strcat(mytext, getToken(selected_token));
|
||
|
selected_token = 0;
|
||
|
updateText();
|
||
|
}
|
||
|
|
||
|
// // Backspace. Clears whole number if full is true
|
||
|
// static void clear(bool full){
|
||
|
// char *num = operator_entered ? num2 : num1; //Create a pointer to the currently edited number
|
||
|
// if(full)
|
||
|
// *num = 0;
|
||
|
// else
|
||
|
// num[strlen(num)-1] = 0;
|
||
|
// text_layer_set_text(s_input_text_layer, num);
|
||
|
// }
|
||
|
|
||
|
//Calculate result, display it and reset
|
||
|
static void calculate(){
|
||
|
// Object obj = parseEval(mytext, &env);
|
||
|
// snprintf(mytext, MAX_LENGTH, "R:%d", obj.number);
|
||
|
// selected_token = 0;
|
||
|
strcpy(mytext, "heythere");
|
||
|
updateText();
|
||
|
if(1)
|
||
|
return;
|
||
|
|
||
|
/*
|
||
|
bool overflow = false; //Overflow flag
|
||
|
// Convert operands to numbers
|
||
|
fixed lhs = str_to_fixed(num1, &overflow);
|
||
|
fixed rhs = str_to_fixed(num2, &overflow);
|
||
|
fixed result = 0;
|
||
|
//Calculate the result
|
||
|
switch(operator){
|
||
|
case 0:
|
||
|
result = fixed_add(lhs, rhs, &overflow);
|
||
|
break;
|
||
|
case 1:
|
||
|
result = fixed_subt(lhs, rhs, &overflow);
|
||
|
break;
|
||
|
case 2:
|
||
|
result = fixed_mult(lhs, rhs, &overflow);
|
||
|
break;
|
||
|
case 3:
|
||
|
result = fixed_div(lhs, rhs);
|
||
|
break;
|
||
|
case 4:
|
||
|
result = fixed_pow(lhs, fixed_to_int(rhs), &overflow); //Exponent must be an int
|
||
|
break;
|
||
|
default:
|
||
|
result = 0;
|
||
|
}
|
||
|
|
||
|
APP_LOG(APP_LOG_LEVEL_DEBUG, "num1: %d num2: %d result: %d", lhs, rhs, result);
|
||
|
|
||
|
//Reset operands, operator_entered and entering_decimal
|
||
|
*num1 = 0;
|
||
|
*num2 = 0;
|
||
|
operator_entered = false;
|
||
|
|
||
|
if(overflow){
|
||
|
text_layer_set_text(s_input_text_layer, "Overflow Error"); //Display message on overflow
|
||
|
}
|
||
|
else{
|
||
|
fixed_repr(result, result_text, MAX_LENGTH); //Convert result to string
|
||
|
text_layer_set_text(s_input_text_layer, result_text); //Display result
|
||
|
strcpy(num1, result_text); //Copy result into num1
|
||
|
num1_is_ans = true;
|
||
|
}
|
||
|
*/
|
||
|
}
|
||
|
|
||
|
// 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);
|
||
|
}
|
||
|
|
||
|
static void init(void) {
|
||
|
// Create a window and get information about the window
|
||
|
s_window = window_create();
|
||
|
Layer *window_layer = window_get_root_layer(s_window);
|
||
|
// Register click config provider
|
||
|
window_set_click_config_provider(s_window, click_config_provider);
|
||
|
|
||
|
// Input text layer setup
|
||
|
GRect text_bounds = GRect(6, 6, 132, 132);
|
||
|
s_input_text_layer = text_layer_create(text_bounds);
|
||
|
text_layer_set_text(s_input_text_layer, getToken(1));
|
||
|
text_layer_set_font(s_input_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
|
||
|
layer_add_child(window_get_root_layer(s_window), text_layer_get_layer(s_input_text_layer));
|
||
|
|
||
|
// 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();
|
||
|
}
|