Added support for String object. Updated print class to support String. Added examples folder from Maple IDE (None of the examples have been tested)

This commit is contained in:
Roger Clark 2014-11-02 20:24:34 +11:00
parent dbabd6fd52
commit 02647ee20c
48 changed files with 8257 additions and 2 deletions

View File

@ -63,6 +63,11 @@ void Print::print(uint8 b, int base) {
print((uint64)b, base);
}
void Print::print(const String &s)
{
return write(s.c_str(), s.length());
}
void Print::print(char c) {
write(c);
}
@ -116,6 +121,17 @@ void Print::println(void) {
print('\n');
}
void Print::println(const String &s)
{
/* SAM version
size_t n = print(s);
n += println();
return n;
*/
print(s);
println();
}
void Print::println(char c) {
print(c);
println();

View File

@ -0,0 +1,746 @@
/*
WString.cpp - String library for Wiring & Arduino
...mostly rewritten by Paul Stoffregen...
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
Copyright 2011, Paul Stoffregen, paul@pjrc.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <wirish/WString.h>
#include "itoa.h"
#include "avr/dtostrf.h"
/*********************************************/
/* Constructors */
/*********************************************/
String::String(const char *cstr)
{
init();
if (cstr) copy(cstr, strlen(cstr));
}
String::String(const String &value)
{
init();
*this = value;
}
String::String(const __FlashStringHelper *pstr)
{
init();
*this = pstr;
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String::String(String &&rval)
{
init();
move(rval);
}
String::String(StringSumHelper &&rval)
{
init();
move(rval);
}
#endif
String::String(char c)
{
init();
char buf[2];
buf[0] = c;
buf[1] = 0;
*this = buf;
}
String::String(unsigned char value, unsigned char base)
{
init();
char buf[1 + 8 * sizeof(unsigned char)];
utoa(value, buf, base);
*this = buf;
}
String::String(int value, unsigned char base)
{
init();
char buf[2 + 8 * sizeof(int)];
itoa(value, buf, base);
*this = buf;
}
String::String(unsigned int value, unsigned char base)
{
init();
char buf[1 + 8 * sizeof(unsigned int)];
utoa(value, buf, base);
*this = buf;
}
String::String(long value, unsigned char base)
{
init();
char buf[2 + 8 * sizeof(long)];
ltoa(value, buf, base);
*this = buf;
}
String::String(unsigned long value, unsigned char base)
{
init();
char buf[1 + 8 * sizeof(unsigned long)];
ultoa(value, buf, base);
*this = buf;
}
String::String(float value, unsigned char decimalPlaces)
{
init();
char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}
String::String(double value, unsigned char decimalPlaces)
{
init();
char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}
String::~String()
{
free(buffer);
}
/*********************************************/
/* Memory Management */
/*********************************************/
inline void String::init(void)
{
buffer = NULL;
capacity = 0;
len = 0;
}
void String::invalidate(void)
{
if (buffer) free(buffer);
buffer = NULL;
capacity = len = 0;
}
unsigned char String::reserve(unsigned int size)
{
if (buffer && capacity >= size) return 1;
if (changeBuffer(size)) {
if (len == 0) buffer[0] = 0;
return 1;
}
return 0;
}
unsigned char String::changeBuffer(unsigned int maxStrLen)
{
char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
if (newbuffer) {
buffer = newbuffer;
capacity = maxStrLen;
return 1;
}
return 0;
}
/*********************************************/
/* Copy and Move */
/*********************************************/
String & String::copy(const char *cstr, unsigned int length)
{
if (!reserve(length)) {
invalidate();
return *this;
}
len = length;
strcpy(buffer, cstr);
return *this;
}
String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
{
if (!reserve(length)) {
invalidate();
return *this;
}
len = length;
strcpy_P(buffer, (PGM_P)pstr);
return *this;
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
void String::move(String &rhs)
{
if (buffer) {
if (capacity >= rhs.len) {
strcpy(buffer, rhs.buffer);
len = rhs.len;
rhs.len = 0;
return;
} else {
free(buffer);
}
}
buffer = rhs.buffer;
capacity = rhs.capacity;
len = rhs.len;
rhs.buffer = NULL;
rhs.capacity = 0;
rhs.len = 0;
}
#endif
String & String::operator = (const String &rhs)
{
if (this == &rhs) return *this;
if (rhs.buffer) copy(rhs.buffer, rhs.len);
else invalidate();
return *this;
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & String::operator = (String &&rval)
{
if (this != &rval) move(rval);
return *this;
}
String & String::operator = (StringSumHelper &&rval)
{
if (this != &rval) move(rval);
return *this;
}
#endif
String & String::operator = (const char *cstr)
{
if (cstr) copy(cstr, strlen(cstr));
else invalidate();
return *this;
}
String & String::operator = (const __FlashStringHelper *pstr)
{
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
else invalidate();
return *this;
}
/*********************************************/
/* concat */
/*********************************************/
unsigned char String::concat(const String &s)
{
return concat(s.buffer, s.len);
}
unsigned char String::concat(const char *cstr, unsigned int length)
{
unsigned int newlen = len + length;
if (!cstr) return 0;
if (length == 0) return 1;
if (!reserve(newlen)) return 0;
strcpy(buffer + len, cstr);
len = newlen;
return 1;
}
unsigned char String::concat(const char *cstr)
{
if (!cstr) return 0;
return concat(cstr, strlen(cstr));
}
unsigned char String::concat(char c)
{
char buf[2];
buf[0] = c;
buf[1] = 0;
return concat(buf, 1);
}
unsigned char String::concat(unsigned char num)
{
char buf[1 + 3 * sizeof(unsigned char)];
itoa(num, buf, 10);
return concat(buf, strlen(buf));
}
unsigned char String::concat(int num)
{
char buf[2 + 3 * sizeof(int)];
itoa(num, buf, 10);
return concat(buf, strlen(buf));
}
unsigned char String::concat(unsigned int num)
{
char buf[1 + 3 * sizeof(unsigned int)];
utoa(num, buf, 10);
return concat(buf, strlen(buf));
}
unsigned char String::concat(long num)
{
char buf[2 + 3 * sizeof(long)];
ltoa(num, buf, 10);
return concat(buf, strlen(buf));
}
unsigned char String::concat(unsigned long num)
{
char buf[1 + 3 * sizeof(unsigned long)];
ultoa(num, buf, 10);
return concat(buf, strlen(buf));
}
unsigned char String::concat(float num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
}
unsigned char String::concat(double num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
}
unsigned char String::concat(const __FlashStringHelper * str)
{
if (!str) return 0;
int length = strlen_P((const char *) str);
if (length == 0) return 1;
unsigned int newlen = len + length;
if (!reserve(newlen)) return 0;
strcpy_P(buffer + len, (const char *) str);
len = newlen;
return 1;
}
/*********************************************/
/* Concatenate */
/*********************************************/
StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, char c)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(c)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, int num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, long num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, float num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, double num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate();
return a;
}
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(rhs)) a.invalidate();
return a;
}
/*********************************************/
/* Comparison */
/*********************************************/
int String::compareTo(const String &s) const
{
if (!buffer || !s.buffer) {
if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
if (buffer && len > 0) return *(unsigned char *)buffer;
return 0;
}
return strcmp(buffer, s.buffer);
}
unsigned char String::equals(const String &s2) const
{
return (len == s2.len && compareTo(s2) == 0);
}
unsigned char String::equals(const char *cstr) const
{
if (len == 0) return (cstr == NULL || *cstr == 0);
if (cstr == NULL) return buffer[0] == 0;
return strcmp(buffer, cstr) == 0;
}
unsigned char String::operator<(const String &rhs) const
{
return compareTo(rhs) < 0;
}
unsigned char String::operator>(const String &rhs) const
{
return compareTo(rhs) > 0;
}
unsigned char String::operator<=(const String &rhs) const
{
return compareTo(rhs) <= 0;
}
unsigned char String::operator>=(const String &rhs) const
{
return compareTo(rhs) >= 0;
}
unsigned char String::equalsIgnoreCase( const String &s2 ) const
{
if (this == &s2) return 1;
if (len != s2.len) return 0;
if (len == 0) return 1;
const char *p1 = buffer;
const char *p2 = s2.buffer;
while (*p1) {
if (tolower(*p1++) != tolower(*p2++)) return 0;
}
return 1;
}
unsigned char String::startsWith( const String &s2 ) const
{
if (len < s2.len) return 0;
return startsWith(s2, 0);
}
unsigned char String::startsWith( const String &s2, unsigned int offset ) const
{
if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
}
unsigned char String::endsWith( const String &s2 ) const
{
if ( len < s2.len || !buffer || !s2.buffer) return 0;
return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
}
/*********************************************/
/* Character Access */
/*********************************************/
char String::charAt(unsigned int loc) const
{
return operator[](loc);
}
void String::setCharAt(unsigned int loc, char c)
{
if (loc < len) buffer[loc] = c;
}
char & String::operator[](unsigned int index)
{
static char dummy_writable_char;
if (index >= len || !buffer) {
dummy_writable_char = 0;
return dummy_writable_char;
}
return buffer[index];
}
char String::operator[]( unsigned int index ) const
{
if (index >= len || !buffer) return 0;
return buffer[index];
}
void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
{
if (!bufsize || !buf) return;
if (index >= len) {
buf[0] = 0;
return;
}
unsigned int n = bufsize - 1;
if (n > len - index) n = len - index;
strncpy((char *)buf, buffer + index, n);
buf[n] = 0;
}
/*********************************************/
/* Search */
/*********************************************/
int String::indexOf(char c) const
{
return indexOf(c, 0);
}
int String::indexOf( char ch, unsigned int fromIndex ) const
{
if (fromIndex >= len) return -1;
const char* temp = strchr(buffer + fromIndex, ch);
if (temp == NULL) return -1;
return temp - buffer;
}
int String::indexOf(const String &s2) const
{
return indexOf(s2, 0);
}
int String::indexOf(const String &s2, unsigned int fromIndex) const
{
if (fromIndex >= len) return -1;
const char *found = strstr(buffer + fromIndex, s2.buffer);
if (found == NULL) return -1;
return found - buffer;
}
int String::lastIndexOf( char theChar ) const
{
return lastIndexOf(theChar, len - 1);
}
int String::lastIndexOf(char ch, unsigned int fromIndex) const
{
if (fromIndex >= len) return -1;
char tempchar = buffer[fromIndex + 1];
buffer[fromIndex + 1] = '\0';
char* temp = strrchr( buffer, ch );
buffer[fromIndex + 1] = tempchar;
if (temp == NULL) return -1;
return temp - buffer;
}
int String::lastIndexOf(const String &s2) const
{
return lastIndexOf(s2, len - s2.len);
}
int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
{
if (s2.len == 0 || len == 0 || s2.len > len) return -1;
if (fromIndex >= len) fromIndex = len - 1;
int found = -1;
for (char *p = buffer; p <= buffer + fromIndex; p++) {
p = strstr(p, s2.buffer);
if (!p) break;
if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
}
return found;
}
String String::substring(unsigned int left, unsigned int right) const
{
if (left > right) {
unsigned int temp = right;
right = left;
left = temp;
}
String out;
if (left > len) return out;
if (right > len) right = len;
char temp = buffer[right]; // save the replaced character
buffer[right] = '\0';
out = buffer + left; // pointer arithmetic
buffer[right] = temp; //restore character
return out;
}
/*********************************************/
/* Modification */
/*********************************************/
void String::replace(char find, char replace)
{
if (!buffer) return;
for (char *p = buffer; *p; p++) {
if (*p == find) *p = replace;
}
}
void String::replace(const String& find, const String& replace)
{
if (len == 0 || find.len == 0) return;
int diff = replace.len - find.len;
char *readFrom = buffer;
char *foundAt;
if (diff == 0) {
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
memcpy(foundAt, replace.buffer, replace.len);
readFrom = foundAt + replace.len;
}
} else if (diff < 0) {
char *writeTo = buffer;
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
unsigned int n = foundAt - readFrom;
memcpy(writeTo, readFrom, n);
writeTo += n;
memcpy(writeTo, replace.buffer, replace.len);
writeTo += replace.len;
readFrom = foundAt + find.len;
len += diff;
}
strcpy(writeTo, readFrom);
} else {
unsigned int size = len; // compute size needed for result
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
readFrom = foundAt + find.len;
size += diff;
}
if (size == len) return;
if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
int index = len - 1;
while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
readFrom = buffer + index + find.len;
memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
len += diff;
buffer[len] = 0;
memcpy(buffer + index, replace.buffer, replace.len);
index--;
}
}
}
void String::remove(unsigned int index){
if (index >= len) { return; }
int count = len - index;
remove(index, count);
}
void String::remove(unsigned int index, unsigned int count){
if (index >= len) { return; }
if (count <= 0) { return; }
if (index + count > len) { count = len - index; }
char *writeTo = buffer + index;
len = len - count;
strncpy(writeTo, buffer + index + count,len - index);
buffer[len] = 0;
}
void String::toLowerCase(void)
{
if (!buffer) return;
for (char *p = buffer; *p; p++) {
*p = tolower(*p);
}
}
void String::toUpperCase(void)
{
if (!buffer) return;
for (char *p = buffer; *p; p++) {
*p = toupper(*p);
}
}
void String::trim(void)
{
if (!buffer || len == 0) return;
char *begin = buffer;
while (isspace(*begin)) begin++;
char *end = buffer + len - 1;
while (isspace(*end) && end >= begin) end--;
len = end + 1 - begin;
if (begin > buffer) memcpy(buffer, begin, len);
buffer[len] = 0;
}
/*********************************************/
/* Parsing / Conversion */
/*********************************************/
long String::toInt(void) const
{
if (buffer) return atol(buffer);
return 0;
}
float String::toFloat(void) const
{
if (buffer) return float(atof(buffer));
return 0;
}

View File

