Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <iostream>
00029 #include <sstream>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string>
00033 #include <getopt.h>
00034 #include <ncursesw/ncurses.h>
00035
00036 #include "ros/ros.h"
00037 #include "iwaki/AtomMsg.h"
00038
00039 using std::string;
00040 using std::iostream;
00041
00042
00046 #define KB_ENTER int('\n')
00047 #define KEY_CONTROL_SMALL_C 3
00048
00049 void publishBufferAtom(string &kb_buffer, ros::Publisher &atom_pub) {
00050
00051 iwaki::AtomMsg newAtomMsg;
00052
00053 iwaki::VarSlot v1, v2, v3, v4, v5;
00054
00055 v1.name = "type";
00056 v1.val = "im";
00057 v1.relation = "equal";
00058 v1.type = "string";
00059 v1.unique_mask = true;
00060
00061 v2.name = "subtype";
00062 v2.val = "user";
00063 v2.relation = "equal";
00064 v2.type = "string";
00065 v2.unique_mask = true;
00066
00067 v3.name = "uu_unhandled";
00068 v3.val = "true";
00069 v3.relation = "equal";
00070 v3.type = "string";
00071 v3.unique_mask = false;
00072
00073 v4.name = "uu_string";
00074 v4.val = kb_buffer;
00075 v4.relation = "equal";
00076 v4.type = "string";
00077 v4.unique_mask = false;
00078
00079 v5.name = "id";
00080 v5.val = "1";
00081 v5.relation = "equal";
00082 v5.type = "string";
00083 v5.unique_mask = false;
00084
00085
00086 newAtomMsg.varslots.push_back(v1);
00087 newAtomMsg.varslots.push_back(v2);
00088 newAtomMsg.varslots.push_back(v3);
00089 newAtomMsg.varslots.push_back(v4);
00090 newAtomMsg.varslots.push_back(v5);
00091
00092 atom_pub.publish(newAtomMsg);
00093 }
00094
00095
00096 void renderKbLines(std::list<string> &kb_lines) {
00097 int nrow, ncol, dy;
00098 clear();
00099 getmaxyx(stdscr, nrow, ncol);
00100 dy = nrow - 1;
00101
00102 move(0,0);
00103
00104 std::list<string>::iterator msg_it = kb_lines.begin();
00105 while (( msg_it != kb_lines.end() ) && (dy >= 0)) {
00106
00107 move(0 + dy, 0);
00108 printw( msg_it->c_str() );
00109 ++msg_it;
00110 --dy;
00111 }
00112 refresh();
00113 }
00114
00115 void updateKbBuffer(string &kb_buffer, int ch, ros::Publisher &atom_pub) {
00116 char buffer[8];
00117 sprintf (buffer, "%c", ch);
00118 if ((ch >= 32) && (ch <= 126)) {
00119
00120 kb_buffer += string(buffer);
00121 } else if (ch == KEY_BACKSPACE) {
00122
00123 if (!kb_buffer.empty()) {
00124 kb_buffer = kb_buffer.substr(0, kb_buffer.size() - 1);
00125 }
00126 } else if (ch == KEY_CONTROL_SMALL_C) {
00127 endwin();
00128 ros::shutdown();
00129 } else if (ch == KB_ENTER) {
00130 publishBufferAtom(kb_buffer, atom_pub);
00131 kb_buffer = "";
00132 }
00133 }
00134
00135 void updateKbLines(std::list<string> &kb_lines, int &ch, ros::Publisher &atom_pub) {
00136 char buffer[8];
00137 string kb_buffer = *kb_lines.begin();
00138
00139 sprintf (buffer, "%c", ch);
00140 if ((ch >= 32) && (ch <= 126)) {
00141
00142 kb_buffer += string(buffer);
00143
00144 kb_lines.pop_front();
00145 kb_lines.push_front(kb_buffer);
00146 } else if (ch == KEY_BACKSPACE) {
00147
00148 if (!kb_buffer.empty()) {
00149 kb_buffer = kb_buffer.substr(0, kb_buffer.size() - 1);
00150
00151 kb_lines.pop_front();
00152 kb_lines.push_front(kb_buffer);
00153 }
00154 } else if (ch == KEY_CONTROL_SMALL_C) {
00155 endwin();
00156 ros::shutdown();
00157 } else if (ch == KB_ENTER) {
00158 publishBufferAtom(kb_buffer, atom_pub);
00159 kb_buffer = "";
00160 kb_lines.push_front(kb_buffer);
00161 }
00162
00163 }
00164
00168 int main(int argc, char **argv) {
00169 int ch;
00170 std::list<string> kb_lines;
00171
00172
00173 kb_lines.push_back("");
00174
00175
00176 ros::init(argc, argv, "console");
00177
00178
00179 setlocale(LC_CTYPE,"");
00180 initscr();
00181 raw();
00182 keypad(stdscr, TRUE);
00183 noecho();
00184 timeout(0);
00185
00186
00187
00188
00189 ros::NodeHandle n;
00190 ros::Publisher atom_pub = n.advertise<iwaki::AtomMsg>("IwakiInputAtom", 1000);
00191
00192
00193
00194
00195
00196 ros::Rate loop_rate(10);
00197 while (ros::ok()) {
00198 ch = getch();
00199 updateKbLines(kb_lines, ch, atom_pub);
00200 renderKbLines(kb_lines);
00201 loop_rate.sleep();
00202 }
00203 }