Skip to content

Commit

Permalink
Merge branch 'xprintf_migrate'
Browse files Browse the repository at this point in the history
  • Loading branch information
Helius committed Mar 19, 2012
2 parents aad6f9e + 17fbf92 commit b1c5fe2
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TARGET=microrl_test
all: microrl_test


microrl_test: example.o ../src/microrl.o unix_misc/unix_misc.o
microrl_test: example.o ../src/microrl.o unix_misc/unix_misc.o
$(CC) $^ -o $@ $(LDFLAGS)

%.o: %.c
Expand Down
9 changes: 9 additions & 0 deletions examples/avr_misc/avr_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ char * set_clear_key [] = {_SCMD_PB, _SCMD_PD};
char * compl_world [_NUM_OF_CMD + 1];


void put_char (unsigned char ch);


//*****************************************************************************
void init (void)
Expand All @@ -40,6 +42,13 @@ void init (void)
DDRD=0xFF;
}

////*****************************************************************************
//void put_char (unsigned char ch)
//{
// while (!( UCSRA & (1<<UDRE)));
// UDR = (unsigned char) ch;
//}

//*****************************************************************************
void print (const char * str)
{
Expand Down
1 change: 0 additions & 1 deletion examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ microrl_t * prl = &rl;
int main (void/*int argc, char ** argv*/)
{
init ();

// call init with ptr to microrl instance and print callback
microrl_init (prl, print);
// set callback for execute
Expand Down
3 changes: 2 additions & 1 deletion examples/unix_misc/unix_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

//*****************************************************************************
//dummy function, no need on linux-PC
void init (void){};
void init (void){
};

//*****************************************************************************
// print callback for microrl library
Expand Down
13 changes: 12 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ Enable Handling terminal ESC sequence. If disabling, then cursor arrow, HOME, EN
use Ctrl+A(B,F,P,N,A,E,H,K,U,C) see README, but decrease code memory.*/
#define _USE_ESC_SEQ

/*
Use snprintf from you standard complier library, but it gives some overhead.
If not defined, use my own u16int_to_str variant, it's save about 800 byte of code size
on AVR (avr-gcc build).
Try to build with and without, and compare total code size for tune library.
*/
#define _USE_LIBC_STDIO

/*
Enable 'interrupt signal' callback, if user press Ctrl+C */
#define _USE_CTLR_C
Expand All @@ -68,7 +76,10 @@ Print prompt at 'microrl_init', if enable, prompt will print at startup,
otherwise first prompt will print after first press Enter in terminal
NOTE!: Enable it, if you call 'microrl_init' after your communication subsystem
already initialize and ready to print message */
//#define _ENABLE_INIT_PROMPT
#undef _ENABLE_INIT_PROMPT




/********** END CONFIG SECTION ************/

Expand Down
50 changes: 47 additions & 3 deletions src/microrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ BUGS and TODO:
-- rewrite history for use more than 256 byte buffer
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "microrl.h"
#ifdef _USE_LIBC_STDIO
#include <stdio.h>
#endif

//#define DBG(...) fprintf(stderr, "\033[33m");fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\033[0m");

Expand Down Expand Up @@ -221,25 +223,67 @@ inline static void terminal_newline (microrl_t * this)
this->print ("\n\r");
}

#ifndef _USE_LIBC_STDIO
//*****************************************************************************
// convert 16 bit value to string
// 0 value not supported!!! just make empty string
static void u16bit_to_str (unsigned int nmb, char * buf)
{
char tmp_str [6] = {0,};
int i = 0;
if (nmb <= 0xFFFF) {
while (nmb > 0) {
tmp_str[i++] = (nmb % 10) + '0';
nmb /=10;
}
for (int j = 0; j < i; ++j)
*(buf++) = tmp_str [i-j-1];
}
*buf = '\0';
}
#endif


//*****************************************************************************
// set cursor at position from begin cmdline (after prompt) + offset
static void terminal_move_cursor (microrl_t * this, int offset)
{
char str[16] = {0,};
#ifdef _USE_LIBC_STDIO
if (offset > 0) {
snprintf (str, 16, "\033[%dC", offset);
} else if (offset < 0) {
snprintf (str, 16, "\033[%dD", -(offset));
}
#else
strcpy (str, "\033[");
if (offset > 0) {
u16bit_to_str (offset, str+2);
strcat (str, "C");
} else if (offset < 0) {
u16bit_to_str (-(offset), str+2);
strcat (str, "D");
} else
return;
#endif
this->print (str);
}

//*****************************************************************************
static void terminal_reset_cursor (microrl_t * this)
{
char str[16];
snprintf (str, 16, "\033[%dD\033[%dC", _COMMAND_LINE_LEN + _PROMPT_LEN + 2,
_PROMPT_LEN);
#ifdef _USE_LIBC_STDIO
snprintf (str, 16, "\033[%dD\033[%dC", \
_COMMAND_LINE_LEN + _PROMPT_LEN + 2, _PROMPT_LEN);

#else
strcpy (str, "\033[");
u16bit_to_str ( _COMMAND_LINE_LEN + _PROMPT_LEN + 2,str+2);
strcat (str, "D\033[");
u16bit_to_str (_PROMPT_LEN, str+strlen(str));
strcat (str, "C");
#endif
this->print (str);
}

Expand Down

0 comments on commit b1c5fe2

Please sign in to comment.