Revision 1084
Added functions for printing hexadecimal numbers to USB, see serial.h
branches/library_refactor/behaviors/library_test/main.c | ||
---|---|---|
68 | 68 |
} |
69 | 69 |
} |
70 | 70 |
|
71 |
static uint8_t hex_digit(uint8_t x) |
|
72 |
{ |
|
73 |
if (x>15) return '?'; |
|
74 |
return "0123456789ABCDEF"[x]; |
|
75 |
} |
|
76 | 71 |
|
77 |
|
|
78 |
static void usb_puth(int i) |
|
79 |
{ |
|
80 |
usb_putc(hex_digit((i>>12)&0xF)); |
|
81 |
usb_putc(hex_digit((i>>8)&0xF)); |
|
82 |
usb_putc(hex_digit((i>>4)&0xF)); |
|
83 |
usb_putc(hex_digit(i&0xF)); |
|
84 |
} |
|
85 |
|
|
86 |
static void usb_puth8(uint8_t i) |
|
87 |
{ |
|
88 |
usb_putc(hex_digit((i>>4)&0xF)); |
|
89 |
usb_putc(hex_digit(i&0xF)); |
|
90 |
} |
|
91 |
|
|
92 |
|
|
93 | 72 |
int main(void) { |
94 | 73 |
// dragonfly_init(ALL_ON); |
95 | 74 |
dragonfly_init(0); |
... | ... | |
103 | 82 |
orb_enable (); |
104 | 83 |
|
105 | 84 |
orb_set(0,0,0); delay_ms (1000); |
85 |
|
|
86 |
usb_init (); |
|
106 | 87 |
|
107 | 88 |
usb_puts ("Startup\r\n"); |
89 |
usb_puth (0x1234); usb_puts ("\r\n"); |
|
90 |
usb_puth (0xFFFF); usb_puts ("\r\n"); |
|
91 |
usb_puth (0); usb_puts ("\r\n"); |
|
92 |
usb_puth (0x12345); usb_puts ("\r\n"); |
|
93 |
|
|
108 | 94 |
|
109 |
|
|
110 | 95 |
// Do some lighting and wheel controlled motors on BTN1 |
111 | 96 |
if (!button2_read ()) |
112 | 97 |
acl (); |
branches/library_refactor/lib/include/libdragonfly/serial.h | ||
---|---|---|
95 | 95 |
int usb_puts(char *s); |
96 | 96 |
/** @brief Print an integer to USB **/ |
97 | 97 |
int usb_puti(int value); |
98 |
/** @brief Determine a hexadecimal digit **/ |
|
99 |
uint8_t hex_digit (uint8_t value); |
|
100 |
/** @brief Print a fixed width hexadecimal representation to USB **/ |
|
101 |
void usb_puth16 (uint16_t value); |
|
102 |
/** @brief Print a fixed width hexadecimal representation to USB **/ |
|
103 |
void usb_puth8(uint8_t value); |
|
104 |
/** @brief Alias for usb_puth16 **/ |
|
105 |
static inline void usb_puth (uint16_t value) { usb_puth16 (value); }; |
|
98 | 106 |
|
107 |
|
|
99 | 108 |
/** @} **/ //end addtogroup |
100 | 109 |
|
101 | 110 |
/** |
... | ... | |
124 | 133 |
/** @brief Read a character from the XBee without blocking **/ |
125 | 134 |
int xbee_getc_nb(char *c); |
126 | 135 |
|
136 |
|
|
137 |
|
|
127 | 138 |
/** @} **/ //end addtogroup |
128 | 139 |
|
129 | 140 |
#endif |
branches/library_refactor/projects/libdragonfly/serial.c | ||
---|---|---|
276 | 276 |
|
277 | 277 |
return 0; |
278 | 278 |
} |
279 |
|
|
280 |
/** |
|
281 |
* Determines a hexadecimal digit in ASCII code. |
|
282 |
* |
|
283 |
* @param value the value of the digit (0<=value<=15) |
|
284 |
* |
|
285 |
* @return the hexadecimal digit in ASCII code, or '?' |
|
286 |
* if the input is invalid. |
|
287 |
**/ |
|
288 |
uint8_t hex_digit (uint8_t value) |
|
289 |
{ |
|
290 |
if (value>15) return '?'; |
|
291 |
// Postcondition: 0<=x<=15 |
|
292 |
|
|
293 |
return "0123456789ABCDEF"[value]; |
|
294 |
} |
|
295 |
|
|
296 |
/** |
|
297 |
* Prints a fixed width hexadecimal representation of an unsigned |
|
298 |
* 16 bit integer in ASCII code to USB. |
|
299 |
* usb_init must be called before this function can be used. |
|
300 |
* |
|
301 |
* @param value the value to print |
|
302 |
* |
|
303 |
* @see usb_init, usb_puti, usb_puts, usb_puth8, hex_digit |
|
304 |
**/ |
|
305 |
void usb_puth16 (uint16_t value) |
|
306 |
{ |
|
307 |
usb_putc (hex_digit((value >>12)&0xF)); |
|
308 |
usb_putc (hex_digit((value >>8 )&0xF)); |
|
309 |
usb_putc (hex_digit((value >>4 )&0xF)); |
|
310 |
usb_putc (hex_digit( value &0xF)); |
|
311 |
} |
|
312 |
|
|
313 |
/** |
|
314 |
* Prints a fixed width hexadecimal representation of an unsigned |
|
315 |
* 8 bit integer in ASCII code to USB. |
|
316 |
* usb_init must be called before this function can be used. |
|
317 |
* |
|
318 |
* @param value the value to print |
|
319 |
* |
|
320 |
* @see usb_init, usb_puti, usb_puts, usb_puth16, hex_digit |
|
321 |
**/ |
|
322 |
void usb_puth8(uint8_t value) |
|
323 |
{ |
|
324 |
usb_putc (hex_digit ((value)>>4 &0xF)); |
|
325 |
usb_putc (hex_digit ( value &0xF)); |
|
326 |
} |
|
327 |
|
branches/library_refactor/projects/libdragonfly/serial.h | ||
---|---|---|
95 | 95 |
int usb_puts(char *s); |
96 | 96 |
/** @brief Print an integer to USB **/ |
97 | 97 |
int usb_puti(int value); |
98 |
/** @brief Determine a hexadecimal digit **/ |
|
99 |
uint8_t hex_digit (uint8_t value); |
|
100 |
/** @brief Print a fixed width hexadecimal representation to USB **/ |
|
101 |
void usb_puth16 (uint16_t value); |
|
102 |
/** @brief Print a fixed width hexadecimal representation to USB **/ |
|
103 |
void usb_puth8(uint8_t value); |
|
104 |
/** @brief Alias for usb_puth16 **/ |
|
105 |
static inline void usb_puth (uint16_t value) { usb_puth16 (value); }; |
|
98 | 106 |
|
107 |
|
|
99 | 108 |
/** @} **/ //end addtogroup |
100 | 109 |
|
101 | 110 |
/** |
... | ... | |
124 | 133 |
/** @brief Read a character from the XBee without blocking **/ |
125 | 134 |
int xbee_getc_nb(char *c); |
126 | 135 |
|
136 |
|
|
137 |
|
|
127 | 138 |
/** @} **/ //end addtogroup |
128 | 139 |
|
129 | 140 |
#endif |
Also available in: Unified diff