1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-30 16:24:09 +03:00

Merge branch 'master' into wifly_integration

This commit is contained in:
amcewen
2011-08-28 20:28:53 +01:00
196 changed files with 6729 additions and 8770 deletions

View File

@ -26,7 +26,7 @@
#include "ArduinoTestSuite.h"
#include "WProgram.h"
#include "Arduino.h"
#include "HardwareSerial.h"
#include "pins_arduino.h"

View File

@ -7,8 +7,8 @@
#include <avr/io.h>
#endif
#ifndef WProgram_h
#include "WProgram.h"
#ifndef Arduino_h
#include "Arduino.h"
#endif
#ifndef HardwareSerial_h
#include "HardwareSerial.h"

View File

@ -7,7 +7,6 @@
//* Oct 16, 2010 <ROA> Test of Arduino Constants
//************************************************************************
#include "WProgram.h"
#include "HardwareSerial.h"
#include <ArduinoTestSuite.h>

View File

@ -1 +1 @@
//************************************************************************
//************************************************************************

View File

@ -8,7 +8,6 @@
//* Oct 18, 2010 <MLS> Added memory testing
//************************************************************************
#include "WProgram.h"
#include "HardwareSerial.h"
#include "pins_arduino.h"
#include <ArduinoTestSuite.h>

View File

@ -7,7 +7,6 @@
//* Oct 16, 2010 <ROA> Started on String Test
//************************************************************************
#include "WProgram.h"
#include "HardwareSerial.h"
#include <ArduinoTestSuite.h>

View File

@ -7,7 +7,6 @@
//* Oct 16, 2010 <ROA> Started on String Test
//************************************************************************
#include "WProgram.h"
#include "HardwareSerial.h"
#include <ArduinoTestSuite.h>

View File

@ -7,7 +7,6 @@
//* Oct 16, 2010 <ROA> Started on String Test
//************************************************************************
#include "WProgram.h"
#include "HardwareSerial.h"
#include <ArduinoTestSuite.h>

View File

@ -0,0 +1,61 @@
#include <ArduinoTestSuite.h>
void Test_Equal(char *testString, char *expected, const String &actual)
{
char buf[100]; actual.toCharArray(buf, 100);
boolean b = (strcmp(buf, expected) == 0);
ATS_PrintTestStatus(testString, b);
if (!b) {
Serial.print("expected '");
Serial.print(expected);
Serial.print("', actual '");
Serial.print(actual);
Serial.println("'");
}
}
void setup()
{
ATS_begin("Arduino", "String Addition Test");
String stringOne = String("string");
String stringTwo = String("other");
String stringThree = stringOne + stringTwo;
Test_Equal("Add strings", "stringother", stringThree);
Test_Equal("Adding strings doesn't change them", "string", stringOne);
Test_Equal("Adding strings doesn't change them", "other", stringTwo);
Test_Equal("Add strings", "stringotherstringstringstringother", stringOne + stringTwo + stringOne + stringOne + stringOne + stringTwo);
Test_Equal("Add string to integer", "string12345", stringOne + 12345);
Test_Equal("Add string to negative integer", "string-12345", stringOne + -12345);
Test_Equal("Add integer to string", "123string", 123 + stringOne);
Test_Equal("Add string to integers", "string123456789", stringOne + 123 + 456 + 789);
Test_Equal("Add integer to string", "123string456789", 123 + stringOne + 456 + 789);
Test_Equal("Add string to long", "string123456789", stringOne + 123456789L);
Test_Equal("Add string to negative long", "string-123456789", stringOne + -123456789L);
Test_Equal("Add string to unsigned long", "string123456789", stringOne + 123456789UL);
Test_Equal("Add string to byte", "string123", stringOne + byte(123));
Test_Equal("Add char", "stringA", stringOne + 'A');
Test_Equal("Add char", "Astring", 'A' + stringOne);
Test_Equal("Add \"string\"", "stringabc", stringOne + "abc");
Test_Equal("Add \"string\"", "abcstring", "abc" + stringOne);
Test_Equal("Add multiple \"string\"", "stringabcdef", stringOne + "abc" + "def");
Test_Equal("Add multiple \"string\"", "abcstringdef", "abc" + stringOne + "def");
Test_Equal("Add \"string\" and int", "bc", "abc" + 1);
ATS_end();
}
void loop() {}

View File

@ -26,7 +26,6 @@
#include "WProgram.h"
#include "HardwareSerial.h"
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)

View File

@ -0,0 +1,61 @@
#include <ArduinoTestSuite.h>
void Test_Equal(long actual, long expected)
{
char buf[100];
boolean b = expected == actual;
ATS_PrintTestStatus("", b);
if (!b) {
Serial.print("expected '");
Serial.print(expected);
Serial.print("', actual '");
Serial.print(actual);
Serial.println("'");
}
}
void setup()
{
byte buf[5] = { 65, 66, 67, 0, 69 };
ATS_begin("Arduino", "Write & Print Return Values Test");
Test_Equal(Serial.write('a'), 1);
Test_Equal(Serial.write(byte(0)), 1);
Test_Equal(Serial.write("abc"), 3);
Test_Equal(Serial.write(""), 0);
Test_Equal(Serial.write(buf, 5), 5);
Test_Equal(Serial.print(0), 1);
Test_Equal(Serial.print(""), 0);
Test_Equal(Serial.print("abc"), 3);
Test_Equal(Serial.print(0), 1);
Test_Equal(Serial.print(1), 1);
Test_Equal(Serial.print(11), 2);
Test_Equal(Serial.print(12345), 5);
Test_Equal(Serial.print(-1), 2);
Test_Equal(Serial.print(-123), 4);
Test_Equal(Serial.println(), 2);
Test_Equal(Serial.println(""), 2);
Test_Equal(Serial.println("abc"), 5);
Test_Equal(Serial.println(0), 3);
Test_Equal(Serial.println(1), 3);
Test_Equal(Serial.println(11), 4);
Test_Equal(Serial.println(12345), 7);
Test_Equal(Serial.println(-1), 4);
Test_Equal(Serial.println(-123), 6);
ATS_end();
}
void loop() {}

View File

@ -22,7 +22,7 @@
******************************************************************************/
#include <avr/eeprom.h>
#include "WConstants.h"
#include "Arduino.h"
#include "EEPROM.h"
/******************************************************************************

View File

@ -5,7 +5,7 @@ extern "C" {
#include "string.h"
}
#include "WProgram.h"
#include "Arduino.h"
#include "Ethernet.h"
#include "Client.h"
@ -70,19 +70,24 @@ int Client::connect(IPAddress ip, uint16_t port) {
return 1;
}
void Client::write(uint8_t b) {
if (_sock != MAX_SOCK_NUM)
send(_sock, &b, 1);
size_t Client::write(uint8_t b) {
return write(&b, 1);
}
void Client::write(const char *str) {
if (_sock != MAX_SOCK_NUM)
send(_sock, (const uint8_t *)str, strlen(str));
size_t Client::write(const char *str) {
return write((const uint8_t *) str, strlen(str));
}
void Client::write(const uint8_t *buf, size_t size) {
if (_sock != MAX_SOCK_NUM)
send(_sock, buf, size);
size_t Client::write(const uint8_t *buf, size_t size) {
if (_sock == MAX_SOCK_NUM) {
setWriteError();
return 0;
}
if (!send(_sock, buf, size)) {
setWriteError();
return 0;
}
return size;
}
int Client::available() {
@ -156,18 +161,8 @@ uint8_t Client::status() {
return W5100.readSnSR(_sock);
}
// the next three functions are a hack so we can compare the client returned
// by Server::available() to null, or use it as the condition in an
// if-statement. this lets us stay compatible with the Processing network
// library.
uint8_t Client::operator==(int p) {
return _sock == MAX_SOCK_NUM;
}
uint8_t Client::operator!=(int p) {
return _sock != MAX_SOCK_NUM;
}
// the next function allows us to use the client returned by
// Server::available() as the condition in an if-statement.
Client::operator bool() {
return _sock != MAX_SOCK_NUM;

View File

@ -1,6 +1,6 @@
#ifndef client_h
#define client_h
#include "WProgram.h"
#include "Arduino.h"
#include "Print.h"
#include "NetClient.h"
#include "IPAddress.h"
@ -14,9 +14,9 @@ public:
uint8_t status();
virtual int connect(IPAddress ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
virtual size_t write(uint8_t);
virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
virtual int available();
virtual int read();
virtual int read(uint8_t *buf, size_t size);

View File

@ -6,7 +6,7 @@
#include <string.h>
#include <stdlib.h>
#include "Dhcp.h"
#include "wiring.h"
#include "Arduino.h"
#include "util.h"
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
@ -271,11 +271,19 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
case routersOnSubnet :
opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read(_dhcpGatewayIp, 4);
for (int i = 0; i < opt_len-4; i++)
{
_dhcpUdpSocket.read();
}
break;
case dns :
opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
for (int i = 0; i < opt_len-4; i++)
{
_dhcpUdpSocket.read();
}
break;
case dhcpServerIdentifier :

View File

@ -9,7 +9,7 @@
#include "Dns.h"
#include <string.h>
//#include <stdlib.h>
#include "wiring.h"
#include "Arduino.h"
#define SOCKET_NONE 255

View File

@ -33,27 +33,37 @@ int EthernetClass::begin(uint8_t *mac_address)
}
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
{
// Assume the DNS server will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress dns_server = local_ip;
dns_server[3] = 1;
begin(mac_address, local_ip, dns_server);
}
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
{
// Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress gateway = local_ip;
gateway[3] = 1;
begin(mac_address, local_ip, gateway);
begin(mac_address, local_ip, dns_server, gateway);
}
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway)
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
{
IPAddress subnet(255, 255, 255, 0);
begin(mac_address, local_ip, gateway, subnet);
begin(mac_address, local_ip, dns_server, gateway, subnet);
}
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress gateway, IPAddress subnet)
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{
W5100.init();
W5100.setMACAddress(mac);
W5100.setIPAddress(local_ip._address);
W5100.setGatewayIp(gateway._address);
W5100.setSubnetMask(subnet._address);
_dnsServerAddress = dns_server;
}
IPAddress EthernetClass::localIP()

View File

@ -20,8 +20,9 @@ public:
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
int begin(uint8_t *mac_address);
void begin(uint8_t *mac_address, IPAddress local_ip);
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway);
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway, IPAddress subnet);
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server);
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
IPAddress localIP();
IPAddress subnetMask();

View File

@ -67,18 +67,20 @@ Client Server::available()
return Client(MAX_SOCK_NUM);
}
void Server::write(uint8_t b)
size_t Server::write(uint8_t b)
{
write(&b, 1);
}
void Server::write(const char *str)
size_t Server::write(const char *str)
{
write((const uint8_t *)str, strlen(str));
}
void Server::write(const uint8_t *buffer, size_t size)
size_t Server::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
@ -86,7 +88,9 @@ void Server::write(const uint8_t *buffer, size_t size)
if (EthernetClass::_server_port[sock] == _port &&
client.status() == SnSR::ESTABLISHED) {
client.write(buffer, size);
n += client.write(buffer, size);
}
}
return n;
}

View File

@ -14,9 +14,9 @@ public:
Server(uint16_t);
Client available();
virtual void begin();
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
virtual size_t write(uint8_t);
virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
};
#endif

View File

@ -102,21 +102,22 @@ int UDP::endPacket()
return sendUDP(_sock);
}
void UDP::write(uint8_t byte)
size_t UDP::write(uint8_t byte)
{
write(&byte, 1);
return write(&byte, 1);
}
void UDP::write(const char *str)
size_t UDP::write(const char *str)
{
size_t len = strlen(str);
write((const uint8_t *)str, len);
return write((const uint8_t *)str, len);
}
void UDP::write(const uint8_t *buffer, size_t size)
size_t UDP::write(const uint8_t *buffer, size_t size)
{
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
_offset += bytes_written;
return bytes_written;
}
int UDP::parsePacket()

View File

@ -67,11 +67,11 @@ public:
// Returns 1 if the packet was sent successfully, 0 if there was an error
int endPacket();
// Write a single byte into the packet
virtual void write(uint8_t);
virtual size_t write(uint8_t);
// Write a string of characters into the packet
virtual void write(const char *str);
virtual size_t write(const char *str);
// Write size bytes from buffer into the packet
virtual void write(const uint8_t *buffer, size_t size);
virtual size_t write(const uint8_t *buffer, size_t size);
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available

View File

@ -0,0 +1,54 @@
/*
DHCP-based IP printer
This sketch uses the DHCP extensions to the Ethernet library
to get an IP address via DHCP and print the address obtained.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 12 April 2011
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client;
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");z
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// print your local IP address:
Serial.print("My IP address: ");
IPAddress myIPAddress = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(myIPAddress[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}

View File

@ -0,0 +1,53 @@
/*
DHCP-based IP printer
This sketch uses the DHCP extensions to the Ethernet library
to get an IP address via DHCP and print the address obtained.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 12 April 2011
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client;
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}

View File

@ -0,0 +1,80 @@
/*
DHCP Chat Server
A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.
THis version attempts to get an IP address using DHCP
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011
by Tom Igoe
Based on ChatServer example by David A. Mellis
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23
Server server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() {
// open the serial port
Serial.begin(9600);
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// initialize the ethernet device not using DHCP:
Ethernet.begin(mac, ip, gateway, subnet);
}
// print your local IP address:
Serial.print("My IP address: ");
ip = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// start listening for clients
server.begin();
}
void loop() {
// wait for a new client:
Client client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!gotAMessage) {
Serial.println("We have a new client");
client.println("Hello, client!");
gotAMessage = true;
}
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.print(thisChar);
}
}

View File

@ -0,0 +1,76 @@
/*
DNS and DHCP-based Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 12 April 2011
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "www.google.com";
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client;
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(serverName, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while(true);
}
}

View File

@ -0,0 +1,124 @@
/*
Twitter Client with Strings
This sketch connects to Twitter using an Ethernet shield. It parses the XML
returned, and looks for <text>this is a tweet</text>
You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,
either one will work, as long as it's got a Wiznet Ethernet module on board.
This example uses the DHCP routines in the Ethernet library which is part of the
Arduino core from version 1.0 beta 1
This example uses the String library, which is part of the Arduino core from
version 0019.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011
by Tom Igoe
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192,168,1,20);
// initialize the library instance:
Client client;
const int requestInterval = 60000; // delay between requests
char serverName[] = "api.twitter.com"; // twitter URL
boolean requested; // whether you've made a request since connecting
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet
boolean readingTweet = false; // if you're currently reading the tweet
void setup() {
// reserve space for the strings:
currentLine.reserve(256);
tweet.reserve(150);
// initialize serial:
Serial.begin(9600);
// attempt a DHCP connection:
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Ethernet.begin(mac, ip);
}
// connect to Twitter:
connectToServer();
}
void loop()
{
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
}
// if the current line ends with <text>, it will
// be followed by the tweet:
if ( currentLine.endsWith("<text>")) {
// tweet is beginning. Clear the tweet string:
readingTweet = true;
tweet = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingTweet) {
if (inChar != '<') {
tweet += inChar;
}
else {
// if you got a "<" character,
// you've reached the end of the tweet:
readingTweet = false;
Serial.println(tweet);
// close the connection to the server:
client.stop();
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}
void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect(serverName, 80)) {
Serial.println("making HTTP request...");
// make HTTP GET request to twitter:
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}

View File

@ -324,6 +324,10 @@ private:
inline static void initSS() { DDRB |= _BV(4); };
inline static void setSS() { PORTB &= ~_BV(4); };
inline static void resetSS() { PORTB |= _BV(4); };
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
inline static void initSS() { DDRB |= _BV(0); };
inline static void setSS() { PORTB &= ~_BV(0); };
inline static void resetSS() { PORTB |= _BV(0); };
#else
inline static void initSS() { DDRB |= _BV(2); };
inline static void setSS() { PORTB &= ~_BV(2); };

View File

@ -3,7 +3,7 @@
#ifndef Firmata_Boards_h
#define Firmata_Boards_h
#include <WProgram.h> // for digitalRead, digitalWrite, etc
#include <Arduino.h> // for digitalRead, digitalWrite, etc
// Normally Servo.h must be included before Firmata.h (which then includes
// this file). If Servo.h wasn't included, this allows the code to still

View File

@ -14,7 +14,7 @@
//* Includes
//******************************************************************************
#include "WProgram.h"
#include "Arduino.h"
#include "HardwareSerial.h"
#include "Firmata.h"

View File

@ -13,7 +13,7 @@
#ifndef Firmata_h
#define Firmata_h
#include <WProgram.h>
#include <Arduino.h>
#include <inttypes.h>
@ -66,8 +66,8 @@
#define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL
// pin modes
//#define INPUT 0x00 // defined in wiring.h
//#define OUTPUT 0x01 // defined in wiring.h
//#define INPUT 0x00 // defined in Arduino.h
//#define OUTPUT 0x01 // defined in Arduino.h
#define ANALOG 0x02 // analog pin in analogInput mode
#define PWM 0x03 // digital pin in PWM output mode
#define SERVO 0x04 // digital pin in Servo output mode

View File

@ -14,12 +14,12 @@ void stringCallback(char *myString)
void sysexCallback(byte command, byte argc, byte*argv)
{
Serial.print(START_SYSEX, BYTE);
Serial.print(command, BYTE);
Serial.write(START_SYSEX);
Serial.write(command);
for(byte i=0; i<argc; i++) {
Serial.print(argv[i], BYTE);
Serial.write(argv[i]);
}
Serial.print(END_SYSEX, BYTE);
Serial.write(END_SYSEX);
}
void setup()

View File

@ -3,7 +3,7 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "WProgram.h"
#include "Arduino.h"
// When the display powers up, it is configured as follows:
//
@ -258,8 +258,9 @@ inline void LiquidCrystal::command(uint8_t value) {
send(value, LOW);
}
inline void LiquidCrystal::write(uint8_t value) {
inline size_t LiquidCrystal::write(uint8_t value) {
send(value, HIGH);
return 1; // assume sucess
}
/************ low level data pushing commands **********/