@ -0,0 +1,170 @@
/*
Copyright (c) 2011 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "itoa.h"
#include <string.h>
#ifdef __cplusplus
extern "C"{
#endif // __cplusplus
#if 0
/* reverse: reverse string s in place */
static void reverse( char s[] )
{
int i, j ;
char c ;
for ( i = 0, j = strlen(s)-1 ; i < j ; i++, j-- )
{
c = s[i] ;
s[i] = s[j] ;
s[j] = c ;
}
}
/* itoa: convert n to characters in s */
extern void itoa( int n, char s[] )
{
int i, sign ;
if ( (sign = n) < 0 ) /* record sign */
{
n = -n; /* make n positive */
}
i = 0;
do
{ /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0) ; /* delete it */
if (sign < 0 )
{
s[i++] = '-';
}
s[i] = '\0';
reverse( s ) ;
}
#else
extern char* itoa( int value, char *string, int radix )
{
return ltoa( value, string, radix ) ;
}
extern char* ltoa( long value, char *string, int radix )
{
char tmp[33];
char *tp = tmp;
long i;
unsigned long v;
int sign;
char *sp;
if ( string == NULL )
{
return 0 ;
}
if (radix > 36 || radix <= 1)
{
return 0 ;
}
sign = (radix == 10 && value < 0);
if (sign)
{
v = -value;
}
else
{
v = (unsigned long)value;
}
while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}
sp = string;
if (sign)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
}
extern char* utoa( unsigned long value, char *string, int radix )
{
return ultoa( value, string, radix ) ;
}
extern char* ultoa( unsigned long value, char *string, int radix )
{
char tmp[33];
char *tp = tmp;
long i;
unsigned long v = value;
char *sp;
if ( string == NULL )
{
return 0;
}
if (radix > 36 || radix <= 1)
{
return 0;
}
while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}
sp = string;
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
}
#endif /* 0 */
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

View File

@ -0,0 +1,42 @@
/*
Copyright (c) 2011 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _ITOA_
#define _ITOA_
#ifdef __cplusplus
extern "C"{
#endif // __cplusplus
#if 0
extern void itoa( int n, char s[] ) ;
#else
extern char* itoa( int value, char *string, int radix ) ;
extern char* ltoa( long value, char *string, int radix ) ;
extern char* utoa( unsigned long value, char *string, int radix ) ;
extern char* ultoa( unsigned long value, char *string, int radix ) ;
#endif /* 0 */
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // _ITOA_

View File

@ -24,6 +24,7 @@
#define _WIRISH_PRINT_H_
#include <libmaple/libmaple_types.h>
#include "WString.h"
enum {
BYTE = 0,
@ -38,6 +39,8 @@ public:
virtual void write(uint8 ch) = 0;
virtual void write(const char *str);
virtual void write(const void *buf, uint32 len);
void print(const String &);
void print(char);
void print(const char[]);
void print(uint8, int=DEC);
@ -49,8 +52,9 @@ public:
void print(unsigned long long, int=DEC);
void print(double, int=2);
void println(void);
void println(char);
void println(const char[]);
void println(const String &s);
void println(char);
void println(const char[]);
void println(uint8, int=DEC);
void println(int, int=DEC);
void println(unsigned int, int=DEC);

View File

@ -0,0 +1,224 @@
/*
WString.h - String library for Wiring & Arduino
...mostly rewritten by Paul Stoffregen...
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
Copyright 2011, Paul Stoffregen, paul@pjrc.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef String_class_h
#define String_class_h
#ifdef __cplusplus
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <avr/pgmspace.h>
// When compiling programs with this class, the following gcc parameters
// dramatically increase performance and memory (RAM) efficiency, typically
// with little or no increase in code size.
// -felide-constructors
// -std=c++0x
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
// An inherited class for holding the result of a concatenation. These
// result objects are assumed to be writable by subsequent concatenations.
class StringSumHelper;
// The string class
class String
{
// use a function pointer to allow for "if (s)" without the
// complications of an operator bool(). for more information, see:
// http://www.artima.com/cppsource/safebool.html
typedef void (String::*StringIfHelperType)() const;
void StringIfHelper() const {}
public:
// constructors
// creates a copy of the initial value.
// if the initial value is null or invalid, or if memory allocation
// fails, the string will be marked as invalid (i.e. "if (s)" will
// be false).
String(const char *cstr = "");
String(const String &str);
String(const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String(String &&rval);
String(StringSumHelper &&rval);
#endif
explicit String(char c);
explicit String(unsigned char, unsigned char base=10);
explicit String(int, unsigned char base=10);
explicit String(unsigned int, unsigned char base=10);
explicit String(long, unsigned char base=10);
explicit String(unsigned long, unsigned char base=10);
explicit String(float, unsigned char decimalPlaces=2);
explicit String(double, unsigned char decimalPlaces=2);
~String(void);
// memory management
// return true on success, false on failure (in which case, the string
// is left unchanged). reserve(0), if successful, will validate an
// invalid string (i.e., "if (s)" will be true afterwards)
unsigned char reserve(unsigned int size);
inline unsigned int length(void) const {return len;}
// creates a copy of the assigned value. if the value is null or
// invalid, or if the memory allocation fails, the string will be
// marked as invalid ("if (s)" will be false).
String & operator = (const String &rhs);
String & operator = (const char *cstr);
String & operator = (const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & operator = (String &&rval);
String & operator = (StringSumHelper &&rval);
#endif
// concatenate (works w/ built-in types)
// returns true on success, false on failure (in which case, the string
// is left unchanged). if the argument is null or invalid, the
// concatenation is considered unsucessful.
unsigned char concat(const String &str);
unsigned char concat(const char *cstr);
unsigned char concat(char c);
unsigned char concat(unsigned char c);
unsigned char concat(int num);
unsigned char concat(unsigned int num);
unsigned char concat(long num);
unsigned char concat(unsigned long num);
unsigned char concat(float num);
unsigned char concat(double num);
unsigned char concat(const __FlashStringHelper * str);
// if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way)
String & operator += (const String &rhs) {concat(rhs); return (*this);}
String & operator += (const char *cstr) {concat(cstr); return (*this);}
String & operator += (char c) {concat(c); return (*this);}
String & operator += (unsigned char num) {concat(num); return (*this);}
String & operator += (int num) {concat(num); return (*this);}
String & operator += (unsigned int num) {concat(num); return (*this);}
String & operator += (long num) {concat(num); return (*this);}
String & operator += (unsigned long num) {concat(num); return (*this);}
String & operator += (float num) {concat(num); return (*this);}
String & operator += (double num) {concat(num); return (*this);}
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
// comparison (only works w/ Strings and "strings")
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
int compareTo(const String &s) const;
unsigned char equals(const String &s) const;
unsigned char equals(const char *cstr) const;
unsigned char operator == (const String &rhs) const {return equals(rhs);}
unsigned char operator == (const char *cstr) const {return equals(cstr);}
unsigned char operator != (const String &rhs) const {return !equals(rhs);}
unsigned char operator != (const char *cstr) const {return !equals(cstr);}
unsigned char operator < (const String &rhs) const;
unsigned char operator > (const String &rhs) const;
unsigned char operator <= (const String &rhs) const;
unsigned char operator >= (const String &rhs) const;
unsigned char equalsIgnoreCase(const String &s) const;
unsigned char startsWith( const String &prefix) const;
unsigned char startsWith(const String &prefix, unsigned int offset) const;
unsigned char endsWith(const String &suffix) const;
// character acccess
char charAt(unsigned int index) const;
void setCharAt(unsigned int index, char c);
char operator [] (unsigned int index) const;
char& operator [] (unsigned int index);
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
{getBytes((unsigned char *)buf, bufsize, index);}
const char * c_str() const { return buffer; }
// search
int indexOf( char ch ) const;
int indexOf( char ch, unsigned int fromIndex ) const;
int indexOf( const String &str ) const;
int indexOf( const String &str, unsigned int fromIndex ) const;
int lastIndexOf( char ch ) const;
int lastIndexOf( char ch, unsigned int fromIndex ) const;
int lastIndexOf( const String &str ) const;
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
// modification
void replace(char find, char replace);
void replace(const String& find, const String& replace);
void remove(unsigned int index);
void remove(unsigned int index, unsigned int count);
void toLowerCase(void);
void toUpperCase(void);
void trim(void);
// parsing/conversion
long toInt(void) const;
float toFloat(void) const;
protected:
char *buffer; // the actual char array
unsigned int capacity; // the array length minus one (for the '\0')
unsigned int len; // the String length (not counting the '\0')
protected:
void init(void);
void invalidate(void);
unsigned char changeBuffer(unsigned int maxStrLen);
unsigned char concat(const char *cstr, unsigned int length);
// copy and move
String & copy(const char *cstr, unsigned int length);
String & copy(const __FlashStringHelper *pstr, unsigned int length);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
void move(String &rhs);
#endif
};
class StringSumHelper : public String
{
public:
StringSumHelper(const String &s) : String(s) {}
StringSumHelper(const char *p) : String(p) {}
StringSumHelper(char c) : String(c) {}
StringSumHelper(unsigned char num) : String(num) {}
StringSumHelper(int num) : String(num) {}
StringSumHelper(unsigned int num) : String(num) {}
StringSumHelper(long num) : String(num) {}
StringSumHelper(unsigned long num) : String(num) {}
StringSumHelper(float num) : String(num) {}
StringSumHelper(double num) : String(num) {}
};
#endif // __cplusplus
#endif // String_class_h

View File

@ -41,7 +41,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wirish/WString.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
@ -58,6 +60,7 @@
#if STM32_MCU_SERIES == STM32_SERIES_F1 /* FIXME [0.0.13?] port to F2 */
#include <wirish/HardwareSPI.h>
#endif
#include <wirish/HardwareSerial.h>
#include <wirish/HardwareTimer.h>
#include <wirish/usb_serial.h>

View File

@ -0,0 +1,56 @@
/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to
65535 and uses the result to set the pulse width modulation (PWM) of
an output pin. Also prints the results to the serial monitor.
(You may need to change the pin numbers analogInPin and analogOutPin
if you're not using a Maple).
The circuit:
* Potentiometer connected to analog pin 15.
Center pin of the potentiometer goes to the analog pin.
Side pins of the potentiometer go to +3.3V and ground.
* LED connected from digital pin 9 to ground
created 29 Dec. 2008
by Tom Igoe
ported to Maple
by LeafLabs
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = 15; // Analog input pin that the potentiometer
// is attached to
const int pwmOutPin = 9; // PWM pin that the LED is attached to
// These variables will change:
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM
void setup() {
// Configure the ADC pin
pinMode(analogInPin, INPUT_ANALOG);
// Configure LED pin
pinMode(pwmOutPin, PWM);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 65535);
// change the analog out value:
pwmWrite(pwmOutPin, outputValue);
// print the results to the serial monitor:
SerialUSB.print("sensor = " );
SerialUSB.print(sensorValue);
SerialUSB.print("\t output = ");
SerialUSB.println(outputValue);
}

View File

@ -0,0 +1,33 @@
/*
Analog input, serial output
Reads an analog input pin, prints the results to the serial monitor.
The circuit:
* Potentiometer connected to analog pin 15.
* Center pin of the potentiometer goes to the analog pin.
* Side pins of the potentiometer go to +3.3V (VCC) and ground
created over and over again
by Tom Igoe and everyone who's ever used Arduino
Ported to Maple 27 May, 2010 by Bryan Newbold
*/
// Analog input pin. You may need to change this number if your board
// can't do analog input on pin 15.
const int analogInputPin = 15;
void setup() {
// Declare analogInputPin as INPUT_ANALOG:
pinMode(analogInputPin, INPUT_ANALOG);
}
void loop() {
// Read the analog input into a variable:
int analogValue = analogRead(analogInputPin);
// print the result:
SerialUSB.println(analogValue);
}

View File

@ -0,0 +1,42 @@
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin
0 and turning on and off the Maple's built-in light emitting diode
(LED). The amount of time the LED will be on and off depends on the
value obtained by analogRead().
Created by David Cuartielles
Modified 16 Jun 2009
By Tom Igoe
http://leaflabs.com/docs/adc.html
Ported to Maple 27 May 2010
by Bryan Newbold
*/
int sensorPin = 0; // Select the input pin for the potentiometer
int sensorValue = 0; // Variable to store the value coming from the sensor
void setup() {
// Declare the sensorPin as INPUT_ANALOG:
pinMode(sensorPin, INPUT_ANALOG);
// Declare the LED's pin as an OUTPUT. (BOARD_LED_PIN is a built-in
// constant which is the pin number of the built-in LED. On the
// Maple, it is 13.)
pinMode(BOARD_LED_PIN, OUTPUT);
}
void loop() {
// Read the value from the sensor:
sensorValue = analogRead(sensorPin);
// Turn the LED pin on:
digitalWrite(BOARD_LED_PIN, HIGH);
// Stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// Turn the LED pin off:
digitalWrite(BOARD_LED_PIN, LOW);
// Stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}

View File

@ -0,0 +1,76 @@
/*
Calibration
Demonstrates one techinque for calibrating sensor input. The sensor
readings during the first five seconds of the sketch execution
define the minimum and maximum of expected values attached to the
sensor pin.
The sensor minumum and maximum initial values may seem backwards.
Initially, you set the minimum high and listen for anything lower,
saving it as the new minumum. Likewise, you set the maximum low and
listen for anything higher as the new maximum.
The circuit:
* Analog sensor (potentiometer will do) attached to analog input 15
created 29 Oct 2008
By David A Mellis
Modified 17 Jun 2009
By Tom Igoe
http://arduino.cc/en/Tutorial/Calibration
Ported to Maple 27 May 2010
by Bryan Newbold
*/
// Constant (won't change):
const int sensorPin = 15; // pin that the sensor is attached to
// Variables:
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sensorValue = 0; // the sensor value
void setup() {
// Declare the sensorPin as INPUT_ANALOG:
pinMode(sensorPin, INPUT_ANALOG);
// Turn on the built-in LED to signal the start of the calibration
// period:
pinMode(BOARD_LED_PIN, OUTPUT);
digitalWrite(BOARD_LED_PIN, HIGH);
// Calibrate during the first five seconds:
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);
// Record the maximum sensor value:
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// Record the minimum sensor value:
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
// Signal the end of the calibration period:
digitalWrite(BOARD_LED_PIN, LOW);
}
void loop() {
// Read the sensor:
sensorValue = analogRead(sensorPin);
// Apply the calibration to the sensor reading:
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 65535);
// In case the sensor value is outside the range seen during calibration:
sensorValue = constrain(sensorValue, 0, 65535);
// Fade the LED using the calibrated value:
pwmWrite(BOARD_LED_PIN, sensorValue);
}

