Skip to content

Commit d98d31e

Browse files
committed
Redoing 448222e without all the extra files.
Adds toInt() to String, WCharacter.h (from Wiring), and an SD Datalogger example.
1 parent 920212e commit d98d31e

File tree

5 files changed

+270
-0
lines changed

5 files changed

+270
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
WCharacter.h - Character utility functions for Wiring & Arduino
3+
Copyright (c) 2010 Hernando Barragan. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library 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 GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef Character_h
21+
#define Character_h
22+
23+
24+
#include "WProgram.h"
25+
26+
// WCharacter.h prototypes
27+
inline boolean isAlphaNumeric(int c) __attribute__((always_inline));
28+
inline boolean isAlpha(int c) __attribute__((always_inline));
29+
inline boolean isAscii(int c) __attribute__((always_inline));
30+
inline boolean isWhitespace(int c) __attribute__((always_inline));
31+
inline boolean isControl(int c) __attribute__((always_inline));
32+
inline boolean isDigit(int c) __attribute__((always_inline));
33+
inline boolean isGraph(int c) __attribute__((always_inline));
34+
inline boolean isLowerCase(int c) __attribute__((always_inline));
35+
inline boolean isPrintable(int c) __attribute__((always_inline));
36+
inline boolean isPunct(int c) __attribute__((always_inline));
37+
inline boolean isSpace(int c) __attribute__((always_inline));
38+
inline boolean isUpperCase(int c) __attribute__((always_inline));
39+
inline boolean isHexadecimalDigit(int c) __attribute__((always_inline));
40+
inline int toAscii(int c) __attribute__((always_inline));
41+
inline int toLowerCase(int c) __attribute__((always_inline));
42+
inline int toUpperCase(int c)__attribute__((always_inline));
43+
44+
45+
// Checks for an alphanumeric character.
46+
// It is equivalent to (isalpha(c) || isdigit(c)).
47+
inline boolean isAlphaNumeric(int c)
48+
{
49+
return ( isalnum(c) == 0 ? false : true);
50+
}
51+
52+
53+
// Checks for an alphabetic character.
54+
// It is equivalent to (isupper(c) || islower(c)).
55+
inline boolean isAlpha(int c)
56+
{
57+
return ( isalpha(c) == 0 ? false : true);
58+
}
59+
60+
61+
// Checks whether c is a 7-bit unsigned char value
62+
// that fits into the ASCII character set.
63+
inline boolean isAscii(int c)
64+
{
65+
return ( isascii (c) == 0 ? false : true);
66+
}
67+
68+
69+
// Checks for a blank character, that is, a space or a tab.
70+
inline boolean isWhitespace(int c)
71+
{
72+
return ( isblank (c) == 0 ? false : true);
73+
}
74+
75+
76+
// Checks for a control character.
77+
inline boolean isControl(int c)
78+
{
79+
return ( iscntrl (c) == 0 ? false : true);
80+
}
81+
82+
83+
// Checks for a digit (0 through 9).
84+
inline boolean isDigit(int c)
85+
{
86+
return ( isdigit (c) == 0 ? false : true);
87+
}
88+
89+
90+
// Checks for any printable character except space.
91+
inline boolean isGraph(int c)
92+
{
93+
return ( isgraph (c) == 0 ? false : true);
94+
}
95+
96+
97+
// Checks for a lower-case character.
98+
inline boolean isLowerCase(int c)
99+
{
100+
return (islower (c) == 0 ? false : true);
101+
}
102+
103+
104+
// Checks for any printable character including space.
105+
inline boolean isPrintable(int c)
106+
{
107+
return ( isprint (c) == 0 ? false : true);
108+
}
109+
110+
111+
// Checks for any printable character which is not a space
112+
// or an alphanumeric character.
113+
inline boolean isPunct(int c)
114+
{
115+
return ( ispunct (c) == 0 ? false : true);
116+
}
117+
118+
119+
// Checks for white-space characters. For the avr-libc library,
120+
// these are: space, formfeed (’\f’), newline (’\n’), carriage
121+
// return (’\r’), horizontal tab (’\t’), and vertical tab (’\v’).
122+
inline boolean isSpace(int c)
123+
{
124+
return ( isspace (c) == 0 ? false : true);
125+
}
126+
127+
128+
// Checks for an uppercase letter.
129+
inline boolean isUpperCase(int c)
130+
{
131+
return ( isupper (c) == 0 ? false : true);
132+
}
133+
134+
135+
// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
136+
// 8 9 a b c d e f A B C D E F.
137+
inline boolean isHexadecimalDigit(int c)
138+
{
139+
return ( isxdigit (c) == 0 ? false : true);
140+
}
141+
142+
143+
// Converts c to a 7-bit unsigned char value that fits into the
144+
// ASCII character set, by clearing the high-order bits.
145+
inline int toAscii(int c)
146+
{
147+
return toascii (c);
148+
}
149+
150+
151+
// Warning:
152+
// Many people will be unhappy if you use this function.
153+
// This function will convert accented letters into random
154+
// characters.
155+
156+
// Converts the letter c to lower case, if possible.
157+
inline int toLowerCase(int c)
158+
{
159+
return tolower (c);
160+
}
161+
162+
163+
// Converts the letter c to upper case, if possible.
164+
inline int toUpperCase(int c)
165+
{
166+
return toupper (c);
167+
}
168+
169+
#endif