View File

@ -79,7 +79,7 @@ public:
void createChar(uint8_t, uint8_t[]);
void setCursor(uint8_t, uint8_t);
virtual void write(uint8_t);
virtual size_t write(uint8_t);
void command(uint8_t);
private:
void send(uint8_t, uint8_t);

View File

@ -0,0 +1,138 @@
/*
LiquidCrystal Library - Custom Characters
Demonstrates how to add custom characters on an LCD display.
The LiquidCrystal library works with all LCD displays that are
compatible with the Hitachi HD44780 driver. There are many of
them out there, and you can usually tell them by the 16-pin interface.
This sketch prints "I <heart> Arduino!" and a little dancing man
to the LCD.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K potentiometer:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* 10K poterntiometer on pin A0
created21 Mar 2011
by Tom Igoe
Based on Adafruit's example at
https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
Also useful:
http://icontexto.com/charactercreator/
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// make some custom characters:
byte heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};
byte smiley[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b10001,
0b01110,
0b00000
};
byte frownie[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b00000,
0b01110,
0b10001
};
byte armsDown[8] = {
0b00100,
0b01010,
0b00100,
0b00100,
0b01110,
0b10101,
0b00100,
0b01010
};
byte armsUp[8] = {
0b00100,
0b01010,
0b00100,
0b10101,
0b01110,
0b00100,
0b00100,
0b01010
};
void setup() {
// create a new character
lcd.createChar(0, heart);
// create a new character
lcd.createChar(1, smiley);
// create a new character
lcd.createChar(2, frownie);
// create a new character
lcd.createChar(3, armsDown);
// create a new character
lcd.createChar(4, armsUp);
// set up the lcd's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the lcd.
lcd.print("I ");
lcd.write(0);
lcd.print(" Arduino! ");
lcd.write(1);
}
void loop() {
// read the potentiometer on A0:
int sensorReading = analogRead(A0);
// map the result to 200 - 1000:
int delayTime = map(sensorReading, 0, 1023, 200, 1000);
// set the cursor to the bottom row, 5th position:
lcd.setCursor(4, 1);
// draw the little man, arms down:
lcd.write(3);
delay(delayTime);
lcd.setCursor(4, 1);
// draw him arms up:
lcd.write(4);
delay(delayTime);
}

View File

@ -71,7 +71,7 @@ void loop() {
thisChar = 'a';
}
// print the character
lcd.print(thisChar, BYTE);
lcd.write(thisChar);
// wait a second:
delay(1000);
// increment the letter:

View File

@ -61,7 +61,7 @@ void loop() {
// set the cursor position:
lcd.setCursor(thisRow,thisCol);
// print the letter:
lcd.print(thisLetter, BYTE);
lcd.write(thisLetter);
delay(200);
}
}

View File

@ -1,229 +0,0 @@
/*
Matrix.cpp - Max7219 LED Matrix library for Arduino & Wiring
Copyright (c) 2006 Nicholas Zambetti. 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
*/
// TODO: Support segment displays in api?
// TODO: Support varying vendor layouts?
/******************************************************************************
* Includes
******************************************************************************/
extern "C" {
// AVR LibC Includes
#include <inttypes.h>
#include <stdlib.h>
// Wiring Core Includes
#undef abs
#include "WConstants.h"
// Wiring Core Prototypes
//void pinMode(uint8_t, uint8_t);
//void digitalWrite(int, uint8_t);
}
#include "Sprite.h"
#include "Matrix.h"
/******************************************************************************
* Definitions
******************************************************************************/
// Matrix registers
#define REG_NOOP 0x00
#define REG_DIGIT0 0x01
#define REG_DIGIT1 0x02
#define REG_DIGIT2 0x03
#define REG_DIGIT3 0x04
#define REG_DIGIT4 0x05
#define REG_DIGIT5 0x06
#define REG_DIGIT6 0x07
#define REG_DIGIT7 0x08
#define REG_DECODEMODE 0x09
#define REG_INTENSITY 0x0A
#define REG_SCANLIMIT 0x0B
#define REG_SHUTDOWN 0x0C
#define REG_DISPLAYTEST 0x0F
/******************************************************************************
* Constructors
******************************************************************************/
Matrix::Matrix(uint8_t data, uint8_t clock, uint8_t load, uint8_t screens /* = 1 */)
{
// record pins for sw spi
_pinData = data;
_pinClock = clock;
_pinLoad = load;
// set ddr for sw spi pins
pinMode(_pinClock, OUTPUT);
pinMode(_pinData, OUTPUT);
pinMode(_pinLoad, OUTPUT);
// allocate screenbuffers
_screens = screens;
_buffer = (uint8_t*)calloc(_screens, 64);
_maximumX = (_screens * 8);
// initialize registers
clear(); // clear display
setScanLimit(0x07); // use all rows/digits
setBrightness(0x0F); // maximum brightness
setRegister(REG_SHUTDOWN, 0x01); // normal operation
setRegister(REG_DECODEMODE, 0x00); // pixels not integers
setRegister(REG_DISPLAYTEST, 0x00); // not in test mode
}
/******************************************************************************
* MAX7219 SPI
******************************************************************************/
// sends a single byte by sw spi (no latching)
void Matrix::putByte(uint8_t data)
{
uint8_t i = 8;
uint8_t mask;
while(i > 0) {
mask = 0x01 << (i - 1); // get bitmask
digitalWrite(_pinClock, LOW); // tick
if (data & mask){ // choose bit
digitalWrite(_pinData, HIGH); // set 1
}else{
digitalWrite(_pinData, LOW); // set 0
}
digitalWrite(_pinClock, HIGH); // tock
--i; // move to lesser bit
}
}
// sets register to a byte value for all screens
void Matrix::setRegister(uint8_t reg, uint8_t data)
{
digitalWrite(_pinLoad, LOW); // begin
for(uint8_t i = 0; i < _screens; ++i){
putByte(reg); // specify register
putByte(data); // send data
}
digitalWrite(_pinLoad, HIGH); // latch in data
digitalWrite(_pinLoad, LOW); // end
}
// syncs row of display with buffer
void Matrix::syncRow(uint8_t row)
{
if (!_buffer) return;
// uint8_t's can't be negative, so don't test for negative row
if (row >= 8) return;
digitalWrite(_pinLoad, LOW); // begin
for(uint8_t i = 0; i < _screens; ++i){
putByte(8 - row); // specify register
putByte(_buffer[row + (8 * i)]); // send data
}
digitalWrite(_pinLoad, HIGH); // latch in data
digitalWrite(_pinLoad, LOW); // end
}
/******************************************************************************
* MAX7219 Configuration
******************************************************************************/
// sets how many digits are displayed
void Matrix::setScanLimit(uint8_t value)
{
setRegister(REG_SCANLIMIT, value & 0x07);
}
// sets brightness of the display
void Matrix::setBrightness(uint8_t value)
{
setRegister(REG_INTENSITY, value & 0x0F);
}
/******************************************************************************
* Helper Functions
******************************************************************************/
void Matrix::buffer(uint8_t x, uint8_t y, uint8_t value)
{
if (!_buffer) return;
// uint8_t's can't be negative, so don't test for negative x and y.
if (x >= _maximumX || y >= 8) return;
uint8_t offset = x; // record x
x %= 8; // make x relative to a single matrix
offset -= x; // calculate buffer offset
// wrap shift relative x for nexus module layout
if (x == 0){
x = 8;
}
--x;
// record value in buffer
if(value){
_buffer[y + offset] |= 0x01 << x;
}else{
_buffer[y + offset] &= ~(0x01 << x);
}
}
/******************************************************************************
* User API
******************************************************************************/
// buffers and writes to screen
void Matrix::write(uint8_t x, uint8_t y, uint8_t value)
{
buffer(x, y, value);
// update affected row
syncRow(y);
}
void Matrix::write(uint8_t x, uint8_t y, Sprite sprite)
{
for (uint8_t i = 0; i < sprite.height(); i++){
for (uint8_t j = 0; j < sprite.width(); j++)
buffer(x + j, y + i, sprite.read(j, i));
syncRow(y + i);
}
}
// clears screens and buffers
void Matrix::clear(void)
{
if (!_buffer) return;
// clear buffer
for(uint8_t i = 0; i < 8; ++i){
for(uint8_t j = 0; j < _screens; ++j){
_buffer[i + (8 * j)] = 0x00;
}
}
// clear registers
for(uint8_t i = 0; i < 8; ++i){
syncRow(i);
}
}

