Skip to content

Commit 1d321b6

Browse files
committed
merge minor API changes of IPAddress, Print, Stream and WString from official Arduion core source. LookaheadMode is still under evaluation.
1 parent 7985f25 commit 1d321b6

File tree

16 files changed

+320
-4
lines changed

16 files changed

+320
-4
lines changed

STM32F1/cores/maple/IPAddress.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,48 @@ IPAddress::IPAddress(const uint8_t *address)
4444
memcpy(_address.bytes, address, sizeof(_address.bytes));
4545
}
4646

47+
bool IPAddress::fromString(const char *address)
48+
{
49+
// TODO: add support for "a", "a.b", "a.b.c" formats
50+
51+
uint16_t acc = 0; // Accumulator
52+
uint8_t dots = 0;
53+
54+
while (*address)
55+
{
56+
char c = *address++;
57+
if (c >= '0' && c <= '9')
58+
{
59+
acc = acc * 10 + (c - '0');
60+
if (acc > 255) {
61+
// Value out of [0..255] range
62+
return false;
63+
}
64+
}
65+
else if (c == '.')
66+
{
67+
if (dots == 3) {
68+
// Too much dots (there must be 3 dots)
69+
return false;
70+
}
71+
_address.bytes[dots++] = acc;
72+
acc = 0;
73+
}
74+
else
75+
{
76+
// Invalid char
77+
return false;
78+
}
79+
}
80+
81+
if (dots != 3) {
82+
// Too few dots (there must be 3 dots)
83+
return false;
84+
}
85+
_address.bytes[3] = acc;
86+
return true;
87+
}
88+
4789
IPAddress& IPAddress::operator=(const uint8_t *address)
4890
{
4991
memcpy(_address.bytes, address, sizeof(_address.bytes));

STM32F1/cores/maple/IPAddress.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class IPAddress : public Printable {
4646
IPAddress(uint32_t address);
4747
IPAddress(const uint8_t *address);
4848

49+
bool fromString(const char *address);
50+
bool fromString(const String &address) { return fromString(address.c_str()); }
51+
4952
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
5053
// to a four-byte uint8_t array is expected
5154
operator uint32_t() const { return _address.dword; };

STM32F1/cores/maple/Print.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ size_t Print::print(double n, int digits) {
124124
return printFloat(n, digits);
125125
}
126126

127+
size_t Print::print(const __FlashStringHelper *ifsh)
128+
{
129+
size_t n = print(ifsh);
130+
n += println();
131+
return n;
132+
}
133+
134+
size_t Print::print(const Printable& x)
135+
{
136+
return x.printTo(*this);
137+
}
138+
127139
size_t Print::println(void)
128140
{
129141
size_t n = print('\r');
@@ -198,6 +210,20 @@ size_t Print::println(double n, int digits) {
198210
return s;
199211
}
200212

213+
size_t Print::println(const __FlashStringHelper *ifsh)
214+
{
215+
size_t n = print(ifsh);
216+
n += println();
217+
return n;
218+
}
219+
220+
size_t Print::println(const Printable& x)
221+
{
222+
size_t n = print(x);
223+
n += println();
224+
return n;
225+
}
226+
201227
#ifdef SUPPORTS_PRINTF
202228
#include <stdio.h>
203229
#include <stdarg.h>

STM32F1/cores/maple/Print.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <libmaple/libmaple_types.h>
2727
#include "WString.h"
28+
#include "Printable.h"
2829

2930
enum {
3031
BYTE = 0,
@@ -51,6 +52,8 @@ class Print {
5152
size_t print(long long, int=DEC);
5253
size_t print(unsigned long long, int=DEC);
5354
size_t print(double, int=2);
55+
size_t print(const __FlashStringHelper *);
56+
size_t print(const Printable&);
5457
size_t println(void);
5558
size_t println(const String &s);
5659
size_t println(char);
@@ -63,6 +66,8 @@ class Print {
6366
size_t println(long long, int=DEC);
6467
size_t println(unsigned long long, int=DEC);
6568
size_t println(double, int=2);
69+
size_t println(const __FlashStringHelper *);
70+
size_t println(const Printable&);
6671
#ifdef SUPPORTS_PRINTF
6772
// Roger Clark. Work in progress to add printf support
6873
int printf(const char * format, ...);

STM32F1/cores/maple/Stream.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,68 @@ String Stream::readStringUntil(char terminator)
268268
return ret;
269269
}
270270

271+
272+
int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
273+
// any zero length target string automatically matches and would make
274+
// a mess of the rest of the algorithm.
275+
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
276+
if (t->len <= 0)
277+
return t - targets;
278+
}
279+
280+
while (1) {
281+
int c = timedRead();
282+
if (c < 0)
283+
return -1;
284+
285+
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
286+
// the simple case is if we match, deal with that first.
287+
if (c == t->str[t->index]) {
288+
if (++t->index == t->len)
289+
return t - targets;
290+
else
291+
continue;
292+
}
293+
294+
// if not we need to walk back and see if we could have matched further
295+
// down the stream (ie '1112' doesn't match the first position in '11112'
296+
// but it will match the second position so we can't just reset the current
297+
// index to 0 when we find a mismatch.
298+
if (t->index == 0)
299+
continue;
300+
301+
int origIndex = t->index;
302+
do {
303+
--t->index;
304+
// first check if current char works against the new current index
305+
if (c != t->str[t->index])
306+
continue;
307+
308+
// if it's the only char then we're good, nothing more to check
309+
if (t->index == 0) {
310+
t->index++;
311+
break;
312+
}
313+
314+
// otherwise we need to check the rest of the found string
315+
int diff = origIndex - t->index;
316+
size_t i;
317+
for (i = 0; i < t->index; ++i) {
318+
if (t->str[i] != t->str[i + diff])
319+
break;
320+
}
321+
322+
// if we successfully got through the previous loop then our current
323+
// index is good.
324+
if (i == t->index) {
325+
t->index++;
326+
break;
327+
}
328+
329+
// otherwise we just try the next index
330+
} while (t->index);
331+
}
332+
}
333+
// unreachable
334+
return -1;
335+
}

STM32F1/cores/maple/Stream.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Stream : public Print
5555
// parsing methods
5656

5757
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
58+
unsigned long getTimeout(void) { return _timeout; }
5859

5960
bool find(char *target); // reads data from the stream until the target string is found
6061
bool find(uint8_t *target) { return find ((char *)target); }
@@ -64,6 +65,8 @@ class Stream : public Print
6465
bool find(uint8_t *target, size_t length) { return find ((char *)target, length); }
6566
// returns true if target string is found, false if timed out
6667

68+
bool find(char target) { return find (&target, 1); }
69+
6770
bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
6871
bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); }
6972