View File

@ -0,0 +1,42 @@
/*
Fading
This example shows how to fade an LED using the pwmWrite() function.
Created 1 Nov 2008
By David A. Mellis
Modified 17 June 2009
By Tom Igoe
Modified by LeafLabs for Maple
http://arduino.cc/en/Tutorial/Fading
For differences between Maple's pwmWrite() and Arduino's analogWrite():
http://leaflabs.com/docs/lang/api/analogwrite.html#arduino-compatibility
*/
int ledPin = 9; // Connect an LED to digital pin 9, or any other
// PWM-capable pin
void setup() {
pinMode(ledPin, PWM); // setup the pin as PWM
}
void loop() {
// Fade in from min to max in increments of 1280 points:
for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) {
// Sets the value (range from 0 to 65535):
pwmWrite(ledPin, fadeValue);
// Wait for 30 milliseconds to see the dimming effect:
delay(30);
}
// Fade out from max to min in increments of 1280 points:
for (int fadeValue = 65535 ; fadeValue >= 0; fadeValue -= 1280) {
// Sets the value (range from 0 to 1280):
pwmWrite(ledPin, fadeValue);
// Wait for 30 milliseconds to see the dimming effect:
delay(30);
}
}

View File

@ -0,0 +1,63 @@
/*
Smoothing
Reads repeatedly from an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.
The circuit:
* Analog sensor (potentiometer will do) attached to pin 15
Created 22 April 2007
By David A. Mellis <dam@mellis.org>
http://www.arduino.cc/en/Tutorial/Smoothing
Ported to Maple 27 May 2010
by Bryan Newbold
*/
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = 15; // analog input pin
void setup() {
// Declare the input pin as INPUT_ANALOG:
pinMode(inputPin, INPUT_ANALOG);
// Initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
// Subtract the last reading:
total = total - readings[index];
// Read from the sensor:
readings[index] = analogRead(inputPin);
// Add the reading to the total:
total = total + readings[index];
// Advance to the next position in the array:
index = index + 1;
// If we're at the end of the array...
if (index >= numReadings) {
// ...wrap around to the beginning:
index = 0;
}
// Calculate the average:
average = total / numReadings;
// Send it to the computer (as ASCII digits)
SerialUSB.println(average, DEC);
}

View File

@ -0,0 +1,80 @@
/*
ASCII table
Connect to the Maple SerialUSB using the Serial Monitor, then press
any key and hit enter.
Prints out byte values in all possible formats:
* as raw binary values
* as ASCII-encoded decimal, hex, octal, and binary values
For more on ASCII, see:
http://www.asciitable.com
http://en.wikipedia.org/wiki/ASCII
No external hardware needed.
created 2006
by Nicholas Zambetti
modified 18 Jan 2009
by Tom Igoe
<http://www.zambetti.com>
Ported to the Maple 27 May 2010
by Bryan Newbold
*/
void setup() {
// Wait for the user to press a key
while (!SerialUSB.available())
continue;
// Prints title with ending line break
SerialUSB.println("ASCII Table ~ Character Map");
}
// First visible ASCII character: '!' is number 33:
int thisByte = 33;
// You can also write ASCII characters in single quotes.
// for example. '!' is the same as 33, so you could also use this:
//int thisByte = '!';
void loop() {
// Prints value unaltered, i.e. the raw binary version of the
// byte. The serial monitor interprets all bytes as
// ASCII, so 33, the first number, will show up as '!'
SerialUSB.print(thisByte, BYTE);
SerialUSB.print(", dec: ");
// Prints value as string as an ASCII-encoded decimal (base 10).
// Decimal is the default format for SerialUSB.print() and
// SerialUSB.println(), so no modifier is needed:
SerialUSB.print(thisByte);
// But you can declare the modifier for decimal if you want to.
// This also works if you uncomment it:
// SerialUSB.print(thisByte, DEC);
SerialUSB.print(", hex: ");
// Prints value as string in hexadecimal (base 16):
SerialUSB.print(thisByte, HEX);
SerialUSB.print(", oct: ");
// Prints value as string in octal (base 8);
SerialUSB.print(thisByte, OCT);
SerialUSB.print(", bin: ");
// Prints value as string in binary (base 2); also prints ending
// line break:
SerialUSB.println(thisByte, BIN);
// If printed last visible character '~' or 126, stop:
if (thisByte == 126) { // You could also use if (thisByte == '~') {
// This loops forever and does nothing
while (true) {
continue;
}
}
// Go on to the next character
thisByte++;
}

View File

@ -0,0 +1,43 @@
/*
Dimmer
Demonstrates sending data from the computer to the Maple, in this
case to control the brightness of an LED. The data is sent in
individual bytes, each of which ranges from 0 to 255. Maple reads
these bytes and uses them to set the brightness of the LED.
The circuit:
LED connected to pin 9.
Serial connection to Processing, Max/MSP, or another serial application.
created 2006
by David A. Mellis
modified 14 Apr 2009
by Tom Igoe and Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Dimmer
http://leaflabs.com/docs/lang/api/pwmwrite.html
Ported to the Maple 28 May 2010
*/
int ledPin = 9;
void setup() {
// Declare ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
int brightness;
// Check if data has been sent from the computer:
if (SerialUSB.available()) {
// Read the most recent byte (which will be from 0 to 255), then
// convert it to be between 0 and 65,535, which are the minimum
// and maximum values usable for PWM:
brightness = map(SerialUSB.read(), 0, 255, 0, 65535);
// Set the brightness of the LED:
pwmWrite(ledPin, brightness);
}
}

View File

@ -0,0 +1,577 @@
/*
Graph
A simple example of communication from the Maple to the computer:
the value of analog input 15 is sent out the serial port. We call
this "serial" communication because the connection appears to both
the Maple and the computer as a serial port, even though it may
actually use a USB cable. Bytes are sent one after another
("serially") from the Maple to the computer.
You can use the Maple IDE Serial Monitor to view the sent data, or
it can be read by Processing, PD, Max/MSP, or any other program
capable of reading data from a serial port. The Processing code
below graphs the data received so you can see the value of the
analog input changing over time.
The circuit:
Any analog input sensor (like a potentiometer) attached to pin 15.
created 2006
by David A. Mellis
modified 14 Apr 2009
by Tom Igoe and Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Graph
*/
// Pin to use for analog input
const int analogInPin = 15;
void setup() {
// Declare pin 15 as an analog input:
pinMode(analogInPin, INPUT_ANALOG);
}
void loop() {
// send the value of analog input 15:
SerialUSB.println(analogRead(analogInPin));
}
/* Processing code for this example
// Graphing sketch
// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, followed by a newline, or newline and carriage return
// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size:
size(400, 300);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
*/
/* Max/MSP v5 patch for this example
{
"boxes" : [ {
"box" : {
"maxclass" : "comment",
"text" : "Graph\n\nThis patch takes a string, containing ASCII formatted number from 0 to 1023, with a carriage return and linefeed at the end. It converts the string to an integer and graphs it.\n\ncreated 2006\nby David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
"linecount" : 10,
"patching_rect" : [ 479.0, 6.0, 344.0, 144.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-32",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "select 0 1",
"patching_rect" : [ 327.0, 80.0, 62.0, 20.0 ],
"numoutlets" : 3,
"fontsize" : 12.0,
"outlettype" : [ "bang", "bang", "" ],
"id" : "obj-30",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "click here to close the serial port",
"patching_rect" : [ 412.0, 231.0, 206.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-26",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "click here to open the serial port",
"patching_rect" : [ 412.0, 205.0, 206.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-27",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "close",
"patching_rect" : [ 327.0, 231.0, 39.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-21",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "port a",
"patching_rect" : [ 349.0, 205.0, 41.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-19",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "multislider",
"candicane7" : [ 0.878431, 0.243137, 0.145098, 1.0 ],
"patching_rect" : [ 302.0, 450.0, 246.0, 167.0 ],
"contdata" : 1,
"numoutlets" : 2,
"peakcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
"slidercolor" : [ 0.066667, 0.058824, 0.776471, 1.0 ],
"candicane8" : [ 0.027451, 0.447059, 0.501961, 1.0 ],
"outlettype" : [ "", "" ],
"setminmax" : [ 0.0, 1023.0 ],
"settype" : 0,
"candicane6" : [ 0.733333, 0.035294, 0.788235, 1.0 ],
"setstyle" : 3,
"bgcolor" : [ 0.231373, 0.713726, 1.0, 1.0 ],
"id" : "obj-1",
"candicane4" : [ 0.439216, 0.619608, 0.070588, 1.0 ],
"candicane5" : [ 0.584314, 0.827451, 0.431373, 1.0 ],
"candicane2" : [ 0.145098, 0.203922, 0.356863, 1.0 ],
"candicane3" : [ 0.290196, 0.411765, 0.713726, 1.0 ],
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click here to get a list of serial ports",
"patching_rect" : [ 412.0, 179.0, 207.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-2",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Here's the number from Arduino's analog input",
"linecount" : 2,
"patching_rect" : [ 153.0, 409.0, 138.0, 34.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-3",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Convert ASCII to symbol",
"patching_rect" : [ 379.0, 378.0, 147.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-4",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Convert integer to ASCII",
"patching_rect" : [ 379.0, 355.0, 147.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-5",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 302.0, 414.0, 37.0, 20.0 ],
"numoutlets" : 2,
"fontsize" : 12.0,
"outlettype" : [ "int", "bang" ],
"bgcolor" : [ 0.866667, 0.866667, 0.866667, 1.0 ],
"id" : "obj-6",
"triscale" : 0.9,
"fontname" : "Arial",
"htextcolor" : [ 0.870588, 0.870588, 0.870588, 1.0 ],
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "fromsymbol",
"patching_rect" : [ 302.0, 378.0, 74.0, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-7",
"fontname" : "Arial",
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "itoa",
"patching_rect" : [ 302.0, 355.0, 46.0, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "int" ],
"id" : "obj-8",
"fontname" : "Arial",
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
"numinlets" : 3
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "zl group 4",
"patching_rect" : [ 302.0, 332.0, 64.0, 20.0 ],
"numoutlets" : 2,
"fontsize" : 12.0,
"outlettype" : [ "", "" ],
"id" : "obj-9",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "select 10 13",
"patching_rect" : [ 244.0, 281.0, 77.0, 20.0 ],
"numoutlets" : 3,
"fontsize" : 12.0,
"outlettype" : [ "bang", "bang", "" ],
"id" : "obj-10",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "toggle",
"patching_rect" : [ 244.0, 43.0, 15.0, 15.0 ],
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-11",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "qmetro 10",
"patching_rect" : [ 244.0, 80.0, 65.0, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "bang" ],
"id" : "obj-12",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "print",
"patching_rect" : [ 369.0, 179.0, 36.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-13",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "serial a 9600",
"patching_rect" : [ 244.0, 255.0, 84.0, 20.0 ],
"numoutlets" : 2,
"fontsize" : 12.0,
"outlettype" : [ "int", "" ],
"id" : "obj-14",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Read serial input buffer every 10 milliseconds",
"linecount" : 2,
"patching_rect" : [ 53.0, 72.0, 185.0, 34.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-15",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "If you get newline (ASCII 10), send the list. If you get return (ASCII 13) do nothing. Any other value, add to the list",
"linecount" : 3,
"patching_rect" : [ 332.0, 269.0, 320.0, 48.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-16",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click to open/close serial port and start/stop patch",
"linecount" : 2,
"patching_rect" : [ 271.0, 32.0, 199.0, 34.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-17",
"fontname" : "Arial",
"numinlets" : 1
}
}
],
"lines" : [ {
"patchline" : {
"source" : [ "obj-6", 0 ],
"destination" : [ "obj-1", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-7", 0 ],
"destination" : [ "obj-6", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-8", 0 ],
"destination" : [ "obj-7", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-9", 0 ],
"destination" : [ "obj-8", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-10", 0 ],
"destination" : [ "obj-9", 0 ],
"hidden" : 0,
"midpoints" : [ 253.5, 308.0, 311.5, 308.0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-10", 2 ],
"destination" : [ "obj-9", 0 ],
"hidden" : 0,
"midpoints" : [ 311.5, 320.0, 311.5, 320.0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-14", 0 ],
"destination" : [ "obj-10", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-12", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-12", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-13", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 378.5, 200.5, 253.5, 200.5 ]
}
}
, {
"patchline" : {
"source" : [ "obj-19", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 358.5, 228.5, 253.5, 228.5 ]
}
}
, {
"patchline" : {
"source" : [ "obj-21", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 336.5, 251.5, 253.5, 251.5 ]
}
}
, {
"patchline" : {
"source" : [ "obj-30", 0 ],
"destination" : [ "obj-21", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-30", 1 ],
"destination" : [ "obj-19", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-30", 0 ],
"hidden" : 0,
"midpoints" : [ 253.0, 71.0, 336.5, 71.0 ]
}
}
]
}
*/

View File

@ -0,0 +1,51 @@
/*
MIDI note player
This sketch shows how to use Serial1 (pins 7 and 8) to send MIDI
note data. If this circuit is connected to a MIDI synth, it will
play the notes F#-0 (0x1E) to F#-5 (0x5A) in sequence.
The circuit:
* Pin 7 connected to MIDI jack pin 5
* MIDI jack pin 2 connected to ground
* MIDI jack pin 4 connected to +5V through 220-ohm resistor
Attach a MIDI cable to the jack, then to a MIDI synth, and play music.
created 13 Jun 2006
modified 2 Jul 2009
by Tom Igoe
http://www.arduino.cc/en/Tutorial/MIDI
Ported to the Maple 27 May 2010
by Bryan Newbold
*/
void setup() {
// Set MIDI baud rate:
Serial1.begin(31250);
}
void loop() {
// Play notes from F#-0 (0x1E) to F#-5 (0x5A):
for (int note = 0x1E; note < 0x5A; note++) {
// Note on channel 1 (0x90), some note value (note), middle
// velocity (0x45):
noteOn(0x90, note, 0x45);
delay(100);
// Note on channel 1 (0x90), some note value (note), silent
// velocity (0x00):
noteOn(0x90, note, 0x00);
delay(100);
}
}
// Plays a MIDI note. Doesn't check to see that cmd is greater than
// 127, or that data values are less than 127:
void noteOn(int cmd, int pitch, int velocity) {
Serial1.print(cmd, BYTE);
Serial1.print(pitch, BYTE);
Serial1.print(velocity, BYTE);
}

