atMETEO
An ATmega based weather station
uart.h
Go to the documentation of this file.
1 /*
2  * atMETEO - An ATmega based weather station
3  * Copyright (C) 2014-2015 Christian Fetzer
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
38 #include <stdlib.h>
39 
40 extern "C" {
41  #include <external/uart/uart.h>
42 }
43 
44 namespace Avr
45 {
46 
74 template <uint16_t baud>
75 class Uart
76 {
77 public:
83  static Uart& instance()
84  {
85  return s_instance;
86  }
87 
93  void sendChar(unsigned char c)
94  {
95  uart0_putc(c);
96  }
97 
103  void sendString(const char *str)
104  {
105  while (*str) {
106  sendChar(*str);
107  ++str;
108  }
109  }
110 
117  void sendLine(const char *str)
118  {
119  sendString(str);
120  sendChar('\n');
121  }
122 
130  void sendUInt(uint32_t value, uint8_t radix = 10)
131  {
132  char buffer[sizeof(uint32_t) * 8 + 1];
133  ultoa(value, buffer, radix);
134  sendString(buffer);
135  }
136 
149  void sendValue(const char *description, uint32_t value, uint8_t radix = 10)
150  {
151  sendString(description);
152  sendString(": ");
153  sendUInt(value, radix);
154  sendChar('\n');
155  }
156 
164  void sendDouble(double value)
165  {
166  if (value > 9999 || value < -9999) value = -1;
167  char buffer[8];
168  dtostrf(value, 5, 2, buffer);
169  sendString(buffer);
170  }
171 
172 private:
173  static Uart s_instance;
174 
175  Uart()
176  {
177  uart0_init(UART_BAUD_SELECT(baud, F_CPU));
178  }
179 };
180 
181 template <uint16_t baud>
183  // \addtogroup libtarget_uart
185 
186 } // namespace Avr
void sendValue(const char *description, uint32_t value, uint8_t radix=10)
Transmits the string representation of the unsigned integer value and its description.
Definition: uart.h:149
static Uart & instance()
Returns the Avr::Uart instance.
Definition: uart.h:83
void sendString(const char *str)
Transmits the null-terminated string str.
Definition: uart.h:103
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
void sendDouble(double value)
Transmits the string representation of the double value.
Definition: uart.h:164
void sendChar(unsigned char c)
Transmits the character c.
Definition: uart.h:93
void sendUInt(uint32_t value, uint8_t radix=10)
Transmits the string representation of the unsigned integer value.
Definition: uart.h:130
void sendLine(const char *str)
Transmits the null-terminated string str followed by a new line character.
Definition: uart.h:117
A C++ wrapper for accessing the built-in UART communication interfaces.
Definition: uart.h:75