Skip to content
This repository
Browse code

write(), print(), and println() now return number of bytes written.

The type is long, and negative values indicate errors.  Needs more testing.
http://code.google.com/p/arduino/issues/detail?id=551
  • Loading branch information...
commit 8059abe58162faf81cd6d1312915d735116fb844 1 parent b788ad5
David A. Mellis authored August 23, 2011
5  hardware/arduino/cores/arduino/HardwareSerial.cpp
@@ -352,12 +352,13 @@ void HardwareSerial::flush()
352 352
     ;
353 353
 }
354 354
 
355  
-void HardwareSerial::write(uint8_t c)
  355
+long HardwareSerial::write(uint8_t c)
356 356
 {
357 357
   int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
358 358
 	
359 359
   // If the output buffer is full, there's nothing for it other than to 
360 360
   // wait for the interrupt handler to empty it a bit
  361
+  // ???: return 0 here instead?
361 362
   while (i == _tx_buffer->tail)
362 363
     ;
363 364
 	
@@ -365,6 +366,8 @@ void HardwareSerial::write(uint8_t c)
365 366
   _tx_buffer->head = i;
366 367
 	
367 368
   sbi(*_ucsrb, _udrie);
  369
+  
  370
+  return 1;
368 371
 }
369 372
 
370 373
 // Preinstantiate Objects //////////////////////////////////////////////////////
2  hardware/arduino/cores/arduino/HardwareSerial.h
@@ -55,7 +55,7 @@ class HardwareSerial : public Stream
55 55
     virtual int peek(void);
56 56
     virtual int read(void);
57 57
     virtual void flush(void);
58  
-    virtual void write(uint8_t);
  58
+    virtual long write(uint8_t);
59 59
     using Print::write; // pull in write(str) and write(buf, size) from Print
60 60
 };
61 61
 
194  hardware/arduino/cores/arduino/Print.cpp
@@ -30,167 +30,196 @@
30 30
 // Public Methods //////////////////////////////////////////////////////////////
31 31
 
32 32
 /* default implementation: may be overridden */
33  
-void Print::write(const char *str)
  33
+long Print::write(const char *str)
34 34
 {
35  
-  while (*str)
36  
-    write(*str++);
  35
+  long n = 0;
  36
+  while (*str) {
  37
+    if (write(*str++) <= 0) break;
  38
+    n++;
  39
+  }
  40
+  return n;
37 41
 }
38 42
 
39 43
 /* default implementation: may be overridden */
40  
-void Print::write(const uint8_t *buffer, size_t size)
  44
+long Print::write(const uint8_t *buffer, size_t size)
41 45
 {
42  
-  while (size--)
43  
-    write(*buffer++);
  46
+  long n = 0;
  47
+  while (size--) {
  48
+    if (write(*buffer++) <= 0) break;
  49
+    n++;
  50
+  }
  51
+  return n;
44 52
 }
45 53
 
46  
-void Print::print(const __FlashStringHelper *ifsh)
  54
+long Print::print(const __FlashStringHelper *ifsh)
47 55
 {
48 56
   const prog_char *p = (const prog_char *)ifsh;
  57
+  long n = 0;
49 58
   while (1) {
50 59
     unsigned char c = pgm_read_byte(p++);
51  
-    if (c == 0) return;
52  
-    write(c);
  60
+    if (c == 0) break;
  61
+    if (write(c) <= 0) break;
  62
+    n++;
53 63
   }
  64
+  return n;
54 65
 }
55 66
 
56  
-void Print::print(const String &s)
  67
+long Print::print(const String &s)
57 68
 {
  69
+  long n = 0;
58 70
   for (int i = 0; i < s.length(); i++) {
59  
-    write(s[i]);
  71
+    if (write(s[i]) < 0) break;
  72
+    n++;
60 73
   }
  74
+  return n;
61 75
 }
62 76
 
63  
-void Print::print(const char str[])
  77
+long Print::print(const char str[])
64 78
 {
65  
-  write(str);
  79
+  return write(str);
66 80
 }
67 81
 
68  
-void Print::print(char c)
  82
+long Print::print(char c)
69 83
 {
70  
-  write(c);
  84
+  return write(c);
71 85
 }
72 86
 
73  
-void Print::print(unsigned char b, int base)
  87