View File

@ -0,0 +1,706 @@
/*
Physical Pixel
An example of using the Maple to receive data from the
computer. In this case, the Maple turns on an LED when
it receives the character 'H', and turns off the LED when it
receives the character 'L'.
The data can be sent from the Maple IDE serial monitor, or another
program like Processing (see code below), Flash (via a serial-net
proxy), PD, or Max/MSP.
No external hardware required (besides a USB cable).
created 2006
by David A. Mellis
modified 14 Apr 2009
by Tom Igoe and Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/PhysicalPixel
Ported to the Maple 27 May 2010
By Bryan Newbold
*/
int incomingByte; // a variable to read incoming serial data into
void setup() {
// Initialize the built-in LED pin as an output:
pinMode(BOARD_LED_PIN, OUTPUT);
}
void loop() {
// See if there's incoming serial data:
if (SerialUSB.available() > 0) {
// Read the oldest byte in the serial buffer:
incomingByte = SerialUSB.read();
// If it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(BOARD_LED_PIN, HIGH);
}
// If it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(BOARD_LED_PIN, LOW);
}
}
}
/* Processing code for this example
// mouseover serial
// Demonstrates how to send data to the Arduino I/O board, in order to
// turn ON a light if the mouse is over a square and turn it off
// if the mouse is not.
// created 2003-4
// based on examples by Casey Reas and Hernando Barragan
// modified 18 Jan 2009
// by Tom Igoe
import processing.serial.*;
float boxX;
float boxY;
int boxSize = 20;
boolean mouseOverBox = false;
Serial port;
void setup() {
size(200, 200);
boxX = width/2.0;
boxY = height/2.0;
rectMode(RADIUS);
// List all the available serial ports in the output pane.
// You will need to choose the port that the Arduino board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());
// Open the port that the Arduino board is connected to (in this case #0)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
background(0);
// Test if the cursor is over the box
if (mouseX > boxX-boxSize && mouseX < boxX+boxSize &&
mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
mouseOverBox = true;
// draw a line around the box and change its color:
stroke(255);
fill(153);
// send an 'H' to indicate mouse is over square:
port.write('H');
}
else {
// return the box to it's inactive state:
stroke(153);
fill(153);
// send an 'L' to turn the LED off:
port.write('L');
mouseOverBox = false;
}
// Draw the box
rect(boxX, boxY, boxSize, boxSize);
}
*/
/*
{
"boxes" : [ {
"box" : {
"maxclass" : "comment",
"text" : "Physical Pixel\n\nThis patch sends an ASCII H or an ASCII L out the serial port to turn on an LED attached to an Arduino board. It can also send alternating H and L characters once every second to make the LED blink.\n\ncreated 2006\nby David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
"linecount" : 11,
"patching_rect" : [ 14.0, 35.0, 354.0, 158.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-1",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click to blink every second",
"patching_rect" : [ 99.0, 251.0, 161.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-38",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "toggle",
"patching_rect" : [ 74.0, 251.0, 21.0, 21.0 ],
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-39",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "p blink",
"patching_rect" : [ 74.0, 286.0, 45.0, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "int" ],
"id" : "obj-37",
"fontname" : "Arial",
"numinlets" : 2,
"patcher" : {
"fileversion" : 1,
"rect" : [ 54.0, 94.0, 640.0, 480.0 ],
"bglocked" : 0,
"defrect" : [ 54.0, 94.0, 640.0, 480.0 ],
"openrect" : [ 0.0, 0.0, 0.0, 0.0 ],
"openinpresentation" : 0,
"default_fontsize" : 10.0,
"default_fontface" : 0,
"default_fontname" : "Verdana",
"gridonopen" : 0,
"gridsize" : [ 25.0, 25.0 ],
"gridsnaponopen" : 0,
"toolbarvisible" : 1,
"boxanimatetime" : 200,
"imprint" : 0,
"boxes" : [ {
"box" : {
"maxclass" : "newobj",
"text" : "* 1000",
"patching_rect" : [ 200.0, 150.0, 46.0, 19.0 ],
"numoutlets" : 1,
"fontsize" : 10.0,
"outlettype" : [ "int" ],
"id" : "obj-12",
"fontname" : "Verdana",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "inlet",
"patching_rect" : [ 200.0, 75.0, 25.0, 25.0 ],
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-11",
"numinlets" : 0,
"comment" : ""
}
}
, {
"box" : {
"maxclass" : "toggle",
"patching_rect" : [ 125.0, 250.0, 20.0, 20.0 ],
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-10",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "metro 1000",
"patching_rect" : [ 115.0, 190.0, 69.0, 19.0 ],
"numoutlets" : 1,
"fontsize" : 10.0,
"outlettype" : [ "bang" ],
"id" : "obj-3",
"fontname" : "Verdana",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "outlet",
"patching_rect" : [ 125.0, 400.0, 25.0, 25.0 ],
"numoutlets" : 0,
"id" : "obj-2",
"numinlets" : 1,
"comment" : ""
}
}
, {
"box" : {
"maxclass" : "inlet",
"patching_rect" : [ 100.0, 25.0, 25.0, 25.0 ],
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-1",
"numinlets" : 0,
"comment" : ""
}
}
],
"lines" : [ {
"patchline" : {
"source" : [ "obj-12", 0 ],
"destination" : [ "obj-3", 1 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-12", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-1", 0 ],
"destination" : [ "obj-3", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-10", 0 ],
"destination" : [ "obj-2", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-3", 0 ],
"destination" : [ "obj-10", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
]
}
,
"saved_object_attributes" : {
"fontface" : 0,
"fontsize" : 10.0,
"default_fontface" : 0,
"default_fontname" : "Verdana",
"default_fontsize" : 10.0,
"fontname" : "Verdana",
"globalpatchername" : ""
}
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "convert to int",
"patching_rect" : [ 154.0, 386.0, 104.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-36",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "send L if 0, H if 1",
"patching_rect" : [ 154.0, 361.0, 104.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-35",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "is it on or off?",
"patching_rect" : [ 179.0, 336.0, 95.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-34",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "atoi",
"patching_rect" : [ 279.0, 386.0, 46.0, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "list" ],
"id" : "obj-33",
"fontname" : "Arial",
"numinlets" : 3
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "H",
"patching_rect" : [ 329.0, 361.0, 32.5, 17.0 ],
"numoutlets" : 1,
"fontsize" : 10.0,
"outlettype" : [ "" ],
"id" : "obj-32",
"fontname" : "Verdana",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "L",
"patching_rect" : [ 279.0, 361.0, 32.5, 17.0 ],
"numoutlets" : 1,
"fontsize" : 10.0,
"outlettype" : [ "" ],
"id" : "obj-31",
"fontname" : "Verdana",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "select 0 1",
"patching_rect" : [ 279.0, 336.0, 62.0, 20.0 ],
"numoutlets" : 3,
"fontsize" : 12.0,
"outlettype" : [ "bang", "bang", "" ],
"id" : "obj-25",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click to turn the LED on and off",
"linecount" : 2,
"patching_rect" : [ 130.0, 205.0, 143.0, 34.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-24",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "toggle",
"patching_rect" : [ 279.0, 211.0, 24.0, 24.0 ],
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-23",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "select 0 1",
"patching_rect" : [ 381.0, 331.0, 62.0, 20.0 ],
"numoutlets" : 3,
"fontsize" : 12.0,
"outlettype" : [ "bang", "bang", "" ],
"id" : "obj-30",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "click here to close the serial port",
"patching_rect" : [ 429.0, 422.0, 206.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-26",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "click here to open the serial port",
"patching_rect" : [ 454.0, 396.0, 206.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-27",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "close",
"patching_rect" : [ 381.0, 422.0, 39.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-21",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "port a",
"patching_rect" : [ 403.0, 396.0, 41.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-19",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click here to get a list of serial ports",
"patching_rect" : [ 474.0, 370.0, 207.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-2",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "toggle",
"patching_rect" : [ 381.0, 181.0, 21.0, 21.0 ],
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-11",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "print",
"patching_rect" : [ 423.0, 370.0, 36.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-13",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "serial a 9600",
"patching_rect" : [ 279.0, 461.0, 84.0, 20.0 ],
"numoutlets" : 2,
"fontsize" : 12.0,
"outlettype" : [ "int", "" ],
"id" : "obj-14",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click to start",
"patching_rect" : [ 408.0, 181.0, 117.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-17",
"fontname" : "Arial",
"numinlets" : 1
}
}
],
"lines" : [ {
"patchline" : {
"source" : [ "obj-39", 0 ],
"destination" : [ "obj-37", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-37", 0 ],
"destination" : [ "obj-25", 0 ],
"hidden" : 0,
"midpoints" : [ 83.5, 320.5, 288.5, 320.5 ]
}
}
, {
"patchline" : {
"source" : [ "obj-33", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-32", 0 ],
"destination" : [ "obj-33", 0 ],
"hidden" : 0,
"midpoints" : [ 338.5, 381.5, 288.5, 381.5 ]
}
}
, {
"patchline" : {
"source" : [ "obj-31", 0 ],
"destination" : [ "obj-33", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-25", 0 ],
"destination" : [ "obj-31", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-25", 1 ],
"destination" : [ "obj-32", 0 ],
"hidden" : 0,
"midpoints" : [ 310.0, 358.0, 338.5, 358.0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-23", 0 ],
"destination" : [ "obj-25", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-13", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 432.5, 389.0, 367.0, 389.0, 367.0, 411.0, 288.5, 411.0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-19", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 412.5, 417.0, 288.5, 417.0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-21", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 390.5, 450.0, 288.5, 450.0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-30", 0 ],
"destination" : [ "obj-21", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-30", 1 ],
"destination" : [ "obj-19", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-30", 0 ],
"hidden" : 0,
"midpoints" : [ 390.5, 322.0, 390.5, 322.0 ]
}
}
]
}
*/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,30 @@
/*
Multiple serial test
Receives from Serial1, sends to SerialUSB.
The circuit:
* Maple connected over SerialUSB
* Serial device (e.g. an Xbee radio, another Maple)
created 30 Dec. 2008
by Tom Igoe
Ported to the Maple 27 May 2010
by Bryan Newbold
*/
int inByte; // Byte read from Serial1
void setup() {
// Initialize Serial1
Serial1.begin(9600);
}
void loop() {
// Read from Serial1, send over USB:
if (Serial1.available()) {
inByte = Serial1.read();
SerialUSB.print(inByte, BYTE);
}
}

View File

