+
+/**
+ * \brief ouputs a zero-terminated string from ram to the console
+ */
+void cli_putstr(char* s){
+ if(!cli_tx)
+ return;
+ while(*s)
+ cli_tx(*s++);
+}
+
+
+/**
+ * \brief ouputs a zero-terminated string from flash to the console
+ */
+void cli_putstr_P(PGM_P s){
+ char c;
+ if(!cli_tx)
+ return;
+ for(;;){
+ c = pgm_read_byte(s++);
+ if(!c)
+ return;
+ cli_tx(c);
+ }
+}
+
+/**
+ * \brief reads a line or max n characters from the console
+ * Writes characters from the console into the supplyed buffer until a '\r'
+ * character is recieved or until n character a read (whatever happens first).
+ * The string will always be terminated by a '\0' character, so the buffer
+ * should have at least a size of n+1.
+ */
+uint8_t cli_getsn(char* s, uint16_t n){
+ char c;
+ if(n==0)
+ return 2;
+ while((c=cli_getc_cecho())!='\0' && c!='\r' && n--){
+ *s++=c;
+ }
+ *s='\0';
+ return (c=='\r')?0:1;
+}
+
+/**
+ * \brief dumps the contents of a buffer to the console
+ * Dumps length bytes from data to the console ouput. The dump
+ * will have 2*n continous hexadecimal characters.
+ */
+void cli_hexdump(void* data, uint16_t length){
+ if(!cli_tx)
+ return;
+ while(length--){
+ cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4)));
+ cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf)));
+ data = (uint8_t*)data +1;
+ }
+}
+
+/**
+ * \brief dumps the contents of a buffer to the console
+ * This function behaves like cli_hexdump except that the
+ * bytes are dumped in reverse order. This is usefull to dump
+ * integers which ar e in little endian order.
+ */
+void cli_hexdump_rev(void* data, uint16_t length){
+ if(!cli_tx)
+ return;
+ data = (uint8_t*)data + length -1;
+ while(length--){
+ cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4)));
+ cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf)));
+ data = (uint8_t*)data -1;
+ }
+}
+
+/**
+ * \brief dumps the contents of a buffer to the console
+ * Like cli_hexdump but bytes are seperated with a single space
+ * on the console output.
+ */
+void cli_hexdump2(void* data, uint16_t length){
+ if(!cli_tx)
+ return;
+ while(length--){
+ cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))>>4)));
+ cli_tx(pgm_read_byte(hexdigit_tab_P +((*((uint8_t*)data))&0xf)));
+ cli_tx(' ');
+ data = (uint8_t*)data +1;
+ }
+}
+
+
+static
+void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){
+ cmdlist_entry_t item;
+ uint16_t i;
+ if(!cli_tx)
+ return;
+
+ cli_putstr_P(PSTR("\r\n[auto help] available commands:\r\n"
+ " <command> - <params> - <address>\r\n"));
+ for(;;){
+ item.cmd_name = (void*)pgm_read_word(cmdlist+0);
+ item.cmd_param_str = (void*)pgm_read_word(cmdlist+2);
+ item.cmd_function = (void_fpt)pgm_read_word(cmdlist+4);
+ cmdlist = (uint8_t*)cmdlist+CMDLIST_ENTRY_SIZE;
+ if(item.cmd_name==NULL){
+ return;
+ }
+ cli_tx(' ');
+ cli_putstr_P(item.cmd_name);
+ i=maxcmdlength-strlen_P(item.cmd_name);
+ while(i--)
+ cli_tx(' ');
+ cli_putstr_P(PSTR(" - "));
+ if(item.cmd_param_str==NULL){
+ cli_putstr_P(PSTR("none \t- 0x"));
+ } else {
+ if(item.cmd_param_str==(void*)1){
+ cli_putstr_P(PSTR("yes \t- 0x"));
+ } else {
+ cli_putstr_P(item.cmd_param_str);
+ cli_putstr_P(PSTR(" \t- 0x"));
+ }