From 418d081f6e106fba5ed05fddcd13bf4f93eab2de Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Fri, 25 Mar 2016 17:21:50 +0100 Subject: [PATCH] Add the trig menu. Change-Id: Ifbe2518cb3a089dace82d56031987fbc7a2e6181 --- app/utils.cpp | 91 +++++++++++++++++++++++++- ion/include/ion/events.h | 4 +- ion/src/shared/events.c | 4 +- ion/src/simulator/keyboard/fltkkbd.cpp | 4 +- kandinsky/include/kandinsky/text.h | 2 +- kandinsky/src/text.c | 2 +- 6 files changed, 99 insertions(+), 8 deletions(-) diff --git a/app/utils.cpp b/app/utils.cpp index 62ef83a00..52afdd5e1 100644 --- a/app/utils.cpp +++ b/app/utils.cpp @@ -34,6 +34,85 @@ static void print_prompt(char* text, int index) { KDDrawChar(text[index], KDPointMake(index * font_size.width, SCREEN_HEIGHT - (PROMPT_HEIGHT / 2)), true); } +static void clear_trig_menu() { + { + KDRect r; + r.x = SCREEN_WIDTH / 4 - 1; + r.y = SCREEN_HEIGHT / 4 - 1; + r.width = SCREEN_WIDTH / 2 + 2; + r.height = SCREEN_HEIGHT / 2 + 2; + KDFillRect(r, 0x00); + } +} + +static void print_trig_menu() { + { + KDRect r; + r.x = SCREEN_WIDTH / 4 - 1; + r.y = SCREEN_HEIGHT / 4 - 1; + r.width = SCREEN_WIDTH / 2 + 2; + r.height = SCREEN_HEIGHT / 2 + 2; + KDFillRect(r, 0xFF); + } + { + KDRect r; + r.x = SCREEN_WIDTH / 4; + r.y = SCREEN_HEIGHT / 4; + r.width = SCREEN_WIDTH / 2; + r.height = SCREEN_HEIGHT / 2; + KDFillRect(r, 0x00); + } +} + +static int get_trig_input(char* input) { + int pos = 0; + const char* kTexts[] = { + "sine", + "cosine", + "tangent", + "cancel" + }; + const char* kOutputs[] = { + "sin( )", + "cos( )", + "tan( )", + }; + const KDPoint orig = KDPointMake(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4); + while (true) { + print_trig_menu(); + + uint16_t vert_acc = 0; + for (int i(0); i<4; i++) { + KDSize text_size = KDStringSize(kTexts[i]); + KDDrawString(kTexts[i], + KDPointMake(orig.x, orig.y + vert_acc + text_size.height / 2), + i==pos); + vert_acc += text_size.height; + } + + ion_event_t event = ion_get_event(); + if (event == UP_ARROW) { + pos--; + if (pos < 0) { + pos = 0; + } + } else if (event == DOWN_ARROW) { + pos++; + if (pos >= 4) { + pos = 3; + } + } else if (event == '=') { + clear_trig_menu(); + if (pos == 3) { + return 0; + } else { + memcpy(input, kOutputs[pos], (size_t) strlen(kOutputs[pos])); + return strlen(kOutputs[pos]); + } + } + } +} + char* get_text() { char input[255] = {0}; int index = 0; @@ -60,10 +139,20 @@ char* get_text() { input[index++] = (char) event; // We are at the end of the line. if (index > max) { - max++; + max=index; input[max] = ' '; input[max+1] = '\0'; } + } else if (event == TRIG_MENU) { + int tmp = get_trig_input(&input[index]); + index+=tmp; + if (index > max) { + max=index; + input[max] = ' '; + input[max+1] = '\0'; + } + // we want to be inside the parenthese if there are some. + index -= (tmp > 2) ? 2 : 0; } } diff --git a/ion/include/ion/events.h b/ion/include/ion/events.h index 3758b742c..31a4b5545 100644 --- a/ion/include/ion/events.h +++ b/ion/include/ion/events.h @@ -75,7 +75,9 @@ typedef enum { LOWER_CASE_Z, LEFT_ARROW = 0x100, // events for things outside of ASCII. RIGHT_ARROW, - MENU_TRIG, + UP_ARROW, + DOWN_ARROW, + TRIG_MENU, ERROR = 0xffffffff, } ion_event_t; diff --git a/ion/src/shared/events.c b/ion/src/shared/events.c index b9c18c27f..4c088ea25 100644 --- a/ion/src/shared/events.c +++ b/ion/src/shared/events.c @@ -49,8 +49,8 @@ static ion_key_event_t ion_get_key_event() { // For now this is a bit silly but needed for instreface purpose. static const ion_event_t kEventForKeyDown[ION_NUMBER_OF_KEYS] = { UPPER_CASE_A, UPPER_CASE_B, UPPER_CASE_C, UPPER_CASE_D, UPPER_CASE_E, - UPPER_CASE_F, UPPER_CASE_G, UPPER_CASE_H, UPPER_CASE_I, UPPER_CASE_J, - UPPER_CASE_K, UPPER_CASE_L, UPPER_CASE_M, LEFT_ARROW, RIGHT_ARROW, + UPPER_CASE_F, UPPER_CASE_G, UPPER_CASE_H, UP_ARROW, DOWN_ARROW, + UPPER_CASE_K, UPPER_CASE_L, TRIG_MENU, LEFT_ARROW, RIGHT_ARROW, SEVEN, EIGHT, NINE, LEFT_PARENTHESIS, RIGHT_PARENTHESIS, FOUR, FIVE, SIX, PRODUCT, DIVISION, ONE, TWO, THREE, PLUS, MINUS, diff --git a/ion/src/simulator/keyboard/fltkkbd.cpp b/ion/src/simulator/keyboard/fltkkbd.cpp index e2148f555..1e8430718 100644 --- a/ion/src/simulator/keyboard/fltkkbd.cpp +++ b/ion/src/simulator/keyboard/fltkkbd.cpp @@ -6,8 +6,8 @@ static const char* kCharForKey[KEYBOARD_ROWS * KEYBOARD_COLUMNS] = { "A", "B", "C", "D", "E", - "F", "G", "H", "I", "J", - "K", "L", "M", "LEFT", "RIGHT", + "F", "G", "H", "UP", "DOWN", + "K", "L", "TRIG", "LEFT", "RIGHT", "7", "8", "9", "(", ")", "4", "5", "6", "*", "/", "1", "2", "3", "+", "-", diff --git a/kandinsky/include/kandinsky/text.h b/kandinsky/include/kandinsky/text.h index 955fc0755..6284ca35b 100644 --- a/kandinsky/include/kandinsky/text.h +++ b/kandinsky/include/kandinsky/text.h @@ -5,6 +5,6 @@ void KDDrawChar(char character, KDPoint p, uint8_t inverse); void KDDrawString(const char * text, KDPoint p, uint8_t inverse); -KDSize KDStringSize(char * text); +KDSize KDStringSize(const char * text); #endif diff --git a/kandinsky/src/text.c b/kandinsky/src/text.c index 9520b0890..f00934b61 100644 --- a/kandinsky/src/text.c +++ b/kandinsky/src/text.c @@ -23,7 +23,7 @@ void KDDrawString(const char * text, KDPoint p, uint8_t inverse) { } } -KDSize KDStringSize(char * text) { +KDSize KDStringSize(const char * text) { return (KDSize){ .width = strlen(text)*BITMAP_FONT_CHARACTER_WIDTH, .height = BITMAP_FONT_CHARACTER_HEIGHT