@ -0,0 +1,702 @@
/*
VirtualColorMixer
This example reads three analog sensors (potentiometers are easiest)
and sends their values serially. The Processing and Max/MSP programs
at the bottom take those three values and use them to change the
background color of the screen.
The circuit:
* potentiometers attached to pins 15, 16, and 17
http://www.arduino.cc/en/Tutorial/VirtualColorMixer
created 2 Dec 2006
by David A. Mellis
modified 14 Apr 2009
by Tom Igoe and Scott Fitzgerald
Ported to the Maple 27 May 2010
by Bryan Newbold
*/
const int redPin = 15; // sensor to control red color
const int greenPin = 16; // sensor to control green color
const int bluePin = 17; // sensor to control blue color
void setup() {
pinMode(redPin, INPUT_ANALOG);
pinMode(greenPin, INPUT_ANALOG);
pinMode(bluePin, INPUT_ANALOG);
}
void loop() {
SerialUSB.print(analogRead(redPin));
SerialUSB.print(",");
SerialUSB.print(analogRead(greenPin));
SerialUSB.print(",");
SerialUSB.println(analogRead(bluePin));
}
/* Processing code for this example
import processing.serial.*;
float redValue = 0; // red value
float greenValue = 0; // green value
float blueValue = 0; // blue value
Serial myPort;
void setup() {
size(200, 200);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
}
void draw() {
// set the background color with the color values:
background(redValue, greenValue, blueValue);
}
void serialEvent(Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the
// resulting substrings into an integer array:
float[] colors = float(split(inString, ","));
// if the array has at least three elements, you know
// you got the whole thing. Put the numbers in the
// color variables:
if (colors.length >=3) {
// map them to the range 0-255:
redValue = map(colors[0], 0, 1023, 0, 255);
greenValue = map(colors[1], 0, 1023, 0, 255);
blueValue = map(colors[2], 0, 1023, 0, 255);
}
}
}
*/
/* Max/MSP patch for this example
{
"boxes" : [ {
"box" : {
"maxclass" : "newobj",
"text" : "/ 4",
"patching_rect" : [ 448.0, 502.0, 32.5, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "int" ],
"id" : "obj-25",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "/ 4",
"patching_rect" : [ 398.0, 502.0, 32.5, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "int" ],
"id" : "obj-24",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "/ 4",
"patching_rect" : [ 348.0, 502.0, 32.5, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "int" ],
"id" : "obj-23",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Virtual color mixer\n\nThis patch takes a string, containing three comma-separated ASCII formatted numbers from 0 to 1023, with a carriage return and linefeed at the end. It converts the string to three integers and uses them to set the background color.\n\n created 2 Dec 2006\n by David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
"linecount" : 11,
"patching_rect" : [ 524.0, 51.0, 398.0, 158.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-32",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "select 0 1",
"patching_rect" : [ 372.0, 125.0, 62.0, 20.0 ],
"numoutlets" : 3,
"fontsize" : 12.0,
"outlettype" : [ "bang", "bang", "" ],
"id" : "obj-30",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "click here to close the serial port",
"patching_rect" : [ 457.0, 276.0, 206.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-26",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "click here to open the serial port",
"patching_rect" : [ 457.0, 250.0, 206.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-27",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "close",
"patching_rect" : [ 372.0, 276.0, 39.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-21",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "port a",
"patching_rect" : [ 394.0, 250.0, 41.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-19",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click here to get a list of serial ports",
"patching_rect" : [ 457.0, 224.0, 207.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-2",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Convert ASCII to symbol",
"patching_rect" : [ 424.0, 423.0, 147.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-4",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Convert integer to ASCII",
"patching_rect" : [ 424.0, 400.0, 147.0, 20.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-5",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "fromsymbol",
"patching_rect" : [ 347.0, 423.0, 74.0, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-7",
"fontname" : "Arial",
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "itoa",
"patching_rect" : [ 347.0, 400.0, 46.0, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "int" ],
"id" : "obj-8",
"fontname" : "Arial",
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
"numinlets" : 3
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "zl group",
"patching_rect" : [ 347.0, 377.0, 53.0, 20.0 ],
"numoutlets" : 2,
"fontsize" : 12.0,
"outlettype" : [ "", "" ],
"id" : "obj-9",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "select 10 13",
"patching_rect" : [ 289.0, 326.0, 77.0, 20.0 ],
"numoutlets" : 3,
"fontsize" : 12.0,
"outlettype" : [ "bang", "bang", "" ],
"id" : "obj-10",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "toggle",
"patching_rect" : [ 289.0, 88.0, 15.0, 15.0 ],
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-11",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "qmetro 10",
"patching_rect" : [ 289.0, 125.0, 65.0, 20.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "bang" ],
"id" : "obj-12",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "print",
"patching_rect" : [ 414.0, 224.0, 36.0, 18.0 ],
"numoutlets" : 1,
"fontsize" : 12.0,
"outlettype" : [ "" ],
"id" : "obj-13",
"fontname" : "Arial",
"numinlets" : 2
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "serial a 9600",
"patching_rect" : [ 289.0, 300.0, 84.0, 20.0 ],
"numoutlets" : 2,
"fontsize" : 12.0,
"outlettype" : [ "int", "" ],
"id" : "obj-14",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Read serial input buffer every 10 milliseconds",
"linecount" : 2,
"patching_rect" : [ 98.0, 117.0, 185.0, 34.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-15",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "If you get newline (ASCII 10), send the list. If you get return (ASCII 13) do nothing. Any other value, add to the list",
"linecount" : 3,
"patching_rect" : [ 377.0, 314.0, 320.0, 48.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-16",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click to open/close serial port and start/stop patch",
"linecount" : 2,
"patching_rect" : [ 316.0, 77.0, 199.0, 34.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-17",
"fontname" : "Arial",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "bgcolor 0 0 0",
"patching_rect" : [ 348.0, 585.0, 169.0, 19.0 ],
"numoutlets" : 0,
"fontsize" : 10.0,
"id" : "obj-6",
"fontname" : "Verdana",
"numinlets" : 4
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "unpack 0 0 0 0 0",
"patching_rect" : [ 347.0, 470.0, 119.0, 19.0 ],
"numoutlets" : 5,
"fontsize" : 10.0,
"outlettype" : [ "int", "int", "int", "int", "int" ],
"id" : "obj-20",
"fontname" : "Verdana",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 448.0, 535.0, 50.0, 19.0 ],
"numoutlets" : 2,
"fontsize" : 10.0,
"outlettype" : [ "int", "bang" ],
"id" : "obj-18",
"fontname" : "Verdana",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 398.0, 535.0, 50.0, 19.0 ],
"numoutlets" : 2,
"fontsize" : 10.0,
"outlettype" : [ "int", "bang" ],
"id" : "obj-1",
"fontname" : "Verdana",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 348.0, 535.0, 50.0, 19.0 ],
"numoutlets" : 2,
"fontsize" : 10.0,
"outlettype" : [ "int", "bang" ],
"id" : "obj-22",
"fontname" : "Verdana",
"numinlets" : 1
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "Here's the numbers from Arduino's analog input",
"linecount" : 3,
"patching_rect" : [ 198.0, 484.0, 138.0, 48.0 ],
"numoutlets" : 0,
"fontsize" : 12.0,
"id" : "obj-3",
"fontname" : "Arial",
"numinlets" : 1
}
}
],
"lines" : [ {
"patchline" : {
"source" : [ "obj-18", 0 ],
"destination" : [ "obj-6", 2 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-1", 0 ],
"destination" : [ "obj-6", 1 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-22", 0 ],
"destination" : [ "obj-6", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-25", 0 ],
"destination" : [ "obj-18", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-20", 4 ],
"destination" : [ "obj-25", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-20", 2 ],
"destination" : [ "obj-24", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-24", 0 ],
"destination" : [ "obj-1", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-23", 0 ],
"destination" : [ "obj-22", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-20", 0 ],
"destination" : [ "obj-23", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-8", 0 ],
"destination" : [ "obj-7", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-14", 0 ],
"destination" : [ "obj-10", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-12", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-12", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-13", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 423.5, 245.5, 298.5, 245.5 ]
}
}
, {
"patchline" : {
"source" : [ "obj-19", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 403.5, 273.5, 298.5, 273.5 ]
}
}
, {
"patchline" : {
"source" : [ "obj-21", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 381.5, 296.5, 298.5, 296.5 ]
}
}
, {
"patchline" : {
"source" : [ "obj-30", 0 ],
"destination" : [ "obj-21", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-30", 1 ],
"destination" : [ "obj-19", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-30", 0 ],
"hidden" : 0,
"midpoints" : [ 298.0, 116.0, 381.5, 116.0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-7", 0 ],
"destination" : [ "obj-20", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-9", 0 ],
"destination" : [ "obj-8", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}
}
, {
"patchline" : {
"source" : [ "obj-10", 0 ],
"destination" : [ "obj-9", 0 ],
"hidden" : 0,
"midpoints" : [ 298.5, 353.0, 356.5, 353.0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-10", 2 ],
"destination" : [ "obj-9", 0 ],
"hidden" : 0,
"midpoints" : [ 356.5, 365.0, 356.5, 365.0 ]
}
}
]
}
*/

View File

@ -0,0 +1,56 @@
/*
Arrays
Demonstrates the use of an array to hold pin numbers in order to
iterate over the pins in a sequence. Lights multiple LEDs in
sequence, then in reverse.
Unlike the for loop tutorial, where the pins have to be
contiguous, here the pins can be in any random order.
The circuit:
* LEDs from pins 2 through 7 to ground, through resistors
created 2006
by David A. Mellis
modified 5 Jul 2009
by Tom Igoe
modifed for Maple
by LeafLabs
http://leaflabs.com/docs/lang/cpp/array.html
*/
int delayTime = 100; // The higher the number, the slower the timing.
int ledPins[] = {
2, 7, 4, 6, 5, 3 }; // An array of pin numbers to which LEDs are attached
int pinCount = 6; // The number of pins (i.e. the length of the array)
void setup() {
int thisPin;
// The array elements are numbered from 0 to (pinCount - 1).
// Use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// Loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// Turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(delayTime);
// Turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// Loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// Turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(delayTime);
// Turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}

View File

@ -0,0 +1,48 @@
/*
for loop iteration
Demonstrates the use of a for() loop.
Lights multiple LEDs in sequence, then in reverse.
The circuit:
* LEDs from pins 2 through 7 to ground, through resistors
created 2006
by David A. Mellis
modified 5 Jul 2009
by Tom Igoe
http://leaflabs.com/docs/lang/cpp/for.html
Modified for Maple
by LeafLabs
*/
int delayTime = 100; // The higher the number, the slower the timing.
void setup() {
// Use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin <= 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// Loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin <= 7; thisPin++) {
// Turn the pin on:
digitalWrite(thisPin, HIGH);
delay(delayTime);
// Turn the pin off:
digitalWrite(thisPin, LOW);
}
// Loop from the highest pin to the lowest:
for (int thisPin = 7; thisPin >= 2; thisPin--) {
// Turn the pin on:
digitalWrite(thisPin, HIGH);
delay(delayTime);
// Turn the pin off:
digitalWrite(thisPin, LOW);
}
}

View File

@ -0,0 +1,52 @@
/*
Conditionals - If statement
This example demonstrates the use of if() statements. It reads the
state of a potentiometer (an analog input) and turns on an LED only
if the LED goes above a certain threshold level. It prints the
analog value regardless of the level.
The circuit:
* Potentiometer connected to pin 15.
Center pin of the potentiometer goes to the Maple pin.
Side pins of the potentiometer go to +3.3V and ground
created 17 Jan 2009
by Tom Igoe
Ported to the Maple 27 May 2010
by Bryan Newbold
http://leaflabs.com/docs/lang/cpp/if.html
*/
// These constants won't change:
const int analogPin = 15; // Pin that the sensor is attached to
const int threshold = 400; // A random threshold level that's in
// the range of the analog input
void setup() {
// Initialize the built-in LED pin as an output:
pinMode(BOARD_LED_PIN, OUTPUT);
// Initialize the potentiometer pin as an analog input:
pinMode(analogPin, INPUT_ANALOG);
}
void loop() {
// Read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// If the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(BOARD_LED_PIN, HIGH);
}
else {
digitalWrite(BOARD_LED_PIN, LOW);
}
// Print the analog value:
SerialUSB.println(analogValue, DEC);
}

View File

@ -0,0 +1,82 @@
/*
Conditionals - while statement
This example demonstrates the use of while() statements.
While the built-in button is pressed, the sketch runs the
calibration routine. The sensor readings during the while loop
define the minimum and maximum of expected values from the photo
resistor.
This is a variation of the Analog > Calibration example.
The circuit:
* Photo resistor connected from +3.3V to pin 15
* 10K resistor connected from ground to pin 15
* LED connected from digital pin 9 to ground through 220 ohm resistor
* 10K resistor attached from pin 2 to ground
created 17 Jan 2009
modified 25 Jun 2009
by Tom Igoe
modified for Maple 13 February 2011
by LeafLabs
http://leaflabs.com/docs/lang/cpp/while.html
*/
// These constants won't change:
const int sensorPin = 2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
// These variables will change:
int sensorMin = 4095; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sensorValue = 0; // the sensor value
void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(ledPin, OUTPUT); // LED on pin 9
pinMode(BOARD_LED_PIN, OUTPUT); // Built-in LED
pinMode(BOARD_BUTTON_PIN, INPUT); // Built-in button
}
void loop() {
// while the button is pressed, take calibration readings:
while (digitalRead(BOARD_BUTTON_PIN) == HIGH) {
// You could also use this:
//while (isButtonPressed()) {
calibrate();
}
// signal the end of the calibration period
digitalWrite(BOARD_LED_PIN, LOW);
// read the sensor:
sensorValue = analogRead(sensorPin);
// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 65535);
// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 65535);
// fade the LED using the calibrated value:
pwmWrite(ledPin, sensorValue);
}
void calibrate() {
// turn on the built-in LED to indicate that calibration is happening:
digitalWrite(BOARD_LED_PIN, HIGH);
// read the sensor:
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

View File

@ -0,0 +1,54 @@
/*
Switch statement
Demonstrates the use of a switch statement. The switch statement
allows you to choose from among a set of discrete values of a
variable. It's like a series of if statements.
To see this sketch in action, but the board and sensor in a well-lit
room, open the serial monitor, and and move your hand gradually down
over the sensor.
The circuit:
* photoresistor from analog in 0 to +5V
* 10K resistor from analog in 0 to ground
created 1 Jul 2009
by Tom Igoe
Ported to the Maple 27 May 2010
by Bryan Newbold
http://leaflabs.com/docs/lang/cpp/switchcase.html
*/
// These constants won't change:
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 600; // sensor maximum, discovered through experiment
void setup() {
pinMode(0, INPUT_ANALOG);
}
void loop() {
// Read the sensor:
int sensorReading = analogRead(0);
// Map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// Do something different depending on the range value:
switch (range) {
case 0: // your hand is on the sensor
SerialUSB.println("dark");
break;
case 1: // your hand is close to the sensor
SerialUSB.println("dim");
break;
case 2: // your hand is a few inches from the sensor
SerialUSB.println("medium");
break;
case 3: // your hand is nowhere near the sensor
SerialUSB.println("bright");
break;
}
}

View File

@ -0,0 +1,63 @@
/*
Switch statement with serial input
Demonstrates the use of a switch statement. The switch statement
allows you to choose from among a set of discrete values of a
variable. It's like a series of if statements.
To see this sketch in action, open the Serial monitor and send any
character. The characters a, b, c, d, and e, will turn on LEDs.
Any other character will turn the LEDs off.
The circuit:
* 5 LEDs attached to pins 2 through 6 through 220-ohm resistors
created 1 Jul 2009
by Tom Igoe
Ported to the Maple 27 May 2010
by Bryan Newbold
http://leaflabs.com/docs/lang/cpp/switchcase.html
*/
void setup() {
// Initialize the LED pins:
for (int thisPin = 2; thisPin <= 6; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// Read the sensor:
if (SerialUSB.available() > 0) {
int inByte = SerialUSB.read();
// Do something different depending on the character received.
// The switch statement expects single number values for each
// case; in this example, though, you're using single quotes
// to tell the controller to get the ASCII value for the
// character. For example 'a' = 97, 'b' = 98, and so forth:
switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(6, HIGH);
break;
default:
// Turn all the LEDs off:
for (int thisPin = 2; thisPin < 7; thisPin++) {
digitalWrite(thisPin, LOW);
}
}
}
}

View File

@ -0,0 +1,19 @@
/*
Blink
Turns on the built-in LED on for one second, then off for one second,
repeatedly.
Ported to Maple from the Arduino example 27 May 2011
By Marti Bolivar
*/
void setup() {
// Set up the built-in LED pin as an output:
pinMode(BOARD_LED_PIN, OUTPUT);
}
void loop() {
toggleLED(); // Turn the LED from off to on, or on to off
delay(1000); // Wait for 1 second (1000 milliseconds)
}

View File

@ -0,0 +1,37 @@
/*
Blink without delay
Turns on and off the built-in light emitting diode (LED), without
using the delay() function. This means that other code can run at
the same time without being interrupted by the LED code.
created 2005
by David A. Mellis
modified 17 Jun 2009
by Tom Igoe
modified for Maple 27 May 2011
by Marti Bolivar
*/
// Variables:
int previousMillis = 0; // will store the last time the LED was updated
int interval = 1000; // interval at which to blink (in milliseconds)
void setup() {
// Set up the built-in LED pin as output:
pinMode(BOARD_LED_PIN, OUTPUT);
}
void loop() {
// Check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time we blinked
// the LED is bigger than the interval at which we want to blink
// the LED.
if (millis() - previousMillis > interval) {
// Save the last time you blinked the LED
previousMillis = millis();
// If the LED is off, turn it on, and vice-versa:
toggleLED();
}
}

View File

@ -0,0 +1,24 @@
/*
Button
Turns on and off the built-in LED when the built-in button is
pressed.
Ported to Maple from the Arduino example 27 May 2011
by Marti Bolivar
*/
void setup() {
// Initialize the built-in LED pin as an output:
pinMode(BOARD_LED_PIN, OUTPUT);
// Initialize the built-in button (labeled BUT) as an input:
pinMode(BOARD_BUTTON_PIN, INPUT);
}
void loop() {
// Check if the button is pressed.
if (isButtonPressed()) {
// If so, turn the LED from on to off, or from off to on:
toggleLED();
}
}

View File

@ -0,0 +1,55 @@
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).
created 21 November 2006
by David A. Mellis
modified 3 Jul 2009
by Limor Fried
modified 15 Jul 2010
by Bryan Newbold; thanks adamfeuer!
*/
// Variables:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int lastDebounceTime = 0; // the last time the output pin was toggled
int debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(BOARD_BUTTON_PIN, INPUT);
pinMode(BOARD_LED_PIN, OUTPUT);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(BOARD_BUTTON_PIN);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
// set the LED using the state of the button:
digitalWrite(BOARD_LED_PIN, buttonState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}

