ATtiny85 Template Code
Code snippets for the ATtiny85
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
uart_format.c
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------*
2 * Formatted serial output functions
3 *---------------------------------------------------------------------------*
4 * 14-Apr-2014 ShaneG
5 *
6 * Provides some simple formatted output functions for serial communications.
7 *--------------------------------------------------------------------------*/
8 #include <stdarg.h>
9 #include <stdbool.h>
10 #include "../hardware.h"
11 #include "softuart.h"
12 
13 // Only if enabled
14 #ifdef UART_ENABLED
15 
16 //---------------------------------------------------------------------------
17 // Helper functions
18 //---------------------------------------------------------------------------
19 
32 static bool printFormat(char ch1, char ch2, va_list *args) {
33  bool skip = true;
34  // Fail fast
35  if(ch1=='%') {
36  // Use the second character to determine what is requested
37  if((ch2=='%')||(ch2=='\0'))
38  uartSend('%');
39  else if(ch2=='c')
40  uartSend(va_arg(*args, int));
41  else if(ch2=='u')
42  uartInt(va_arg(*args, unsigned int));
43  else if(ch2=='x')
44  uartHex(va_arg(*args, unsigned int));
45  else if(ch2=='s')
46  uartPrint(va_arg(*args, char *));
47  else if(ch2=='S')
48  uartPrintP(va_arg(*args, char *));
49  }
50  else {
51  uartSend(ch1);
52  skip = false;
53  }
54  return skip;
55  }
56 
57 //---------------------------------------------------------------------------
58 // Public API
59 //---------------------------------------------------------------------------
60 
82 void uartFormat(const char *cszString, ...) {
83  va_list args;
84  va_start(args, cszString);
85  char ch1, ch2 = *cszString;
86  for(int index=1; ch2!='\0'; index++) {
87  ch1 = ch2;
88  ch2 = cszString[index];
89  if(printFormat(ch1, ch2, &args)) {
90  // Move ahead an extra character so we wind up jumping by two
91  ch1 = ch2;
92  ch2 = cszString[++index];
93  }
94  }
95  va_end(args);
96  }
97 
119 void uartFormatP(const char *cszString, ...) {
120  va_list args;
121  va_start(args, cszString);
122  char ch2 = pgm_read_byte_near(cszString), ch1 = ch2;
123  for(int index=1; ch2!='\0'; index++) {
124  ch1 = ch2;
125  ch2 = pgm_read_byte_near(cszString + index);
126  if(printFormat(ch1, ch2, &args)) {
127  // Move ahead an extra character so we wind up jumping by two
128  index++;
129  ch1 = ch2;
130  ch2 = pgm_read_byte_near(cszString + index);
131  }
132  }
133  va_end(args);
134  }
135 
136 #endif /* UART_ENABLED */
137 
void uartFormatP(const char *cszString,...)
Definition: uart_format.c:119
void uartFormat(const char *cszString,...)
Definition: uart_format.c:82
void uartInt(uint16_t value)
Definition: uart_print.c:50
void uartPrint(const char *cszString)
Definition: uart_print.c:22
void uartPrintP(const char *cszString)
Definition: uart_print.c:36
void uartHex(uint16_t value)
Definition: uart_print.c:74
void uartSend(char ch)
Definition: uart_send.c:52