View File

@ -1,54 +0,0 @@
/*
Matrix.h - Max7219 LED Matrix library for Arduino & Wiring
Copyright (c) 2006 Nicholas Zambetti. 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 Matrix_h
#define Matrix_h
#include <inttypes.h>
class Sprite;
class Matrix
{
private:
uint8_t _pinData;
uint8_t _pinClock;
uint8_t _pinLoad;
uint8_t* _buffer;
uint8_t _screens;
uint8_t _maximumX;
void putByte(uint8_t);
void setRegister(uint8_t, uint8_t);
void syncRow(uint8_t);
void setScanLimit(uint8_t);
void buffer(uint8_t, uint8_t, uint8_t);
public:
Matrix(uint8_t, uint8_t, uint8_t, uint8_t = 1);
void setBrightness(uint8_t);
void write(uint8_t, uint8_t, uint8_t);
void write(uint8_t, uint8_t, Sprite);
void clear(void);
};
#endif

View File

@ -1,42 +0,0 @@
#include <Sprite.h>
#include <Matrix.h>
// Hello Matrix
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates the use of the Matrix library
// For MAX7219 LED Matrix Controllers
// Blinks welcoming face on screen
// Created 13 February 2006
/* create a new Matrix instance
pin 0: data (din)
pin 1: load (load)
pin 2: clock (clk)
*/
Matrix myMatrix = Matrix(0, 2, 1);
void setup()
{
}
void loop()
{
myMatrix.clear(); // clear display
delay(1000);
// turn some pixels on
myMatrix.write(1, 5, HIGH);
myMatrix.write(2, 2, HIGH);
myMatrix.write(2, 6, HIGH);
myMatrix.write(3, 6, HIGH);
myMatrix.write(4, 6, HIGH);
myMatrix.write(5, 2, HIGH);
myMatrix.write(5, 6, HIGH);
myMatrix.write(6, 5, HIGH);
delay(1000);
}

View File

@ -1,48 +0,0 @@
#include <Sprite.h>
#include <Matrix.h>
// Sprite Animation
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates the use of the Matrix & Sprite libraries
// Displays animated waveform graphic on screen
// Created 29 March 2006
/* create a new Matrix instance
pin 0: data (din)
pin 1: load (load)
pin 2: clock (clk)
*/
Matrix myMatrix = Matrix(0, 2, 1);
/* create a new Sprite instance
8 pixels wide, 4 pixels tall
*/
Sprite wave = Sprite(
8, 4,
B00011000,
B00100100,
B01000010,
B10000001
);
void setup()
{
}
int x = 0;
void loop()
{
myMatrix.write(x, 2, wave); // place sprite on screen
myMatrix.write(x - 8, 2, wave); // place sprite again, elsewhere on screen
delay(75); // wait a little bit
myMatrix.clear(); // clear the screen for next animation frame
if(x == 8) // if reached end of animation sequence
{
x = 0; // start from beginning
}
x++; // advance x coordinate to the right
}

View File

@ -1,22 +0,0 @@
#######################################
# Syntax Coloring Map For Matrix
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Matrix KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
setBrightness KEYWORD2
write KEYWORD2
clear KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################

View File