View File

@ -0,0 +1,69 @@
/*
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the
time, but you just need to know when the input changes from one state
to another. For example, you want to know when a button goes from
OFF to ON. This is called state change detection, or edge detection.
This example shows how to detect when the built-in button changes
from off to on and on to off.
To use this example, connect to the Maple using the USB serial port.
Then push the button a few times and see what happens.
created 27 Sep 2005
modified 30 Dec 2009
by Tom Igoe
Ported to the Maple 27 May 2010
by Bryan Newbold
*/
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(BOARD_BUTTON_PIN, INPUT);
// initialize the LED as an output:
pinMode(BOARD_LED_PIN, OUTPUT);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(BOARD_BUTTON_PIN);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH, then the button went from
// off to on:
buttonPushCounter++;
SerialUSB.println("on");
SerialUSB.print("number of button pushes: ");
SerialUSB.println(buttonPushCounter, DEC);
}
else {
// if the current state is LOW, then the button went from
// on to off:
SerialUSB.println("off");
}
// save the current state as the last state, for next time
// through the loop
lastButtonState = buttonState;
}
// turns on the LED every four button pushes by checking the
// modulo of the button push counter. Modulo (percent sign, %)
// gives you the remainder of the division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(BOARD_LED_PIN, HIGH);
} else {
digitalWrite(BOARD_LED_PIN, LOW);
}
}

View File

@ -0,0 +1,121 @@
/*
Row-Column Scanning an 8x8 LED matrix with X-Y input
This example controls an 8x8 LED matrix using two analog inputs
This example works for the Lumex LDM-24488NI Matrix. See
http://sigma.octopart.com/140413/datasheet/Lumex-LDM-24488NI.pdf
for the pin connections
For other LED cathode column matrixes, you should only need to change
the pin numbers in the row[] and column[] arrays
rows are the anodes
cols are the cathodes
---------
Pin numbers:
Matrix:
* Digital pins 2 through 13,
* analog pins 2 through 5 used as digital 16 through 19
Potentiometers:
* center pins are attached to analog pins 0 and 1, respectively
* side pins attached to +5V and ground, respectively.
http://www.arduino.cc/en/Tutorial/RowColumnScanning
see also http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more
created 27 May 2009
modified 29 Jun 2009
by Tom Igoe
modified for Maple
by LeafLabs
*/
// 2-dimensional array of row pin numbers:
const int row[8] = {
2, 7, 19, 5, 13, 18, 12, 16 };
// 2-dimensional array of column pin numbers:
const int col[8] = {
6, 11, 10, 3, 17, 4, 8, 9 };
// Analog input pins:
const int analogPin1 = 0;
const int analogPin2 = 1;
// 2-dimensional array of pixels:
int pixels[8][8];
// cursor position:
int x = 5;
int y = 5;
void setup() {
// initialize the analog input pins as INPUT_ANALOG
pinMode(analogPin1, INPUT_ANALOG);
pinMode(analogPin2, INPUT_ANALOG);
// initialize the I/O pins as outputs:
// iterate over the pins:
for (int thisPin = 0; thisPin < 8; thisPin++) {
// initialize the output pins:
pinMode(col[thisPin], OUTPUT);
pinMode(row[thisPin], OUTPUT);
// take the col pins (i.e. the cathodes) high to ensure that
// the LEDS are off:
digitalWrite(col[thisPin], HIGH);
}
// initialize the pixel matrix:
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
pixels[x][y] = HIGH;
}
}
}
void loop() {
// read input:
readSensors();
// draw the screen:
refreshScreen();
}
void readSensors() {
// turn off the last position:
pixels[x][y] = HIGH;
// read the sensors for X and Y values:
x = 7 - map(analogRead(analogPin1), 0, 4095, 0, 7);
y = map(analogRead(analogPin2), 0, 4095, 0, 7);
// set the new pixel position low so that the LED will turn on
// in the next screen refresh:
pixels[x][y] = LOW;
}
void refreshScreen() {
// iterate over the rows (anodes):
for (int thisRow = 0; thisRow < 8; thisRow++) {
// take the row pin (anode) high:
digitalWrite(row[thisRow], HIGH);
// iterate over the cols (cathodes):
for (int thisCol = 0; thisCol < 8; thisCol++) {
// get the state of the current pixel;
int thisPixel = pixels[thisRow][thisCol];
// when the row is HIGH and the col is LOW,
// the LED where they meet turns on:
digitalWrite(col[thisCol], thisPixel);
// turn the pixel off:
if (thisPixel == LOW) {
digitalWrite(col[thisCol], HIGH);
}
}
// take the row pin low to turn off the whole row:
digitalWrite(row[thisRow], LOW);
}
}

View File

@ -0,0 +1,58 @@
/*
LED bar graph
Turns on a series of LEDs based on the value of an analog sensor.
This is a simple way to make a bar graph display. Though this graph
uses 10 LEDs, you can use any number by changing the LED count and
the pins in the array.
This method can be used to control any series of digital outputs
that depends on an analog input.
The circuit:
* LEDs from pins 2 through 11 to ground
created 26 Jun 2009
by Tom Igoe
modified for Maple
by LeafLabs
http://www.arduino.cc/en/Tutorial/BarGraph
*/
// these constants won't change:
const int analogPin = 0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // an array of pin numbers to which LEDs are attached
void setup() {
// set up analogPin for analog input:
pinMode(analogPin, INPUT_ANALOG);
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 4095, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}

View File

@ -0,0 +1,200 @@
/*
Crude VGA Output
Outputs a red and white leaf to VGA. This implementation is crude and noisy,
but a fun demo. It should run most VGA monitors at 640x480, though it does
not follow the timing spec very carefully. Real twisted or shielded wires,
proper grounding, and not doing this on a breadboard are recommended (but
it seems to work ok without). In short, this example is "unsupported"; this
surely isn't the best way to do VGA video with a Maple, but it demonstrates
the Timer functionality and is a cool hack so here it is.
SerialUSB is disabled to get rid of most interrupts (which mess with timing);
the SysTick is probably the source of the remaining flickers. This means that
you have to use perpetual bootloader or the reset button to flash new
programs.
How to wire this to a VGA port:
D5 via ~200ohms to VGA Red (1)
D6 via ~200ohms to VGA Green (2)
D7 via ~200ohms to VGA Blue (3)
D11 to VGA VSync (14) (swapped?)
D12 to VGA HSync (13) (swapped?)
GND to VGA Ground (5)
GND to VGA Sync Ground (10)
See also:
- notes/vga.txt for timing numbers and more caveats
- http://www-mtl.mit.edu/Courses/6.111/labkit/vga.shtml
- http://pinouts.ru/Video/VGA15_pinout.shtml
- http://www.epanorama.net/documents/pc/vga_timing.html
Created 20 July 2010
By Bryan Newbold for LeafLabs
This code is released with no strings attached.
*/
#define LED_PIN 13
// Pinouts
#define VGA_R 5 // STM32: B6
#define VGA_G 6 // STM32: A8
#define VGA_B 7 // STM32: A9
#define VGA_V 11 // STM32: A6
#define VGA_H 12 // STM32: A7
// These low level macros make GPIO writes much faster
#define VGA_R_HIGH (GPIOB_BASE)->BSRR = BIT(6)
#define VGA_R_LOW (GPIOB_BASE)->BRR = BIT(6)
#define VGA_G_HIGH (GPIOA_BASE)->BSRR = BIT(8)
#define VGA_G_LOW (GPIOA_BASE)->BRR = BIT(8)
#define VGA_B_HIGH (GPIOA_BASE)->BSRR = BIT(9)
#define VGA_B_LOW (GPIOA_BASE)->BRR = BIT(9)
#define VGA_V_HIGH (GPIOA_BASE)->BSRR = BIT(6)
#define VGA_V_LOW (GPIOA_BASE)->BRR = BIT(6)
#define VGA_H_HIGH (GPIOA_BASE)->BSRR = BIT(7)
#define VGA_H_LOW (GPIOA_BASE)->BRR = BIT(7)
void isr_porch(void);
void isr_start(void);
void isr_stop(void);
void isr_update(void);
uint8 toggle;
uint16 x = 0; // X coordinate
uint16 y = 0; // Y coordinate
uint8 v_active = 1; // Are we in the image?
// 1-bit!
uint8 logo[18][16] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,},
{0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,},
{0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,},
{0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,},
{0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,},
{0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,0,},
{0,1,0,0,0,0,1,1,1,0,0,0,0,1,0,0,},
{0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,},
{1,0,0,0,1,0,0,1,0,0,1,0,0,0,1,0,},
{1,0,0,0,0,1,0,1,0,1,0,0,0,0,1,0,},
{1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,0,},
{0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,},
{0,0,1,1,0,0,0,1,0,0,0,1,1,0,0,0,},
{0,0,0,0,1,1,1,0,1,1,1,0,0,0,0,0,},
{0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}, };
void setup() {
// Setup our pins
pinMode(LED_PIN, OUTPUT);
pinMode(VGA_R, OUTPUT);
pinMode(VGA_G, OUTPUT);
pinMode(VGA_B, OUTPUT);
pinMode(VGA_V, OUTPUT);
pinMode(VGA_H, OUTPUT);
digitalWrite(VGA_R, LOW);
digitalWrite(VGA_G, LOW);
digitalWrite(VGA_B, LOW);
digitalWrite(VGA_H, HIGH);
digitalWrite(VGA_V, HIGH);
// This gets rid of the majority of the interrupt artifacts;
// a SysTick.end() is required as well
SerialUSB.end();
// Configure
Timer4.pause(); // while we configure
Timer4.setPrescaleFactor(1); // Full speed
Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE);
Timer4.setChannel2Mode(TIMER_OUTPUTCOMPARE);
Timer4.setChannel3Mode(TIMER_OUTPUTCOMPARE);
Timer4.setChannel4Mode(TIMER_OUTPUTCOMPARE);
Timer4.setOverflow(2287); // Total line time
Timer4.setCompare1(200);
Timer4.attachCompare1Interrupt(isr_porch);
Timer4.setCompare2(300);
Timer4.attachCompare2Interrupt(isr_start);
Timer4.setCompare3(2170);
Timer4.attachCompare3Interrupt(isr_stop);
Timer4.setCompare4(1); // Could be zero I guess
Timer4.attachCompare4Interrupt(isr_update);
Timer4.setCount(0); // Ready...
Timer4.resume(); // Go!
}
void loop() {
toggle ^= 1;
digitalWrite(LED_PIN, toggle);
delay(100);
// Everything happens in the interrupts!
}
// This ISR will end horizontal sync for most of the image and
// setup the vertical sync for higher line counts
void isr_porch(void) {
VGA_H_HIGH;
y++;
// Back to the top
if(y>=523) {
y=1;
v_active = 1;
return;
}
// Other vsync stuff below the image
if(y>=492) {
VGA_V_HIGH;
return;
}
if(y>=490) {
VGA_V_LOW;
return;
}
if(y>=479) {
v_active = 0;
return;
}
}
// This is the main horizontal sweep
void isr_start(void) {
// Skip if we're not in the image at all
if(!v_active) { return; }
// Start Red
VGA_R_LOW;
VGA_R_HIGH;
// For each "pixel" (really 20 or so screen pixels?) go red or white
for(x=0; x<32; x++) {
if(logo[y/28][x/2]) {
VGA_G_HIGH;
VGA_B_HIGH;
} else {
VGA_G_LOW;
VGA_B_LOW;
}
}
}
// End of the horizontal line
void isr_stop(void) {
if(!v_active) { return; }
VGA_R_LOW;
VGA_G_LOW;
VGA_B_LOW;
}
// Setup horizonal sync
void isr_update(void) {
VGA_H_LOW;
}

View File

