librfn
An ad-hoc utility library
consoledemo.c
Go to the documentation of this file.
1 /*
2  * consoledemo.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <assert.h>
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include "librfn.h"
19 
20 /*
21  * console_add is not protothreaded. It can simply return PT_EXITED.
22  */
23 static pt_state_t console_add(console_t *c)
24 {
25  if (c->argc != 3)
26  fprintf(c->out, "Usage: add <x> <y>\n");
27  else
28  fprintf(c->out, "%ld\n", strtol(c->argv[1], NULL, 0) +
29  strtol(c->argv[2], NULL, 0));
30 
31  return PT_EXITED;
32 }
33 static const console_cmd_t cmd_add =
34  CONSOLE_CMD_VAR_INIT("add", console_add);
35 
36 
37 /*
38  * console_udelay is a busy-wait (scheduler cannot go idle) that yields to other
39  * protothreads of fibres.
40  */
41 static pt_state_t console_udelay(console_t *c)
42 {
43  PT_BEGIN(&c->pt);
44 
45  if (c->argc != 2) {
46  fprintf(c->out, "Usage: udelay <time>\n");
47  PT_EXIT();
48  }
49 
50  /* parse arguments to local variables and copy to scratch when argument
51  * parsing is complete.
52  */
53  uint32_t delay = strtol(c->argv[1], NULL, 0);
54  c->scratch.u32[0] = time_now();
55  c->scratch.u32[1] = c->scratch.u32[0] + delay;
56 
57  while (cyclecmp32(time_now(), c->scratch.u32[1]) < 0)
58  PT_YIELD();
59 
60  fprintf(c->out, "udelay completed after %uus\n",
61  time_now() - c->scratch.u32[0]);
62 
63  PT_END();
64 }
65 static const console_cmd_t cmd_udelay =
66  CONSOLE_CMD_VAR_INIT("udelay", console_udelay);
67 
68 /*
69  * console_usleep is a fully fledged sleeping wait based upon the librfn fibre
70  * scheduler.
71  */
72 static pt_state_t console_usleep(console_t *c)
73 {
74  PT_BEGIN(&c->pt);
75 
76  if (c->argc != 2) {
77  fprintf(c->out, "Usage: usleep <time>\n");
78  PT_EXIT();
79  }
80 
81  /* parse arguments to local variables and copy to scratch when argument
82  * parsing is complete.
83  */
84  uint32_t delay = strtol(c->argv[1], NULL, 0);
85  c->scratch.u32[0] = time_now();
86  c->scratch.u32[1] = c->scratch.u32[0] + delay;
87 
89 
90  fprintf(c->out, "usleep completed after %uus\n",
91  time_now() - c->scratch.u32[0]);
92 
93  PT_END();
94 }
95 static const console_cmd_t cmd_usleep =
96  CONSOLE_CMD_VAR_INIT("usleep", console_usleep);
97 
98 int main(int argc, char *argv[])
99 {
101 
102  console_init(&console, stdout);
103  console_register(&cmd_usleep);
104  console_register(&cmd_add);
105  console_register(&cmd_udelay);
106 
108 
109  return 0;
110 }
struct charlie c
#define PT_EXIT()
Definition: protothreads.h:132
Console descriptor.
Definition: console.h:66
uint32_t u32[SCRATCH_SIZE/4]
Definition: console.h:85
#define PT_WAIT_UNTIL(c)
Definition: protothreads.h:116
void fibre_scheduler_main_loop(void)
Definition: fibre_default.c:17
#define CONSOLE_CMD_VAR_INIT(n, f)
Definition: console.h:52
#define PT_BEGIN(pt)
Definition: protothreads.h:93
#define PT_YIELD()
Definition: protothreads.h:124
#define PT_END()
Definition: protothreads.h:100
int argc
Definition: console.h:90
int32_t cyclecmp32(uint32_t a, uint32_t b)
Compares values that may be subject to overflow.
Definition: util.c:19
int main(int argc, char *argv[])
Definition: consoledemo.c:98
union console::@0 scratch
Console command descriptor.
Definition: console.h:47
FILE * out
Definition: console.h:69
char * argv[4]
Definition: console.h:91
int console_register(const console_cmd_t *cmd)
Register a new command.
Definition: console.c:151
uint32_t time_now(void)
pt_state_t
Definition: protothreads.h:80
pt_t pt
Definition: console.h:94
bool fibre_timeout(uint32_t duetime)
Definition: fibre.c:208
void console_init(console_t *c, FILE *f)
Initialized the console handler.
Definition: console.c:136