hardware/arduino/cores/arduino/WString.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,19 @@ void String::toCharArray(char *buf, unsigned int bufsize)
434434
strncpy(buf, _buffer, len);
435435
buf[len] = 0;
436436
}
437+
438+
439+
long String::toInt() {
440+
String temp = _buffer;
441+
long value = 0;
442+
443+
for (unsigned int charPos = 0; charPos < _length; charPos++) {
444+
int thisChar = temp[charPos];
445+
if (isdigit(thisChar)) {
446+
value *= 10;
447+
value += (thisChar - '0');
448+
}
449+
}
450+
451+
return value;
452+
}

hardware/arduino/cores/arduino/WString.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class String
7878
String trim( ) const;
7979
void getBytes(unsigned char *buf, unsigned int bufsize);
8080
void toCharArray(char *buf, unsigned int bufsize);
81+
long toInt( );
8182
const String& concat( const String &str );
8283
String replace( char oldChar, char newChar );
8384
String replace( const String& match, const String& replace );

hardware/arduino/cores/arduino/wiring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ extern "C"{
9696
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
9797
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
9898

99+
99100
typedef unsigned int word;
100101

101102
#define bit(b) (1UL << (b))
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
SD card datalogger
3+
4+
This example shows how to log data from three analog sensors
5+
to an SD card using the SD library.
6+
7+
The circuit:
8+
* analog sensors on analog ins 0, 1, and 2
9+
* SD card attached to SPI bus as follows:
10+
** MOSI - pin 11
11+
** MISO - pin 12
12+
** CLK - pin 13
13+
** CS - pin 4
14+
15+
created 24 Nov 2010
16+
by Tom Igoe
17+
18+
This example code is in the public domain.
19+
20+
*/
21+
22+
#include <SD.h>
23+
24+
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
25+
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
26+
// 53 on the Mega) must be left as an output or the SD library
27+
// functions will not work.
28+
const int chipSelect = 4;
29+
30+
void setup()
31+
{
32+
Serial.begin(9600);
33+
Serial.print("Initializing SD card...");
34+
35+
pinMode(10, OUTPUT);
36+
37+
// see if the card is present and can be initialized:
38+
if (!SD.begin(chipSelect)) {
39+
Serial.println("Card failed, or not present");
40+
// don't do anything more:
41+
return;
42+
}
43+
Serial.println("card initialized.");
44+
}
45+
46+
void loop()
47+
{
48+
// make a string for assembling the data to log:
49+
String dataString = "";
50+
51+
// read three sensors and append to the string:
52+
for (int analogPin = 0; analogPin < 3; analogPin++) {
53+
int sensor = analogRead(analogPin);
54+
dataString += String(sensor);
55+
if (analogPin < 2) {
56+
dataString += ",";
57+
}
58+
}
59+
60+
// open the file:
61+
File dataFile = SD.open("datalog.txt", true, true);
62+
63+
// if the file is available, write to it:
64+
if (dataFile) {
65+
dataFile.println(dataString);
66+
dataFile.close();
67+
// print to the serial port too:
68+
Serial.println(dataString);
69+
}
70+
// if the file isn't open, pop up an error:
71+
else {
72+
Serial.println("error opening datalog.txt");
73+
}
74+
}
75+
76+
77+
78+
79+
80+
81+
82+
83+

0 commit comments

Comments
 (0)