Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
iostream.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 23.05.2013 20:57:19 EDT
3 // File: iostream.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __IOSTREAM_HEADER
12 #define __IOSTREAM_HEADER
13 
14 #include "color.hpp"
15 #include "iomanip.hpp"
16 #include "type_traits.hpp"
17 #include "../util/has_print.hpp"
18 
19 #include <Print.h>
20 #include <Arduino.h>
21 
22 namespace ustd {
23 
24  struct endl_class {
25  endl_class(): end(true) {
26  }
27  endl_class & operator()(bool break_connection) {
28  end = break_connection; //for i2c for now
29  return (*this);
30  }
31  bool end;
32  } endl;
33 
34  class cout_class : public Print{
35  private:
36  #define TYPEDEF(NAME) typedef typename NAME::NAME ## _type NAME ## _type; NAME ## _type NAME ## _;
37 
38  TYPEDEF(setw)
39  TYPEDEF(setfill)
40  TYPEDEF(setprecision)
41  TYPEDEF(setbase)
42 
43  #undef TYPEDEF
44 
45  size_t write(uint8_t) {
46  return 1;
47  }
48  //------------------- same fct as in Print.h-------------------
49  size_t impl_chooser(const __FlashStringHelper * arg, Print * printer) {return printer->print(arg);}
50  size_t impl_chooser(const String & arg, Print * printer) {return printer->print(arg);}
51  size_t impl_chooser(const char arg[], Print * printer) {return printer->print(arg);}
52  size_t impl_chooser(char arg, Print * printer) {return printer->print(arg);}
53  size_t impl_chooser(const Printable& arg, Print * printer) {return printer->print(arg);}
54 
55  size_t impl_chooser(unsigned char arg, Print * printer) {return printer->print(arg, setbase_);}
56  size_t impl_chooser(int arg, Print * printer) {return printer->print(arg, setbase_);}
57  size_t impl_chooser(unsigned int arg, Print * printer) {return printer->print(arg, setbase_);}
58  size_t impl_chooser(long arg, Print * printer) {return printer->print(arg, setbase_);}
59  size_t impl_chooser(unsigned long arg, Print * printer) {return printer->print(arg, setbase_);}
60  size_t impl_chooser(uint64_t arg, Print * printer) {return printer->print((unsigned long)arg, setbase_);}
61 
62  size_t impl_chooser(double arg, Print * printer) {return printer->print(arg, setprecision_);}
63 
64  public:
65  void init(long const & speed = 460800lu) {
66  Serial.begin(speed);
67  Serial.setTimeout(100000000L);
68  setw_ = 0;
69  setfill_ = ' ';
70  setprecision_ = 2;
71  setbase_ = 10;
72  }
73  template<typename T>
74  typename enable_if<util::has_print<T>::value, cout_class>::type & operator<<(T const & arg) {
75  arg.print(*this);
76  return *this;
77  }
78  template<typename T>
79  typename disable_if<util::has_print<T>::value, cout_class>::type & operator<<(T const & arg) {
80  setw_type s = impl_chooser(arg, this);
81  if(s < setw_)
82  for(setw_type i = s; i < setw_; ++i)
83  Serial.print(setfill_);
84 
85  setw_ = 0;
86 
87  impl_chooser(arg, &Serial);
88  Serial.flush();
89  return *this;
90  }
92  Serial.flush();
93  return (*this);
94  }
96  Serial.println();
97  Serial.flush();
98  return (*this);
99  }
100  cout_class & operator<<(detail::dummy_struct const &) {
101  return (*this);
102  }
103 
104  #define IOMANIP_IMPL(NAME) \
105  cout_class & operator<<(NAME const & NAME ## _in) { \
106  NAME ## _ = NAME ## _in(); \
107  return (*this); \
108  }
109 
110  IOMANIP_IMPL(setw)
111  IOMANIP_IMPL(setfill)
112  IOMANIP_IMPL(setprecision)
113  IOMANIP_IMPL(setbase)
114 
115  #undef IOMANIP_IMPL
116  cout_class & operator()(uint8_t const & option = 2) {
117  if(option == 0) {
118  (*this) << NONE;
119  }
120  else if(option == 1) {
121  setbase_ = 10;
122  setfill_ = ' ';
123  setprecision_ = 2;
124  }
125  else if(option == 2) {
126  (*this) << NONE;
127  setbase_ = 10;
128  setfill_ = ' ';
129  setprecision_ = 2;
130  }
131  return (*this);
132  }
133  } cout;
134 //=================== cin ===================
135  struct cin_class {
136  cin_class(): silent_(false) {
137  }
138  operator bool() const {
139  return Serial.available();
140  }
141  //~ template<typename T>
142  //~ cin_class const & operator>>(T & in) const {
143  //~ while(!Serial.available())
144  //~ ;
145  //~ in = Serial.read();
146  //~ return *this;
147  //~ }
148  template<typename T>
149  typename enable_if<is_arithmetic<T>::value, cin_class>::type const & operator>>(T & in) const {
150  const uint8_t key_enter = 13;
151  const uint8_t key_space = ' ';
152 
153  uint8_t chr;
154 
155  auto read = [&](){
156  while(!Serial.available());
157  chr = Serial.read();
158  };
159 
160  read();
161 
162  in = 0;
163  bool neg = false;
164  uint8_t dot_pos = 0xFF;
165 
167  if(chr == '-') {
168  neg = true;
169  ustd::cout << '-';
170  read();
171  }
172 
173  while(true) {
174  if(chr == key_enter or chr == key_space) {
175  if(chr == key_enter)
177  break;
178  }
179  if(chr >= '0' and chr <= '9') {
180  in *= 10;
181  in += chr - '0';
182  ustd::cout << chr - '0';
183  if(dot_pos != 0xFF)
184  ++dot_pos;
185  }
186  else if(is_floating_point<T>::value) {
187  if(dot_pos == 0xFF and chr == '.') {
188  dot_pos = 0;
189  ustd::cout << '.';
190  }
191  }
192  else break;
193 
194 
195  read();
196  }
198  if(dot_pos != 0xFF)
199  for(;dot_pos; --dot_pos)
200  in /= 10;
201 
203  if(neg)
204  in *= -1;
205 
206  return *this;
207  }
208 
209  char peek() const {
210  return Serial.peek();
211  }
212  char read() const {
213  return Serial.read();
214  }
215  void set_silent(bool const & s) {
216  silent_ = s;
217  }
218  private:
219  bool silent_;
220  } cin;
221 }
222 
223 #endif //__IOSTREAM_HEADER
bool end
Definition: iostream.hpp:31
void init(long const &speed=460800lu)
Definition: iostream.hpp:65
disable_if< util::has_print< T >::value, cout_class >::type & operator<<(T const &arg)
Definition: iostream.hpp:79
enable_if< is_arithmetic< T >::value, cin_class >::type const & operator>>(T &in) const
Definition: iostream.hpp:149
void set_silent(bool const &s)
Definition: iostream.hpp:215
Definition: type_traits.hpp:228
#define IOMANIP_IMPL(NAME)
Definition: iostream.hpp:104
endl_class & operator()(bool break_connection)
Definition: iostream.hpp:27
cout_class & operator<<(endl_class &endl_)
Definition: iostream.hpp:95
cin_class()
Definition: iostream.hpp:136
Definition: iostream.hpp:34
#define TYPEDEF(NAME)
Definition: iostream.hpp:36
struct ustd::cin_class cin
#define NONE
Definition: color.hpp:37
Definition: type_traits.hpp:235
enable_if< util::has_print< T >::value, cout_class >::type & operator<<(T const &arg)
Definition: iostream.hpp:74
serial_class Serial
struct ustd::endl_class endl
char peek() const
Definition: iostream.hpp:209
char read() const
Definition: iostream.hpp:212
size_type available()
Definition: std_serial.hpp:82
cout_class & operator<<(detail::dummy_struct const &)
Definition: iostream.hpp:100
endl_class()
Definition: iostream.hpp:25
cout_class & operator()(uint8_t const &option=2)
Definition: iostream.hpp:116
Definition: type_traits.hpp:169
Definition: iostream.hpp:24
ustd::cout_class cout
void flush()
Definition: std_serial.hpp:117
Definition: type_traits.hpp:73
char read()
Definition: std_serial.hpp:110
Definition: iostream.hpp:135
cout_class & flush()
Definition: iostream.hpp:91