@ -14,52 +14,138 @@
#include <SD.h>
void File::write(uint8_t val) {
SD.file.write(val);
/* for debugging file open/close leaks
uint8_t nfilecount=0;
*/
File::File(SdFile f, char *n) {
// oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
_file = (SdFile *)malloc(sizeof(SdFile));
if (_file) {
memcpy(_file, &f, sizeof(SdFile));
strncpy(_name, n, 12);
_name[12] = 0;
/* for debugging file open/close leaks
nfilecount++;
Serial.print("Created \"");
Serial.print(n);
Serial.print("\": ");
Serial.println(nfilecount, DEC);
*/
}
}
void File::write(const char *str) {
SD.file.write(str);
File::File(void) {
_file = 0;
_name[0] = 0;
//Serial.print("Created empty file object");
}
void File::write(const uint8_t *buf, size_t size) {
SD.file.write(buf, size);
File::~File(void) {
// Serial.print("Deleted file object");
}
// returns a pointer to the file name
char *File::name(void) {
return _name;
}
// a directory is a special type of file
boolean File::isDirectory(void) {
return (_file && _file->isDir());
}
size_t File::write(uint8_t val) {
return write(&val, 1);
}
size_t File::write(const char *str) {
return write((const uint8_t *) str, strlen(str));
}
size_t File::write(const uint8_t *buf, size_t size) {
size_t t;
if (!_file) {
setWriteError();
return 0;
}
_file->clearWriteError();
t = _file->write(buf, size);
if (_file->writeError()) {
setWriteError();
return 0;
}
return t;
}
int File::peek() {
char c = SD.file.read();
if (c != -1) SD.file.seekCur(-1);
if (! _file)
return 0;
int c = _file->read();
if (c != -1) _file->seekCur(-1);
return c;
}
int File::read() {
return SD.file.read();
if (_file)
return _file->read();
return -1;
}
// buffered read for more efficient, high speed reading
int File::read(void *buf, uint16_t nbyte) {
if (_file)
return _file->read(buf, nbyte);
return 0;
}
int File::available() {
if (! _file) return 0;
return size() - position();
}
void File::flush() {
SD.file.sync();
if (_file)
_file->sync();
}
boolean File::seek(uint32_t pos) {
return SD.file.seekSet(pos);
if (! _file) return false;
return _file->seekSet(pos);
}
uint32_t File::position() {
return SD.file.curPosition();
if (! _file) return -1;
return _file->curPosition();
}
uint32_t File::size() {
return SD.file.fileSize();
if (! _file) return 0;
return _file->fileSize();
}
void File::close() {
SD.file.close();
if (_file) {
_file->close();
free(_file);
_file = 0;
/* for debugging file open/close leaks
nfilecount--;
Serial.print("Deleted ");
Serial.println(nfilecount, DEC);
*/
}
}
File::operator bool() {
return SD.file.isOpen();
if (_file)
return _file->isOpen();
return false;
}

View File

@ -9,3 +9,5 @@ License: GNU General Public License V3
(C) Copyright 2010 SparkFun Electronics
Now better than ever with optimization, multiple file support, directory handling, etc - ladyada!

View File

@ -275,10 +275,10 @@ boolean callback_makeDirPath(SdFile& parentDir, char *filePathComponent,
}
/*
boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
boolean isLastComponent, void *object) {
/*
Callback used to open a file specified by a filepath that may
specify one or more directories above it.
@ -293,7 +293,6 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
Returns false once the file has been opened--to prevent the traversal
from descending further. (This may be unnecessary.)
*/
if (isLastComponent) {
SDClass *p_SD = static_cast<SDClass*>(object);
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
@ -305,6 +304,8 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
}
return true;
}
*/
boolean callback_remove(SdFile& parentDir, char *filePathComponent,
@ -345,6 +346,64 @@ boolean SDClass::begin(uint8_t csPin) {
}
// this little helper is used to traverse paths
SdFile SDClass::getParentDir(char *filepath, int *index) {
// get parent directory
SdFile d1 = root; // start with the mostparent, root!
SdFile d2;
// we'll use the pointers to swap between the two objects
SdFile *parent = &d1;
SdFile *subdir = &d2;
char *origpath = filepath;
while (strchr(filepath, '/')) {
// get rid of leading /'s
if (filepath[0] == '/') {
filepath++;
continue;
}
if (! strchr(filepath, '/')) {
// it was in the root directory, so leave now
break;
}
// extract just the name of the next subdirectory
uint8_t idx = strchr(filepath, '/') - filepath;
if (idx > 12)
idx = 12; // dont let them specify long names
char subdirname[13];
strncpy(subdirname, filepath, idx);
subdirname[idx] = 0;
// close the subdir (we reuse them) if open
subdir->close();
if (! subdir->open(parent, subdirname, O_READ)) {
// failed to open one of the subdirectories
return SdFile();
}
// move forward to the next subdirectory
filepath += idx;
// we reuse the objects, close it.
parent->close();
// swap the pointers
SdFile *t = parent;
parent = subdir;
subdir = t;
}
*index = (int)(filepath - origpath);
// parent is now the parent diretory of the file!
return *parent;
}
File SDClass::open(char *filepath, uint8_t mode) {
/*
@ -369,6 +428,72 @@ File SDClass::open(char *filepath, uint8_t mode) {
*/
int pathidx;
// do the interative search
SdFile parentdir = getParentDir(filepath, &pathidx);
// no more subdirs!
filepath += pathidx;
if (! filepath[0]) {
// it was the directory itself!
return File(parentdir, "/");
}
// Open the file itself
SdFile file;
// failed to open a subdir!
if (!parentdir.isOpen())
return File();
// there is a special case for the Root directory since its a static dir
if (parentdir.isRoot()) {
if ( ! file.open(SD.root, filepath, mode)) {
// failed to open the file :(
return File();
}
// dont close the root!
} else {
if ( ! file.open(parentdir, filepath, mode)) {
return File();
}
// close the parent
parentdir.close();
}
if (mode & (O_APPEND | O_WRITE))
file.seekSet(file.fileSize());
return File(file, filepath);
}
/*
File SDClass::open(char *filepath, uint8_t mode) {
//
Open the supplied file path for reading or writing.
The file content can be accessed via the `file` property of
the `SDClass` object--this property is currently
a standard `SdFile` object from `sdfatlib`.
Defaults to read only.
If `write` is true, default action (when `append` is true) is to
append data to the end of the file.
If `append` is false then the file will be truncated first.
If the file does not exist and it is opened for writing the file
will be created.
An attempt to open a file for reading that does not exist is an
error.
//
// TODO: Allow for read&write? (Possibly not, as it requires seek.)
fileOpenMode = mode;
@ -377,6 +502,7 @@ File SDClass::open(char *filepath, uint8_t mode) {
return File();
}
*/
//boolean SDClass::close() {
@ -436,4 +562,55 @@ boolean SDClass::remove(char *filepath) {
return walkPath(filepath, root, callback_remove);
}
SDClass SD;
// allows you to recurse into a directory
File File::openNextFile(uint8_t mode) {
dir_t p;
//Serial.print("\t\treading dir...");
while (_file->readDir(&p) > 0) {
// done if past last used entry
if (p.name[0] == DIR_NAME_FREE) {
//Serial.println("end");
return File();
}
// skip deleted entry and entries for . and ..
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') {
//Serial.println("dots");
continue;
}
// only list subdirectories and files
if (!DIR_IS_FILE_OR_SUBDIR(&p)) {
//Serial.println("notafile");
continue;
}
// print file name with possible blank fill
SdFile f;
char name[13];
_file->dirName(p, name);
//Serial.print("try to open file ");
//Serial.println(name);
if (f.open(_file, name, mode)) {
//Serial.println("OK!");
return File(f, name);
} else {
//Serial.println("ugh");
return File();
}
}
//Serial.println("nothing");
return File();
}
void File::rewindDirectory(void) {
if (isDirectory())
_file->rewind();
}
SDClass SD;

View File

@ -15,28 +15,41 @@
#ifndef __SD_H__
#define __SD_H__
#include <WProgram.h>
#include <Arduino.h>
#include <utility/SdFat.h>
#include <utility/SdFatUtil.h>
#define FILE_READ O_READ
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_SYNC)
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT)
class File : public Stream {
private:
char _name[13]; // our name
SdFile *_file; // underlying file pointer
public:
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
File(SdFile f, char *name); // wraps an underlying SdFile
File(void); // 'empty' constructor
~File(void); // destructor
virtual size_t write(uint8_t);
virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
virtual int read();
virtual int peek();
virtual int available();
virtual void flush();
int read(void *buf, uint16_t nbyte);
boolean seek(uint32_t pos);
uint32_t position();
uint32_t size();
void close();
operator bool();
char * name();
boolean isDirectory(void);
File openNextFile(uint8_t mode = O_RDONLY);
void rewindDirectory(void);
};
class SDClass {
@ -47,6 +60,8 @@ private:
SdVolume volume;
SdFile root;
// my quick&dirty iterator, should be replaced
SdFile getParentDir(char *filepath, int *indx);
public:
// This needs to be called to set up the connection to the SD card
// before other methods are used.
@ -70,7 +85,6 @@ public:
boolean rmdir(char *filepath);
private:
SdFile file;
// This is used to determine the mode used to open a file
// it's here because it's the easiest place to pass the

View File

@ -0,0 +1,111 @@
/*
SD card test
This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS - depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples
created 28 Mar 2011
by Limor Fried
modified 16 Mar 2011
by Tom Igoe
*/
// include the SD library:
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;
void setup()
{
Serial.begin(9600);
Serial.print("\nInitializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT); // change this to 53 on a mega
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card is inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard type: ");
switch(card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
long volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop(void) {
}

View File

@ -0,0 +1,77 @@
/*
SD card basic file example
This example shows how to create and destroy an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File root;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("done!");
}
void loop()
{
// nothing happens after setup finishes.
}
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
}
}

View File

@ -17,7 +17,7 @@
* along with the Arduino Sd2Card Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <WProgram.h>
#include <Arduino.h>
#include "Sd2Card.h"
//------------------------------------------------------------------------------
#ifndef SOFTWARE_SPI

View File

@ -141,7 +141,7 @@ class SdFile : public Print {
* Set writeError to false before calling print() and/or write() and check
* for true after calls to print() and/or write().
*/
bool writeError;
//bool writeError;
/**
* Cancel unbuffered reads for this file.
* See setUnbufferedRead()
@ -283,9 +283,9 @@ class SdFile : public Print {
}
/** \return SdVolume that contains this file. */
SdVolume* volume(void) const {return vol_;}
void write(uint8_t b);
int16_t write(const void* buf, uint16_t nbyte);
void write(const char* str);
size_t write(uint8_t b);
size_t write(const void* buf, uint16_t nbyte);
size_t write(const char* str);
void write_P(PGM_P str);
void writeln_P(PGM_P str);
//------------------------------------------------------------------------------

View File

@ -23,7 +23,7 @@
* \file
* Useful utility functions.
*/
#include <WProgram.h>
#include <Arduino.h>
#include <avr/pgmspace.h>
/** Store and print a string in flash memory.*/
#define PgmPrint(x) SerialPrint_P(PSTR(x))

View File

@ -19,7 +19,7 @@
*/
#include <SdFat.h>
#include <avr/pgmspace.h>
#include <WProgram.h>
#include <Arduino.h>
//------------------------------------------------------------------------------
// callback function for date/time
void (*SdFile::dateTime_)(uint16_t* date, uint16_t* time) = NULL;
@ -1121,7 +1121,7 @@ uint8_t SdFile::truncate(uint32_t length) {
* for a read-only file, device is full, a corrupt file system or an I/O error.
*
*/
int16_t SdFile::write(const void* buf, uint16_t nbyte) {
size_t SdFile::write(const void* buf, uint16_t nbyte) {
// convert void* to uint8_t* - must be before goto statements
const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
@ -1210,8 +1210,9 @@ int16_t SdFile::write(const void* buf, uint16_t nbyte) {
writeErrorReturn:
// return for write error
writeError = true;
return -1;
//writeError = true;
setWriteError();
return 0;
}
//------------------------------------------------------------------------------
/**
@ -1219,8 +1220,8 @@ int16_t SdFile::write(const void* buf, uint16_t nbyte) {
*
* Use SdFile::writeError to check for errors.
*/
void SdFile::write(uint8_t b) {
write(&b, 1);
size_t SdFile::write(uint8_t b) {
return write(&b, 1);
}
//------------------------------------------------------------------------------
/**
@ -1228,8 +1229,8 @@ void SdFile::write(uint8_t b) {
*
* Use SdFile::writeError to check for errors.
*/
void SdFile::write(const char* str) {
write(str, strlen(str));
size_t SdFile::write(const char* str) {
return write(str, strlen(str));
}
//------------------------------------------------------------------------------
/**

View File

@ -12,7 +12,7 @@
#define _SPI_H_INCLUDED
#include <stdio.h>
#include <WProgram.h>
#include <Arduino.h>
#include <avr/pgmspace.h>
#define SPI_CLOCK_DIV4 0x00

View File

@ -43,7 +43,7 @@
*/
#include <avr/interrupt.h>
#include <WProgram.h>
#include <Arduino.h>
#include "Servo.h"

View File

@ -1,227 +1,515 @@
/*
SoftwareSerial.cpp - Software serial library
Copyright (c) 2006 David A. Mellis. 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
*/
/******************************************************************************
* Includes
******************************************************************************/
#include "WConstants.h"
#include "SoftwareSerial.h"
/******************************************************************************
* Definitions
******************************************************************************/
/******************************************************************************
* Constructors
******************************************************************************/
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin)
{
_receivePin = receivePin;
_transmitPin = transmitPin;
_baudRate = 0;
}
/******************************************************************************
* User API
******************************************************************************/
void SoftwareSerial::begin(long speed)
{
_baudRate = speed;
_bitPeriod = 1000000 / _baudRate;
digitalWrite(_transmitPin, HIGH);
delayMicroseconds( _bitPeriod); // if we were low this establishes the end
}
int SoftwareSerial::read()
{
int val = 0;
int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50);
// one byte of serial data (LSB first)
// ...--\ /--\/--\/--\/--\/--\/--\/--\/--\/--...
// \--/\--/\--/\--/\--/\--/\--/\--/\--/
// start 0 1 2 3 4 5 6 7 stop
while (digitalRead(_receivePin));
// confirm that this is a real start bit, not line noise
if (digitalRead(_receivePin) == LOW) {
// frame start indicated by a falling edge and low start bit
// jump to the middle of the low start bit
delayMicroseconds(bitDelay / 2 - clockCyclesToMicroseconds(50));
// offset of the bit in the byte: from 0 (LSB) to 7 (MSB)
for (int offset = 0; offset < 8; offset++) {
// jump to middle of next bit
delayMicroseconds(bitDelay);
// read bit
val |= digitalRead(_receivePin) << offset;
}
delayMicroseconds(_bitPeriod);
return val;
}
return -1;
}
void SoftwareSerial::print(uint8_t b)
{
if (_baudRate == 0)
return;
int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); // a digitalWrite is about 50 cycles
byte mask;
digitalWrite(_transmitPin, LOW);
delayMicroseconds(bitDelay);
for (mask = 0x01; mask; mask <<= 1) {
if (b & mask){ // choose bit
digitalWrite(_transmitPin,HIGH); // send 1
}
else{
digitalWrite(_transmitPin,LOW); // send 1
}
delayMicroseconds(bitDelay);
}
digitalWrite(_transmitPin, HIGH);
delayMicroseconds(bitDelay);
}
void SoftwareSerial::print(const char *s)
{
while (*s)
print(*s++);
}
void SoftwareSerial::print(char c)
{
print((uint8_t) c);
}
void SoftwareSerial::print(int n)
{
print((long) n);
}
void SoftwareSerial::print(unsigned int n)
{
print((unsigned long) n);
}
void SoftwareSerial::print(long n)
{
if (n < 0) {
print('-');
n = -n;
}
printNumber(n, 10);
}
void SoftwareSerial::print(unsigned long n)
{
printNumber(n, 10);
}
void SoftwareSerial::print(long n, int base)
{
if (base == 0)
print((char) n);
else if (base == 10)
print(n);
else
printNumber(n, base);
}
void SoftwareSerial::println(void)
{
print('\r');
print('\n');
}
void SoftwareSerial::println(char c)
{
print(c);
println();
}
void SoftwareSerial::println(const char c[])
{
print(c);
println();
}
void SoftwareSerial::println(uint8_t b)
{
print(b);
println();
}
void SoftwareSerial::println(int n)
{
print(n);
println();
}
void SoftwareSerial::println(long n)
{
print(n);
println();
}
void SoftwareSerial::println(unsigned long n)
{
print(n);
println();
}
void SoftwareSerial::println(long n, int base)
{
print(n, base);
println();
}
// Private Methods /////////////////////////////////////////////////////////////
void SoftwareSerial::printNumber(unsigned long n, uint8_t base)
{
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
if (n == 0) {
print('0');
return;
}
while (n > 0) {
buf[i++] = n % base;
n /= base;
}
for (; i > 0; i--)
print((char) (buf[i - 1] < 10 ? '0' + buf[i - 1] : 'A' + buf[i - 1] - 10));
}
/*
SoftwareSerial.cpp (formerly NewSoftSerial.cpp) -
Multi-instance software serial library for Arduino/Wiring
-- Interrupt-driven receive and other improvements by ladyada
(http://ladyada.net)
-- Tuning, circular buffer, derivation from class Print/Stream,
multi-instance support, porting to 8MHz processors,
various optimizations, PROGMEM delay tables, inverse logic and
direct port writing by Mikal Hart (http://www.arduiniana.org)
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.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
The latest version of this library can always be found at
http://arduiniana.org.
*/
// When set, _DEBUG co-opts pins 11 and 13 for debugging with an
// oscilloscope or logic analyzer. Beware: it also slightly modifies
// the bit times, so don't rely on it too much at high baud rates
#define _DEBUG 0
#define _DEBUG_PIN1 11
#define _DEBUG_PIN2 13
//
// Includes
//
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "Arduino.h"
#include "SoftwareSerial.h"
//
// Lookup table
//
typedef struct _DELAY_TABLE
{
long baud;
unsigned short rx_delay_centering;
unsigned short rx_delay_intrabit;
unsigned short rx_delay_stopbit;
unsigned short tx_delay;
} DELAY_TABLE;
#if F_CPU == 16000000
static const DELAY_TABLE PROGMEM table[] =
{
// baud rxcenter rxintra rxstop tx
{ 115200, 1, 17, 17, 12, },
{ 57600, 10, 37, 37, 33, },
{ 38400, 25, 57, 57, 54, },
{ 31250, 31, 70, 70, 68, },
{ 28800, 34, 77, 77, 74, },
{ 19200, 54, 117, 117, 114, },
{ 14400, 74, 156, 156, 153, },
{ 9600, 114, 236, 236, 233, },
{ 4800, 233, 474, 474, 471, },
{ 2400, 471, 950, 950, 947, },
{ 1200, 947, 1902, 1902, 1899, },
{ 300, 3804, 7617, 7617, 7614, },
};
const int XMIT_START_ADJUSTMENT = 5;
#elif F_CPU == 8000000
static const DELAY_TABLE table[] PROGMEM =
{
// baud rxcenter rxintra rxstop tx
{ 115200, 1, 5, 5, 3, },
{ 57600, 1, 15, 15, 13, },
{ 38400, 2, 25, 26, 23, },
{ 31250, 7, 32, 33, 29, },
{ 28800, 11, 35, 35, 32, },
{ 19200, 20, 55, 55, 52, },
{ 14400, 30, 75, 75, 72, },
{ 9600, 50, 114, 114, 112, },
{ 4800, 110, 233, 233, 230, },
{ 2400, 229, 472, 472, 469, },
{ 1200, 467, 948, 948, 945, },
{ 300, 1895, 3805, 3805, 3802, },
};
const int XMIT_START_ADJUSTMENT = 4;
#elif F_CPU == 20000000
// 20MHz support courtesy of the good people at macegr.com.
// Thanks, Garrett!
static const DELAY_TABLE PROGMEM table[] =
{
// baud rxcenter rxintra rxstop tx
{ 115200, 3, 21, 21, 18, },
{ 57600, 20, 43, 43, 41, },
{ 38400, 37, 73, 73, 70, },
{ 31250, 45, 89, 89, 88, },
{ 28800, 46, 98, 98, 95, },
{ 19200, 71, 148, 148, 145, },
{ 14400, 96, 197, 197, 194, },
{ 9600, 146, 297, 297, 294, },
{ 4800, 296, 595, 595, 592, },
{ 2400, 592, 1189, 1189, 1186, },
{ 1200, 1187, 2379, 2379, 2376, },
{ 300, 4759, 9523, 9523, 9520, },
};
const int XMIT_START_ADJUSTMENT = 6;
#else
#error This version of SoftwareSerial supports only 20, 16 and 8MHz processors
#endif
//
// Statics
//
SoftwareSerial *SoftwareSerial::active_object = 0;
char SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF];
volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
//
// Debugging
//
// This function generates a brief pulse
// for debugging or measuring on an oscilloscope.
inline void DebugPulse(uint8_t pin, uint8_t count)
{
#if _DEBUG
volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));
uint8_t val = *pport;
while (count--)
{
*pport = val | digitalPinToBitMask(pin);
*pport = val;
}
#endif
}
//
// Private methods
//
/* static */
inline void SoftwareSerial::tunedDelay(uint16_t delay) {
uint8_t tmp=0;
asm volatile("sbiw %0, 0x01 \n\t"
"ldi %1, 0xFF \n\t"
"cpi %A0, 0xFF \n\t"
"cpc %B0, %1 \n\t"
"brne .-10 \n\t"
: "+r" (delay), "+a" (tmp)
: "0" (delay)
);
}
// This function sets the current object as the "listening"
// one and returns true if it replaces another
bool SoftwareSerial::listen()
{
if (active_object != this)
{
_buffer_overflow = false;
uint8_t oldSREG = SREG;
cli();
_receive_buffer_head = _receive_buffer_tail = 0;
active_object = this;
SREG = oldSREG;
return true;
}
return false;
}
//
// The receive routine called by the interrupt handler
//
void SoftwareSerial::recv()
{
#if GCC_VERSION < 40302
// Work-around for avr-gcc 4.3.0 OSX version bug
// Preserve the registers that the compiler misses
// (courtesy of Arduino forum user *etracer*)
asm volatile(
"push r18 \n\t"
"push r19 \n\t"
"push r20 \n\t"
"push r21 \n\t"
"push r22 \n\t"
"push r23 \n\t"
"push r26 \n\t"
"push r27 \n\t"
::);
#endif
uint8_t d = 0;
// If RX line is high, then we don't see any start bit
// so interrupt is probably not for us
if (_inverse_logic ? rx_pin_read() : !rx_pin_read())
{
// Wait approximately 1/2 of a bit width to "center" the sample
tunedDelay(_rx_delay_centering);
DebugPulse(_DEBUG_PIN2, 1);
// Read each of the 8 bits
for (uint8_t i=0x1; i; i <<= 1)
{
tunedDelay(_rx_delay_intrabit);
DebugPulse(_DEBUG_PIN2, 1);
uint8_t noti = ~i;
if (rx_pin_read())
d |= i;
else // else clause added to ensure function timing is ~balanced
d &= noti;
}
// skip the stop bit
tunedDelay(_rx_delay_stopbit);
DebugPulse(_DEBUG_PIN2, 1);
if (_inverse_logic)
d = ~d;
// if buffer full, set the overflow flag and return
if ((_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF != _receive_buffer_head)
{
// save new data in buffer: tail points to where byte goes
_receive_buffer[_receive_buffer_tail] = d; // save new byte
_receive_buffer_tail = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
}
else
{
#if _DEBUG // for scope: pulse pin as overflow indictator
DebugPulse(_DEBUG_PIN1, 1);
#endif
_buffer_overflow = true;
}
}
#if GCC_VERSION < 40302
// Work-around for avr-gcc 4.3.0 OSX version bug
// Restore the registers that the compiler misses
asm volatile(
"pop r27 \n\t"
"pop r26 \n\t"
"pop r23 \n\t"
"pop r22 \n\t"
"pop r21 \n\t"
"pop r20 \n\t"
"pop r19 \n\t"
"pop r18 \n\t"
::);
#endif
}
void SoftwareSerial::tx_pin_write(uint8_t pin_state)
{
if (pin_state == LOW)
*_transmitPortRegister &= ~_transmitBitMask;
else
*_transmitPortRegister |= _transmitBitMask;
}
uint8_t SoftwareSerial::rx_pin_read()
{
return *_receivePortRegister & _receiveBitMask;
}
//
// Interrupt handling
//
/* static */
inline void SoftwareSerial::handle_interrupt()
{
if (active_object)
{
active_object->recv();
}
}
#if defined(PCINT0_vect)
ISR(PCINT0_vect)
{
SoftwareSerial::handle_interrupt();
}
#endif
#if defined(PCINT1_vect)
ISR(PCINT1_vect)
{
SoftwareSerial::handle_interrupt();
}
#endif
#if defined(PCINT2_vect)
ISR(PCINT2_vect)
{
SoftwareSerial::handle_interrupt();
}
#endif
#if defined(PCINT3_vect)
ISR(PCINT3_vect)
{
SoftwareSerial::handle_interrupt();
}
#endif
//
// Constructor
//
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
_rx_delay_centering(0),
_rx_delay_intrabit(0),
_rx_delay_stopbit(0),
_tx_delay(0),
_buffer_overflow(false),
_inverse_logic(inverse_logic)
{
setTX(transmitPin);
setRX(receivePin);
}
//
// Destructor
//
SoftwareSerial::~SoftwareSerial()
{
end();
}
void SoftwareSerial::setTX(uint8_t tx)
{
pinMode(tx, OUTPUT);
digitalWrite(tx, HIGH);
_transmitBitMask = digitalPinToBitMask(tx);
uint8_t port = digitalPinToPort(tx);
_transmitPortRegister = portOutputRegister(port);
}
void SoftwareSerial::setRX(uint8_t rx)
{
pinMode(rx, INPUT);
if (!_inverse_logic)
digitalWrite(rx, HIGH); // pullup for normal logic!
_receivePin = rx;
_receiveBitMask = digitalPinToBitMask(rx);
uint8_t port = digitalPinToPort(rx);
_receivePortRegister = portInputRegister(port);
}
//
// Public methods
//
void SoftwareSerial::begin(long speed)
{
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
{
long baud = pgm_read_dword(&table[i].baud);
if (baud == speed)
{
_rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering);
_rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit);
_rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit);
_tx_delay = pgm_read_word(&table[i].tx_delay);
break;
}
}
// Set up RX interrupts, but only if we have a valid RX baud rate
if (_rx_delay_stopbit)
{
if (digitalPinToPCICR(_receivePin))
{
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
*digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
}
tunedDelay(_tx_delay); // if we were low this establishes the end
}
#if _DEBUG
pinMode(_DEBUG_PIN1, OUTPUT);
pinMode(_DEBUG_PIN2, OUTPUT);
#endif
listen();
}
void SoftwareSerial::end()
{
if (digitalPinToPCMSK(_receivePin))
*digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
}
// Read data from buffer
int SoftwareSerial::read()
{
if (!isListening())
return -1;
// Empty buffer?
if (_receive_buffer_head == _receive_buffer_tail)
return -1;
// Read from "head"
uint8_t d = _receive_buffer[_receive_buffer_head]; // grab next byte
_receive_buffer_head = (_receive_buffer_head + 1) % _SS_MAX_RX_BUFF;
return d;
}
int SoftwareSerial::available()
{
if (!isListening())
return 0;
return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
}
size_t SoftwareSerial::write(uint8_t b)
{
if (_tx_delay == 0) {
setWriteError();
return 0;
}
uint8_t oldSREG = SREG;
cli(); // turn off interrupts for a clean txmit
// Write the start bit
tx_pin_write(_inverse_logic ? HIGH : LOW);
tunedDelay(_tx_delay + XMIT_START_ADJUSTMENT);
// Write each of the 8 bits
if (_inverse_logic)
{
for (byte mask = 0x01; mask; mask <<= 1)
{
if (b & mask) // choose bit
tx_pin_write(LOW); // send 1
else
tx_pin_write(HIGH); // send 0
tunedDelay(_tx_delay);
}
tx_pin_write(LOW); // restore pin to natural state
}
else
{
for (byte mask = 0x01; mask; mask <<= 1)
{
if (b & mask) // choose bit
tx_pin_write(HIGH); // send 1
else
tx_pin_write(LOW); // send 0
tunedDelay(_tx_delay);
}
tx_pin_write(HIGH); // restore pin to natural state
}
SREG = oldSREG; // turn interrupts back on
tunedDelay(_tx_delay);
return 1;
}
void SoftwareSerial::flush()
{
if (!isListening())
return;
uint8_t oldSREG = SREG;
cli();
_receive_buffer_head = _receive_buffer_tail = 0;
SREG = oldSREG;
}
int SoftwareSerial::peek()
{
if (!isListening())
return -1;
// Empty buffer?
if (_receive_buffer_head == _receive_buffer_tail)
return -1;
// Read from "head"
return _receive_buffer[_receive_buffer_head];
}

View File

@ -1,56 +1,110 @@
/*
SoftwareSerial.h - Software serial library
Copyright (c) 2006 David A. Mellis. 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 SoftwareSerial_h
#define SoftwareSerial_h
#include <inttypes.h>
class SoftwareSerial
{
private:
uint8_t _receivePin;
uint8_t _transmitPin;
long _baudRate;
int _bitPeriod;
void printNumber(unsigned long, uint8_t);
public:
SoftwareSerial(uint8_t, uint8_t);
void begin(long);
int read();
void print(char);
void print(const char[]);
void print(uint8_t);
void print(int);
void print(unsigned int);
void print(long);
void print(unsigned long);
void print(long, int);
void println(void);
void println(char);
void println(const char[]);
void println(uint8_t);
void println(int);
void println(long);
void println(unsigned long);
void println(long, int);
};
#endif
/*
SoftwareSerial.h (formerly NewSoftSerial.h) -
Multi-instance software serial library for Arduino/Wiring
-- Interrupt-driven receive and other improvements by ladyada
(http://ladyada.net)
-- Tuning, circular buffer, derivation from class Print/Stream,
multi-instance support, porting to 8MHz processors,
various optimizations, PROGMEM delay tables, inverse logic and
direct port writing by Mikal Hart (http://www.arduiniana.org)
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.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
The latest version of this library can always be found at
http://arduiniana.org.
*/
#ifndef SoftwareSerial_h
#define SoftwareSerial_h
#include <inttypes.h>
#include <Stream.h>
/******************************************************************************
* Definitions
******************************************************************************/
#define _SS_MAX_RX_BUFF 64 // RX buffer size
#ifndef GCC_VERSION
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif
class SoftwareSerial : public Stream
{
private:
// per object data
uint8_t _receivePin;
uint8_t _receiveBitMask;
volatile uint8_t *_receivePortRegister;
uint8_t _transmitBitMask;
volatile uint8_t *_transmitPortRegister;
uint16_t _rx_delay_centering;
uint16_t _rx_delay_intrabit;
uint16_t _rx_delay_stopbit;
uint16_t _tx_delay;
uint16_t _buffer_overflow:1;
uint16_t _inverse_logic:1;
// static data
static char _receive_buffer[_SS_MAX_RX_BUFF];
static volatile uint8_t _receive_buffer_tail;
static volatile uint8_t _receive_buffer_head;
static SoftwareSerial *active_object;
// private methods
void recv();
uint8_t rx_pin_read();
void tx_pin_write(uint8_t pin_state);
void setTX(uint8_t transmitPin);
void setRX(uint8_t receivePin);
// private static method for timing
static inline void tunedDelay(uint16_t delay);
public:
// public methods
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
~SoftwareSerial();
void begin(long speed);
bool listen();
void end();
bool isListening() { return this == active_object; }
bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
int peek();
virtual size_t write(uint8_t byte);
virtual int read();
virtual int available();
virtual void flush();
// public only for easy access by interrupt handlers
static inline void handle_interrupt();
};
// Arduino 0012 workaround
#undef int
#undef char
#undef long
#undef byte
#undef float
#undef abs
#undef round
#endif

View File

@ -0,0 +1,21 @@
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
void setup()
{
Serial.begin(57600);
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}

View File

@ -0,0 +1,50 @@
#include <SoftwareSerial.h>
SoftwareSerial ss(2, 3);
SoftwareSerial ss2(4, 5);
/* This sample shows how to correctly process received data
on two different "soft" serial ports. Here we listen on
the first port (ss) until we receive a '?' character. Then
we begin listening on the other soft port.
*/
void setup()
{
// Start the HW serial port
Serial.begin(57600);
// Start each soft serial port
ss.begin(4800);
ss2.begin(4800);
// By default, the most recently "begun" port is listening.
// We want to listen on ss, so let's explicitly select it.
ss.listen();
// Simply wait for a ? character to come down the pipe
Serial.println("Data from the first port: ");
char c = 0;
do
if (ss.available())
{
c = (char)ss.read();
Serial.print(c);
}
while (c != '?');
// Now listen on the second port
ss2.listen();
Serial.println("Data from the second port: ");
}
void loop()
{
if (ss2.available())
{
char c = (char)ss2.read();
Serial.print(c);
}
}

View File

@ -0,0 +1,78 @@
/*
Software serial multple serial test
Receives from the two software serial ports,
sends to the hardware serial port.
In order to listen on a software port, you call port.listen().
When using two software serial ports, you have to switch ports
by listen()ing on each one in turn. Pick a logical time to switch
ports, like the end of an expected transmission, or when the
buffer is empty. This example switches ports when there is nothing
more to read from a port
The circuit:
Two devices which communicate serially are needed.
* First serial device's TX attached to digital pin 2, RX to pin 3
* Second serial device's TX attached to digital pin 4, RX to pin 5
created 18 Apr. 2011
by Tom Igoe
based on Mikal Hart's twoPortRXExample
This example code is in the public domain.
*/
#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 2, RX = digital pin 3
SoftwareSerial portOne(2, 3);
// software serial #2: TX = digital pin 4, RX = digital pin 5
SoftwareSerial portTwo(4, 5);
void setup()
{
// Start the hardware serial port
Serial.begin(9600);
// Start each software serial port
portOne.begin(9600);
portTwo.begin(9600);
}
void loop()
{
// By default, the last intialized port is listening.
// when you want to listen on a port, explicitly select it:
portOne.listen();
Serial.println("Data from port one:");
// while there is data coming in, read it
// and send to the hardware serial port:
while (portOne.available() > 0) {
char inByte = portOne.read();
Serial.write(inByte);
}
// blank line to separate data from the two ports:
Serial.println();
// Now listen on the second port
portTwo.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
Serial.println("Data from port two:");
while (portTwo.available() > 0) {
char inByte = portTwo.read();
Serial.write(inByte);
}
// blank line to separate data from the two ports:
Serial.println();
}

13
libraries/SoftwareSerial/keywords.txt Normal file → Executable file
View File

@ -1,17 +1,26 @@
#######################################
# Syntax Coloring Map For Ultrasound
# Syntax Coloring Map for NewSoftSerial
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
SoftwareSerial KEYWORD1
NewSoftSerial KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
end KEYWORD2
read KEYWORD2
available KEYWORD2
isListening KEYWORD2
overflow KEYWORD2
flush KEYWORD2
listen KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################

View File

@ -1,95 +0,0 @@
/*
Sprite.cpp - 2D sprite buffer library for Arduino & Wiring
Copyright (c) 2006 David A. Mellis. 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 <stdlib.h>
#include <stdarg.h>
//#include <stdio.h>
#include "Sprite.h"
void Sprite::init(uint8_t width, uint8_t height)
{
_width = width >= 8 ? 8 : width;
_height = height >= 8 ? 8 : height;
// for now, do nothing if this allocation fails. methods that require it
// should silently fail if _buffer is null.
_buffer = (uint8_t *) calloc(_height, 1);
}
Sprite::Sprite(uint8_t width, uint8_t height)
{
init(width, height);
}
Sprite::Sprite(uint8_t width, uint8_t height, uint8_t row, ...)
{
init(width, height);
if (!_buffer) return;
va_list ap;
va_start(ap, row);
int y = 0;
for (y = 0; ; y++) {
for (int x = 0; x < width && x < 8; x++)
write(x, y, (row >> (width - x - 1)) & 0x01);
if (y == height - 1)
break;
row = va_arg(ap, int); // using '...' promotes uint8_t to int
}
va_end(ap);
}
uint8_t Sprite::width() const
{
return _width;
}
uint8_t Sprite::height() const
{
return _height;
}
void Sprite::write(uint8_t x, uint8_t y, uint8_t value)
{
if (!_buffer) return;
// uint8_t's can't be negative, so don't test for negative x and y.
if (x >= _width || y >= _height) return;
// we need to bitwise-or the value of the other pixels in the byte with
// the new value, masked and shifted into the proper bits.
_buffer[y] = (_buffer[y] & ~(0x01 << x)) | ((value & 0x01) << x);
}
uint8_t Sprite::read(uint8_t x, uint8_t y) const
{
if (!_buffer) return 0;
// uint8_t's can't be negative, so don't test for negative x and y.
if (x >= _width || y >= _height) return 0;
return (_buffer[y] >> x) & 0x01;
}

View File

@ -1,48 +0,0 @@
/*
Sprite.cpp - 2D sprite buffers library for Arduino & Wiring
Copyright (c) 2006 David A. Mellis. 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 Sprite_h
#define Sprite_h
#include <inttypes.h>
#include "binary.h"
class Sprite
{
private:
uint8_t _width;
uint8_t _height;
uint8_t _depth;
uint8_t _ppb;
uint8_t _bpr;
uint8_t _mask;
uint8_t *_buffer;
void init(uint8_t width, uint8_t height);
public:
Sprite(uint8_t width, uint8_t height);
Sprite(uint8_t width, uint8_t height, uint8_t row, ...);
uint8_t width() const;
uint8_t height() const;
void write(uint8_t x, uint8_t y, uint8_t value);
uint8_t read(uint8_t x, uint8_t y) const;
};
#endif

View File

@ -1,515 +0,0 @@
#ifndef Binary_h
#define Binary_h
#define B0 0
#define B00 0
#define B000 0
#define B0000 0
#define B00000 0
#define B000000 0
#define B0000000 0
#define B00000000 0
#define B1 1
#define B01 1
#define B001 1
#define B0001 1
#define B00001 1
#define B000001 1
#define B0000001 1
#define B00000001 1
#define B10 2
#define B010 2
#define B0010 2
#define B00010 2
#define B000010 2
#define B0000010 2
#define B00000010 2
#define B11 3
#define B011 3
#define B0011 3
#define B00011 3
#define B000011 3
#define B0000011 3
#define B00000011 3
#define B100 4
#define B0100 4
#define B00100 4
#define B000100 4
#define B0000100 4
#define B00000100 4
#define B101 5
#define B0101 5
#define B00101 5
#define B000101 5
#define B0000101 5
#define B00000101 5
#define B110 6
#define B0110 6
#define B00110 6
#define B000110 6
#define B0000110 6
#define B00000110 6
#define B111 7
#define B0111 7
#define B00111 7
#define B000111 7
#define B0000111 7
#define B00000111 7
#define B1000 8
#define B01000 8
#define B001000 8
#define B0001000 8
#define B00001000 8
#define B1001 9
#define B01001 9
#define B001001 9
#define B0001001 9
#define B00001001 9
#define B1010 10
#define B01010 10
#define B001010 10
#define B0001010 10
#define B00001010 10
#define B1011 11
#define B01011 11
#define B001011 11
#define B0001011 11
#define B00001011 11
#define B1100 12
#define B01100 12
#define B001100 12
#define B0001100 12
#define B00001100 12
#define B1101 13
#define B01101 13
#define B001101 13
#define B0001101 13
#define B00001101 13
#define B1110 14
#define B01110 14
#define B001110 14
#define B0001110 14
#define B00001110 14
#define B1111 15
#define B01111 15
#define B001111 15
#define B0001111 15
#define B00001111 15
#define B10000 16
#define B010000 16
#define B0010000 16
#define B00010000 16
#define B10001 17
#define B010001 17
#define B0010001 17
#define B00010001 17
#define B10010 18
#define B010010 18
#define B0010010 18
#define B00010010 18
#define B10011 19
#define B010011 19
#define B0010011 19
#define B00010011 19
#define B10100 20
#define B010100 20
#define B0010100 20
#define B00010100 20
#define B10101 21
#define B010101 21
#define B0010101 21
#define B00010101 21
#define B10110 22
#define B010110 22
#define B0010110 22
#define B00010110 22
#define B10111 23
#define B010111 23
#define B0010111 23
#define B00010111 23
#define B11000 24
#define B011000 24
#define B0011000 24
#define B00011000 24
#define B11001 25
#define B011001 25
#define B0011001 25
#define B00011001 25
#define B11010 26
#define B011010 26
#define B0011010 26
#define B00011010 26
#define B11011 27
#define B011011 27
#define B0011011 27
#define B00011011 27
#define B11100 28
#define B011100 28
#define B0011100 28
#define B00011100 28
#define B11101 29
#define B011101 29
#define B0011101 29
#define B00011101 29
#define B11110 30
#define B011110 30
#define B0011110 30
#define B00011110 30
#define B11111 31
#define B011111 31
#define B0011111 31
#define B00011111 31
#define B100000 32
#define B0100000 32
#define B00100000 32
#define B100001 33
#define B0100001 33
#define B00100001 33
#define B100010 34
#define B0100010 34
#define B00100010 34
#define B100011 35
#define B0100011 35
#define B00100011 35
#define B100100 36
#define B0100100 36
#define B00100100 36
#define B100101 37
#define B0100101 37
#define B00100101 37
#define B100110 38
#define B0100110 38
#define B00100110 38
#define B100111 39
#define B0100111 39
#define B00100111 39
#define B101000 40
#define B0101000 40
#define B00101000 40
#define B101001 41
#define B0101001 41
#define B00101001 41
#define B101010 42
#define B0101010 42
#define B00101010 42
#define B101011 43
#define B0101011 43
#define B00101011 43
#define B101100 44
#define B0101100 44
#define B00101100 44
#define B101101 45
#define B0101101 45
#define B00101101 45
#define B101110 46
#define B0101110 46
#define B00101110 46
#define B101111 47
#define B0101111 47
#define B00101111 47
#define B110000 48
#define B0110000 48
#define B00110000 48
#define B110001 49
#define B0110001 49
#define B00110001 49
#define B110010 50
#define B0110010 50
#define B00110010 50
#define B110011 51
#define B0110011 51
#define B00110011 51
#define B110100 52
#define B0110100 52
#define B00110100 52
#define B110101 53
#define B0110101 53
#define B00110101 53
#define B110110 54
#define B0110110 54
#define B00110110 54
#define B110111 55
#define B0110111 55
#define B00110111 55
#define B111000 56
#define B0111000 56
#define B00111000 56
#define B111001 57
#define B0111001 57
#define B00111001 57
#define B111010 58
#define B0111010 58
#define B00111010 58
#define B111011 59
#define B0111011 59
#define B00111011 59
#define B111100 60
#define B0111100 60
#define B00111100 60
#define B111101 61
#define B0111101 61
#define B00111101 61
#define B111110 62
#define B0111110 62
#define B00111110 62
#define B111111 63
#define B0111111 63
#define B00111111 63
#define B1000000 64
#define B01000000 64
#define B1000001 65
#define B01000001 65
#define B1000010 66
#define B01000010 66
#define B1000011 67
#define B01000011 67
#define B1000100 68
#define B01000100 68
#define B1000101 69
#define B01000101 69
#define B1000110 70
#define B01000110 70
#define B1000111 71
#define B01000111 71
#define B1001000 72
#define B01001000 72
#define B1001001 73
#define B01001001 73
#define B1001010 74
#define B01001010 74
#define B1001011 75
#define B01001011 75
#define B1001100 76
#define B01001100 76
#define B1001101 77
#define B01001101 77
#define B1001110 78
#define B01001110 78
#define B1001111 79
#define B01001111 79
#define B1010000 80
#define B01010000 80
#define B1010001 81
#define B01010001 81
#define B1010010 82
#define B01010010 82
#define B1010011 83
#define B01010011 83
#define B1010100 84
#define B01010100 84
#define B1010101 85
#define B01010101 85
#define B1010110 86
#define B01010110 86
#define B1010111 87
#define B01010111 87
#define B1011000 88
#define B01011000 88
#define B1011001 89
#define B01011001 89
#define B1011010 90
#define B01011010 90
#define B1011011 91
#define B01011011 91
#define B1011100 92
#define B01011100 92
#define B1011101 93
#define B01011101 93
#define B1011110 94
#define B01011110 94
#define B1011111 95
#define B01011111 95
#define B1100000 96
#define B01100000 96
#define B1100001 97
#define B01100001 97
#define B1100010 98
#define B01100010 98
#define B1100011 99
#define B01100011 99
#define B1100100 100
#define B01100100 100
#define B1100101 101
#define B01100101 101
#define B1100110 102
#define B01100110 102
#define B1100111 103
#define B01100111 103
#define B1101000 104
#define B01101000 104
#define B1101001 105
#define B01101001 105
#define B1101010 106
#define B01101010 106
#define B1101011 107
#define B01101011 107
#define B1101100 108
#define B01101100 108
#define B1101101 109
#define B01101101 109
#define B1101110 110
#define B01101110 110
#define B1101111 111
#define B01101111 111
#define B1110000 112
#define B01110000 112
#define B1110001 113
#define B01110001 113
#define B1110010 114
#define B01110010 114
#define B1110011 115
#define B01110011 115
#define B1110100 116
#define B01110100 116
#define B1110101 117
#define B01110101 117
#define B1110110 118
#define B01110110 118
#define B1110111 119
#define B01110111 119
#define B1111000 120
#define B01111000 120
#define B1111001 121
#define B01111001 121
#define B1111010 122
#define B01111010 122
#define B1111011 123
#define B01111011 123
#define B1111100 124
#define B01111100 124
#define B1111101 125
#define B01111101 125
#define B1111110 126
#define B01111110 126
#define B1111111 127
#define B01111111 127
#define B10000000 128
#define B10000001 129
#define B10000010 130
#define B10000011 131
#define B10000100 132
#define B10000101 133
#define B10000110 134
#define B10000111 135
#define B10001000 136
#define B10001001 137
#define B10001010 138
#define B10001011 139
#define B10001100 140
#define B10001101 141
#define B10001110 142
#define B10001111 143
#define B10010000 144
#define B10010001 145
#define B10010010 146
#define B10010011 147
#define B10010100 148
#define B10010101 149
#define B10010110 150
#define B10010111 151
#define B10011000 152
#define B10011001 153
#define B10011010 154
#define B10011011 155
#define B10011100 156
#define B10011101 157
#define B10011110 158
#define B10011111 159
#define B10100000 160
#define B10100001 161
#define B10100010 162
#define B10100011 163
#define B10100100 164
#define B10100101 165
#define B10100110 166
#define B10100111 167
#define B10101000 168
#define B10101001 169
#define B10101010 170
#define B10101011 171
#define B10101100 172
#define B10101101 173
#define B10101110 174
#define B10101111 175
#define B10110000 176
#define B10110001 177
#define B10110010 178
#define B10110011 179
#define B10110100 180
#define B10110101 181
#define B10110110 182
#define B10110111 183
#define B10111000 184
#define B10111001 185
#define B10111010 186
#define B10111011 187
#define B10111100 188
#define B10111101 189
#define B10111110 190
#define B10111111 191
#define B11000000 192
#define B11000001 193
#define B11000010 194
#define B11000011 195
#define B11000100 196
#define B11000101 197
#define B11000110 198
#define B11000111 199
#define B11001000 200
#define B11001001 201
#define B11001010 202
#define B11001011 203
#define B11001100 204
#define B11001101 205
#define B11001110 206
#define B11001111 207
#define B11010000 208
#define B11010001 209
#define B11010010 210
#define B11010011 211
#define B11010100 212
#define B11010101 213
#define B11010110 214
#define B11010111 215
#define B11011000 216
#define B11011001 217
#define B11011010 218
#define B11011011 219
#define B11011100 220
#define B11011101 221
#define B11011110 222
#define B11011111 223
#define B11100000 224
#define B11100001 225
#define B11100010 226
#define B11100011 227
#define B11100100 228
#define B11100101 229
#define B11100110 230
#define B11100111 231
#define B11101000 232
#define B11101001 233
#define B11101010 234
#define B11101011 235
#define B11101100 236
#define B11101101 237
#define B11101110 238
#define B11101111 239
#define B11110000 240
#define B11110001 241
#define B11110010 242
#define B11110011 243
#define B11110100 244
#define B11110101 245
#define B11110110 246
#define B11110111 247
#define B11111000 248
#define B11111001 249
#define B11111010 250
#define B11111011 251
#define B11111100 252
#define B11111101 253
#define B11111110 254
#define B11111111 255
#endif

View File

@ -1,534 +0,0 @@
#######################################
# Syntax Coloring Map For Sprite
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Sprite KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
width KEYWORD2
height KEYWORD2
write KEYWORD2
read KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
B0 LITERAL1
B00 LITERAL1
B000 LITERAL1
B0000 LITERAL1
B00000 LITERAL1
B000000 LITERAL1
B0000000 LITERAL1
B00000000 LITERAL1
B1 LITERAL1
B01 LITERAL1
B001 LITERAL1
B0001 LITERAL1
B00001 LITERAL1
B000001 LITERAL1
B0000001 LITERAL1
B00000001 LITERAL1
B10 LITERAL1
B010 LITERAL1
B0010 LITERAL1
B00010 LITERAL1
B000010 LITERAL1
B0000010 LITERAL1
B00000010 LITERAL1
B11 LITERAL1
B011 LITERAL1
B0011 LITERAL1
B00011 LITERAL1
B000011 LITERAL1
B0000011 LITERAL1
B00000011 LITERAL1
B100 LITERAL1
B0100 LITERAL1
B00100 LITERAL1
B000100 LITERAL1
B0000100 LITERAL1
B00000100 LITERAL1
B101 LITERAL1
B0101 LITERAL1
B00101 LITERAL1
B000101 LITERAL1
B0000101 LITERAL1
B00000101 LITERAL1
B110 LITERAL1
B0110 LITERAL1
B00110 LITERAL1
B000110 LITERAL1
B0000110 LITERAL1
B00000110 LITERAL1
B111 LITERAL1
B0111 LITERAL1
B00111 LITERAL1
B000111 LITERAL1
B0000111 LITERAL1
B00000111 LITERAL1
B1000 LITERAL1
B01000 LITERAL1
B001000 LITERAL1
B0001000 LITERAL1
B00001000 LITERAL1
B1001 LITERAL1
B01001 LITERAL1
B001001 LITERAL1
B0001001 LITERAL1
B00001001 LITERAL1
B1010 LITERAL1
B01010 LITERAL1
B001010 LITERAL1
B0001010 LITERAL1
B00001010 LITERAL1
B1011 LITERAL1
B01011 LITERAL1
B001011 LITERAL1
B0001011 LITERAL1
B00001011 LITERAL1
B1100 LITERAL1
B01100 LITERAL1
B001100 LITERAL1
B0001100 LITERAL1
B00001100 LITERAL1
B1101 LITERAL1
B01101 LITERAL1
B001101 LITERAL1
B0001101 LITERAL1
B00001101 LITERAL1
B1110 LITERAL1
B01110 LITERAL1
B001110 LITERAL1
B0001110 LITERAL1
B00001110 LITERAL1
B1111 LITERAL1
B01111 LITERAL1
B001111 LITERAL1
B0001111 LITERAL1
B00001111 LITERAL1
B10000 LITERAL1
B010000 LITERAL1
B0010000 LITERAL1
B00010000 LITERAL1
B10001 LITERAL1
B010001 LITERAL1
B0010001 LITERAL1
B00010001 LITERAL1
B10010 LITERAL1
B010010 LITERAL1
B0010010 LITERAL1
B00010010 LITERAL1
B10011 LITERAL1
B010011 LITERAL1
B0010011 LITERAL1
B00010011 LITERAL1
B10100 LITERAL1
B010100 LITERAL1
B0010100 LITERAL1
B00010100 LITERAL1
B10101 LITERAL1
B010101 LITERAL1
B0010101 LITERAL1
B00010101 LITERAL1
B10110 LITERAL1
B010110 LITERAL1
B0010110 LITERAL1
B00010110 LITERAL1
B10111 LITERAL1
B010111 LITERAL1
B0010111 LITERAL1
B00010111 LITERAL1
B11000 LITERAL1
B011000 LITERAL1
B0011000 LITERAL1
B00011000 LITERAL1
B11001 LITERAL1
B011001 LITERAL1
B0011001 LITERAL1
B00011001 LITERAL1
B11010 LITERAL1
B011010 LITERAL1
B0011010 LITERAL1
B00011010 LITERAL1
B11011 LITERAL1
B011011 LITERAL1
B0011011 LITERAL1
B00011011 LITERAL1
B11100 LITERAL1
B011100 LITERAL1
B0011100 LITERAL1
B00011100 LITERAL1
B11101 LITERAL1
B011101 LITERAL1
B0011101 LITERAL1
B00011101 LITERAL1
B11110 LITERAL1
B011110 LITERAL1
B0011110 LITERAL1
B00011110 LITERAL1
B11111 LITERAL1
B011111 LITERAL1
B0011111 LITERAL1
B00011111 LITERAL1
B100000 LITERAL1
B0100000 LITERAL1
B00100000 LITERAL1
B100001 LITERAL1
B0100001 LITERAL1
B00100001 LITERAL1
B100010 LITERAL1
B0100010 LITERAL1
B00100010 LITERAL1
B100011 LITERAL1
B0100011 LITERAL1
B00100011 LITERAL1
B100100 LITERAL1
B0100100 LITERAL1
B00100100 LITERAL1
B100101 LITERAL1
B0100101 LITERAL1
B00100101 LITERAL1
B100110 LITERAL1
B0100110 LITERAL1
B00100110 LITERAL1
B100111 LITERAL1
B0100111 LITERAL1
B00100111 LITERAL1
B101000 LITERAL1
B0101000 LITERAL1
B00101000 LITERAL1
B101001 LITERAL1
B0101001 LITERAL1
B00101001 LITERAL1
B101010 LITERAL1
B0101010 LITERAL1
B00101010 LITERAL1
B101011 LITERAL1
B0101011 LITERAL1
B00101011 LITERAL1
B101100 LITERAL1
B0101100 LITERAL1
B00101100 LITERAL1
B101101 LITERAL1
B0101101 LITERAL1
B00101101 LITERAL1
B101110 LITERAL1
B0101110 LITERAL1
B00101110 LITERAL1
B101111 LITERAL1
B0101111 LITERAL1
B00101111 LITERAL1
B110000 LITERAL1
B0110000 LITERAL1
B00110000 LITERAL1
B110001 LITERAL1
B0110001 LITERAL1
B00110001 LITERAL1
B110010 LITERAL1
B0110010 LITERAL1
B00110010 LITERAL1
B110011 LITERAL1
B0110011 LITERAL1
B00110011 LITERAL1
B110100 LITERAL1
B0110100 LITERAL1
B00110100 LITERAL1
B110101 LITERAL1
B0110101 LITERAL1
B00110101 LITERAL1
B110110 LITERAL1
B0110110 LITERAL1
B00110110 LITERAL1
B110111 LITERAL1
B0110111 LITERAL1
B00110111 LITERAL1
B111000 LITERAL1
B0111000 LITERAL1
B00111000 LITERAL1
B111001 LITERAL1
B0111001 LITERAL1
B00111001 LITERAL1
B111010 LITERAL1
B0111010 LITERAL1
B00111010 LITERAL1
B111011 LITERAL1
B0111011 LITERAL1
B00111011 LITERAL1
B111100 LITERAL1
B0111100 LITERAL1
B00111100 LITERAL1
B111101 LITERAL1
B0111101 LITERAL1
B00111101 LITERAL1
B111110 LITERAL1
B0111110 LITERAL1
B00111110 LITERAL1
B111111 LITERAL1
B0111111 LITERAL1
B00111111 LITERAL1
B1000000 LITERAL1
B01000000 LITERAL1
B1000001 LITERAL1
B01000001 LITERAL1
B1000010 LITERAL1
B01000010 LITERAL1
B1000011 LITERAL1
B01000011 LITERAL1
B1000100 LITERAL1
B01000100 LITERAL1
B1000101 LITERAL1
B01000101 LITERAL1
B1000110 LITERAL1
B01000110 LITERAL1
B1000111 LITERAL1
B01000111 LITERAL1
B1001000 LITERAL1
B01001000 LITERAL1
B1001001 LITERAL1
B01001001 LITERAL1
B1001010 LITERAL1
B01001010 LITERAL1
B1001011 LITERAL1
B01001011 LITERAL1
B1001100 LITERAL1
B01001100 LITERAL1
B1001101 LITERAL1
B01001101 LITERAL1
B1001110 LITERAL1
B01001110 LITERAL1
B1001111 LITERAL1
B01001111 LITERAL1
B1010000 LITERAL1
B01010000 LITERAL1
B1010001 LITERAL1
B01010001 LITERAL1
B1010010 LITERAL1
B01010010 LITERAL1
B1010011 LITERAL1
B01010011 LITERAL1
B1010100 LITERAL1
B01010100 LITERAL1
B1010101 LITERAL1
B01010101 LITERAL1
B1010110 LITERAL1
B01010110 LITERAL1
B1010111 LITERAL1
B01010111 LITERAL1
B1011000 LITERAL1
B01011000 LITERAL1
B1011001 LITERAL1
B01011001 LITERAL1
B1011010 LITERAL1
B01011010 LITERAL1
B1011011 LITERAL1
B01011011 LITERAL1
B1011100 LITERAL1
B01011100 LITERAL1
B1011101 LITERAL1
B01011101 LITERAL1
B1011110 LITERAL1
B01011110 LITERAL1
B1011111 LITERAL1
B01011111 LITERAL1
B1100000 LITERAL1
B01100000 LITERAL1
B1100001 LITERAL1
B01100001 LITERAL1
B1100010 LITERAL1
B01100010 LITERAL1
B1100011 LITERAL1
B01100011 LITERAL1
B1100100 LITERAL1
B01100100 LITERAL1
B1100101 LITERAL1
B01100101 LITERAL1
B1100110 LITERAL1
B01100110 LITERAL1
B1100111 LITERAL1
B01100111 LITERAL1
B1101000 LITERAL1
B01101000 LITERAL1
B1101001 LITERAL1
B01101001 LITERAL1
B1101010 LITERAL1
B01101010 LITERAL1
B1101011 LITERAL1
B01101011 LITERAL1
B1101100 LITERAL1
B01101100 LITERAL1
B1101101 LITERAL1
B01101101 LITERAL1
B1101110 LITERAL1
B01101110 LITERAL1
B1101111 LITERAL1
B01101111 LITERAL1
B1110000 LITERAL1
B01110000 LITERAL1
B1110001 LITERAL1
B01110001 LITERAL1
B1110010 LITERAL1
B01110010 LITERAL1
B1110011 LITERAL1
B01110011 LITERAL1
B1110100 LITERAL1
B01110100 LITERAL1
B1110101 LITERAL1
B01110101 LITERAL1
B1110110 LITERAL1
B01110110 LITERAL1
B1110111 LITERAL1
B01110111 LITERAL1
B1111000 LITERAL1
B01111000 LITERAL1
B1111001 LITERAL1
B01111001 LITERAL1
B1111010 LITERAL1
B01111010 LITERAL1
B1111011 LITERAL1
B01111011 LITERAL1
B1111100 LITERAL1
B01111100 LITERAL1
B1111101 LITERAL1
B01111101 LITERAL1
B1111110 LITERAL1
B01111110 LITERAL1
B1111111 LITERAL1
B01111111 LITERAL1
B10000000 LITERAL1
B10000001 LITERAL1
B10000010 LITERAL1
B10000011 LITERAL1
B10000100 LITERAL1
B10000101 LITERAL1
B10000110 LITERAL1
B10000111 LITERAL1
B10001000 LITERAL1
B10001001 LITERAL1
B10001010 LITERAL1
B10001011 LITERAL1
B10001100 LITERAL1
B10001101 LITERAL1
B10001110 LITERAL1
B10001111 LITERAL1
B10010000 LITERAL1
B10010001 LITERAL1
B10010010 LITERAL1
B10010011 LITERAL1
B10010100 LITERAL1
B10010101 LITERAL1
B10010110 LITERAL1
B10010111 LITERAL1
B10011000 LITERAL1
B10011001 LITERAL1
B10011010 LITERAL1
B10011011 LITERAL1
B10011100 LITERAL1
B10011101 LITERAL1
B10011110 LITERAL1
B10011111 LITERAL1
B10100000 LITERAL1
B10100001 LITERAL1
B10100010 LITERAL1
B10100011 LITERAL1
B10100100 LITERAL1
B10100101 LITERAL1
B10100110 LITERAL1
B10100111 LITERAL1
B10101000 LITERAL1
B10101001 LITERAL1
B10101010 LITERAL1
B10101011 LITERAL1
B10101100 LITERAL1
B10101101 LITERAL1
B10101110 LITERAL1
B10101111 LITERAL1
B10110000 LITERAL1
B10110001 LITERAL1
B10110010 LITERAL1
B10110011 LITERAL1
B10110100 LITERAL1
B10110101 LITERAL1
B10110110 LITERAL1
B10110111 LITERAL1
B10111000 LITERAL1
B10111001 LITERAL1
B10111010 LITERAL1
B10111011 LITERAL1
B10111100 LITERAL1
B10111101 LITERAL1
B10111110 LITERAL1
B10111111 LITERAL1
B11000000 LITERAL1
B11000001 LITERAL1
B11000010 LITERAL1
B11000011 LITERAL1
B11000100 LITERAL1
B11000101 LITERAL1
B11000110 LITERAL1
B11000111 LITERAL1
B11001000 LITERAL1
B11001001 LITERAL1
B11001010 LITERAL1
B11001011 LITERAL1
B11001100 LITERAL1
B11001101 LITERAL1
B11001110 LITERAL1
B11001111 LITERAL1
B11010000 LITERAL1
B11010001 LITERAL1
B11010010 LITERAL1
B11010011 LITERAL1
B11010100 LITERAL1
B11010101 LITERAL1
B11010110 LITERAL1
B11010111 LITERAL1
B11011000 LITERAL1
B11011001 LITERAL1
B11011010 LITERAL1
B11011011 LITERAL1
B11011100 LITERAL1
B11011101 LITERAL1
B11011110 LITERAL1
B11011111 LITERAL1
B11100000 LITERAL1
B11100001 LITERAL1
B11100010 LITERAL1
B11100011 LITERAL1
B11100100 LITERAL1
B11100101 LITERAL1
B11100110 LITERAL1
B11100111 LITERAL1
B11101000 LITERAL1
B11101001 LITERAL1
B11101010 LITERAL1
B11101011 LITERAL1
B11101100 LITERAL1
B11101101 LITERAL1
B11101110 LITERAL1
B11101111 LITERAL1
B11110000 LITERAL1
B11110001 LITERAL1
B11110010 LITERAL1
B11110011 LITERAL1
B11110100 LITERAL1
B11110101 LITERAL1
B11110110 LITERAL1
B11110111 LITERAL1
B11111000 LITERAL1
B11111001 LITERAL1
B11111010 LITERAL1
B11111011 LITERAL1
B11111100 LITERAL1
B11111101 LITERAL1
B11111110 LITERAL1
B11111111 LITERAL1

View File

@ -45,7 +45,7 @@ http://www.arduino.cc/en/Tutorial/Stepper
*/
#include "WProgram.h"
#include "Arduino.h"
#include "Stepper.h"
/*

View File

@ -124,13 +124,14 @@ uint8_t TwoWire::endTransmission(void)
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void TwoWire::send(uint8_t data)
size_t TwoWire::write(uint8_t data)
{
if(transmitting){
// in master transmitter mode
// don't bother if buffer is full
if(txBufferLength >= BUFFER_LENGTH){
return;
setWriteError();
return 0;
}
// put byte in tx buffer
txBuffer[txBufferIndex] = data;
@ -142,45 +143,39 @@ void TwoWire::send(uint8_t data)
// reply to master
twi_transmit(&data, 1);
}
return 1;
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void TwoWire::send(uint8_t* data, uint8_t quantity)
size_t TwoWire::write(const uint8_t *data, size_t quantity)
{
if(transmitting){
// in master transmitter mode
for(uint8_t i = 0; i < quantity; ++i){
send(data[i]);
for(size_t i = 0; i < quantity; ++i){
write(data[i]);
}
}else{
// in slave send mode
// reply to master
twi_transmit(data, quantity);
}
return quantity;
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void TwoWire::send(char* data)
size_t TwoWire::write(const char *data)
{
send((uint8_t*)data, strlen(data));
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void TwoWire::send(int data)
{
send((uint8_t)data);
return write((uint8_t*)data, strlen(data));
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
uint8_t TwoWire::available(void)
int TwoWire::available(void)
{
return rxBufferLength - rxBufferIndex;
}
@ -188,11 +183,9 @@ uint8_t TwoWire::available(void)
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
uint8_t TwoWire::receive(void)
int TwoWire::read(void)
{
// default to returning null char
// for people using with char strings
uint8_t value = '\0';
int value = -1;
// get each successive byte on each call
if(rxBufferIndex < rxBufferLength){
@ -203,6 +196,25 @@ uint8_t TwoWire::receive(void)
return value;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int TwoWire::peek(void)
{
int value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
}
return value;
}
void TwoWire::flush(void)
{
// XXX: to be implemented.
}
// behind the scenes function that is called when data is received
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
{

View File

@ -21,10 +21,11 @@
#define TwoWire_h
#include <inttypes.h>
#include "Stream.h"
#define BUFFER_LENGTH 32
class TwoWire
class TwoWire : public Stream
{
private:
static uint8_t rxBuffer[];
@ -51,12 +52,13 @@ class TwoWire
uint8_t endTransmission(void);
uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(int, int);
void send(uint8_t);
void send(uint8_t*, uint8_t);
void send(int);
void send(char*);
uint8_t available(void);
uint8_t receive(void);
virtual size_t write(uint8_t);
virtual size_t write(const char *);
virtual size_t write(const uint8_t *, size_t);
virtual int available(void);
virtual int read(void);
virtual int peek(void);
virtual void flush(void);
void onReceive( void (*)(int) );
void onRequest( void (*)(void) );
};

View File

@ -26,8 +26,8 @@ void loop()
Wire.beginTransmission(112); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.send(0x00); // sets register pointer to the command register (0x00)
Wire.send(0x50); // command sensor to measure in "inches" (0x50)
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting
@ -37,7 +37,7 @@ void loop()
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(112); // transmit to device #112
Wire.send(0x02); // sets register pointer to echo #1 register (0x02)
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
@ -46,9 +46,9 @@ void loop()
// step 5: receive reading from sensor
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.receive(); // receive high byte (overwrites previous reading)
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.receive(); // receive low byte as lower 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
Serial.println(reading); // print the reading
}
@ -64,23 +64,23 @@ void loop()
void changeAddress(byte oldAddress, byte newAddress)
{
Wire.beginTransmission(oldAddress);
Wire.send(0x00);
Wire.send(0xA0);
Wire.write(byte(0x00));
Wire.write(byte(0xA0));
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.send(0x00);
Wire.send(0xAA);
Wire.write(byte(0x00));
Wire.write(byte(0xAA));
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.send(0x00);
Wire.send(0xA5);
Wire.write(byte(0x00));
Wire.write(byte(0xA5));
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.send(0x00);
Wire.send(newAddress);
Wire.write(byte(0x00));
Wire.write(newAddress);
Wire.endTransmission();
}

View File

@ -25,8 +25,8 @@ void loop()
{
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
// device address is specified in datasheet
Wire.send(0x00); // sends instruction byte
Wire.send(val); // sends potentiometer value byte
Wire.write(byte(0x00)); // sends instruction byte
Wire.write(val); // sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
val++; // increment value

View File

@ -24,7 +24,7 @@ void loop()
while(Wire.available()) // slave may send less than requested
{
char c = Wire.receive(); // receive a byte as character
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}

View File

@ -22,8 +22,8 @@ byte x = 0;
void loop()
{
Wire.beginTransmission(4); // transmit to device #4
Wire.send("x is "); // sends five bytes
Wire.send(x); // sends one byte
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
x++;

View File

@ -30,9 +30,9 @@ void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.receive(); // receive byte as a character
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.receive(); // receive byte as an integer
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}

View File

@ -27,6 +27,6 @@ void loop()
// this function is registered as an event, see setup()
void requestEvent()
{
Wire.send("hello "); // respond with message of 6 bytes
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}

View File

@ -79,7 +79,7 @@ void twi_init(void)
// initialize twi prescaler and bit rate
cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1);
TWBR = ((CPU_FREQ / TWI_FREQ) - 16) / 2;
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
/* twi bit rate formula from atmega128 manual pg 204
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
@ -232,7 +232,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
* 2 not slave transmitter
* 0 ok
*/
uint8_t twi_transmit(uint8_t* data, uint8_t length)
uint8_t twi_transmit(const uint8_t* data, uint8_t length)
{
uint8_t i;

View File

@ -24,10 +24,6 @@
//#define ATMEGA8
#ifndef CPU_FREQ
#define CPU_FREQ 16000000L
#endif
#ifndef TWI_FREQ
#define TWI_FREQ 100000L
#endif
@ -46,7 +42,7 @@
void twi_setAddress(uint8_t);
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t);
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t);
uint8_t twi_transmit(uint8_t*, uint8_t);
uint8_t twi_transmit(const uint8_t*, uint8_t);
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );
void twi_attachSlaveTxEvent( void (*)(void) );
void twi_reply(uint8_t);