+long Print::print(unsigned char b, int base)
74 88
 {
75  
-  print((unsigned long) b, base);
  89
+  return print((unsigned long) b, base);
76 90
 }
77 91
 
78  
-void Print::print(int n, int base)
  92
+long Print::print(int n, int base)
79 93
 {
80  
-  print((long) n, base);
  94
+  return print((long) n, base);
81 95
 }
82 96
 
83  
-void Print::print(unsigned int n, int base)
  97
+long Print::print(unsigned int n, int base)
84 98
 {
85  
-  print((unsigned long) n, base);
  99
+  return print((unsigned long) n, base);
86 100
 }
87 101
 
88  
-void Print::print(long n, int base)
  102
+long Print::print(long n, int base)
89 103
 {
90 104
   if (base == 0) {
91  
-    write(n);
  105
+    return write(n);
92 106
   } else if (base == 10) {
93 107
     if (n < 0) {
94  
-      print('-');
  108
+      long t = print('-');
  109
+      if (t <= 0) return t;
95 110
       n = -n;
  111
+      return printNumber(n, 10) + 1;
96 112
     }
97  
-    printNumber(n, 10);
  113
+    return printNumber(n, 10);
98 114
   } else {
99  
-    printNumber(n, base);
  115
+    return printNumber(n, base);
100 116
   }
101 117
 }
102 118
 
103  
-void Print::print(unsigned long n, int base)
  119
+long Print::print(unsigned long n, int base)
104 120
 {
105  
-  if (base == 0) write(n);
106  
-  else printNumber(n, base);
  121
+  if (base == 0) return write(n);
  122
+  else return printNumber(n, base);
107 123
 }
108 124
 
109  
-void Print::print(double n, int digits)
  125
+long Print::print(double n, int digits)
110 126
 {
111  
-  printFloat(n, digits);
  127
+  return printFloat(n, digits);
112 128
 }
113 129
 
114  
-void Print::println(const __FlashStringHelper *ifsh)
  130
+long Print::println(const __FlashStringHelper *ifsh)
115 131
 {
116  
-  print(ifsh);
117  
-  println();
  132
+  long n = print(ifsh);
  133
+  if (n >= 0) n += println();
  134
+  return n;
118 135
 }
119 136
 
120  
-void Print::print(const Printable& x)
  137
+long Print::print(const Printable& x)
121 138
 {
122  
-  x.printTo(*this);
  139
+  return x.printTo(*this);
123 140
 }
124 141
 
125  
-void Print::println(void)
  142
+long Print::println(void)
126 143
 {
127  
-  print('\r');
128  
-  print('\n');
  144
+  long t = print('\r');
  145
+  if (t <= 0) return t;
  146
+  if (print('\n') <= 0) return 1;
  147
+  return 2;
129 148
 }
130 149
 
131  
-void Print::println(const String &s)
  150
+long Print::println(const String &s)
132 151
 {
133  
-  print(s);
134  
-  println();
  152
+  long n = print(s);
  153
+  if (n >= 0) n += println();
  154
+  return n;
135 155
 }
136 156
 
137  
-void Print::println(const char c[])
  157
+long Print::println(const char c[])
138 158
 {
139  
-  print(c);
140  
-  println();
  159
+  long n = print(c);
  160
+  if (n >= 0) n += println();
  161
+  return n;
141 162
 }
142 163
 
143  
-void Print::println(char c)
  164
+long Print::println(char c)
144 165
 {
145  
-  print(c);
146  
-  println();
  166
+  long n = print(c);
  167
+  if (n > 0) n += println();
  168
+  return n;
147 169
 }
148 170
 
149  
-void Print::println(unsigned char b, int base)
  171
+long Print::println(unsigned char b, int base)
150 172
 {
151  
-  print(b, base);
152  
-  println();
  173
+  long n = print(b, base);
  174
+  if (n >= 0) n += println();
  175
+  return n;
153 176
 }
154 177
 
155  
-void Print::println(int n, int base)
  178
+long Print::println(int num, int base)
156 179
 {
157  
-  print(n, base);
158  
-  println();
  180
+  long n = print(num, base);
  181
+  if (n >= 0) n += println();
  182
+  return n;
159 183
 }
160 184
 
161  
-void Print::println(unsigned int n, int base)
  185
