diff --git a/app/app.cpp b/app/app.cpp index 2651db497..46b459262 100644 --- a/app/app.cpp +++ b/app/app.cpp @@ -5,35 +5,11 @@ extern "C" { } #include +#include "../poincare/src/layout/string_layout.h" #include "utils.h" -void draw_lines_from_center() { - KDCoordinate width = SCREEN_WIDTH; - KDCoordinate height = SCREEN_HEIGHT; - - KDColor c = 0xFF; - - KDPoint center = KDPointMake(width/2, height/2); - int step = 2; - - for (KDCoordinate x=0; xcreateLayout(); + user_expression.simplified = user_expression.expression->simplify(); + user_expression.simplified_layout = user_expression.simplified->createLayout(); + } else { + user_expression.expression_layout = + new StringLayout(kParsingErrorMessage, strlen(kParsingErrorMessage)); + } + return user_expression; +} + +static int16_t print_user_input(user_expression_t user_expression, int16_t yOffset) { + if (user_expression.expression_layout) { + int16_t height = user_expression.expression_layout->size().height; + if (yOffset + height < SCREEN_HEIGHT) { + user_expression.expression_layout->draw(KDPointMake(0, yOffset)); + } + yOffset += height; + } + if (user_expression.simplified_layout) { + int16_t height = user_expression.simplified_layout->size().height; + if (yOffset + height < SCREEN_HEIGHT) { + int16_t xOffset = SCREEN_WIDTH - user_expression.simplified_layout->size().width; + user_expression.simplified_layout->draw(KDPointMake(xOffset, yOffset)); + } + yOffset += height; + } + return yOffset; } static void interactive_expression_parsing() { + UserExpressions user_inputs; while (1) { - char * text_input = get_text(); - clear_screen(); - Expression * e = Expression::parse(text_input); - if (e) { - ExpressionLayout * l = e->createLayout(); - int16_t yOffset = 10; - if (l) { - l->draw(KDPointMake(0, yOffset)); - yOffset += l->size().height; - delete l; - } - Expression * simplified = e->simplify(); - // Print the simplification. - if (simplified) { - ExpressionLayout * simplified_l = simplified->createLayout(); - if (simplified_l) { - int16_t xOffset = SCREEN_WIDTH - simplified_l->size().width; - simplified_l->draw(KDPointMake(xOffset, yOffset)); - delete simplified_l; - } - if (simplified != e) { - delete simplified; - } - } - delete e; - } else { - KDDrawString("PARSING ERROR", KDPointMake(10,10), 0); + text_event_t text_event = get_text(nullptr); + if (text_event.event == EQUAL) { + user_inputs.append_expression(create_user_input(text_event.text)); + } + int16_t yOffset = 0; + for (int i=0; iSCREEN_HEIGHT) { + break; + } } - // We dealocate the memory allocated by get_text; - free(text_input); } } diff --git a/app/utils.cpp b/app/utils.cpp index 01efea1d3..79bfcdf39 100644 --- a/app/utils.cpp +++ b/app/utils.cpp @@ -1,10 +1,12 @@ extern "C" { +#include #include #include #include -#include } +#include "utils.h" + #define PROMPT_HEIGHT 30 void clear_screen() { @@ -105,18 +107,27 @@ static int get_trig_input(char* input) { } } -char* get_text() { +text_event_t get_text(char* txt) { char input[255] = {0}; int index = 0; int max = 0; input[max] = ' '; input[max+1] = '\0'; + text_event_t text_event = {nullptr, ERROR}; + + if (txt != nullptr) { + assert(false); + } while (1) { clear_prompt(); print_prompt(input, index); ion_event_t event = ion_get_event(); if (event == EQUAL) { + input[max] = '\0'; + text_event.event = EQUAL; + text_event.text = (char*) malloc(sizeof(char) * (index + 1)); + memcpy(text_event.text, input, (size_t) (index + 1)); break; } else if (event == LEFT_ARROW) { index--; @@ -158,8 +169,5 @@ char* get_text() { } clear_prompt(); - input[max] = '\0'; - char* output = (char*) malloc(sizeof(char) * (index + 1)); - memcpy(output, input, (size_t) (index + 1)); - return output; + return text_event; } diff --git a/app/utils.h b/app/utils.h index ca78fcae1..bc48f0b8d 100644 --- a/app/utils.h +++ b/app/utils.h @@ -1,9 +1,21 @@ #ifndef APP_UTILS_H #define APP_UTILS_H -/* Returns a pointer to an input text, allocated by the functions (it is thus - * the caller's role to free it). */ -char* get_text(); +extern "C" { +#include +} + +typedef struct { + char* text; + ion_event_t event; +} text_event_t; + +/* Returns a pointer to an input text allocated by the functions. + * Also returns an event, which is its return reason. + * + * This function can get a text to work on instead of starting from an empty + * string. */ +text_event_t get_text(char* txt); void clear_screen();