µracoli Manual  Version foo
hif.h
1 /* Copyright (c) 2007 Axel Wachtler
2  All rights reserved.
3 
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions
6  are met:
7 
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the authors nor the names of its contributors
14  may be used to endorse or promote products derived from this software
15  without specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  POSSIBILITY OF SUCH DAMAGE. */
28 
29 /* $Id$ */
36 #ifndef HIF_H
37 #define HIF_H
38 
39 
40 /* === types =========================================== */
41 #include <stdarg.h>
42 #include <avr/pgmspace.h>
43 #include <string.h>
44 
49 #define FLASH_STRING_T PGM_P
50 #define FLASH_STRING(x) PSTR(x)
51 
52 #if HIF_TYPE != HIF_NONE || defined DOXYGEN
53 
54 # define PRINTF(fmt, ...) hif_printf(FLASH_STRING(fmt), __VA_ARGS__)
55 
56 # define PRINT(fmt) hif_echo(FLASH_STRING(fmt))
57 
58 # define DUMP(sz,ptr) hif_dump(sz,ptr)
59 # define HIF_PUTS_NEWLINE() hif_puts_p(FLASH_STRING("\n\r"))
60 #else
61 # define PRINTF(fmt, ...)
62 # define PRINT(fmt)
63 # define DUMP(sz,ptr)
64 # define HIF_PUTS_NEWLINE()
65 #endif
66 
67 
68 /* === Prototypes ====================================== */
69 
75 void hif_init(const uint32_t baudrate);
76 
83 void hif_puts_p(const char *progmem_s);
84 
91 void hif_puts(const char *s );
92 
100 uint8_t hif_put_blk(unsigned char *data, uint8_t size);
101 
108 int hif_putc(int c);
109 
115 void hif_echo(FLASH_STRING_T str);
116 
123 void hif_printf(FLASH_STRING_T fmt, ...);
124 
131 void hif_dump(uint16_t sz, uint8_t *d);
132 
138 int hif_getc(void);
139 
147 uint8_t hif_get_blk(unsigned char *data, uint8_t max_size);
148 
149 
162 static inline int hif_split_args(char *txtline, int maxargs, char **argv)
163 {
164 uint8_t argc = 0, nextarg = 1;
165 
166  while((*txtline !=0) && (argc < maxargs))
167  {
168  if (*txtline == ' ')
169  {
170  *txtline = 0;
171  nextarg = 1;
172  }
173  else
174  {
175  if(nextarg)
176  {
177  argv[argc] = txtline;
178  argc++;
179  nextarg = 0;
180  }
181  }
182  txtline++;
183  }
184 
185  return argc;
186 }
187 
192 static inline int hif_get_dec_number(void)
193 {
194  int rv = 0;
195  int scale = 0;
196  char c;
197 
198 
199  while(1)
200  {
201  c = hif_getc();
202  if (c == '\n' || c == '\r')
203  {
204  rv *= scale;
205  break;
206  }
207  else if (c == '-' && scale == 0)
208  {
209  scale = -1;
210  }
211  else if ('0' <= c || c >= '9' )
212  {
213  if (scale == 0)
214  {
215  scale = 1;
216  }
217  rv = 10 * rv + (c - '0');
218  }
219  else if ('\b' <= c )
220  {
221  rv /= 10;
222  }
223 
224  }
225  return rv;
226 }
227 
237 static inline uint16_t hif_get_number(int8_t base)
238 {
239 char buf[8];
240 int idx = 0;
241 unsigned int tmp;
242 
243  buf[0] = 0;
244  do
245  {
246  tmp = hif_getc();
247  if (tmp < 0x100)
248  {
249  buf[idx++] = (char) tmp & 0xff;
250  buf[idx] = 0;
251  hif_putc(tmp);
252  }
253  }
254  while( (tmp != '\n') && (tmp != '\r') && (idx < 7) );
255  return (uint16_t)strtol(buf, NULL, base);
256 }
257 
259 #endif /* HIF_H */
static uint16_t hif_get_number(int8_t base)
Definition: hif.h:237
void hif_puts_p(const char *progmem_s)
Send a programm memory string to the interface.
uint8_t hif_get_blk(unsigned char *data, uint8_t max_size)
Get a block of bytes from the host interface.
int hif_putc(int c)
Send a character to the interface.
void hif_printf(FLASH_STRING_T fmt,...)
Print a formated string to the interface.
void hif_puts(const char *s)
Send string to the interface.
static int hif_split_args(char *txtline, int maxargs, char **argv)
Split a null terminated string.
Definition: hif.h:162
void hif_init(const uint32_t baudrate)
Initialize host interface.
uint8_t hif_put_blk(unsigned char *data, uint8_t size)
Send a block of characters to the interface.
int hif_getc(void)
Get a charakter byte from the host interface.
static int hif_get_dec_number(void)
Read a decimal number with hif_getc()
Definition: hif.h:192
void hif_dump(uint16_t sz, uint8_t *d)
Print hexdump of a data array to the interface.
void hif_echo(FLASH_STRING_T str)
Print a string to the interface.