-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathHelpers.cpp
More file actions
66 lines (59 loc) · 1.84 KB
/
Helpers.cpp
File metadata and controls
66 lines (59 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Pom.h"
//////////////////////////////////////////////////////////////////////////
//
U32 SerialPrintf( char *str, ... ) {
U32 count=0, j=0;
char temp[ARDBUFFER+1];
// Count # of arguments
for ( U32 i=0; str[i] != '\0'; i++ )
if ( str[i] == '%' )
count++;
va_list argv;
va_start( argv, str );
U8 c = ' ';
for( j=0; c != '\0'; ) {
c = *str++;
if ( c == '%' ) {
// Print existing buffer as string and reset
temp[j] = '\0';
Serial.print( temp );
j=0;
temp[0] = '\0';
// Handle argument
switch( *str++ ) {
case 'c': Serial.print( (char) va_arg( argv, int ) ); break;
case 'd': Serial.print( va_arg( argv, int ) ); break;
case 'x': Serial.print( va_arg( argv, int ), HEX ); break;
case 'l': Serial.print( va_arg( argv, long ) ); break;
case 'f': Serial.print( va_arg( argv, double ), 8 ); break;
case 's': Serial.print( va_arg( argv, char* ) ); break;
};
} else {
// Append a regular character
temp[j++] = c;
if ( c == '\0' || c == '\n' || j == ARDBUFFER ) {
temp[j] = '\0';
if ( c == '\n' ) {
temp[--j] = '\0'; // Erase the '\n' we just wrote before
Serial.println( temp ); // Line feed
} else {
Serial.print( temp ); // Regular print
}
j = 0;
temp[0] = '\0';
}
}
}
return count + 1;
}
//////////////////////////////////////////////////////////////////////////
//
bool BufferedRead( U8 _pin, int _expectedValue, U8 _duration, U8& _counter ) {
int value = digitalRead( _pin );
if ( value == _expectedValue ) {
_counter++; // Increment counter of expected values
} else {
_counter = 0; // Reset as soon as we have a different value than what we want
}
return _counter >= _duration; // We only return true if we have got the expected values for at least the specified duration
}