From d8cbdc79fa21a70eaa8c42fb43a9a75d59b70fd3 Mon Sep 17 00:00:00 2001 From: iggy Date: Sat, 30 Jul 2011 19:28:37 +0200 Subject: [PATCH] string input + demo --- firmware/applications/inputtest.c | 29 ++++++++ firmware/basic/Makefile | 1 + firmware/basic/basic.h | 2 + firmware/basic/stringin.c | 107 ++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 firmware/applications/inputtest.c create mode 100644 firmware/basic/stringin.c diff --git a/firmware/applications/inputtest.c b/firmware/applications/inputtest.c new file mode 100644 index 0000000..0512abe --- /dev/null +++ b/firmware/applications/inputtest.c @@ -0,0 +1,29 @@ +#include + +#include "basic/basic.h" +#include "lcd/render.h" +#include "lcd/display.h" +#include "lcd/allfonts.h" + +#define CHARWIDTH 12 +#define CHARSPACE 0x20 + +void main_inputtest(void) { + backlightInit(); + + char nick[20]; + memset(nick,0,20); + strcpy(nick,"iggy"); + + while(1) { + input("Nick?", nick, 0x20, 0x7f, 20); + + lcdFill(0); + font=&Font_Orbitron14pt; + DoString(20,20,nick); + lcdDisplay(); + + while(getInput() != BTN_ENTER){} + } +} + diff --git a/firmware/basic/Makefile b/firmware/basic/Makefile index eb3b501..5f772fd 100644 --- a/firmware/basic/Makefile +++ b/firmware/basic/Makefile @@ -20,6 +20,7 @@ OBJS += random.o OBJS += idle.o OBJS += config.o OBJS += itoa.o +OBJS += stringin.o LIBNAME=basic diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index d218cd6..ec82179 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -153,6 +153,8 @@ uint8_t getInputRaw(void); uint8_t getInputWait(void); void getInputWaitRelease(void); +// stringin.c +void input(char prompt[], char line[], uint8_t asciistart, uint8_t asciiend, uint8_t maxlength); //uuid.c #include "basic/uuid.h" diff --git a/firmware/basic/stringin.c b/firmware/basic/stringin.c new file mode 100644 index 0000000..20e72bb --- /dev/null +++ b/firmware/basic/stringin.c @@ -0,0 +1,107 @@ + +#include "basic/basic.h" +#include "lcd/render.h" +#include "lcd/display.h" +#include "lcd/allfonts.h" + +#define CHARWIDTH 12 +#define CHARSPACE 0x20 + +struct in{ + char *line, *prompt; + uint8_t pos, dcursor, maxlength, asciistart, asciiend; + bool done; +} s_input; + +void inputInit(char p[],char s[], uint8_t l, uint8_t as, uint8_t ae) { + //TODO: Check length! + s_input.prompt = p; + s_input.line = s; + s_input.maxlength = l; + s_input.asciistart = as; + s_input.asciiend = ae; + s_input.pos = 0; + s_input.dcursor = 0; + s_input.done = false; +} + + +void inputMove() { + char *cur = s_input.line+s_input.pos+s_input.dcursor; + char key = getInput(); + if (key == BTN_LEFT) { + if (s_input.dcursor >0) { + s_input.dcursor --; + } else if (s_input.pos > 0) { + s_input.pos --; + } + } else if (key == BTN_RIGHT) { + if (s_input.dcursor s_input.asciiend) { + *cur = s_input.asciiend; + } else { + *cur = *cur - 1; + } + } else if (key == BTN_UP) { + if (*cur >= s_input.asciiend) { + *cur = s_input.asciistart; + } else if (*cur < s_input.asciistart) { + *cur = s_input.asciistart; + } else { + *cur = *cur + 1; + } + } else if (key == (BTN_ENTER)) { + s_input.done = true; + } +} + +void inputDraw() { + lcdFill(0); + DoString(0,0,s_input.prompt); + for (int dx = 0; dx<= RESX/CHARWIDTH && s_input.pos+dx=0) { + s_input.line[x] = 0; + x--; + } + return; + } + } +} + +void input(char prompt[], char line[], uint8_t asciistart, uint8_t asciiend, uint8_t maxlength){ + font=&Font_7x8; + inputInit(prompt, line, maxlength, asciistart, asciiend); + lcdFill(0); + while (!s_input.done) { + lcdDisplay(); + inputMove(); + inputDraw(); + } + inputClean(); + return; +} +