summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Richardson <wfrichar@chromium.org>2014-06-04 18:04:41 -0700
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-06-05 23:14:34 +0000
commitf4b12223e2cbf586f84147432b607129e4845e98 (patch)
treec120130eaf922146826d91d22df91831359bb89a
parent31369a69dbf34cff8d9135f5d503efa717e3da35 (diff)
downloadchrome-ec-f4b12223e2cbf586f84147432b607129e4845e98.tar.gz
Tweaks to lightbar simulation
Improve the get_time() function, add support for GNU readline if desired. BUG=none BRANCH=ToT TEST=manual cd extra make ./lightbar Change-Id: Iedf84253b0e616a6a1b502415a487a2e6248cb2e Signed-off-by: Bill Richardson <wfrichar@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/202698 Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--extra/Makefile8
-rw-r--r--extra/input.c38
-rw-r--r--extra/main.c26
-rw-r--r--extra/simulation.h1
4 files changed, 69 insertions, 4 deletions
diff --git a/extra/Makefile b/extra/Makefile
index 0b6c7f7cf7..e7ef73ec00 100644
--- a/extra/Makefile
+++ b/extra/Makefile
@@ -6,10 +6,18 @@ PROG= lightbar
HEADERS= simulation.h
SRCS= main.c windows.c input.c ../common/lightbar.c
+# comment this out if you don't have libreadline installed
+HAS_GNU_READLINE=1
+
INCLUDE= -I. -I../include
CFLAGS= -g -Wall -Werror -pthread ${INCLUDE} -DLIGHTBAR_SIMULATION
LDFLAGS = -lX11 -lxcb -lrt
+ifneq ($(HAS_GNU_READLINE),)
+CFLAGS += -DHAS_GNU_READLINE
+LDFLAGS += -lreadline
+endif
+
all: ${PROG}
${PROG} : ${SRCS} ${HEADERS} Makefile
diff --git a/extra/input.c b/extra/input.c
index dcb2a98891..fe4f7cd19f 100644
--- a/extra/input.c
+++ b/extra/input.c
@@ -11,6 +11,40 @@
#include "simulation.h"
+#ifdef HAS_GNU_READLINE
+#include <readline/readline.h>
+#include <readline/history.h>
+
+char *get_input(const char *prompt)
+{
+ static char *line;
+
+ if (line) {
+ free(line);
+ line = 0;
+ }
+
+ line = readline(prompt);
+
+ if (line && *line)
+ add_history(line);
+
+ return line;
+}
+
+#else /* no readline */
+
+char *get_input(const char *prompt)
+{
+ static char mybuf[80];
+ char *got;
+ printf("%s", prompt);
+ got = fgets(mybuf, sizeof(mybuf), stdin);
+ return got;
+}
+
+#endif /* HAS_GNU_READLINE */
+
void *entry_input(void *ptr)
{
char *got, buf[80];
@@ -20,9 +54,9 @@ void *entry_input(void *ptr)
int ret;
do {
- printf("lightbar%% ");
- got = fgets(buf, sizeof(buf), stdin);
+ got = get_input("lightbar% ");
if (got) {
+ strcpy(buf, got);
argc = 0;
argv[argc++] = "lightbar";
word = str = buf;
diff --git a/extra/main.c b/extra/main.c
index 9c9e26d08d..6433f49557 100644
--- a/extra/main.c
+++ b/extra/main.c
@@ -5,12 +5,14 @@
*/
#include <assert.h>
#include <errno.h>
+#include <inttypes.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <time.h>
#include "simulation.h"
@@ -28,6 +30,7 @@ int main(int argc, char *argv[])
printf("\nLook at the README file.\n");
printf("Click in the window.\n");
printf("Type \"help\" for commands.\n\n");
+ fflush(stdout);
init_windows();
@@ -77,7 +80,7 @@ uint32_t task_wait_event(int timeout_us)
if (timeout_us > 0) {
clock_gettime(CLOCK_REALTIME, &t);
- timespec_incr(&t, 0, timeout_us * TS_USEC);
+ timespec_incr(&t, timeout_us / SECOND, timeout_us * TS_USEC);
if (ETIMEDOUT == pthread_cond_timedwait(&task_cond,
&task_mutex, &t))
@@ -124,9 +127,28 @@ void cprintf(int zero, const char *fmt, ...)
free(newfmt);
}
+void cprints(int zero, const char *fmt, ...)
+{
+ va_list ap;
+
+ printf("[TT ");
+ va_start(ap, fmt);
+ vprintf(fmt, ap);
+ va_end(ap);
+ printf("]\n");
+}
+
timestamp_t get_time(void)
{
- timestamp_t ret = { .val = 0UL };
+ static struct timespec t_start;
+ struct timespec t;
+ timestamp_t ret;
+
+ if (!t_start.tv_sec)
+ clock_gettime(CLOCK_REALTIME, &t_start);
+ clock_gettime(CLOCK_REALTIME, &t);
+ ret.val = (t.tv_sec - t_start.tv_sec) * SECOND +
+ (t.tv_nsec - t_start.tv_nsec) / TS_USEC;
return ret;
}
diff --git a/extra/simulation.h b/extra/simulation.h
index 7ec55ce0cd..a746ca8549 100644
--- a/extra/simulation.h
+++ b/extra/simulation.h
@@ -47,6 +47,7 @@ int fake_consolecmd_lightbar(int argc, char *argv[]);
/* Non-standard standard library functions */
void cprintf(int zero, const char *fmt, ...);
+void cprints(int zero, const char *fmt, ...);
#define ccprintf(fmt...) cprintf(0, fmt)
#define strtoi strtol