From 5112af887ae59c9c54eabaec5c1ae2f5a53de180 Mon Sep 17 00:00:00 2001 From: M1K Date: Tue, 1 May 2018 18:39:46 +0100 Subject: [PATCH] Added command history to terminal with other bug fixes, added new song to song_list.h (#2855) * Implemented Terminal + added song to song_list.h * Added wait() in order to prevent misbehaviour of 'command not found' --- docs/feature_terminal.md | 31 ++++- quantum/audio/song_list.h | 6 + quantum/process_keycode/process_terminal.c | 125 ++++++++++++++++++--- 3 files changed, 146 insertions(+), 16 deletions(-) diff --git a/docs/feature_terminal.md b/docs/feature_terminal.md index 334a46c2d4..1863599f84 100644 --- a/docs/feature_terminal.md +++ b/docs/feature_terminal.md @@ -14,12 +14,14 @@ When enabled, a `> ` prompt will appear, where you'll be able to type, backspace `#define TERMINAL_HELP` enables some other output helpers that aren't really needed with this page. +Pressing "up" and "down" will allow you to cycle through the past 5 commands entered. + ## Future Ideas * Keyboard/user-extensible commands * Smaller footprint * Arrow key support -* Command history +* Command history - Done * SD card support * LCD support for buffer display * Keycode -> name string LUT @@ -43,14 +45,39 @@ QMK Firmware Built: 2017-08-29-20:24:44 ``` + +### `print-buffer` + +Outputs the last 5 commands entered + +``` +> print-buffer +0. print-buffer +1. help +2. about +3. keymap 0 +4. help +5. flush-buffer +``` + +### `flush-buffer` + +Clears command buffer +``` +> flush-buffer +Buffer cleared! +``` + + ### `help` + Prints out the available commands: ``` > help commands available: - about help keycode keymap exit + about help keycode keymap exit print-buffer flush-buffer ``` ### `keycode ` diff --git a/quantum/audio/song_list.h b/quantum/audio/song_list.h index a66c4d8644..1ddcfb345c 100644 --- a/quantum/audio/song_list.h +++ b/quantum/audio/song_list.h @@ -20,6 +20,12 @@ #define NO_SOUND +#define LP_NUMB \ + H__NOTE(_CS5), H__NOTE(_E5), H__NOTE(_CS5), WD_NOTE(_FS5), \ + WD_NOTE(_A5), WD_NOTE(_GS5), WD_NOTE(_REST), H__NOTE(_CS5), H__NOTE(_E5), \ + H__NOTE(_CS5), WD_NOTE(_A5), WD_NOTE(_GS5), WD_NOTE(_E5), + + #define ODE_TO_JOY \ Q__NOTE(_E4), Q__NOTE(_E4), Q__NOTE(_F4), Q__NOTE(_G4), \ Q__NOTE(_G4), Q__NOTE(_F4), Q__NOTE(_E4), Q__NOTE(_D4), \ diff --git a/quantum/process_keycode/process_terminal.c b/quantum/process_keycode/process_terminal.c index deb1543e3d..bc365dddfa 100644 --- a/quantum/process_keycode/process_terminal.c +++ b/quantum/process_keycode/process_terminal.c @@ -20,10 +20,20 @@ #include #include +#ifndef CMD_BUFF_SIZE + #define CMD_BUFF_SIZE 5 +#endif + + bool terminal_enabled = false; char buffer[80] = ""; +char cmd_buffer[CMD_BUFF_SIZE][80]; +bool cmd_buffer_enabled = true; //replace with ifdef? char newline[2] = "\n"; char arguments[6][20]; +bool firstTime = true; + +short int current_cmd_buffer_pos = 0; //used for up/down arrows - keeps track of where you are in the command buffer __attribute__ ((weak)) const char terminal_prompt[8] = "> "; @@ -34,36 +44,37 @@ const char terminal_prompt[8] = "> "; #endif float terminal_song[][2] = TERMINAL_SONG; #define TERMINAL_BELL() PLAY_SONG(terminal_song) -#else - #define TERMINAL_BELL() +#else + #define TERMINAL_BELL() #endif __attribute__ ((weak)) const char keycode_to_ascii_lut[58] = { 0, 0, 0, 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0, 0, 0, '\t', ' ', '-', '=', '[', ']', '\\', 0, ';', '\'', '`', ',', '.', '/' -}; +}; __attribute__ ((weak)) const char shifted_keycode_to_ascii_lut[58] = { 0, 0, 0, 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 0, 0, 0, '\t', ' ', '_', '+', '{', '}', '|', 0, ':', '\'', '~', '<', '>', '?' -}; +}; -struct stringcase { - char* string; - void (*func)(void); +struct stringcase { + char* string; + void (*func)(void); } typedef stringcase; void enable_terminal(void) { terminal_enabled = true; strcpy(buffer, ""); + memset(cmd_buffer,0,CMD_BUFF_SIZE * 80); for (int i = 0; i < 6; i++) strcpy(arguments[i], ""); // select all text to start over @@ -73,6 +84,29 @@ void enable_terminal(void) { void disable_terminal(void) { terminal_enabled = false; + SEND_STRING("\n"); +} + +void push_to_cmd_buffer(void) { +if (cmd_buffer_enabled) { + if (cmd_buffer == NULL) { + return; + } else { + if (firstTime) { + firstTime = false; + strcpy(cmd_buffer[0],buffer); + return; + } + + for (int i= CMD_BUFF_SIZE - 1;i > 0 ;--i) { + strncpy(cmd_buffer[i],cmd_buffer[i-1],80); + } + + strcpy(cmd_buffer[0],buffer); + + return; + } + } } void terminal_about(void) { @@ -136,11 +170,34 @@ void terminal_keymap(void) { } } -stringcase terminal_cases[] = { +void print_cmd_buff(void) { + /* without the below wait, a race condition can occur wherein the + buffer can be printed before it has been fully moved */ + wait_ms(250); + for(int i=0;istring, buffer ) ) { @@ -192,6 +250,16 @@ void process_terminal_command(void) { send_string(terminal_prompt); } } +void check_pos(void) { + if (current_cmd_buffer_pos >= CMD_BUFF_SIZE) { //if over the top, move it back down to the top of the buffer so you can climb back down... + current_cmd_buffer_pos = CMD_BUFF_SIZE - 1; + } else if (current_cmd_buffer_pos < 0) { //...and if you fall under the bottom of the buffer, reset back to 0 so you can climb back up + current_cmd_buffer_pos = 0; + } +} + + + bool process_terminal(uint16_t keycode, keyrecord_t *record) { @@ -210,6 +278,8 @@ bool process_terminal(uint16_t keycode, keyrecord_t *record) { char char_to_add; switch (keycode) { case KC_ENTER: + push_to_cmd_buffer(); + current_cmd_buffer_pos = 0; process_terminal_command(); return false; break; case KC_ESC: @@ -226,9 +296,36 @@ bool process_terminal(uint16_t keycode, keyrecord_t *record) { return false; } break; case KC_LEFT: + return false; break; case KC_RIGHT: - case KC_UP: + return false; break; + case KC_UP: // 0 = recent + check_pos(); //check our current buffer position is valid + if (current_cmd_buffer_pos <= CMD_BUFF_SIZE - 1) { //once we get to the top, dont do anything + str_len = strlen(buffer); + for(int i= 0;i < str_len ;++i) { + send_string(SS_TAP(X_BSPACE)); //clear w/e is on the line already + //process_terminal(KC_BSPC,record); + } + strncpy(buffer,cmd_buffer[current_cmd_buffer_pos],80); + + send_string(buffer); + ++current_cmd_buffer_pos; //get ready to access the above cmd if up/down is pressed again + } + return false; break; case KC_DOWN: + check_pos(); + if (current_cmd_buffer_pos >= 0) { //once we get to the bottom, dont do anything + str_len = strlen(buffer); + for(int i= 0;i < str_len ;++i) { + send_string(SS_TAP(X_BSPACE)); //clear w/e is on the line already + //process_terminal(KC_BSPC,record); + } + strncpy(buffer,cmd_buffer[current_cmd_buffer_pos],79); + + send_string(buffer); + --current_cmd_buffer_pos; //get ready to access the above cmd if down/up is pressed again + } return false; break; default: if (keycode <= 58) { @@ -240,7 +337,7 @@ bool process_terminal(uint16_t keycode, keyrecord_t *record) { } if (char_to_add != 0) { strncat(buffer, &char_to_add, 1); - } + } } break; } @@ -249,4 +346,4 @@ bool process_terminal(uint16_t keycode, keyrecord_t *record) { } } return true; -} \ No newline at end of file +}