MAV'RIC
|
00001 /******************************************************************************* 00002 * Copyright (c) 2009-2016, MAV'RIC Development Team 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of the copyright holder nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software without 00017 * specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 ******************************************************************************/ 00031 00032 /******************************************************************************* 00033 * \file console.hxx 00034 * 00035 * \author MAV'RIC Team 00036 * 00037 * \brief Write to any write-able module in human-readable format 00038 * 00039 ******************************************************************************/ 00040 00041 #ifndef CONSOLE_HXX_ 00042 #define CONSOLE_HXX_ 00043 00044 #include "util/maths.h" 00045 #include "util/string_util.hpp" 00046 00047 00048 00049 template <typename Writeable> 00050 Console<Writeable>::Console(Writeable& stream): 00051 stream_(stream) 00052 { 00053 ; 00054 } 00055 00056 00065 template <typename Writeable> 00066 bool Console<Writeable>::write(const uint8_t* data, uint32_t size) 00067 { 00068 return stream_.write(data, size); 00069 } 00070 00078 template <typename Writeable> 00079 template <typename T> 00080 bool Console<Writeable>::write(T number) 00081 { 00082 uint8_t data_tmp[str::MAX_DIGITS10_LONG + 1]; 00083 uint8_t length; 00084 uint8_t* data = str::format_integer(number, data_tmp, &length); 00085 00086 return stream_.write(data, length); 00087 } 00088 00097 template <typename Writeable> 00098 template <typename T> 00099 bool Console<Writeable>::write_floating(T num, uint8_t after_digits) 00100 { 00101 uint8_t data_tmp[str::MAX_DIGITS10_LONG + str::MAX_AFTERDIGITS + 2]; 00102 uint8_t length; 00103 uint8_t* data = str::format_floating(num, data_tmp, &length, after_digits, str::MAX_DIGITS10_LONG); 00104 00105 return stream_.write(data, length); 00106 } 00107 00108 00118 template <typename Writeable> 00119 bool Console<Writeable>::write(float number, uint8_t after_digits) 00120 { 00121 return write_floating<float>(number, after_digits); 00122 } 00123 00133 template <typename Writeable> 00134 bool Console<Writeable>::write(double number, uint8_t after_digits) 00135 { 00136 return write_floating(number, after_digits); 00137 } 00138 00139 00147 template <typename Writeable> 00148 bool Console<Writeable>::write(bool value) 00149 { 00150 if (value) 00151 { 00152 const char* answer = "true"; 00153 return write((uint8_t*)answer, 4); 00154 } 00155 else 00156 { 00157 const char* answer = "false"; 00158 return write((uint8_t*)answer, 5); 00159 } 00160 } 00161 00169 template <typename Writeable> 00170 bool Console<Writeable>::write(const char* text) 00171 { 00172 uint8_t* data = (uint8_t*)text; 00173 return stream_.write(data, str::strlen(text)); 00174 } 00175 00183 template <typename Writeable> 00184 bool Console<Writeable>::writeln(const char* text) 00185 { 00186 bool result = write(text); 00187 newline(); 00188 flush(); 00189 return result; 00190 } 00191 00197 template <typename Writeable> 00198 void Console<Writeable>::flush() 00199 { 00200 stream_.flush(); 00201 } 00202 00208 template <typename Writeable> 00209 void Console<Writeable>::newline() 00210 { 00211 stream_.newline(); 00212 } 00213 00223 template <typename Writeable> 00224 template <typename T> 00225 Console<Writeable>& Console<Writeable>::operator<<(const T& data) 00226 { 00227 write(data); 00228 return *this; 00229 } 00230 00239 template <typename Writeable> 00240 Console<Writeable>& Console<Writeable>::operator<<(ConsoleManipulator manip) 00241 { 00242 return manip(*this); 00243 } 00244 00253 template <typename Writeable> 00254 Console<Writeable>& endl(Console<Writeable>& console) 00255 { 00256 console.newline(); 00257 console.flush(); 00258 return console; 00259 } 00260 00266 template <typename Writeable> 00267 Writeable* Console<Writeable>::get_stream() 00268 { 00269 return &stream_; 00270 } 00271 00272 #endif /* CONSOLE_HXX_ */