From bbff2a35a9e4b70a8d02751a904bbf491ba0c12a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 4 May 2015 13:59:56 -0700 Subject: 1255 - keyboard support --- cpp/.traces/clear-line-erases-printed-characters2 | 2 +- cpp/.traces/print-character-at-top-left2 | 2 +- cpp/.traces/screen_in_scenario | 2 +- cpp/.traces/screen_in_scenario_error | 2 +- cpp/070display.cc | 30 ++++++++++++++++++++++- cpp/keyboard.mu | 14 +++++++++++ 6 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 cpp/keyboard.mu (limited to 'cpp') diff --git a/cpp/.traces/clear-line-erases-printed-characters2 b/cpp/.traces/clear-line-erases-printed-characters2 index 282bfe35..0c239a4e 100644 --- a/cpp/.traces/clear-line-erases-printed-characters2 +++ b/cpp/.traces/clear-line-erases-printed-characters2 @@ -1691,7 +1691,7 @@ mem/0: location 1147 is 1031 run/0: result 0 is 1031 mem/0: storing 1031 in location 900 run/0: instruction clear-line-erases-printed-characters2/2 -run/0: screen-should-contain/59 {name: " +run/0: screen-should-contain/60 {name: " . . . . . . diff --git a/cpp/.traces/print-character-at-top-left2 b/cpp/.traces/print-character-at-top-left2 index 896cc9c5..d92f9676 100644 --- a/cpp/.traces/print-character-at-top-left2 +++ b/cpp/.traces/print-character-at-top-left2 @@ -524,7 +524,7 @@ mem/0: location 1076 is 1031 run/0: result 0 is 1031 mem/0: storing 1031 in location 900 run/0: instruction print-character-at-top-left2/2 -run/0: screen-should-contain/59 {name: " +run/0: screen-should-contain/60 {name: " .a . . . ", value: 0, type: 0, properties: [" diff --git a/cpp/.traces/screen_in_scenario b/cpp/.traces/screen_in_scenario index 3605e5f0..81511645 100644 --- a/cpp/.traces/screen_in_scenario +++ b/cpp/.traces/screen_in_scenario @@ -870,7 +870,7 @@ mem/0: location 1085 is 1031 run/0: result 0 is 1031 mem/0: storing 1031 in location 900 run/0: instruction screen-in-scenario/2 -run/0: screen-should-contain/59 {name: " +run/0: screen-should-contain/60 {name: " # 01234 .a . . . diff --git a/cpp/.traces/screen_in_scenario_error b/cpp/.traces/screen_in_scenario_error index abcf8d15..f1cf680c 100644 --- a/cpp/.traces/screen_in_scenario_error +++ b/cpp/.traces/screen_in_scenario_error @@ -870,7 +870,7 @@ mem/0: location 1085 is 1031 run/0: result 0 is 1031 mem/0: storing 1031 in location 900 run/0: instruction screen-in-scenario-error/2 -run/0: screen-should-contain/59 {name: " +run/0: screen-should-contain/60 {name: " # 01234 .b . . . diff --git a/cpp/070display.cc b/cpp/070display.cc index b2bb8d06..922b1e4b 100644 --- a/cpp/070display.cc +++ b/cpp/070display.cc @@ -167,7 +167,35 @@ Recipe_number["wait-for-key-from-keyboard"] = WAIT_FOR_KEY_FROM_KEYBOARD; :(before "End Primitive Recipe Implementations") case WAIT_FOR_KEY_FROM_KEYBOARD: { struct tb_event event; - tb_poll_event(&event); + do { + tb_poll_event(&event); + } while (event.type != TB_EVENT_KEY); + vector result; + result.push_back(event.ch); + write_memory(current_instruction().products[0], result); + break; +} + +:(before "End Primitive Recipe Declarations") +READ_KEY_FROM_KEYBOARD, +:(before "End Primitive Recipe Numbers") +Recipe_number["read-key-from-keyboard"] = READ_KEY_FROM_KEYBOARD; +:(before "End Primitive Recipe Implementations") +case READ_KEY_FROM_KEYBOARD: { + struct tb_event event; + int event_type = tb_peek_event(&event, 5/*ms*/); + vector result; + vector found; + if (event_type != TB_EVENT_KEY) { + result.push_back(0); + found.push_back(false); + } + else { + result.push_back(event.ch); + found.push_back(true); + } + write_memory(current_instruction().products[0], result); + write_memory(current_instruction().products[1], found); break; } diff --git a/cpp/keyboard.mu b/cpp/keyboard.mu new file mode 100644 index 00000000..d6f728f6 --- /dev/null +++ b/cpp/keyboard.mu @@ -0,0 +1,14 @@ +# example reading keys from keyboard +# +# Keeps printing 'a' until you press a key. Then prints the key you pressed +# and exits. +recipe main [ + switch-to-display + { + c:character, found?:boolean <- read-key-from-keyboard + break-if found?:boolean + print-character-to-display 97:literal + loop + } + return-to-console +] -- cgit 1.4.1-2-gfad0