@ -0,0 +1,724 @@
/*
Interactive Test Session for LeafLabs Maple
Useful for testing Maple features and troubleshooting.
Communicates over SerialUSB.
This code is released into the public domain.
*/
// ASCII escape character
#define ESC ((uint8)27)
// Default USART baud rate
#define BAUD 9600
uint8 gpio_state[BOARD_NR_GPIO_PINS];
const char* dummy_data = ("qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-="
"qwertyuiopasdfghjklzxcvbnm,./1234567890");
// -- setup() and loop() ------------------------------------------------------
void setup() {
// Set up the LED to blink
pinMode(BOARD_LED_PIN, OUTPUT);
// Start up the serial ports
Serial1.begin(BAUD);
Serial2.begin(BAUD);
Serial3.begin(BAUD);
// Send a message out over SerialUSB interface
SerialUSB.println(" ");
SerialUSB.println(" __ __ _ _");
SerialUSB.println(" | \\/ | __ _ _ __ | | ___| |");
SerialUSB.println(" | |\\/| |/ _` | '_ \\| |/ _ \\ |");
SerialUSB.println(" | | | | (_| | |_) | | __/_|");
SerialUSB.println(" |_| |_|\\__,_| .__/|_|\\___(_)");
SerialUSB.println(" |_|");
SerialUSB.println(" by leaflabs");
SerialUSB.println("");
SerialUSB.println("");
SerialUSB.println("Maple interactive test program (type '?' for help)");
SerialUSB.println("----------------------------------------------------------");
SerialUSB.print("> ");
}
void loop () {
toggleLED();
delay(100);
while (SerialUSB.available()) {
uint8 input = SerialUSB.read();
SerialUSB.println(input);
switch(input) {
case '\r':
break;
case ' ':
SerialUSB.println("spacebar, nice!");
break;
case '?':
case 'h':
cmd_print_help();
break;
case 'u':
SerialUSB.println("Hello World!");
break;
case 'w':
Serial1.println("Hello World!");
Serial2.println("Hello World!");
Serial3.println("Hello World!");
break;
case 'm':
cmd_serial1_serial3();
break;
case '.':
while (!SerialUSB.available()) {
Serial1.print(".");
Serial2.print(".");
Serial3.print(".");
SerialUSB.print(".");
}
break;
case 'n':
cmd_adc_stats();
break;
case 'N':
cmd_stressful_adc_stats();
break;
case 'e':
cmd_everything();
break;
case 'W':
while (!SerialUSB.available()) {
Serial1.print(dummy_data);
Serial2.print(dummy_data);
Serial3.print(dummy_data);
}
break;
case 'U':
SerialUSB.println("Dumping data to USB. Press any key.");
while (!SerialUSB.available()) {
SerialUSB.print(dummy_data);
}
break;
case 'g':
cmd_sequential_gpio_writes();
break;
case 'G':
cmd_gpio_toggling();
break;
case 'f':
SerialUSB.println("Wiggling D4 as fast as possible in bursts. "
"Press any key.");
pinMode(4, OUTPUT);
while (!SerialUSB.available()) {
fast_gpio(4);
delay(1);
}
break;
case 'p':
cmd_sequential_pwm_test();
break;
case '_':
SerialUSB.println("Delaying for 5 seconds...");
delay(5000);
break;
// Be sure to update cmd_print_help() if you implement these:
case 't': // TODO
SerialUSB.println("Unimplemented.");
break;
case 'T': // TODO
SerialUSB.println("Unimplemented.");
break;
case 's':
cmd_servo_sweep();
break;
case 'd':
SerialUSB.println("Pulling down D4, D22. Press any key.");
pinMode(22, INPUT_PULLDOWN);
pinMode(4, INPUT_PULLDOWN);
while (!SerialUSB.available()) {
continue;
}
SerialUSB.println("Pulling up D4, D22. Press any key.");
pinMode(22, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
while (!SerialUSB.available()) {
continue;
}
SerialUSB.read();
pinMode(4, OUTPUT);
break;
// Be sure to update cmd_print_help() if you implement these:
case 'i': // TODO
SerialUSB.println("Unimplemented.");
break;
case 'I': // TODO
SerialUSB.println("Unimplemented.");
break;
case 'r':
cmd_gpio_monitoring();
break;
case 'a':
cmd_sequential_adc_reads();
break;
case 'b':
cmd_board_info();
break;
case '+':
cmd_gpio_qa();
break;
default: // -------------------------------
SerialUSB.print("Unexpected: ");
SerialUSB.print(input);
SerialUSB.println(", press h for help.");
}
SerialUSB.print("> ");
}
}
// -- Commands ----------------------------------------------------------------
void cmd_print_help(void) {
SerialUSB.println("");
SerialUSB.println("Command Listing");
SerialUSB.println("\t?: print this menu");
SerialUSB.println("\th: print this menu");
SerialUSB.println("\tw: print Hello World on all 3 USARTS");
SerialUSB.println("\tn: measure noise and do statistics");
SerialUSB.println("\tN: measure noise and do statistics with background stuff");
SerialUSB.println("\ta: show realtime ADC info");
SerialUSB.println("\t.: echo '.' until new input");
SerialUSB.println("\tu: print Hello World on USB");
SerialUSB.println("\t_: do as little as possible for a couple seconds (delay)");
SerialUSB.println("\tp: test all PWM channels sequentially");
SerialUSB.println("\tW: dump data as fast as possible on all 3 USARTS");
SerialUSB.println("\tU: dump data as fast as possible on USB");
SerialUSB.println("\tg: toggle GPIOs sequentially");
SerialUSB.println("\tG: toggle GPIOs at the same time");
SerialUSB.println("\tf: toggle pin 4 as fast as possible in bursts");
SerialUSB.println("\tr: monitor and print GPIO status changes");
SerialUSB.println("\ts: output a sweeping servo PWM on all PWM channels");
SerialUSB.println("\tm: output data on USART1 and USART3 with various rates");
SerialUSB.println("\tb: print information about the board.");
SerialUSB.println("\t+: test shield mode (for quality assurance testing)");
SerialUSB.println("Unimplemented:");
SerialUSB.println("\te: do everything all at once until new input");
SerialUSB.println("\tt: output a 1khz squarewave on all GPIOs");
SerialUSB.println("\tT: output a 1hz squarewave on all GPIOs");
SerialUSB.println("\ti: print out a bunch of info about system state");
SerialUSB.println("\tI: print out status of all headers");
}
void cmd_adc_stats(void) {
SerialUSB.println("Taking ADC noise stats.");
digitalWrite(BOARD_LED_PIN, 0);
for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) {
delay(5);
measure_adc_noise(boardADCPins[i]);
}
}
void cmd_stressful_adc_stats(void) {
SerialUSB.println("Taking ADC noise stats under duress.");
for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) {
for (uint32 j = 0; j < BOARD_NR_PWM_PINS; j++) {
if (boardADCPins[i] != boardPWMPins[j]) {
pinMode(boardPWMPins[j], PWM);
pwmWrite(boardPWMPins[j], 1000 + i);
}
}
Serial1.print(dummy_data);
measure_adc_noise(boardADCPins[i]);
for (uint32 j = 0; j < BOARD_NR_PWM_PINS; j++) {
if (boardADCPins[i] != boardPWMPins[j]) {
pinMode(boardPWMPins[j], OUTPUT);
digitalWrite(boardPWMPins[j], LOW);
}
}
}
}
void cmd_everything(void) { // TODO
// Be sure to update cmd_print_help() if you implement this.
// print to usart
// print to usb
// toggle gpios
// enable pwm
SerialUSB.println("Unimplemented.");
}
void cmd_serial1_serial3(void) {
HardwareSerial *serial_1_and_3[] = {&Serial1, &Serial3};
SerialUSB.println("Testing 57600 baud on USART1 and USART3. "
"Press any key to stop.");
usart_baud_test(serial_1_and_3, 2, 57600);
SerialUSB.read();
SerialUSB.println("Testing 115200 baud on USART1 and USART3. "
"Press any key to stop.");
usart_baud_test(serial_1_and_3, 2, 115200);
SerialUSB.read();
SerialUSB.println("Testing 9600 baud on USART1 and USART3. "
"Press any key to stop.");
usart_baud_test(serial_1_and_3, 2, 9600);
SerialUSB.read();
SerialUSB.println("Resetting USART1 and USART3...");
Serial1.begin(BAUD);
Serial3.begin(BAUD);
}
void cmd_gpio_monitoring(void) {
SerialUSB.println("Monitoring pin state changes. Press any key to stop.");
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
pinMode(i, INPUT_PULLDOWN);
gpio_state[i] = (uint8)digitalRead(i);
}
while (!SerialUSB.available()) {
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
uint8 current_state = (uint8)digitalRead(i);
if (current_state != gpio_state[i]) {
SerialUSB.print("State change on pin ");
SerialUSB.print(i, DEC);
if (current_state) {
SerialUSB.println(":\tHIGH");
} else {
SerialUSB.println(":\tLOW");
}
gpio_state[i] = current_state;
}
}
}
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
pinMode(i, OUTPUT);
}
}
void cmd_sequential_adc_reads(void) {
SerialUSB.print("Sequentially reading most ADC ports.");
SerialUSB.println("Press any key for next port, or ESC to stop.");
for (uint32 i = 0; i < BOARD_NR_ADC_PINS; i++) {
if (boardUsesPin(i))
continue;
SerialUSB.print("Reading pin ");
SerialUSB.print(boardADCPins[i], DEC);
SerialUSB.println("...");
pinMode(boardADCPins[i], INPUT_ANALOG);
while (!SerialUSB.available()) {
int sample = analogRead(boardADCPins[i]);
SerialUSB.print(boardADCPins[i], DEC);
SerialUSB.print("\t");
SerialUSB.print(sample, DEC);
SerialUSB.print("\t");
SerialUSB.print("|");
for (int j = 0; j < 4096; j += 100) {
if (sample >= j) {
SerialUSB.print("#");
} else {
SerialUSB.print(" ");
}
}
SerialUSB.print("| ");
for (int j = 0; j < 12; j++) {
if (sample & (1 << (11 - j))) {
SerialUSB.print("1");
} else {
SerialUSB.print("0");
}
}
SerialUSB.println("");
}
pinMode(boardADCPins[i], OUTPUT);
digitalWrite(boardADCPins[i], 0);
if (SerialUSB.read() == ESC)
break;
}
}
bool test_single_pin_is_high(int high_pin, const char* err_msg) {
bool ok = true;
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i)) continue;
if (digitalRead(i) == HIGH && i != high_pin) {
SerialUSB.println();
SerialUSB.print("\t*** FAILURE! pin ");
SerialUSB.print(i, DEC);
SerialUSB.print(' ');
SerialUSB.println(err_msg);
ok = false;
}
}
return ok;
}
bool wait_for_low_transition(uint8 pin) {
uint32 start = millis();
while (millis() - start < 2000) {
if (digitalRead(pin) == LOW) {
return true;
}
}
return false;
}
void cmd_gpio_qa(void) {
bool all_pins_ok = true;
const int not_a_pin = -1;
SerialUSB.println("Doing QA testing for unused GPIO pins.");
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i)) continue;
pinMode(i, INPUT);
}
SerialUSB.println("Waiting to start.");
ASSERT(!boardUsesPin(0));
while (digitalRead(0) == LOW) continue;
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i)) {
SerialUSB.print("Skipping pin ");
SerialUSB.println(i, DEC);
continue;
}
bool pin_ok = true;
SerialUSB.print("Checking pin ");
SerialUSB.print(i, DEC);
while (digitalRead(i) == LOW) continue;
pin_ok = pin_ok && test_single_pin_is_high(i, "is also HIGH");
if (!wait_for_low_transition(i)) {
SerialUSB.println("Transition to low timed out; something is "
"very wrong. Aborting test.");
return;
}
pin_ok = pin_ok && test_single_pin_is_high(not_a_pin, "is still HIGH");
if (pin_ok) {
SerialUSB.println(": ok");
}
all_pins_ok = all_pins_ok && pin_ok;
}
if (all_pins_ok) {
SerialUSB.println("Finished; test passes.");
} else {
SerialUSB.println("**** TEST FAILS *****");
}
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i)) continue;
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
gpio_state[i] = 0;
}
}
void cmd_sequential_gpio_writes(void) {
SerialUSB.println("Sequentially toggling all unused pins. "
"Press any key for next pin, ESC to stop.");
for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
SerialUSB.print("Toggling pin ");
SerialUSB.print((int)i, DEC);
SerialUSB.println("...");
pinMode(i, OUTPUT);
do {
togglePin(i);
} while (!SerialUSB.available());
digitalWrite(i, LOW);
if (SerialUSB.read() == ESC)
break;
}
}
void cmd_gpio_toggling(void) {
SerialUSB.println("Toggling all unused pins simultaneously. "
"Press any key to stop.");
for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
pinMode(i, OUTPUT);
}
while (!SerialUSB.available()) {
for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
togglePin(i);
}
}
for (uint32 i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
digitalWrite(i, LOW);
}
}
void cmd_sequential_pwm_test(void) {
SerialUSB.println("Sequentially testing PWM on all unused pins. "
"Press any key for next pin, ESC to stop.");
for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) {
if (boardUsesPin(i))
continue;
SerialUSB.print("PWM out on header D");
SerialUSB.print(boardPWMPins[i], DEC);
SerialUSB.println("...");
pinMode(boardPWMPins[i], PWM);
pwmWrite(boardPWMPins[i], 16000);
while (!SerialUSB.available()) {
delay(10);
}
pinMode(boardPWMPins[i], OUTPUT);
digitalWrite(boardPWMPins[i], 0);
if (SerialUSB.read() == ESC)
break;
}
}
void cmd_servo_sweep(void) {
SerialUSB.println("Testing all PWM headers with a servo sweep. "
"Press any key to stop.");
SerialUSB.println();
disable_usarts();
init_all_timers(21);
for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) {
if (boardUsesPin(i))
continue;
pinMode(boardPWMPins[i], PWM);
pwmWrite(boardPWMPins[i], 4000);
}
// 1.25ms = 4096counts = 0deg
// 1.50ms = 4915counts = 90deg
// 1.75ms = 5734counts = 180deg
int rate = 4096;
while (!SerialUSB.available()) {
rate += 20;
if (rate > 5734)
rate = 4096;
for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) {
if (boardUsesPin(i))
continue;
pwmWrite(boardPWMPins[i], rate);
}
delay(20);
}
for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) {
if (boardUsesPin(i))
continue;
pinMode(boardPWMPins[i], OUTPUT);
}
init_all_timers(1);
enable_usarts();
}
void cmd_board_info(void) { // TODO print more information
SerialUSB.println("Board information");
SerialUSB.println("=================");
SerialUSB.print("* Clock speed (cycles/us): ");
SerialUSB.println(CYCLES_PER_MICROSECOND);
SerialUSB.print("* BOARD_LED_PIN: ");
SerialUSB.println(BOARD_LED_PIN);
SerialUSB.print("* BOARD_BUTTON_PIN: ");
SerialUSB.println(BOARD_BUTTON_PIN);
SerialUSB.print("* GPIO information (BOARD_NR_GPIO_PINS = ");
SerialUSB.print(BOARD_NR_GPIO_PINS);
SerialUSB.println("):");
print_board_array("ADC pins", boardADCPins, BOARD_NR_ADC_PINS);
print_board_array("PWM pins", boardPWMPins, BOARD_NR_PWM_PINS);
print_board_array("Used pins", boardUsedPins, BOARD_NR_USED_PINS);
}
// -- Helper functions --------------------------------------------------------
void measure_adc_noise(uint8 pin) {
uint16 data[100];
float mean = 0;
float delta = 0;
float M2 = 0;
pinMode(pin, INPUT_ANALOG);
// Variance algorithm from Welford, via Knuth, by way of Wikipedia:
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm
for (int i = 0; i < 100; i++) {
data[i] = analogRead(pin);
delta = data[i] - mean;
mean = mean + delta / (i + 1);
M2 = M2 + delta * (data[i] - mean);
}
SerialUSB.print("header: D");
SerialUSB.print(pin, DEC);
SerialUSB.print("\tn: ");
SerialUSB.print(100, DEC);
SerialUSB.print("\tmean: ");
SerialUSB.print(mean);
SerialUSB.print("\tvariance: ");
SerialUSB.println(M2 / 99.0);
pinMode(pin, OUTPUT);
}
void fast_gpio(int maple_pin) {
gpio_dev *dev = PIN_MAP[maple_pin].gpio_device;
uint32 bit = PIN_MAP[maple_pin].gpio_bit;
gpio_write_bit(dev, bit, 1);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
gpio_toggle_bit(dev, bit);
}
void usart_baud_test(HardwareSerial **serials, int n, unsigned baud) {
for (int i = 0; i < n; i++) {
serials[i]->begin(baud);
}
while (!SerialUSB.available()) {
for (int i = 0; i < n; i++) {
serials[i]->println(dummy_data);
if (serials[i]->available()) {
serials[i]->println(serials[i]->read());
delay(1000);
}
}
}
}
static uint16 init_all_timers_prescale = 0;
static void set_prescale(timer_dev *dev) {
timer_set_prescaler(dev, init_all_timers_prescale);
}
void init_all_timers(uint16 prescale) {
init_all_timers_prescale = prescale;
timer_foreach(set_prescale);
}
void enable_usarts(void) {
// FIXME generalize after USART refactor
Serial1.begin(BAUD);
Serial2.begin(BAUD);
Serial3.begin(BAUD);
}
void disable_usarts(void) {
// FIXME generalize after USART refactor
Serial1.end();
Serial2.end();
Serial3.end();
}
void print_board_array(const char* msg, const uint8 arr[], int len) {
SerialUSB.print("\t");
SerialUSB.print(msg);
SerialUSB.print(" (");
SerialUSB.print(len);
SerialUSB.print("): ");
for (int i = 0; i < len; i++) {
SerialUSB.print(arr[i], DEC);
if (i < len - 1) SerialUSB.print(", ");
}
SerialUSB.println();
}