@@ -97,6 +100,16 @@ class Stream : public Print
97100
// this allows format characters (typically commas) in values to be ignored
98101

99102
float parseFloat(char skipChar); // as above but the given skipChar is ignored
103+
104+
struct MultiTarget {
105+
const char *str; // string you're searching for
106+
size_t len; // length of string you're searching for
107+
size_t index; // index used by the search routine.
108+
};
109+
110+
// This allows you to search for an arbitrary number of strings.
111+
// Returns index of the target that is found first or -1 if timeout occurs.
112+
int findMulti(struct MultiTarget *targets, int tCount);
100113
};
101114

102115
#endif

STM32F1/cores/maple/WString.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
195195
void String::move(String &rhs)
196196
{
197197
if (buffer) {
198-
if (capacity >= rhs.len) {
198+
if (rhs && capacity >= rhs.len) {
199199
strcpy(buffer, rhs.buffer);
200200
len = rhs.len;
201201
rhs.len = 0;

STM32F1/cores/maple/WString.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ class String
161161
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
162162
{getBytes((unsigned char *)buf, bufsize, index);}
163163
const char * c_str() const { return buffer; }
164+
char* begin() { return buffer; }
165+
char* end() { return buffer + length(); }
166+
const char* begin() const { return c_str(); }
167+
const char* end() const { return c_str() + length(); }
164168

165169
// search
166170
int indexOf( char ch ) const;

STM32F4/cores/maple/IPAddress.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,48 @@ IPAddress::IPAddress(const uint8_t *address)
4444
memcpy(_address.bytes, address, sizeof(_address.bytes));
4545
}
4646

47+
bool IPAddress::fromString(const char *address)
48+
{
49+
// TODO: add support for "a", "a.b", "a.b.c" formats
50+
51+
uint16_t acc = 0; // Accumulator
52+
uint8_t dots = 0;
53+
54+
while (*address)
55+
{
56+
char c = *address++;
57+
if (c >= '0' && c <= '9')
58+
{
59+
acc = acc * 10 + (c - '0');
60+
if (acc > 255) {
61+
// Value out of [0..255] range
62+
return false;
63+
}
64+
}
65+
else if (c == '.')
66+
{
67+
if (dots == 3) {
68+
// Too much dots (there must be 3 dots)
69+
return false;
70+
}
71+
_address.bytes[dots++] = acc;
72+
acc = 0;
73+
}
74+
else
75+
{
76+
// Invalid char
77+
return false;
78+
}
79+
}
80+
81+
if (dots != 3) {
82+
// Too few dots (there must be 3 dots)
83+
return false;
84+
}
85+
_address.bytes[3] = acc;
86+
return true;
87+
}
88+
4789
IPAddress& IPAddress::operator=(const uint8_t *address)
4890
{
4991
memcpy(_address.bytes, address, sizeof(_address.bytes));

STM32F4/cores/maple/IPAddress.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class IPAddress : public Printable {
4646
IPAddress(uint32_t address);
4747
IPAddress(const uint8_t *address);
4848

49+
bool fromString(const char *address);
50+
bool fromString(const String &address) { return fromString(address.c_str()); }
51+
4952
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
5053
// to a four-byte uint8_t array is expected
5154
operator uint32_t() const { return _address.dword; };

0 commit comments

Comments
 (0)