+long Print::println(unsigned int num, int base)
162 186
 {
163  
-  print(n, base);
164  
-  println();
  187
+  long n = print(num, base);
  188
+  if (n >= 0) n += println();
  189
+  return n;
165 190
 }
166 191
 
167  
-void Print::println(long n, int base)
  192
+long Print::println(long num, int base)
168 193
 {
169  
-  print(n, base);
170  
-  println();
  194
+  long n = print(num, base);
  195
+  if (n >= 0) n += println();
  196
+  return n;
171 197
 }
172 198
 
173  
-void Print::println(unsigned long n, int base)
  199
+long Print::println(unsigned long num, int base)
174 200
 {
175  
-  print(n, base);
176  
-  println();
  201
+  long n = print(num, base);
  202
+  if (n >= 0) n += println();
  203
+  return n;
177 204
 }
178 205
 
179  
-void Print::println(double n, int digits)
  206
+long Print::println(double num, int digits)
180 207
 {
181  
-  print(n, digits);
182  
-  println();
  208
+  long n = print(num, digits);
  209
+  if (n >= 0) n += println();
  210
+  return n;
183 211
 }
184 212
 
185  
-void Print::println(const Printable& x)
  213
+long Print::println(const Printable& x)
186 214
 {
187  
-  print(x);
188  
-  println();
  215
+  long n = print(x);
  216
+  if (n >= 0) n += println();
  217
+  return n;
189 218
 }
190 219
 
191 220
 // Private Methods /////////////////////////////////////////////////////////////
192 221
 