View File

@ -0,0 +1,66 @@
/*
Slave mode for Quality Assurance test
Used as follows:
1) Connect all non-used pins on the test board to their
corresponding pins on a board running InteractiveTest.
2) Connect a serial monitor to the InteractiveTest board and
enter "+" (a plus sign, without the quotes).
This program pulses each unused pin in order, starting from pin 0.
The InteractiveTest "+" command detects these pulses, and makes
sure that no other pins change state at the same time.
If you hold the button on the board running this program, the
pulses run slower.
Useful as a simple test of functionality for GPIO pins.
*/
#define INTER_TOGGLE_DELAY_NORMAL 5
#define INTER_TOGGLE_DELAY_SLOW 80
void interToggleDelay(void);
void setup() {
pinMode(BOARD_LED_PIN, OUTPUT);
pinMode(BOARD_BUTTON_PIN, INPUT);
// All unused pins start out low.
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
}
void loop() {
toggleLED();
delay(100);
toggleLED();
for (int i = 0; i < BOARD_NR_GPIO_PINS; i++) {
if (boardUsesPin(i))
continue;
// Bring just this pin high.
digitalWrite(i, HIGH);
// Give the master time to detect if any other pins also went high.
interToggleDelay();
// Bring this pin back low again; all pins should now be low.
digitalWrite(i, LOW);
// Give the master time to detect if any pins are still high.
interToggleDelay();
}
}
void interToggleDelay(void) {
if (digitalRead(BOARD_BUTTON_PIN)) { // don't pay the debouncing time
delay(INTER_TOGGLE_DELAY_SLOW);
} else {
delay(INTER_TOGGLE_DELAY_NORMAL);
}
}

View File

@ -0,0 +1,145 @@
/*
SerialUSB Stress Test
Connect via Serial2 (header pins D0 and D1 on the Maple) for the back channel;
otherwise just connect with SerialUSB and press BUT to cycle through (hold
it for at least a second).
The output will be pretty cryptic; see inline comments for more info.
Note that the ONOFF test will result in a hard disconnect which crashes some
serial port monitors and will require reopening that port after 5-10 seconds.
Created 20 July 2010
By Bryan Newbold for LeafLabs
This code is released with no strings attached.
*/
#define LED_PIN 13
#define BUT_PIN 38
uint32 state = 0;
#define QUICKPRINT 0
#define BIGSTUFF 1
#define NUMBERS 2
#define SIMPLE 3
#define ONOFF 4
void setup()
{
// Set up the LED to blink
pinMode(LED_PIN, OUTPUT);
// Set up the Button
pinMode(BUT_PIN, INPUT_PULLUP);
// Setup the back channel
Serial2.begin(9600);
Serial2.println("Hello world! This is the debug channel.");
}
int toggle = 0;
void loop() {
toggle ^= 1;
digitalWrite(LED_PIN, toggle);
delay(300);
// If the button is pressed go to the next state
if(digitalRead(BUT_PIN)) {
while(digitalRead(BUT_PIN)) {};
state++;
}
switch(state) {
// sends more than a buffer worth of characters indefinately.
// this pattern makes it easy to see dropped bytes.
case QUICKPRINT:
for(int i = 0; i<40; i++) {
SerialUSB.print('.');
SerialUSB.print('|');
}
Serial2.println(SerialUSB.pending(),DEC);
SerialUSB.println();
break;
// Send large/log stuff
case BIGSTUFF:
SerialUSB.println("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
SerialUSB.println((uint32)123456789,DEC);
SerialUSB.println(3.1415926535);
Serial2.println(SerialUSB.pending(),DEC);
break;
// Try a bunch of different types and formats
case NUMBERS:
SerialUSB.println("Numbers! -----------------------------");
Serial2.println("Numbers! -----------------------------");
SerialUSB.println('1');
Serial2.println('1');
SerialUSB.println(1,DEC);
Serial2.println(1,DEC);
SerialUSB.println(-1,DEC);
Serial2.println(-1,DEC);
SerialUSB.println(3.14159265);
Serial2.println(3.14159265);
SerialUSB.println(3.14159265,9);
Serial2.println(3.14159265,9);
SerialUSB.println(123456789,DEC);
Serial2.println(123456789,DEC);
SerialUSB.println(-123456789,DEC);
Serial2.println(-123456789,DEC);
SerialUSB.println(65535,HEX);
Serial2.println(65535,HEX);
SerialUSB.println(65535,OCT);
Serial2.println(65535,OCT);
SerialUSB.println(65535,BIN);
Serial2.println(65535,BIN);
break;
// Very basic prints
case SIMPLE:
Serial2.println("Trying write('a')");
SerialUSB.write('a');
Serial2.println("Trying write(\"b\")");
SerialUSB.write("b");
Serial2.println("Trying print('c')");
SerialUSB.print('c');
Serial2.println("Trying print(\"d\")");
SerialUSB.print("d");
Serial2.println("Trying print(\"efg\")");
SerialUSB.print("efg");
Serial2.println("Trying println(\"hij\\n\\r\")");
SerialUSB.print("hij\n\r");
SerialUSB.write(' ');
SerialUSB.println();
Serial2.println("Trying println(123456789,DEC)");
SerialUSB.println(123456789,DEC);
Serial2.println("Trying println(3.141592)");
SerialUSB.println(3.141592);
Serial2.println("Trying println(\"DONE\")");
SerialUSB.println("DONE");
break;
// Disables the USB peripheral, then brings it back up a
// few seconds later
case ONOFF:
Serial2.println("Shutting down...");
SerialUSB.println("Shutting down...");
SerialUSB.end();
Serial2.println("Waiting 4seconds...");
delay(4000);
Serial2.println("Starting up...");
SerialUSB.begin();
SerialUSB.println("Hello World!");
Serial2.println("Waiting 4seconds...");
delay(4000);
state++;
break;
default:
state = 0;
}
}

View File

@ -0,0 +1,86 @@
/*
Timer Interrupts Example
Demonstrates usage of the HardwareTimer classes by blinking the LED
Created 22 April 2010, last updated 8 June 2010
By Bryan Newbold for LeafLabs
This code is released with no strings attached.
*/
#define LED_PIN 13
#define BUTTON_PIN 38
#define LED_RATE 500000 // in microseconds; should give 0.5Hz toggles
void handler_led(void);
void handler_count1(void);
void handler_count2(void);
int toggle = 0;
int count1 = 0;
int count2 = 0;
void setup()
{
// Set up the LED to blink
pinMode(LED_PIN, OUTPUT);
// Set up BUT for input
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Setup LED Timer
Timer2.setChannel1Mode(TIMER_OUTPUTCOMPARE);
Timer2.setPeriod(LED_RATE); // in microseconds
Timer2.setCompare1(1); // overflow might be small
Timer2.attachCompare1Interrupt(handler_led);
// Setup Counting Timers
Timer3.setChannel1Mode(TIMER_OUTPUTCOMPARE);
Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE);
Timer3.pause();
Timer4.pause();
Timer3.setCount(0);
Timer4.setCount(0);
Timer3.setOverflow(30000);
Timer4.setOverflow(30000);
Timer3.setCompare1(1000); // somewhere in the middle
Timer4.setCompare1(1000);
Timer3.attachCompare1Interrupt(handler1);
Timer4.attachCompare1Interrupt(handler2);
Timer3.resume();
Timer4.resume();
}
void loop() {
// Display the running counts
SerialUSB.print("Count 1: ");
SerialUSB.print(count1);
SerialUSB.print("\t\tCount 2: ");
SerialUSB.println(count2);
// Run... while BUT is held, pause Count2
for(int i = 0; i<1000; i++) {
if(digitalRead(BUTTON_PIN)) {
Timer4.pause();
} else {
Timer4.resume();
}
delay(1);
}
}
void handler_led(void) {
toggle ^= 1;
digitalWrite(LED_PIN, toggle);
}
void handler1(void) {
count1++;
}
void handler2(void) {
count2++;
}

View File

@ -0,0 +1,51 @@
/*
Knock Sensor
This sketch reads a piezo element to detect a knocking sound. It
reads an analog pin and compares the result to a set threshold. If
the result is greater than the threshold, it writes "knock" to the
serial port, and toggles the LED on pin 13.
The circuit:
* + connection of the piezo attached to analog in 0
* - connection of the piezo attached to ground
* 1-megohm resistor attached from analog in 0 to ground
http://www.arduino.cc/en/Tutorial/Knock
created 25 Mar 2007
by David Cuartielles <http://www.0j0.org>
modified 30 Jun 2009
by Tom Igoe
Ported to the Maple
by LeafLabs
*/
// these constants won't change:
const int knockSensor = 0; // the piezo is connected to analog pin 0
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
void setup() {
// Declare the knockSensor as an analog input:
pinMode(knockSensor, INPUT_ANALOG);
// declare the built-in LED pin as an output:
pinMode(BOARD_LED_PIN, OUTPUT);
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// toggle the built-in LED
toggleLED();
// send the string "Knock!" back to the computer, followed by newline
SerialUSB.println("Knock!");
}
delay(100); // delay to avoid printing too often
}

View File

@ -0,0 +1,10 @@
void setup() {
pinMode(2, INPUT_ANALOG);
pinMode(6, PWM);
}
void loop() {
int sensorValue = analogRead(2);
int ledFadeValue = map(sensorValue, 0, 4095, 0, 65535);
pwmWrite(6, ledFadeValue);
}

View File

@ -0,0 +1,11 @@
void setup() {
pinMode(0, INPUT_ANALOG);
}
void loop() {
int sensorValue = analogRead(0);
SerialUSB.println(sensorValue, DEC);
}

View File

@ -0,0 +1,9 @@
void setup() {
}
void loop() {
}

View File

@ -0,0 +1,8 @@
void setup() {
pinMode(2, INPUT);
}
void loop() {
int sensorValue = digitalRead(2);
SerialUSB.println(sensorValue, DEC);
}

View File

@ -0,0 +1,9 @@
void setup() {
pinMode(BOARD_LED_PIN, OUTPUT);
}
void loop() {
int switchValue = digitalRead(2);
digitalWrite(BOARD_LED_PIN, switchValue);
}

View File

@ -0,0 +1,7 @@
void setup() {
}
void loop() {
SerialUSB.println("Hello World!");
}

29
examples/pde.txt Normal file
View File

@ -0,0 +1,29 @@
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Analog\AnalogInOutSerial\AnalogInOutSerial.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Analog\AnalogInput\AnalogInput.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Analog\AnalogInSerial\AnalogInSerial.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Analog\Calibration\Calibration.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Analog\Fading\Fading.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Analog\Smoothing\Smoothing.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\ASCIITable\ASCIITable.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\Dimmer\Dimmer.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\Graph\Graph.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\MIDI\Midi.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\PhysicalPixel\PhysicalPixel.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\SerialCallResponse\SerialCallResponse.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\SerialCallResponseASCII\SerialCallResponseASCII.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\SerialPassthrough\SerialPassthrough.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Communication\VirtualColorMixer\VirtualColorMixer.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Control\Arrays\Arrays.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Control\ForLoopIteration\ForLoopIteration.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Control\IfStatementConditional\IfStatementConditional.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Control\switchCase\switchCase.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Control\switchCase2\switchCase2.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Control\WhileStatementConditional\WhileStatementConditional.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Digital\Blink\Blink.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Digital\BlinkWithoutDelay\BlinkWithoutDelay.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Digital\Button\Button.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Digital\Debounce\Debounce.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Digital\StateChangeDetection\StateChangeDetection.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Display\barGraph\barGraph.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Display\RowColumnScanning\RowColumnScanning.pde
c:\Users\rclark\Documents\Arduino\hardware\Arduino_STM32\examples\Maple\CrudeVGA\CrudeVGA.pde