librfn
An ad-hoc utility library
console.h
Go to the documentation of this file.
1 /*
2  * console.h
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 #ifndef RF_CONSOLE_H_
15 #define RF_CONSOLE_H_
16 
17 #include <stdio.h>
18 
19 #include "fibre.h"
20 #include "ringbuf.h"
21 
39 struct console;
40 
47 typedef struct {
48  const char *name;
49  pt_state_t (*fn)(struct console *c);
51 
52 #define CONSOLE_CMD_VAR_INIT(n, f) \
53  { \
54  .name = (n), \
55  .fn = (f) \
56  }
57 
58 #define SCRATCH_SIZE 80
59 
66 typedef struct console {
68 
69  FILE *out;
70 
71  char ringbuf[16];
73 
81  union {
83  uint8_t u8[SCRATCH_SIZE];
84  uint16_t u16[SCRATCH_SIZE/2];
85  uint32_t u32[SCRATCH_SIZE/4];
86  void *p[SCRATCH_SIZE/4];
87  } scratch;
88  char *bufp;
89 
90  int argc;
91  char *argv[4];
92 
95 } console_t;
96 
103 void console_init(console_t *c, FILE *f);
104 
111 void console_hwinit(console_t *c);
112 
118 static inline void console_silent(console_t *c) { c->argc = 1; }
119 
159 int console_register(const console_cmd_t *cmd);
160 
168 void console_putchar(console_t *c, char d);
169 
176 pt_state_t console_eval(pt_t *pt, console_t *c, const char *cmd);
177 
198 
217 
233 void console_process(console_t *c, char d);
234 
238 typedef enum {
244 
251 typedef struct {
253  uintptr_t port;
254  uint32_t pin;
255  int flags;
257 
262 
263 #define CONSOLE_GPIO_VAR_INIT(name, port_, pin_, flags_) \
264  { \
265  .cmd = CONSOLE_CMD_VAR_INIT(name, console_gpio_do_cmd), \
266  .port = port_, .pin = pin_, .flags = flags_ \
267  }
268 
272 int console_gpio_register(const console_gpio_t *gpio);
273 
275 #endif // RF_CONSOLE_H_
struct charlie c
Console descriptor.
Definition: console.h:66
const char * name
Definition: console.h:48
void * p[SCRATCH_SIZE/4]
Definition: console.h:86
void console_hwinit(console_t *c)
Platform dependant function that will be called during console_init().
uint32_t u32[SCRATCH_SIZE/4]
Definition: console.h:85
Ring buffer descriptor.
Definition: ringbuf.h:38
void console_putchar(console_t *c, char d)
Asynchronously send a character to the command processor.
Definition: console.c:177
ringbuf_t ring
Definition: console.h:72
uint16_t pt_t
Definition: protothreads.h:86
const console_cmd_t * cmd
Definition: console.h:93
#define SCRATCH_SIZE
Definition: console.h:58
int console_gpio_register(const console_gpio_t *gpio)
Register a new GPIO command.
int argc
Definition: console.h:90
void console_process(console_t *c, char d)
Synchronous console function for use in threaded environments.
Definition: console.c:254
char * bufp
Definition: console.h:88
union console::@0 scratch
Console command descriptor.
Definition: console.h:47
FILE * out
Definition: console.h:69
fibre_t fibre
Definition: console.h:67
console_cmd_t cmd
Definition: console.h:252
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 pin
Definition: console.h:254
struct console console_t
Console descriptor.
char buf[SCRATCH_SIZE]
Definition: console.h:82
pt_state_t
Definition: protothreads.h:80
pt_state_t console_run(console_t *c)
Console protothread entrypoint.
Definition: console.c:217
pt_t pt
Definition: console.h:94
int console_getch(console_t *c)
Fetch a character from the command processors queue.
Definition: console.c:172
char ringbuf[16]
Definition: console.h:71
pt_state_t console_eval(pt_t *pt, console_t *c, const char *cmd)
Proto-thread to inject a string into the command parser.
Definition: console.c:189
console_gpio_flags_t
Request special features from the GPIO command.
Definition: console.h:238
pt_state_t console_gpio_do_cmd(console_t *c)
uint8_t u8[SCRATCH_SIZE]
Definition: console.h:83
Fibre descriptor.
Definition: fibre.h:64
uintptr_t port
Definition: console.h:253
uint16_t u16[SCRATCH_SIZE/2]
Definition: console.h:84
GPIO command descriptor.
Definition: console.h:251
void console_init(console_t *c, FILE *f)
Initialized the console handler.
Definition: console.c:136