Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
48 | mjames | 1 | /* |
2 | * simple_print.c |
||
3 | * |
||
4 | * Created on: 19 Aug 2013 |
||
5 | * Author: jammic |
||
6 | * |
||
7 | * This implements a small set of useful text output |
||
8 | * routines for use in small beacon code. |
||
9 | * redirects output to the serial port ... |
||
10 | * |
||
11 | */ |
||
12 | #include "config.h" |
||
13 | #include "simple_print.h" |
||
14 | #include "serial.h" |
||
15 | |||
16 | #include <string.h> |
||
17 | |||
18 | #if 0 |
||
19 | /***********/ |
||
20 | /* checksum handling used in final selftest frame of 'XML' printed by |
||
21 | the beacon */ |
||
22 | static uint16_t sum = 0; /** the checksum value */ |
||
23 | static char use_sum =0; /** if non-zero then checksum the values */ |
||
24 | static uint8_t * use_ptTab = 0; /** pointer to a string buffer if used */ |
||
25 | |||
26 | /** |
||
27 | * Initialise the checksum , and enable the checksumming of printed data |
||
28 | * |
||
29 | * @param None |
||
30 | * @return None |
||
31 | * |
||
32 | */ |
||
33 | void print_init_sum(void) |
||
34 | { |
||
35 | sum = 0; |
||
36 | use_sum = 1; |
||
37 | } |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Stop checksumming the characters printed |
||
42 | * |
||
43 | * @param None |
||
44 | * @return None |
||
45 | * |
||
46 | */ |
||
47 | void print_stop_sum(void) |
||
48 | { |
||
49 | use_sum=0; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Print the checksum value as an unsigned decimal number |
||
54 | * |
||
55 | * @param None |
||
56 | * @return None |
||
57 | * |
||
58 | */ |
||
59 | void print_sum(void) |
||
60 | { |
||
61 | print_dec(sum); |
||
62 | } |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Function to print a string to the serial port |
||
67 | * |
||
68 | * this computes the checksum of the data as it goes. |
||
69 | * |
||
70 | * @param[in] s A pointer to a null terminated array of characters |
||
71 | * |
||
72 | * @return None |
||
73 | */ |
||
74 | |||
75 | void print_str(char * s) |
||
76 | { |
||
77 | |||
78 | int i=0; |
||
79 | while (s&& *s) |
||
80 | { |
||
81 | if(use_sum) |
||
82 | { |
||
83 | checksumByte((uint8_t) *s, &sum); |
||
84 | } |
||
85 | |||
86 | if (use_ptTab==0) |
||
87 | { |
||
88 | PutCharTim(*s++); |
||
89 | } |
||
90 | else |
||
91 | { |
||
92 | use_ptTab[i++]=(*s++); |
||
93 | use_ptTab[i]='\0'; |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | /** |
||
98 | * Function to print carriage return |
||
99 | * |
||
100 | * The side effect is that the values of the characters printed are included in the |
||
101 | * checksum if required |
||
102 | * @param None |
||
103 | * @return None |
||
104 | */ |
||
105 | |||
106 | void print_crlf(void) |
||
107 | { |
||
108 | print_str("\r\n"); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Function to print comma |
||
113 | * |
||
114 | * The side effect is that the values of the characters printed are included in the |
||
115 | * checksum if required |
||
116 | */ |
||
117 | void print_comma(void) |
||
118 | { |
||
119 | print_str(","); |
||
120 | } |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Function to print unsigned number |
||
125 | * |
||
126 | * Printed as a variable number of digits |
||
127 | * sufficient to display the value of the number |
||
128 | * @param[in] n Unsigned long number to print |
||
129 | * @return None |
||
130 | */ |
||
131 | void print_dec(unsigned long n) |
||
132 | { |
||
133 | char buff[11]; // 10 chars |
||
134 | buff[10]=0; // terminate |
||
135 | char *p=buff+9; |
||
136 | do // at least one loop to ensure a 0 is printed. |
||
137 | { |
||
138 | char d; |
||
139 | d=n%10; |
||
140 | n/=10; |
||
141 | *p-- = d+'0' ; |
||
142 | } while(n); |
||
143 | p++; |
||
144 | print_str(p); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Function to print unsigned number |
||
149 | * |
||
150 | * Number is printed as a fixed number of digits with |
||
151 | * leading zeros. Will only print width least significant digits eg. |
||
152 | * print_dec_width(123456,4) will print 3456. |
||
153 | * print_dec_width(23, 4) will print 0023 |
||
154 | * |
||
155 | * @param[in] n The number to print |
||
156 | * @param[in] width The field width to use |
||
157 | * @return None |
||
158 | */ |
||
159 | void print_dec_width(unsigned long n, unsigned char width) |
||
160 | { |
||
161 | |||
162 | char buff[11]; // 10 chars |
||
163 | buff[10]=0; // terminate |
||
164 | char *p=buff+9; |
||
165 | do // at least one loop to ensure a 0 is printed. |
||
166 | { |
||
167 | char d; |
||
168 | d=n%10; |
||
169 | n/=10; |
||
170 | *p-- = d+'0' ; |
||
171 | } while(--width > 0); |
||
172 | p++; |
||
173 | print_str(p); |
||
174 | } |
||
175 | |||
176 | |||
177 | |||
178 | /** |
||
179 | * Function to print a signed number |
||
180 | * |
||
181 | * If the number is negative, print with a leading '-' sign |
||
182 | * as a variable number of digits |
||
183 | * @param[in] n long signed number |
||
184 | * @return None |
||
185 | */ |
||
186 | void print_signed(long n) |
||
187 | { |
||
188 | if(n<0) |
||
189 | { |
||
190 | print_str("-"); |
||
191 | n=0-n; |
||
192 | } |
||
193 | print_dec(n); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Print hex to the buffer pointed at |
||
198 | * |
||
199 | * This is legacy code - may not be needed in current beacons |
||
200 | * |
||
201 | * @param[in] tabOut A pointer to a buffer to fill |
||
202 | * @param[in] n The value to print |
||
203 | * @param[in] fmt A format specifier |
||
204 | * |
||
205 | * @return None |
||
206 | **/ |
||
207 | void format_hex(uint8_t * tabOut, unsigned long n, eHexFmt fmt) |
||
208 | { |
||
209 | use_ptTab=tabOut; |
||
210 | print_hex( n, fmt); |
||
211 | use_ptTab=0; |
||
212 | } |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Function to print unsigned number |
||
217 | * |
||
218 | * This prints as a fixed number of hexadecimal digits with |
||
219 | * leading zeros. |
||
220 | * print_hex(1000,h4) will print 3FA. |
||
221 | * print_dec_width(23, h04) will print 0017 |
||
222 | * |
||
223 | * |
||
224 | * @param[in] n The number to print |
||
225 | * @param[in] fmt A member of the eHexFmt enumeration |
||
226 | * @return None |
||
227 | * |
||
228 | */ |
||
229 | |||
230 | void print_hex(unsigned long n, eHexFmt fmt) |
||
231 | { |
||
232 | char buff[9]; // 8 spaces with a terminating \0 |
||
233 | int i; |
||
234 | char c; |
||
235 | char *p=buff+7; // point at least significant char |
||
236 | |||
237 | if(fmt & 0x10 ) // space prefixed hex |
||
238 | { |
||
239 | c=' '; |
||
240 | fmt &=0x0F; // remove flag leaving number of chars |
||
241 | } |
||
242 | else |
||
243 | { |
||
244 | c='0'; |
||
245 | } |
||
246 | for(i=0; i<8; i++) |
||
247 | { |
||
248 | buff[i]=c; |
||
249 | } |
||
250 | buff[8]=0; |
||
251 | |||
252 | // lay out the hex digits right justified in buffer, stop when done. |
||
253 | // the remainder of the buffer to the left is either spaces or zeros. |
||
254 | do // at least one loop to ensure a '0' is produced. |
||
255 | { |
||
256 | char d; |
||
257 | d=n & 0x0f; |
||
258 | n>>=4; |
||
259 | *p-- = d>9 ? d-10+'A' : d+'0' ; |
||
260 | } while(n); |
||
261 | |||
262 | p=buff + 8 - fmt; // index in the right number of chars from the start to output |
||
263 | |||
264 | print_str(p); |
||
265 | |||
266 | } |
||
267 | // end of SIMPLE_PRINT block |
||
268 | #endif |