193  
-void Print::printNumber(unsigned long n, uint8_t base) {
  222
+long Print::printNumber(unsigned long n, uint8_t base) {
194 223
   char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
195 224
   char *str = &buf[sizeof(buf) - 1];
196 225
 
@@ -206,15 +235,17 @@ void Print::printNumber(unsigned long n, uint8_t base) {
206 235
     *--str = c < 10 ? c + '0' : c + 'A' - 10;
207 236
   } while(n);
208 237
 
209  
-  write(str);
  238
+  return write(str);
210 239
 }
211 240
 
212  
-void Print::printFloat(double number, uint8_t digits) 
  241
+long Print::printFloat(double number, uint8_t digits) 
213 242
 { 
  243
+  long n = 0, t;
  244
+  
214 245
   // Handle negative numbers
215 246
   if (number < 0.0)
216 247
   {
217  
-     print('-');
  248
+     if ((n = print('-')) <= 0) return n;
218 249
      number = -number;
219 250
   }
220 251
 
@@ -228,18 +259,27 @@ void Print::printFloat(double number, uint8_t digits)
228 259
   // Extract the integer part of the number and print it
229 260
   unsigned long int_part = (unsigned long)number;
230 261
   double remainder = number - (double)int_part;
231  
-  print(int_part);
  262
+  if ((t = print(int_part)) < 0) return n;
  263
+  
  264
+  n += t;
232 265
 
233 266
   // Print the decimal point, but only if there are digits beyond
234  
-  if (digits > 0)
235  
-    print("."); 
  267
+  if (digits > 0) {
  268
+    t = print("."); 
  269
+    if (t <= 0) return n;
  270
+    n += t;
  271
+  }
236 272
 
237 273
   // Extract digits from the remainder one at a time
238 274
   while (digits-- > 0)
239 275
   {
240 276
     remainder *= 10.0;
241 277
     int toPrint = int(remainder);
242  
-    print(toPrint);
  278
+    t = print(toPrint);
  279
+    if (t <= 0) return n;
  280
+    n += t;
243 281
     remainder -= toPrint; 
244 282
   } 
  283
+  
  284
+  return n;
245 285
 }
56  hardware/arduino/cores/arduino/Print.h
@@ -34,37 +34,37 @@
34 34
 class Print
35 35
 {
36 36
   private:
37  
-    void printNumber(unsigned long, uint8_t);
38  
-    void printFloat(double, uint8_t);
  37
+    long printNumber(unsigned long, uint8_t);
  38
+    long printFloat(double, uint8_t);
39 39
   public:
40  
-    virtual void write(uint8_t) = 0;
41  
-    virtual void write(const char *str);
42  
-    virtual void write(const uint8_t *buffer, size_t size);
  40
+    virtual long write(uint8_t) = 0;
  41
+    virtual long write(const char *str);
  42
+    virtual long write(const uint8_t *buffer, size_t size);
43 43
     
44  
-    void print(const __FlashStringHelper *);
45  
-    void print(const String &);
46  
-    void print(const char[]);
47  
-    void print(char);
48  
-    void print(unsigned char, int = DEC);
49  
-    void print(int, int = DEC);
50  
-    void print(unsigned int, int = DEC);
51  
-    void print(long, int = DEC);
52  
-    void print(unsigned long, int = DEC);
53  
-    void print(double, int = 2);
54  
-    void print(const Printable&);
  44
+    long print(const __FlashStringHelper *);
  45
+    long print(const String &);
  46
+    long print(const char[]);
  47
+    long print(char);
  48
+    long print(unsigned char, int = DEC);
  49
+    long print(int, int = DEC);
  50
+    long print(unsigned int, int = DEC);
  51
+    long print(long, int = DEC);
  52
+    long print(unsigned long, int = DEC);
  53
+    long print(double, int = 2);
  54
+    long print(const Printable&);
55 55
 
56  
-    void println(const __FlashStringHelper *);
57  
-    void println(const String &s);
58  
-    void println(const char[]);
59  
-    void println(char);
60  
-    void println(unsigned char, int = DEC);
61  
-    void println(int, int = DEC);
62  
-    void println(unsigned int, int = DEC);
63  
-    void println(long, int = DEC);
64  
-    void println(unsigned long, int = DEC);
65  
-    void println(double, int = 2);
66  
-    void println(const Printable&);
67  
-    void println(void);
  56
+    long println(const __FlashStringHelper *);
  57
+    long println(const String &s);
  58
+    long println(const char[]);
  59
+    long println(char);
  60
+    long println(unsigned char, int = DEC);
  61
+    long println(int, int = DEC);
  62
+    long println(unsigned int, int = DEC);
  63
+    long println(long, int = DEC);
  64
+    long println(unsigned long, int = DEC);
  65
+    long println(double, int = 2);
  66
+    long println(const Printable&);
  67
+    long println(void);
68 68
 };
69 69
 
70 70
 #endif
2  hardware/arduino/cores/arduino/Printable.h
@@ -30,7 +30,7 @@ class Print;
30 30
 class Printable
31 31
 {
32 32
   public:
33  
-    virtual void printTo(Print& p) const = 0;
  33
+    virtual long printTo(Print& p) const = 0;
34 34
 };
35 35
 
36 36
 #endif
61  libraries/ArduinoTestSuite/examples/ATS_Write_Print/ATS_Write_Print.ino
... ...
@@ -0,0 +1,61 @@
  1
+#include <ArduinoTestSuite.h>
  2
+
  3
+void Test_Equal(long actual, long expected)
  4
+{
  5
+  char buf[100];
  6
+  boolean b = expected == actual;
  7
+  ATS_PrintTestStatus("", b);
  8
+  if (!b) {
  9
+    Serial.print("expected '");
  10
+    Serial.print(expected);
  11
+    Serial.print("', actual '");
  12
+    Serial.print(actual);
  13
+    Serial.println("'");
  14
+  }
  15
+}
  16
+
  17
+void setup()
  18
+{
  19
+  byte buf[5] = { 65, 66, 67, 0, 69 };
  20
+  ATS_begin("Arduino", "Write & Print Return Values Test");
  21
+
  22
+  Test_Equal(Serial.write('a'), 1);
  23
+  Test_Equal(Serial.write(byte(0)), 1);
  24
+  Test_Equal(Serial.write("abc"), 3);
  25
+  Test_Equal(Serial.write(""), 0);
  26
+  Test_Equal(Serial.write(buf, 5), 5);
  27
+  Test_Equal(Serial.print(0), 1);
  28
+  Test_Equal(Serial.print(""), 0);
  29
+  Test_Equal(Serial.print("abc"), 3);
  30
+  Test_Equal(Serial.print(0), 1);
  31
+  Test_Equal(Serial.print(1), 1);
  32
+  Test_Equal(Serial.print(11), 2);
  33
+  Test_Equal(Serial.print(12345), 5);
  34
+  Test_Equal(Serial.print(-1), 2);
  35
+  Test_Equal(Serial.print(-123), 4);
  36
+  Test_Equal(Serial.println(), 2);
  37
+  Test_Equal(Serial.println(""), 2);
  38
+  Test_Equal(Serial.println("abc"), 5);
  39
+  Test_Equal(Serial.println(0), 3);
  40
+  Test_Equal(Serial.println(1), 3);
  41
+  Test_Equal(Serial.println(11), 4);
  42
+  Test_Equal(Serial.println(12345), 7);
  43
+  Test_Equal(Serial.println(-1), 4);
  44
+  Test_Equal(Serial.println(-123), 6);
  45
+  
  46
+  ATS_end();
  47
+}
  48
+
  49
+void loop() {}
  50
+
  51
+
  52
+
  53
+
  54
+
  55
+
  56
+
  57
+
  58
+
  59
+
  60
+
  61
+
17  libraries/Ethernet/Client.cpp
@@ -70,19 +70,18 @@ int Client::connect(IPAddress ip, uint16_t port) {
70 70
   return 1;
71 71
 }
72 72
 
73  
-void Client::write(uint8_t b) {
74  
-  if (_sock != MAX_SOCK_NUM)
75  
-    send(_sock, &b, 1);
  73
+long Client::write(uint8_t b) {
  74
+  return write(&b, 1);
76 75
 }
77 76
 
78  
-void Client::write(const char *str) {
79  
-  if (_sock != MAX_SOCK_NUM)
80  
-    send(_sock, (const uint8_t *)str, strlen(str));
  77
+long Client::write(const char *str) {
  78
+  return write((const uint8_t *) str, strlen(str));
81 79
 }
82 80
 
83  
-void Client::write(const uint8_t *buf, size_t size) {
84  
-  if (_sock != MAX_SOCK_NUM)
85  
-    send(_sock, buf, size);
  81
+long Client::write(const uint8_t *buf, size_t size) {
  82
+  if (_sock == MAX_SOCK_NUM) return -1;
  83
+  if (!send(_sock, buf, size)) return -2;
  84
+  return size;
86 85
 }
87 86
 
88 87
 int Client::available() {
6  libraries/Ethernet/Client.h
@@ -12,9 +12,9 @@ class Client : public Stream {
12 12
   uint8_t status();
13 13
   int connect(IPAddress ip, uint16_t port);
14 14
   int connect(const char *host, uint16_t port);
15  
-  virtual void write(uint8_t);
16  
-  virtual void write(const char *str);
17  
-  virtual void write(const uint8_t *buf, size_t size);
  15
+  virtual long write(uint8_t);
  16
+  virtual long write(const char *str);
  17
+  virtual long write(const uint8_t *buf, size_t size);
18 18
   virtual int available();
19 19
   virtual int read();
20 20
   virtual int read(uint8_t *buf, size_t size);
10  libraries/Ethernet/IPAddress.cpp
@@ -42,13 +42,15 @@ bool IPAddress::operator==(const uint8_t* addr)
42 42
     return memcmp(addr, _address, sizeof(_address)) == 0;
43 43
 }
44 44
 
45  
-void IPAddress::printTo(Print& p) const
  45
+long IPAddress::printTo(Print& p) const
46 46
 {
  47
+    long n = 0, t;
47 48
     for (int i =0; i < 3; i++)
48 49
     {
49  
-        p.print(_address[i], DEC);
50  
-        p.print('.');
  50
+        if ((t = p.print(_address[i], DEC)) > 0) n += t;
  51
+        if ((t = p.print('.')) > 0) n+= t;
51 52
     }
52  
-    p.print(_address[3], DEC);
  53
+    if ((t = p.print(_address[3], DEC)) > 0) n += t;
  54
+    return n;
53 55
 }
54 56
 
2  libraries/Ethernet/IPAddress.h
@@ -60,7 +60,7 @@ class IPAddress : public Printable {
60 60
     IPAddress& operator=(const uint8_t *address);
61 61
     IPAddress& operator=(uint32_t address);
62 62
 
63  
-    virtual void printTo(Print& p) const;
  63
+    virtual long printTo(Print& p) const;
64 64
 
65 65
     friend class EthernetClass;
66 66
     friend class UDP;
12  libraries/Ethernet/Server.cpp
@@ -67,18 +67,20 @@ Client Server::available()
67 67
   return Client(MAX_SOCK_NUM);
68 68
 }
69 69
 
70  
-void Server::write(uint8_t b) 
  70
+long Server::write(uint8_t b) 
71 71
 {
72 72
   write(&b, 1);
73 73
 }
74 74
 
75  
-void Server::write(const char *str) 
  75
+long Server::write(const char *str) 
76 76
 {
77 77
   write((const uint8_t *)str, strlen(str));
78 78
 }
79 79
 
80  
-void Server::write(const uint8_t *buffer, size_t size) 
  80
+long Server::write(const uint8_t *buffer, size_t size) 
81 81
 {
  82
+  long n = 0;
  83
+  
82 84
   accept();
83 85
 
84 86
   for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
@@ -86,7 +88,9 @@ void Server::write(const uint8_t *buffer, size_t size)
86 88
 
87 89
     if (EthernetClass::_server_port[sock] == _port &&
88 90
       client.status() == SnSR::ESTABLISHED) {
89  
-      client.write(buffer, size);
  91
+      n += client.write(buffer, size);
90 92
     }
91 93
   }
  94
+  
  95
+  return n;
92 96
 }
6  libraries/Ethernet/Server.h
@@ -14,9 +14,9 @@ public Print {
14 14
   Server(uint16_t);
15 15
   Client available();
16 16
   void begin();
17  
-  virtual void write(uint8_t);
18  
-  virtual void write(const char *str);
19  
-  virtual void write(const uint8_t *buf, size_t size);
  17
+  virtual long write(uint8_t);
  18
+  virtual long write(const char *str);
  19
+  virtual long write(const uint8_t *buf, size_t size);
20 20
 };
21 21
 
22 22
 #endif
11  libraries/Ethernet/Udp.cpp
@@ -102,21 +102,22 @@ int UDP::endPacket()
102 102
   return sendUDP(_sock);
103 103
 }
104 104
 
105  
-void UDP::write(uint8_t byte)
  105
+long UDP::write(uint8_t byte)
106 106
 {
107  
-  write(&byte, 1);
  107
+  return write(&byte, 1);
108 108
 }
109 109
 
110  
-void UDP::write(const char *str)
  110
+long UDP::write(const char *str)
111 111
 {
112 112
   size_t len = strlen(str);
113  
-  write((const uint8_t *)str, len);
  113
+  return write((const uint8_t *)str, len);
114 114
 }
115 115
 
116  
-void UDP::write(const uint8_t *buffer, size_t size)
  116
+long UDP::write(const uint8_t *buffer, size_t size)
117 117
 {
118 118
   uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
119 119
   _offset += bytes_written;
  120
+  return bytes_written;
120 121
 }
121 122
 
122 123
 int UDP::parsePacket()
6  libraries/Ethernet/Udp.h
@@ -67,11 +67,11 @@ class UDP : public Stream {
67 67
   // Returns 1 if the packet was sent successfully, 0 if there was an error
68 68
   int endPacket();
69 69
   // Write a single byte into the packet
70  
-  virtual void write(uint8_t);
  70
+  virtual long write(uint8_t);
71 71
   // Write a string of characters into the packet
72  
-  virtual void write(const char *str);
  72
+  virtual long write(const char *str);
73 73
   // Write size bytes from buffer into the packet
74  
-  virtual void write(const uint8_t *buffer, size_t size);
  74
+  virtual long write(const uint8_t *buffer, size_t size);
75 75
 
76 76
   // Start processing the next available incoming packet
77 77
   // Returns the size of the packet in bytes, or 0 if no packets are available
3  libraries/LiquidCrystal/LiquidCrystal.cpp
@@ -258,8 +258,9 @@ inline void LiquidCrystal::command(uint8_t value) {
258 258
   send(value, LOW);
259 259
 }
260 260
 
261  
-inline void LiquidCrystal::write(uint8_t value) {
  261
+inline long LiquidCrystal::write(uint8_t value) {
262 262
   send(value, HIGH);
  263
+  return 1; // assume sucess
263 264
 }
264 265
 
265 266
 /************ low level data pushing commands **********/
2  libraries/LiquidCrystal/LiquidCrystal.h
@@ -79,7 +79,7 @@ class LiquidCrystal : public Print {
79 79
 
80 80
   void createChar(uint8_t, uint8_t[]);
81 81
   void setCursor(uint8_t, uint8_t); 
82  
-  virtual void write(uint8_t);
  82
+  virtual long write(uint8_t);
83 83
   void command(uint8_t);
84 84
 private:
85 85
   void send(uint8_t, uint8_t);
19  libraries/SD/File.cpp
@@ -58,19 +58,20 @@ boolean File::isDirectory(void) {
58 58
 }
59 59
 
60 60
 
61  
-void File::write(uint8_t val) {
62  
-  if (_file)
63  
-    _file->write(val);
  61
+long File::write(uint8_t val) {
  62
+  return write(&val, 1);
64 63
 }
65 64
 
66  
-void File::write(const char *str) {
67  
-  if (_file) 
68  
-    _file->write(str);
  65
+long File::write(const char *str) {
  66
+  return write((const uint8_t *) str, strlen(str));
69 67
 }
70 68
 
71  
-void File::write(const uint8_t *buf, size_t size) {
72  
-  if (_file)
73  
-    _file->write(buf, size);
  69
+long File::write(const uint8_t *buf, size_t size) {
  70
+  long t;
  71
+  if (!_file) return -1;
  72
+  t = _file->write(buf, size);
  73
+  if (t < 0) return t - 1;
  74
+  return t;
74 75
 }
75 76
 
76 77
 int File::peek() {
6  libraries/SD/SD.h
@@ -32,9 +32,9 @@ class File : public Stream {
32 32
   File(SdFile f, char *name);     // wraps an underlying SdFile
33 33
   File(void);      // 'empty' constructor
34 34
   ~File(void);     // destructor
35  
-  virtual void write(uint8_t);
36  
-  virtual void write(const char *str);
37  
-  virtual void write(const uint8_t *buf, size_t size);
  35
+  virtual long write(uint8_t);
  36
+  virtual long write(const char *str);
  37
+  virtual long write(const uint8_t *buf, size_t size);
38 38
   virtual int read();
39 39
   virtual int peek();
40 40
   virtual int available();
6  libraries/SD/utility/SdFat.h
@@ -283,9 +283,9 @@ class SdFile : public Print {
283 283
   }
284 284
   /** \return SdVolume that contains this file. */
285 285
   SdVolume* volume(void) const {return vol_;}
286  
-  void write(uint8_t b);
287  
-  int16_t write(const void* buf, uint16_t nbyte);
288  
-  void write(const char* str);
  286
+  long write(uint8_t b);
  287
+  long write(const void* buf, uint16_t nbyte);
  288
+  long write(const char* str);
289 289
   void write_P(PGM_P str);
290 290
   void writeln_P(PGM_P str);
291 291
 //------------------------------------------------------------------------------
10  libraries/SD/utility/SdFile.cpp
@@ -1121,7 +1121,7 @@ uint8_t SdFile::truncate(uint32_t length) {
1121 1121
  * for a read-only file, device is full, a corrupt file system or an I/O error.
1122 1122
  *
1123 1123
  */
1124  
-int16_t SdFile::write(const void* buf, uint16_t nbyte) {
  1124
+long SdFile::write(const void* buf, uint16_t nbyte) {
1125 1125
   // convert void* to uint8_t*  -  must be before goto statements
1126 1126
   const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
1127 1127
 
@@ -1219,8 +1219,8 @@ int16_t SdFile::write(const void* buf, uint16_t nbyte) {
1219 1219
  *
1220 1220
  * Use SdFile::writeError to check for errors.
1221 1221
  */
1222  
-void SdFile::write(uint8_t b) {
1223  
-  write(&b, 1);
  1222
+long SdFile::write(uint8_t b) {
  1223
+  return write(&b, 1);
1224 1224
 }
1225 1225
 //------------------------------------------------------------------------------
1226 1226
 /**
@@ -1228,8 +1228,8 @@ void SdFile::write(uint8_t b) {
1228 1228
  *
1229 1229
  * Use SdFile::writeError to check for errors.
1230 1230
  */
1231  
-void SdFile::write(const char* str) {
1232  
-  write(str, strlen(str));
  1231
+long SdFile::write(const char* str) {
  1232
+  return write(str, strlen(str));
1233 1233
 }
1234 1234
 //------------------------------------------------------------------------------
1235 1235
 /**
6  libraries/SoftwareSerial/SoftwareSerial.cpp
@@ -440,10 +440,10 @@ int SoftwareSerial::available()
440 440
   return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
441 441
 }
442 442
 
443  
-void SoftwareSerial::write(uint8_t b)
  443
+long SoftwareSerial::write(uint8_t b)
444 444
 {
445 445
   if (_tx_delay == 0)
446  
-    return;
  446
+    return -1;
447 447
 
448 448
   uint8_t oldSREG = SREG;
449 449
   cli();  // turn off interrupts for a clean txmit
@@ -484,6 +484,8 @@ void SoftwareSerial::write(uint8_t b)
484 484
 
485 485
   SREG = oldSREG; // turn interrupts back on
486 486
   tunedDelay(_tx_delay);
  487
+  
  488
+  return 1;
487 489
 }
488 490
 
489 491
 void SoftwareSerial::flush()
2  libraries/SoftwareSerial/SoftwareSerial.h
@@ -89,7 +89,7 @@ class SoftwareSerial : public Stream
89 89
   bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
90 90
   int peek();
91 91
 
92  
-  virtual void write(uint8_t byte);
  92
+  virtual long write(uint8_t byte);
93 93
   virtual int read();
94 94
   virtual int available();
95 95
   virtual void flush();
14  libraries/Wire/Wire.cpp
@@ -124,13 +124,13 @@ uint8_t TwoWire::endTransmission(void)
124 124
 // must be called in:
125 125
 // slave tx event callback
126 126
 // or after beginTransmission(address)
127  
-void TwoWire::write(uint8_t data)
  127
+long TwoWire::write(uint8_t data)
128 128
 {
129 129
   if(transmitting){
130 130
   // in master transmitter mode
131 131
     // don't bother if buffer is full
132 132
     if(txBufferLength >= BUFFER_LENGTH){
133  
-      return;
  133
+      return -1;
134 134
     }
135 135
     // put byte in tx buffer
136 136
     txBuffer[txBufferIndex] = data;
@@ -142,31 +142,33 @@ void TwoWire::write(uint8_t data)
142 142
     // reply to master
143 143
     twi_transmit(&data, 1);
144 144
   }
  145
+  return 1;
145 146
 }
146 147
 
147 148
 // must be called in:
148 149
 // slave tx event callback
149 150
 // or after beginTransmission(address)
150  
-void TwoWire::write(const uint8_t *data, size_t quantity)
  151
+long TwoWire::write(const uint8_t *data, size_t quantity)
151 152
 {
152 153
   if(transmitting){
153 154
   // in master transmitter mode
154 155
     for(size_t i = 0; i < quantity; ++i){
155  
-      write(data[i]);
  156
+      if (write(data[i]) < 0) return i;
156 157
     }
157 158
   }else{
158 159
   // in slave send mode
159 160
     // reply to master
160 161
     twi_transmit(data, quantity);
161 162
   }
  163
+  return quantity;
162 164
 }
163 165
 
164 166
 // must be called in:
165 167
 // slave tx event callback
166 168
 // or after beginTransmission(address)
167  
-void TwoWire::write(const char *data)
  169
+long TwoWire::write(const char *data)
168 170
 {
169  
-  write((uint8_t*)data, strlen(data));
  171
+  return write((uint8_t*)data, strlen(data));
170 172
 }
171 173
 
172 174
 // must be called in:
6  libraries/Wire/Wire.h
@@ -52,9 +52,9 @@ class TwoWire : public Stream
52 52
     uint8_t endTransmission(void);
53 53
     uint8_t requestFrom(uint8_t, uint8_t);
54 54
     uint8_t requestFrom(int, int);
55  
-    virtual void write(uint8_t);
56  
-    virtual void write(const char *);
57  
-    virtual void write(const uint8_t *, size_t);
  55
+    virtual long write(uint8_t);
  56
+    virtual long write(const char *);
  57
+    virtual long write(const uint8_t *, size_t);
58 58
     virtual int available(void);
59 59
     virtual int read(void);
60 60
     virtual int peek(void);

0 notes on commit 8059abe

Please sign in to comment.
Something went wrong with that request. Please try again.