From db605dd18b11ecfb5cd9f92c721c52cb70543384 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Mon, 1 Jun 2009 08:32:11 +0000 Subject: First integration of the Arduino code in Processing 5503: PreProcessor and Compiler have been integrated with changes to the Sketch. Compilation still has problems (Thread error on success, and can't handle non-pde files in a sketch). Modified the Mac OS X make.sh to copy the hardware, avr tools, and example over. Removing some of the antlr stuff. Disabling the Commander (command-line execution) for now. Added Library, LibraryManager, and Target. Added support for prefixed preferences (e.g. for boards and programmers). --- libraries/EEPROM/EEPROM.cpp | 50 + libraries/EEPROM/EEPROM.h | 35 + .../EEPROM/examples/eeprom_clear/eeprom_clear.pde | 21 + .../EEPROM/examples/eeprom_read/eeprom_read.pde | 38 + .../EEPROM/examples/eeprom_write/eeprom_write.pde | 38 + libraries/EEPROM/keywords.txt | 18 + libraries/Ethernet/Client.cpp | 140 +++ libraries/Ethernet/Client.h | 31 + libraries/Ethernet/Ethernet.cpp | 38 + libraries/Ethernet/Ethernet.h | 22 + libraries/Ethernet/Server.cpp | 91 ++ libraries/Ethernet/Server.h | 25 + .../Ethernet/examples/ChatServer/ChatServer.pde | 34 + .../Ethernet/examples/WebClient/WebClient.pde | 41 + .../Ethernet/examples/WebServer/WebServer.pde | 61 + libraries/Ethernet/utility/socket.c | 558 +++++++++ libraries/Ethernet/utility/socket.h | 23 + libraries/Ethernet/utility/spi.h | 58 + libraries/Ethernet/utility/types.h | 165 +++ libraries/Ethernet/utility/w5100.c | 1302 ++++++++++++++++++++ libraries/Ethernet/utility/w5100.h | 299 +++++ libraries/Firmata/Firmata.cpp | 445 +++++++ libraries/Firmata/Firmata.h | 169 +++ libraries/Firmata/LICENSE.txt | 458 +++++++ libraries/Firmata/TODO.txt | 14 + .../examples/AnalogFirmata/AnalogFirmata.pde | 83 ++ libraries/Firmata/examples/AnalogFirmata/Makefile | 263 ++++ .../Firmata/examples/EchoString/EchoString.pde | 40 + libraries/Firmata/examples/EchoString/Makefile | 263 ++++ libraries/Firmata/examples/ServoFirmata/Makefile | 263 ++++ .../Firmata/examples/ServoFirmata/ServoFirmata.pde | 39 + .../Firmata/examples/SimpleAnalogFirmata/Makefile | 263 ++++ .../SimpleAnalogFirmata/SimpleAnalogFirmata.pde | 32 + .../Firmata/examples/SimpleDigitalFirmata/Makefile | 263 ++++ .../SimpleDigitalFirmata/SimpleDigitalFirmata.pde | 58 + .../Firmata/examples/StandardFirmata/LICENSE.txt | 458 +++++++ .../Firmata/examples/StandardFirmata/Makefile | 261 ++++ .../examples/StandardFirmata/StandardFirmata.pde | 226 ++++ libraries/Firmata/keywords.txt | 62 + libraries/LiquidCrystal/LiquidCrystal.cpp | 128 ++ libraries/LiquidCrystal/LiquidCrystal.h | 31 + .../examples/HelloWorld/HelloWorld.pde | 18 + .../examples/SerialDisplay/SerialDisplay.pde | 34 + libraries/LiquidCrystal/keywords.txt | 23 + libraries/Matrix/Matrix.cpp | 229 ++++ libraries/Matrix/Matrix.h | 54 + .../Matrix/examples/hello_matrix/hello_matrix.pde | 42 + .../examples/sprite_animation/sprite_animation.pde | 48 + libraries/Matrix/keywords.txt | 22 + libraries/Servo/Servo.cpp | 133 ++ libraries/Servo/Servo.h | 52 + libraries/Servo/examples/Knob/Knob.pde | 22 + libraries/Servo/examples/Sweep/Sweep.pde | 29 + libraries/Servo/keywords.txt | 22 + libraries/SoftwareSerial/SoftwareSerial.cpp | 227 ++++ libraries/SoftwareSerial/SoftwareSerial.h | 56 + libraries/SoftwareSerial/keywords.txt | 18 + libraries/Sprite/Sprite.cpp | 95 ++ libraries/Sprite/Sprite.h | 48 + libraries/Sprite/binary.h | 515 ++++++++ libraries/Sprite/keywords.txt | 534 ++++++++ libraries/Stepper/Stepper.cpp | 220 ++++ libraries/Stepper/Stepper.h | 83 ++ libraries/Stepper/examples/MotorKnob/MotorKnob.pde | 40 + libraries/Stepper/keywords.txt | 28 + libraries/Wire/Wire.cpp | 265 ++++ libraries/Wire/Wire.h | 67 + .../examples/SFRRanger_reader/SFRRanger_reader.pde | 84 ++ .../digital_potentiometer.pde | 34 + .../Wire/examples/master_reader/master_reader.pde | 29 + .../Wire/examples/master_writer/master_writer.pde | 28 + .../examples/slave_receiver/slave_receiver.pde | 35 + .../Wire/examples/slave_sender/slave_sender.pde | 29 + libraries/Wire/keywords.txt | 31 + libraries/Wire/utility/twi.c | 474 +++++++ libraries/Wire/utility/twi.h | 57 + 76 files changed, 10632 insertions(+) create mode 100755 libraries/EEPROM/EEPROM.cpp create mode 100755 libraries/EEPROM/EEPROM.h create mode 100644 libraries/EEPROM/examples/eeprom_clear/eeprom_clear.pde create mode 100644 libraries/EEPROM/examples/eeprom_read/eeprom_read.pde create mode 100644 libraries/EEPROM/examples/eeprom_write/eeprom_write.pde create mode 100644 libraries/EEPROM/keywords.txt create mode 100644 libraries/Ethernet/Client.cpp create mode 100644 libraries/Ethernet/Client.h create mode 100644 libraries/Ethernet/Ethernet.cpp create mode 100644 libraries/Ethernet/Ethernet.h create mode 100644 libraries/Ethernet/Server.cpp create mode 100644 libraries/Ethernet/Server.h create mode 100644 libraries/Ethernet/examples/ChatServer/ChatServer.pde create mode 100644 libraries/Ethernet/examples/WebClient/WebClient.pde create mode 100644 libraries/Ethernet/examples/WebServer/WebServer.pde create mode 100755 libraries/Ethernet/utility/socket.c create mode 100755 libraries/Ethernet/utility/socket.h create mode 100755 libraries/Ethernet/utility/spi.h create mode 100755 libraries/Ethernet/utility/types.h create mode 100755 libraries/Ethernet/utility/w5100.c create mode 100755 libraries/Ethernet/utility/w5100.h create mode 100644 libraries/Firmata/Firmata.cpp create mode 100644 libraries/Firmata/Firmata.h create mode 100644 libraries/Firmata/LICENSE.txt create mode 100644 libraries/Firmata/TODO.txt create mode 100644 libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.pde create mode 100644 libraries/Firmata/examples/AnalogFirmata/Makefile create mode 100644 libraries/Firmata/examples/EchoString/EchoString.pde create mode 100644 libraries/Firmata/examples/EchoString/Makefile create mode 100644 libraries/Firmata/examples/ServoFirmata/Makefile create mode 100644 libraries/Firmata/examples/ServoFirmata/ServoFirmata.pde create mode 100644 libraries/Firmata/examples/SimpleAnalogFirmata/Makefile create mode 100644 libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde create mode 100644 libraries/Firmata/examples/SimpleDigitalFirmata/Makefile create mode 100644 libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.pde create mode 100644 libraries/Firmata/examples/StandardFirmata/LICENSE.txt create mode 100644 libraries/Firmata/examples/StandardFirmata/Makefile create mode 100644 libraries/Firmata/examples/StandardFirmata/StandardFirmata.pde create mode 100644 libraries/Firmata/keywords.txt create mode 100755 libraries/LiquidCrystal/LiquidCrystal.cpp create mode 100755 libraries/LiquidCrystal/LiquidCrystal.h create mode 100644 libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.pde create mode 100644 libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.pde create mode 100755 libraries/LiquidCrystal/keywords.txt create mode 100755 libraries/Matrix/Matrix.cpp create mode 100755 libraries/Matrix/Matrix.h create mode 100644 libraries/Matrix/examples/hello_matrix/hello_matrix.pde create mode 100644 libraries/Matrix/examples/sprite_animation/sprite_animation.pde create mode 100644 libraries/Matrix/keywords.txt create mode 100755 libraries/Servo/Servo.cpp create mode 100755 libraries/Servo/Servo.h create mode 100644 libraries/Servo/examples/Knob/Knob.pde create mode 100644 libraries/Servo/examples/Sweep/Sweep.pde create mode 100755 libraries/Servo/keywords.txt create mode 100755 libraries/SoftwareSerial/SoftwareSerial.cpp create mode 100755 libraries/SoftwareSerial/SoftwareSerial.h create mode 100644 libraries/SoftwareSerial/keywords.txt create mode 100644 libraries/Sprite/Sprite.cpp create mode 100644 libraries/Sprite/Sprite.h create mode 100644 libraries/Sprite/binary.h create mode 100644 libraries/Sprite/keywords.txt create mode 100644 libraries/Stepper/Stepper.cpp create mode 100644 libraries/Stepper/Stepper.h create mode 100644 libraries/Stepper/examples/MotorKnob/MotorKnob.pde create mode 100644 libraries/Stepper/keywords.txt create mode 100755 libraries/Wire/Wire.cpp create mode 100755 libraries/Wire/Wire.h create mode 100755 libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.pde create mode 100644 libraries/Wire/examples/digital_potentiometer/digital_potentiometer.pde create mode 100644 libraries/Wire/examples/master_reader/master_reader.pde create mode 100644 libraries/Wire/examples/master_writer/master_writer.pde create mode 100644 libraries/Wire/examples/slave_receiver/slave_receiver.pde create mode 100644 libraries/Wire/examples/slave_sender/slave_sender.pde create mode 100644 libraries/Wire/keywords.txt create mode 100644 libraries/Wire/utility/twi.c create mode 100755 libraries/Wire/utility/twi.h (limited to 'libraries') diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp new file mode 100755 index 0000000..3a361e7 --- /dev/null +++ b/libraries/EEPROM/EEPROM.cpp @@ -0,0 +1,50 @@ +/* + EEPROM.cpp - EEPROM 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 +#include "WConstants.h" +#include "EEPROM.h" + +/****************************************************************************** + * Definitions + ******************************************************************************/ + +/****************************************************************************** + * Constructors + ******************************************************************************/ + +/****************************************************************************** + * User API + ******************************************************************************/ + +uint8_t EEPROMClass::read(int address) +{ + return eeprom_read_byte((unsigned char *) address); +} + +void EEPROMClass::write(int address, uint8_t value) +{ + eeprom_write_byte((unsigned char *) address, value); +} + +EEPROMClass EEPROM; diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h new file mode 100755 index 0000000..aa2b577 --- /dev/null +++ b/libraries/EEPROM/EEPROM.h @@ -0,0 +1,35 @@ +/* + EEPROM.h - EEPROM 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 EEPROM_h +#define EEPROM_h + +#include + +class EEPROMClass +{ + public: + uint8_t read(int); + void write(int, uint8_t); +}; + +extern EEPROMClass EEPROM; + +#endif + diff --git a/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.pde b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.pde new file mode 100644 index 0000000..36af68e --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.pde @@ -0,0 +1,21 @@ +/* + * EEPROM Clear + * + * Sets all of the bytes of the EEPROM to 0. + */ + +#include + +void setup() +{ + // write a 0 to all 512 bytes of the EEPROM + for (int i = 0; i < 512; i++) + EEPROM.write(i, 0); + + // turn the LED on when we're done + digitalWrite(13, HIGH); +} + +void loop() +{ +} diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.pde b/libraries/EEPROM/examples/eeprom_read/eeprom_read.pde new file mode 100644 index 0000000..2e30708 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.pde @@ -0,0 +1,38 @@ +/* + * EEPROM Read + * + * Reads the value of each byte of the EEPROM and prints it + * to the computer. + */ + +#include + +// start reading from the first byte (address 0) of the EEPROM +int address = 0; +byte value; + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + // read a byte from the current address of the EEPROM + value = EEPROM.read(address); + + Serial.print(address); + Serial.print("\t"); + Serial.print(value, DEC); + Serial.println(); + + // advance to the next address of the EEPROM + address = address + 1; + + // there are only 512 bytes of EEPROM, from 0 to 511, so if we're + // on address 512, wrap around to address 0 + if (address == 512) + address = 0; + + delay(500); +} diff --git a/libraries/EEPROM/examples/eeprom_write/eeprom_write.pde b/libraries/EEPROM/examples/eeprom_write/eeprom_write.pde new file mode 100644 index 0000000..ae7c57e --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_write/eeprom_write.pde @@ -0,0 +1,38 @@ +/* + * EEPROM Write + * + * Stores values read from analog input 0 into the EEPROM. + * These values will stay in the EEPROM when the board is + * turned off and may be retrieved later by another sketch. + */ + +#include + +// the current address in the EEPROM (i.e. which byte +// we're going to write to next) +int addr = 0; + +void setup() +{ +} + +void loop() +{ + // need to divide by 4 because analog inputs range from + // 0 to 1023 and each byte of the EEPROM can only hold a + // value from 0 to 255. + int val = analogRead(0) / 4; + + // write the value to the appropriate byte of the EEPROM. + // these values will remain there when the board is + // turned off. + EEPROM.write(addr, val); + + // advance to the next address. there are 512 bytes in + // the EEPROM, so go back to 0 when we hit 512. + addr = addr + 1; + if (addr == 512) + addr = 0; + + delay(100); +} diff --git a/libraries/EEPROM/keywords.txt b/libraries/EEPROM/keywords.txt new file mode 100644 index 0000000..d3218fe --- /dev/null +++ b/libraries/EEPROM/keywords.txt @@ -0,0 +1,18 @@ +####################################### +# Syntax Coloring Map For Ultrasound +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +EEPROM KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/Ethernet/Client.cpp b/libraries/Ethernet/Client.cpp new file mode 100644 index 0000000..ebbb08d --- /dev/null +++ b/libraries/Ethernet/Client.cpp @@ -0,0 +1,140 @@ +extern "C" { + #include "types.h" + #include "w5100.h" + #include "socket.h" + #include "string.h" +} + +#include "WProgram.h" + +#include "Ethernet.h" +#include "Client.h" +#include "Server.h" + +uint16_t Client::_srcport = 0; + +Client::Client(uint8_t sock) { + _sock = sock; +} + +Client::Client(uint8_t *ip, uint16_t port) { + _ip = ip; + _port = port; + _sock = 255; +} + +uint8_t Client::connect() { + if (_sock != 255) + return 0; + + for (int i = 0; i < MAX_SOCK_NUM; i++) { + uint8_t s = getSn_SR(i); + if (s == SOCK_CLOSED || s == SOCK_FIN_WAIT) { + _sock = i; + break; + } + } + + if (_sock == 255) + return 0; + + _srcport++; + if (_srcport + 1024 == 0) _srcport = 0; + socket(_sock, Sn_MR_TCP, _srcport + 1024, 0); + + if (!::connect(_sock, _ip, _port)) { + _sock = 255; + return 0; + } + + while (status() != SOCK_ESTABLISHED) { + delay(1); + if (status() == SOCK_CLOSED) { + _sock = 255; + return 0; + } + } + + return 1; +} + +void Client::write(uint8_t b) { + if (_sock != 255) + send(_sock, &b, 1); +} + +void Client::write(const char *str) { + if (_sock != 255) + send(_sock, (const uint8_t *)str, strlen(str)); +} + +void Client::write(const uint8_t *buf, size_t size) { + if (_sock != 255) + send(_sock, buf, size); +} + +int Client::available() { + if (_sock != 255) + return getSn_RX_RSR(_sock); + return 0; +} + +int Client::read() { + uint8_t b; + if (!available()) + return -1; + recv(_sock, &b, 1); + return b; +} + +void Client::flush() { + while (available()) + read(); +} + +void Client::stop() { + if (_sock == 255) + return; + + // attempt to close the connection gracefully (send a FIN to other side) + disconnect(_sock); + unsigned long start = millis(); + + // wait a second for the connection to close + while (status() != SOCK_CLOSED && millis() - start < 1000) + delay(1); + + // if it hasn't closed, close it forcefully + if (status() != SOCK_CLOSED) + close(_sock); + + EthernetClass::_server_port[_sock] = 0; + _sock = 255; +} + +uint8_t Client::connected() { + uint8_t s = status(); + return !(s == SOCK_LISTEN || s == SOCK_CLOSED || s == SOCK_FIN_WAIT || + (s == SOCK_CLOSE_WAIT && !available())); +} + +uint8_t Client::status() { + return getSn_SR(_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 == 255; +} + +uint8_t Client::operator!=(int p) { + return _sock != 255; +} + +Client::operator bool() { + return _sock != 255; +} diff --git a/libraries/Ethernet/Client.h b/libraries/Ethernet/Client.h new file mode 100644 index 0000000..7c0ccdf --- /dev/null +++ b/libraries/Ethernet/Client.h @@ -0,0 +1,31 @@ +#ifndef Client_h +#define Client_h + +#include "Print.h" + +class Client : public Print { +private: + static uint16_t _srcport; + uint8_t _sock; + uint8_t *_ip; + uint16_t _port; +public: + Client(uint8_t); + Client(uint8_t *, uint16_t); + uint8_t status(); + uint8_t connect(); + virtual void write(uint8_t); + virtual void write(const char *str); + virtual void write(const uint8_t *buf, size_t size); + int available(); + int read(); + void flush(); + void stop(); + uint8_t connected(); + uint8_t operator==(int); + uint8_t operator!=(int); + operator bool(); + friend class Server; +}; + +#endif diff --git a/libraries/Ethernet/Ethernet.cpp b/libraries/Ethernet/Ethernet.cpp new file mode 100644 index 0000000..cb74876 --- /dev/null +++ b/libraries/Ethernet/Ethernet.cpp @@ -0,0 +1,38 @@ +extern "C" { + #include "types.h" + #include "w5100.h" +} + +#include "Ethernet.h" + +// XXX: don't make assumptions about the value of MAX_SOCK_NUM. +uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; +uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; + +void EthernetClass::begin(uint8_t *mac, uint8_t *ip) +{ + uint8_t gateway[4]; + gateway[0] = ip[0]; + gateway[1] = ip[1]; + gateway[2] = ip[2]; + gateway[3] = 1; + begin(mac, ip, gateway); +} + +void EthernetClass::begin(uint8_t *mac, uint8_t *ip, uint8_t *gateway) +{ + uint8_t subnet[] = { 255, 255, 255, 0 }; + begin(mac, ip, gateway, subnet); +} + +void EthernetClass::begin(uint8_t *mac, uint8_t *ip, uint8_t *gateway, uint8_t *subnet) +{ + iinchip_init(); + sysinit(0x55, 0x55); + setSHAR(mac); + setSIPR(ip); + setGAR(gateway); + setSUBR(subnet); +} + +EthernetClass Ethernet; diff --git a/libraries/Ethernet/Ethernet.h b/libraries/Ethernet/Ethernet.h new file mode 100644 index 0000000..bdfc4dd --- /dev/null +++ b/libraries/Ethernet/Ethernet.h @@ -0,0 +1,22 @@ +#ifndef Ethernet_h +#define Ethernet_h + +#include +#include "Client.h" +#include "Server.h" + +class EthernetClass { +private: +public: + static uint8_t _state[MAX_SOCK_NUM]; + static uint16_t _server_port[MAX_SOCK_NUM]; + void begin(uint8_t *, uint8_t *); + void begin(uint8_t *, uint8_t *, uint8_t *); + void begin(uint8_t *, uint8_t *, uint8_t *, uint8_t *); + friend class Client; + friend class Server; +}; + +extern EthernetClass Ethernet; + +#endif diff --git a/libraries/Ethernet/Server.cpp b/libraries/Ethernet/Server.cpp new file mode 100644 index 0000000..d17a5d3 --- /dev/null +++ b/libraries/Ethernet/Server.cpp @@ -0,0 +1,91 @@ +extern "C" { + #include "types.h" + #include "w5100.h" + #include "socket.h" + #include "string.h" +} + +#include "Ethernet.h" +#include "Client.h" +#include "Server.h" + +Server::Server(uint16_t port) +{ + _port = port; +} + +void Server::begin() +{ + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + Client client(sock); + if (client.status() == SOCK_CLOSED) { + socket(sock, Sn_MR_TCP, _port, 0); + listen(sock); + EthernetClass::_server_port[sock] = _port; + break; + } + } +} + +void Server::accept() +{ + int listening = 0; + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + Client client(sock); + + if (EthernetClass::_server_port[sock] == _port) { + if (client.status() == SOCK_LISTEN) { + listening = 1; + } else if (client.status() == SOCK_CLOSE_WAIT && !client.available()) { + client.stop(); + } + } + } + + if (!listening) { + begin(); + } +} + +Client Server::available() +{ + accept(); + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + Client client(sock); + if (EthernetClass::_server_port[sock] == _port && + client.status() == SOCK_ESTABLISHED) { + if (client.available()) { + // XXX: don't always pick the lowest numbered socket. + return client; + } + } + } + + return Client(255); +} + +void Server::write(uint8_t b) +{ + write(&b, 1); +} + +void Server::write(const char *str) +{ + write((const uint8_t *)str, strlen(str)); +} + +void Server::write(const uint8_t *buffer, size_t size) +{ + accept(); + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + Client client(sock); + + if (EthernetClass::_server_port[sock] == _port && + client.status() == SOCK_ESTABLISHED) { + client.write(buffer, size); + } + } +} diff --git a/libraries/Ethernet/Server.h b/libraries/Ethernet/Server.h new file mode 100644 index 0000000..73d6a5e --- /dev/null +++ b/libraries/Ethernet/Server.h @@ -0,0 +1,25 @@ +#ifndef Server_h +#define Server_h + +extern "C" { + #include "utility/types.h" +} + +#include "Print.h" + +class Client; + +class Server : public Print { +private: + uint16_t _port; + void accept(); +public: + Server(uint16_t); + Client available(); + void begin(); + virtual void write(uint8_t); + virtual void write(const char *str); + virtual void write(const uint8_t *buf, size_t size); +}; + +#endif diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.pde b/libraries/Ethernet/examples/ChatServer/ChatServer.pde new file mode 100644 index 0000000..825d2f8 --- /dev/null +++ b/libraries/Ethernet/examples/ChatServer/ChatServer.pde @@ -0,0 +1,34 @@ +/* + * Chat Server + * + * A simple server that distributes any incoming messages to all + * connected clients. To use telnet to 10.0.0.177 and type! + */ + +#include + +// network configuration. gateway and subnet are optional. +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +byte ip[] = { 10, 0, 0, 177 }; +byte gateway[] = { 10, 0, 0, 1 }; +byte subnet[] = { 255, 255, 0, 0 }; + +// telnet defaults to port 23 +Server server(23); + +void setup() +{ + // initialize the ethernet device + Ethernet.begin(mac, ip, gateway, subnet); + + // start listening for clients + server.begin(); +} + +void loop() +{ + Client client = server.available(); + if (client) { + server.write(client.read()); + } +} diff --git a/libraries/Ethernet/examples/WebClient/WebClient.pde b/libraries/Ethernet/examples/WebClient/WebClient.pde new file mode 100644 index 0000000..74a6094 --- /dev/null +++ b/libraries/Ethernet/examples/WebClient/WebClient.pde @@ -0,0 +1,41 @@ +#include + +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +byte ip[] = { 10, 0, 0, 177 }; +byte server[] = { 64, 233, 187, 99 }; // Google + +Client client(server, 80); + +void setup() +{ + Ethernet.begin(mac, ip); + Serial.begin(9600); + + delay(1000); + + Serial.println("connecting..."); + + if (client.connect()) { + Serial.println("connected"); + client.println("GET /search?q=arduino HTTP/1.0"); + client.println(); + } else { + Serial.println("connection failed"); + } +} + +void loop() +{ + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + for(;;) + ; + } +} diff --git a/libraries/Ethernet/examples/WebServer/WebServer.pde b/libraries/Ethernet/examples/WebServer/WebServer.pde new file mode 100644 index 0000000..b337a8e --- /dev/null +++ b/libraries/Ethernet/examples/WebServer/WebServer.pde @@ -0,0 +1,61 @@ +/* + * Web Server + * + * A simple web server that shows the value of the analog input pins. + */ + +#include + +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +byte ip[] = { 10, 0, 0, 177 }; + +Server server(80); + +void setup() +{ + Ethernet.begin(mac, ip); + server.begin(); +} + +void loop() +{ + Client client = server.available(); + if (client) { + // an http request ends with a blank line + boolean current_line_is_blank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + // if we've gotten to the end of the line (received a newline + // character) and the line is blank, the http request has ended, + // so we can send a reply + if (c == '\n' && current_line_is_blank) { + // send a standard http response header + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/html"); + client.println(); + + // output the value of each analog input pin + for (int i = 0; i < 6; i++) { + client.print("analog input "); + client.print(i); + client.print(" is "); + client.print(analogRead(i)); + client.println("
"); + } + break; + } + if (c == '\n') { + // we're starting a new line + current_line_is_blank = true; + } else if (c != '\r') { + // we've gotten a character on the current line + current_line_is_blank = false; + } + } + } + // give the web browser time to receive the data + delay(1); + client.stop(); + } +} diff --git a/libraries/Ethernet/utility/socket.c b/libraries/Ethernet/utility/socket.c new file mode 100755 index 0000000..88c81a8 --- /dev/null +++ b/libraries/Ethernet/utility/socket.c @@ -0,0 +1,558 @@ +/* +* +@file socket.c +@brief setting chip register for socket + last update : 2008. Jan +* +*/ + +#include "types.h" +#include "w5100.h" +#include "socket.h" + +static uint16 local_port; + + +/** +@brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it. +@return 1 for sucess else 0. +*/ +uint8 socket( + SOCKET s, /**< for socket number */ + uint8 protocol, /**< for socket protocol */ + uint16 port, /**< the source port for the socket */ + uint8 flag /**< the option for the socket */ + ) +{ + uint8 ret; +#ifdef __DEF_IINCHIP_DBG__ + printf("socket()\r\n"); +#endif + if ((protocol == Sn_MR_TCP) || (protocol == Sn_MR_UDP) || (protocol == Sn_MR_IPRAW) || (protocol == Sn_MR_MACRAW) || (protocol == Sn_MR_PPPOE)) + { + close(s); + IINCHIP_WRITE(Sn_MR(s),protocol | flag); + if (port != 0) { + IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff)); + } else { + local_port++; // if don't set the source port, set local_port number. + IINCHIP_WRITE(Sn_PORT0(s),(uint8)((local_port & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(local_port & 0x00ff)); + } + IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN); // run sockinit Sn_CR + + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; + /* ------- */ + ret = 1; + } + else + { + ret = 0; + } +#ifdef __DEF_IINCHIP_DBG__ + printf("Sn_SR = %.2x , Protocol = %.2x\r\n", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s))); +#endif + return ret; +} + + +/** +@brief This function close the socket and parameter is "s" which represent the socket number +*/ +void close(SOCKET s) +{ +#ifdef __DEF_IINCHIP_DBG__ + printf("close()\r\n"); +#endif + + IINCHIP_WRITE(Sn_CR(s),Sn_CR_CLOSE); + + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; + /* ------- */ + + /* +2008.01 [hwkim]: clear interrupt */ + #ifdef __DEF_IINCHIP_INT__ + /* m2008.01 [bj] : all clear */ + putISR(s, 0x00); + #else + /* m2008.01 [bj] : all clear */ + IINCHIP_WRITE(Sn_IR(s), 0xFF); + #endif +} + + +/** +@brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer. +@return 1 for success else 0. +*/ +uint8 listen( + SOCKET s /**< the socket number */ + ) +{ + uint8 ret; +#ifdef __DEF_IINCHIP_DBG__ + printf("listen()\r\n"); +#endif + if (IINCHIP_READ(Sn_SR(s)) == SOCK_INIT) + { + IINCHIP_WRITE(Sn_CR(s),Sn_CR_LISTEN); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; + /* ------- */ + ret = 1; + } + else + { + ret = 0; +#ifdef __DEF_IINCHIP_DBG__ + printf("Fail[invalid ip,port]\r\n"); +#endif + } + return ret; +} + + +/** +@brief This function established the connection for the channel in Active (client) mode. + This function waits for the untill the connection is established. + +@return 1 for success else 0. +*/ +uint8 connect(SOCKET s, uint8 * addr, uint16 port) +{ + uint8 ret; +#ifdef __DEF_IINCHIP_DBG__ + printf("connect()\r\n"); +#endif + if + ( + ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) || + ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || + (port == 0x00) + ) + { + ret = 0; +#ifdef __DEF_IINCHIP_DBG__ + printf("Fail[invalid ip,port]\r\n"); +#endif + } + else + { + ret = 1; + // set destination IP + IINCHIP_WRITE(Sn_DIPR0(s),addr[0]); + IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]); + IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]); + IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]); + IINCHIP_WRITE(Sn_DPORT0(s),(uint8)((port & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_DPORT0(s) + 1),(uint8)(port & 0x00ff)); + IINCHIP_WRITE(Sn_CR(s),Sn_CR_CONNECT); + /* m2008.01 [bj] : wait for completion */ + while ( IINCHIP_READ(Sn_CR(s)) ) ; + + } + + return ret; +} + + + +/** +@brief This function used for disconnect the socket and parameter is "s" which represent the socket number +@return 1 for success else 0. +*/ +void disconnect(SOCKET s) +{ +#ifdef __DEF_IINCHIP_DBG__ + printf("disconnect()\r\n"); +#endif + IINCHIP_WRITE(Sn_CR(s),Sn_CR_DISCON); + + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; + /* ------- */ +} + + +/** +@brief This function used to send the data in TCP mode +@return 1 for success else 0. +*/ +uint16 send( + SOCKET s, /**< the socket index */ + const uint8 * buf, /**< a pointer to data */ + uint16 len /**< the data size to be send */ + ) +{ + uint8 status=0; + uint16 ret=0; + uint16 freesize=0; +#ifdef __DEF_IINCHIP_DBG__ + printf("send()\r\n"); +#endif + + if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size. + else ret = len; + + // if freebuf is available, start. + do + { + freesize = getSn_TX_FSR(s); + status = IINCHIP_READ(Sn_SR(s)); + if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT)) + { + ret = 0; + break; + } +#ifdef __DEF_IINCHIP_DBG__ + printf("socket %d freesize(%d) empty or error\r\n", s, freesize); +#endif + } while (freesize < ret); + + // copy data + send_data_processing(s, (uint8 *)buf, ret); + IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND); + + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; + /* ------- */ + +/* +2008.01 bj */ +#ifdef __DEF_IINCHIP_INT__ + while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) +#else + while ( (IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) +#endif + { + /* m2008.01 [bj] : reduce code */ + if ( IINCHIP_READ(Sn_SR(s)) == SOCK_CLOSED ) + { +#ifdef __DEF_IINCHIP_DBG__ + printf("SOCK_CLOSED.\r\n"); +#endif + close(s); + return 0; + } + } +/* +2008.01 bj */ +#ifdef __DEF_IINCHIP_INT__ + putISR(s, getISR(s) & (~Sn_IR_SEND_OK)); +#else + IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK); +#endif + return ret; +} + + +/** +@brief This function is an application I/F function which is used to receive the data in TCP mode. + It continues to wait for data as much as the application wants to receive. + +@return received data size for success else -1. +*/ +uint16 recv( + SOCKET s, /**< socket index */ + uint8 * buf, /**< a pointer to copy the data to be received */ + uint16 len /**< the data size to be read */ + ) +{ + uint16 ret=0; +#ifdef __DEF_IINCHIP_DBG__ + printf("recv()\r\n"); +#endif + + + if ( len > 0 ) + { + recv_data_processing(s, buf, len); + IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV); + + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; + /* ------- */ + ret = len; + } + return ret; +} + + +/** +@brief This function is an application I/F function which is used to send the data for other then TCP mode. + Unlike TCP transmission, The peer's destination address and the port is needed. + +@return This function return send data size for success else -1. +*/ +uint16 sendto( + SOCKET s, /**< socket index */ + const uint8 * buf, /**< a pointer to the data */ + uint16 len, /**< the data size to send */ + uint8 * addr, /**< the peer's Destination IP address */ + uint16 port /**< the peer's destination port number */ + ) +{ +// uint8 status=0; +// uint8 isr=0; + uint16 ret=0; + +#ifdef __DEF_IINCHIP_DBG__ + printf("sendto()\r\n"); +#endif + if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size. + else ret = len; + + if + ( + ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || + ((port == 0x00)) ||(ret == 0) + ) + { + /* +2008.01 [bj] : added return value */ + ret = 0; +#ifdef __DEF_IINCHIP_DBG__ + printf("%d Fail[%.2x.%.2x.%.2x.%.2x, %.d, %d]\r\n",s, addr[0], addr[1], addr[2], addr[3] , port, len); + printf("Fail[invalid ip,port]\r\n"); +#endif + } + else + { + IINCHIP_WRITE(Sn_DIPR0(s),addr[0]); + IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]); + IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]); + IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]); + IINCHIP_WRITE(Sn_DPORT0(s),(uint8)((port & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_DPORT0(s) + 1),(uint8)(port & 0x00ff)); + + // copy data + send_data_processing(s, (uint8 *)buf, ret); + IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND); + + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; + /* ------- */ + +/* +2008.01 bj */ +#ifdef __DEF_IINCHIP_INT__ + while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) +#else + while ( (IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) +#endif + { +#ifdef __DEF_IINCHIP_INT__ + if (getISR(s) & Sn_IR_TIMEOUT) +#else + if (IINCHIP_READ(Sn_IR(s)) & Sn_IR_TIMEOUT) +#endif + { +#ifdef __DEF_IINCHIP_DBG__ + printf("send fail.\r\n"); +#endif +/* +2008.01 [bj]: clear interrupt */ +#ifdef __DEF_IINCHIP_INT__ + putISR(s, getISR(s) & ~(Sn_IR_SEND_OK | Sn_IR_TIMEOUT)); /* clear SEND_OK & TIMEOUT */ +#else + IINCHIP_WRITE(Sn_IR(s), (Sn_IR_SEND_OK | Sn_IR_TIMEOUT)); /* clear SEND_OK & TIMEOUT */ +#endif + return 0; + } + } + +/* +2008.01 bj */ +#ifdef __DEF_IINCHIP_INT__ + putISR(s, getISR(s) & (~Sn_IR_SEND_OK)); +#else + IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK); +#endif + + } + return ret; +} + + +/** +@brief This function is an application I/F function which is used to receive the data in other then + TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well. + +@return This function return received data size for success else -1. +*/ +uint16 recvfrom( + SOCKET s, /**< the socket number */ + uint8 * buf, /**< a pointer to copy the data to be received */ + uint16 len, /**< the data size to read */ + uint8 * addr, /**< a pointer to store the peer's IP address */ + uint16 *port /**< a pointer to store the peer's port number. */ + ) +{ + uint8 head[8]; + uint16 data_len=0; + uint16 ptr=0; +#ifdef __DEF_IINCHIP_DBG__ + printf("recvfrom()\r\n"); +#endif + + if ( len > 0 ) + { + ptr = IINCHIP_READ(Sn_RX_RD0(s)); + ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_RX_RD0(s) + 1); +#ifdef __DEF_IINCHIP_DBG__ + printf("ISR_RX: rd_ptr : %.4x\r\n", ptr); +#endif + switch (IINCHIP_READ(Sn_MR(s)) & 0x07) + { + case Sn_MR_UDP : + read_data(s, (uint8 *)ptr, head, 0x08); + ptr += 8; + // read peer's IP address, port number. + addr[0] = head[0]; + addr[1] = head[1]; + addr[2] = head[2]; + addr[3] = head[3]; + *port = head[4]; + *port = (*port << 8) + head[5]; + data_len = head[6]; + data_len = (data_len << 8) + head[7]; + +#ifdef __DEF_IINCHIP_DBG__ + printf("UDP msg arrived\r\n"); + printf("source Port : %d\r\n", *port); + printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]); +#endif + + read_data(s, (uint8 *)ptr, buf, data_len); // data copy. + ptr += data_len; + + IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff)); + break; + + case Sn_MR_IPRAW : + read_data(s, (uint8 *)ptr, head, 0x06); + ptr += 6; + + addr[0] = head[0]; + addr[1] = head[1]; + addr[2] = head[2]; + addr[3] = head[3]; + data_len = head[4]; + data_len = (data_len << 8) + head[5]; + +#ifdef __DEF_IINCHIP_DBG__ + printf("IP RAW msg arrived\r\n"); + printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]); +#endif + read_data(s, (uint8 *)ptr, buf, data_len); // data copy. + ptr += data_len; + + IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff)); + break; + case Sn_MR_MACRAW : + read_data(s,(uint8*)ptr,head,2); + ptr+=2; + data_len = head[0]; + data_len = (data_len<<8) + head[1] - 2; + + read_data(s,(uint8*) ptr,buf,data_len); + ptr += data_len; + IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff)); + +#ifdef __DEF_IINCHIP_DGB__ + printf("MAC RAW msg arrived\r\n"); + printf("dest mac=%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\r\n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]); + printf("src mac=%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\r\n",buf[6],buf[7],buf[8],buf[9],buf[10],buf[11]); + printf("type =%.2X%.2X\r\n",buf[12],buf[13]); +#endif + break; + + default : + break; + } + IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV); + + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; + /* ------- */ + } +#ifdef __DEF_IINCHIP_DBG__ + printf("recvfrom() end ..\r\n"); +#endif + return data_len; +} + + +uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len) +{ + uint8 status=0; +// uint8 isr=0; + uint16 ret=0; + +#ifdef __DEF_IINCHIP_DBG__ + printf("igmpsend()\r\n"); +#endif + if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size. + else ret = len; + + if (ret == 0) + { + ; +#ifdef __DEF_IINCHIP_DBG__ + printf("%d Fail[%d]\r\n",len); +#endif + } + else + { + // copy data + send_data_processing(s, (uint8 *)buf, ret); + IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND); +/* +2008.01 bj */ + while( IINCHIP_READ(Sn_CR(s)) ) + ; +/* ------- */ + +/* +2008.01 bj */ +#ifdef __DEF_IINCHIP_INT__ + while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) +#else + while ( (IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) +#endif + { + status = IINCHIP_READ(Sn_SR(s)); +#ifdef __DEF_IINCHIP_INT__ + if (getISR(s) & Sn_IR_TIMEOUT) +#else + if (IINCHIP_READ(Sn_IR(s)) & Sn_IR_TIMEOUT) +#endif + { +#ifdef __DEF_IINCHIP_DBG__ + printf("igmpsend fail.\r\n"); +#endif + /* in case of igmp, if send fails, then socket closed */ + /* if you want change, remove this code. */ + close(s); + /* ----- */ + + return 0; + } + } + +/* +2008.01 bj */ +#ifdef __DEF_IINCHIP_INT__ + putISR(s, getISR(s) & (~Sn_IR_SEND_OK)); +#else + IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK); +#endif + } + return ret; +} + diff --git a/libraries/Ethernet/utility/socket.h b/libraries/Ethernet/utility/socket.h new file mode 100755 index 0000000..a5cce42 --- /dev/null +++ b/libraries/Ethernet/utility/socket.h @@ -0,0 +1,23 @@ +/* +* +@file socket.h +@brief define function of socket API +* +*/ + +#ifndef _SOCKET_H_ +#define _SOCKET_H_ + +extern uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag); // Opens a socket(TCP or UDP or IP_RAW mode) +extern void close(SOCKET s); // Close socket +extern uint8 connect(SOCKET s, uint8 * addr, uint16 port); // Establish TCP connection (Active connection) +extern void disconnect(SOCKET s); // disconnect the connection +extern uint8 listen(SOCKET s); // Establish TCP connection (Passive connection) +extern uint16 send(SOCKET s, const uint8 * buf, uint16 len); // Send data (TCP) +extern uint16 recv(SOCKET s, uint8 * buf, uint16 len); // Receive data (TCP) +extern uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port); // Send data (UDP/IP RAW) +extern uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port); // Receive data (UDP/IP RAW) + +extern uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len); +#endif +/* _SOCKET_H_ */ diff --git a/libraries/Ethernet/utility/spi.h b/libraries/Ethernet/utility/spi.h new file mode 100755 index 0000000..000705d --- /dev/null +++ b/libraries/Ethernet/utility/spi.h @@ -0,0 +1,58 @@ +//----------------------------------------------------------------------------- +//AVR Mega168 SPI HAL +#define BIT0 0x01 +#define BIT1 0x02 +#define BIT2 0x04 +#define BIT3 0x08 +#define BIT4 0x10 +#define BIT5 0x20 +#define BIT6 0x40 +#define BIT7 0x80 + +#define SPI0_SS_BIT BIT2 +#define SPI0_SS_DDR DDRB +#define SPI0_SS_PORT PORTB + +#define SPI0_SCLK_BIT BIT5 +#define SPI0_SCLK_DDR DDRB +#define SPI0_SCLK_PORT PORTB + +#define SPI0_MOSI_BIT BIT3 +#define SPI0_MOSI_DDR DDRB +#define SPI0_MOSI_PORT PORTB + +#define SPI0_MISO_BIT BIT4 +#define SPI0_MISO_DDR DDRB +#define SPI0_MISO_PORT PORTB + + +#define SPI0_WaitForReceive() +#define SPI0_RxData() (SPDR) + +#define SPI0_TxData(Data) (SPDR = Data) +#define SPI0_WaitForSend() while( (SPSR & 0x80)==0x00 ) + +#define SPI0_SendByte(Data) SPI0_TxData(Data);SPI0_WaitForSend() +#define SPI0_RecvBute() SPI0_RxData() + +// PB4(MISO), PB3(MOSI), PB5(SCK), PB2(/SS) // CS=1, waiting for SPI start // SPI mode 0, 4MHz +#define SPI0_Init() DDRB |= SPI0_SS_BIT|SPI0_SCLK_BIT|SPI0_MOSI_BIT;\ + PORTB |= SPI0_SS_BIT; PORTB &= ~(SPI0_SCLK_BIT|SPI0_MOSI_BIT);\ + SPCR = 0x50 +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +//IInChip SPI HAL +#define IINCHIP_SpiInit SPI0_Init +#define IINCHIP_SpiSendData SPI0_SendByte +#define IINCHIP_SpiRecvData SPI0_RxData + + +#define IINCHIP_CS_BIT BIT2 +#define IINCHIP_CS_DDR DDRB +#define IINCHIP_CS_PORT PORTB + +#define IINCHIP_CSInit() (IINCHIP_CS_DDR |= IINCHIP_CS_BIT) +#define IINCHIP_CSon() (IINCHIP_CS_PORT |= IINCHIP_CS_BIT) +#define IINCHIP_CSoff() (IINCHIP_CS_PORT &= ~IINCHIP_CS_BIT) +//----------------------------------------------------------------------------- diff --git a/libraries/Ethernet/utility/types.h b/libraries/Ethernet/utility/types.h new file mode 100755 index 0000000..6c350da --- /dev/null +++ b/libraries/Ethernet/utility/types.h @@ -0,0 +1,165 @@ +/* +* +@file type.h +* +*/ + +#ifndef _TYPE_H_ +#define _TYPE_H_ + + +/*************************************************** + * attribute for mcu ( types, ... ) + ***************************************************/ +//#include "mcu_define.h" +#define __MCU_AVR__ 1 +#define __MCU_TYPE__ __MCU_AVR__ + +//---- Refer "Rom File Maker Manual Vx.x.pdf" +#include + +#define _ENDIAN_LITTLE_ 0 /**< This must be defined if system is little-endian alignment */ +#define _ENDIAN_BIG_ 1 +#define SYSTEM_ENDIAN _ENDIAN_LITTLE_ + +#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */ +#define CLK_CPU F_CPU /**< 8Mhz(for serial) */ + +/* ## __DEF_IINCHIP_xxx__ : define option for iinchip driver *****************/ +//#define __DEF_IINCHIP_DBG__ /* involve debug code in driver (socket.c) */ +//#define __DEF_IINCHIP_INT__ /**< involve interrupt service routine (socket.c) */ +//#define __DEF_IINCHIP_PPP__ /* involve pppoe routine (socket.c) */ + /* If it is defined, the source files(md5.h,md5.c) must be included in your project. + Otherwize, the source files must be removed in your project. */ + +#define __DEF_IINCHIP_DIRECT_MODE__ 1 +#define __DEF_IINCHIP_INDIRECT_MODE__ 2 +#define __DEF_IINCHIP_SPI_MODE__ 3 + +//#define __DEF_IINCHIP_BUS__ __DEF_IINCHIP_DIRECT_MODE__ +//#define __DEF_IINCHIP_BUS__ __DEF_IINCHIP_INDIRECT_MODE__ +#define __DEF_IINCHIP_BUS__ __DEF_IINCHIP_SPI_MODE__ /*Enable SPI_mode*/ + + +/** +@brief __DEF_IINCHIP_MAP_xxx__ : define memory map for iinchip +*/ +#define __DEF_IINCHIP_MAP_BASE__ 0x8000 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) + #define COMMON_BASE __DEF_IINCHIP_MAP_BASE__ +#else + #define COMMON_BASE 0x0000 +#endif +#define __DEF_IINCHIP_MAP_TXBUF__ (COMMON_BASE + 0x4000) /* Internal Tx buffer address of the iinchip */ +#define __DEF_IINCHIP_MAP_RXBUF__ (COMMON_BASE + 0x6000) /* Internal Rx buffer address of the iinchip */ + + +#if (__MCU_TYPE__ == __MCU_AVR__) + #ifdef __DEF_IINCHIP_INT__ + // iinchip use external interrupt 4 + #define IINCHIP_ISR_DISABLE() (EIMSK &= ~(0x10)) + #define IINCHIP_ISR_ENABLE() (EIMSK |= 0x10) + #define IINCHIP_ISR_GET(X) (X = EIMSK) + #define IINCHIP_ISR_SET(X) (EIMSK = X) + #else + #define IINCHIP_ISR_DISABLE() + #define IINCHIP_ISR_ENABLE() + #define IINCHIP_ISR_GET(X) + #define IINCHIP_ISR_SET(X) + #endif +#else +#error "unknown MCU type" +#endif + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +//typedef enum { false, true } bool; + +#ifndef _SIZE_T +#define _SIZE_T +typedef unsigned int size_t; +#endif + +/** + * The 8-bit signed data type. + */ +typedef char int8; +/** + * The volatile 8-bit signed data type. + */ +typedef volatile char vint8; +/** + * The 8-bit unsigned data type. + */ +typedef unsigned char uint8; +/** + * The volatile 8-bit unsigned data type. + */ +typedef volatile unsigned char vuint8; + +/** + * The 16-bit signed data type. + */ +typedef int int16; +/** + * The volatile 16-bit signed data type. + */ +typedef volatile int vint16; +/** + * The 16-bit unsigned data type. + */ +typedef unsigned int uint16; +/** + * The volatile 16-bit unsigned data type. + */ +typedef volatile unsigned int vuint16; +/** + * The 32-bit signed data type. + */ +typedef long int32; +/** + * The volatile 32-bit signed data type. + */ +typedef volatile long vint32; +/** + * The 32-bit unsigned data type. + */ +typedef unsigned long uint32; +/** + * The volatile 32-bit unsigned data type. + */ +typedef volatile unsigned long vuint32; + +/* bsd */ +typedef uint8 u_char; /**< 8-bit value */ +typedef uint8 SOCKET; +typedef uint16 u_short; /**< 16-bit value */ +typedef uint16 u_int; /**< 16-bit value */ +typedef uint32 u_long; /**< 32-bit value */ + +typedef union _un_l2cval { + u_long lVal; + u_char cVal[4]; +}un_l2cval; + +typedef union _un_i2cval { + u_int iVal; + u_char cVal[2]; +}un_i2cval; + + +/** global define */ +#define FW_VERSION 0x01010000 /* System F/W Version : 1.1.0.0 */ +#define HW_VERSION 0x01000000 + + +#define TX_RX_MAX_BUF_SIZE 2048 +#define TX_BUF 0x1100 +#define RX_BUF (TX_BUF+TX_RX_MAX_BUF_SIZE) + +#define UART_DEVICE_CNT 1 /**< UART device number */ +/* #define SUPPORT_UART_ONE */ + +#endif /* _TYPE_H_ */ diff --git a/libraries/Ethernet/utility/w5100.c b/libraries/Ethernet/utility/w5100.c new file mode 100755 index 0000000..7d0f55a --- /dev/null +++ b/libraries/Ethernet/utility/w5100.c @@ -0,0 +1,1302 @@ +/* + * (c)COPYRIGHT + * ALL RIGHT RESERVED + * + * FileName : w5100.c + * Revision History : + * ---------- ------- ------------------------------------------------ + * Date version Description + * ---------- ------- ------------------------------------------------ + * 01/25/2007 1.1 Bug is Fixed in the Indirect Mode + * : Memory mapping error + * ---------- ------- ------------------------------------------------ + * 01/08/2008 1.2 Modification of Socket Command Part + * : Check if the appropriately performed after writing Sn_CR + * + * Modification of SPI Part + * : SPI code changed by adding 'spi.h'. + * : Change control type for SPI port from byte to bit. + * ---------- ------- ------------------------------------------------ + * 01/15/2008 1.3 Bug is Fixed in the pppinit() fuction. + * : do not clear interrupt value, so fixed. + * + * Modification of ISR + * : Do not exit ISR, if there is interrupt. + * ---------- ------- ------------------------------------------------ + * 03/21/2008 1.4 Modification of SetMR() function + * : Use IINCHIP_WRITE() function in Direct or SPI mode. + * ---------- ------- ------------------------------------------------ + */ +#include +#include + +#include +// #include + +#include "types.h" +#include "socket.h" +#include "w5100.h" + + + +#ifdef __DEF_IINCHIP_PPP__ + #include "md5.h" +#endif + + +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) +#include "spi.h" //+2007113[jhpark] +#endif + +static uint8 I_STATUS[MAX_SOCK_NUM]; +static uint16 SMASK[MAX_SOCK_NUM]; /**< Variable for Tx buffer MASK in each channel */ +static uint16 RMASK[MAX_SOCK_NUM]; /**< Variable for Rx buffer MASK in each channel */ +static uint16 SSIZE[MAX_SOCK_NUM]; /**< Max Tx buffer size by each channel */ +static uint16 RSIZE[MAX_SOCK_NUM]; /**< Max Rx buffer size by each channel */ +static uint16 SBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Tx buffer base address by each channel */ +static uint16 RBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Rx buffer base address by each channel */ + +uint8 getISR(uint8 s) +{ + return I_STATUS[s]; +} + +void putISR(uint8 s, uint8 val) +{ + I_STATUS[s] = val; +} + +uint16 getIINCHIP_RxMAX(uint8 s) +{ + return RSIZE[s]; +} +uint16 getIINCHIP_TxMAX(uint8 s) +{ + return SSIZE[s]; +} +uint16 getIINCHIP_RxMASK(uint8 s) +{ + return RMASK[s]; +} +uint16 getIINCHIP_TxMASK(uint8 s) +{ + return SMASK[s]; +} +uint16 getIINCHIP_RxBASE(uint8 s) +{ + return RBUFBASEADDRESS[s]; +} +uint16 getIINCHIP_TxBASE(uint8 s) +{ + return SBUFBASEADDRESS[s]; +} + + /** +@brief This function writes the data into W5100 registers. +*/ +uint8 IINCHIP_WRITE(uint16 addr,uint8 data) +{ +// DIRECT MODE I/F +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) + IINCHIP_ISR_DISABLE(); + *((vuint8*)(addr)) = data; + IINCHIP_ISR_ENABLE(); +#elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) /* INDIRECT MODE I/F */ + IINCHIP_ISR_DISABLE(); + *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8); + *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF); + *((vuint8*)IDM_DR) = data; + IINCHIP_ISR_ENABLE(); +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) + IINCHIP_ISR_DISABLE(); + IINCHIP_SpiInit(); + + //SPI MODE I/F + IINCHIP_CSoff(); // CS=0, SPI start + + IINCHIP_SpiSendData(0xF0); + IINCHIP_SpiSendData((addr & 0xFF00) >> 8); + IINCHIP_SpiSendData(addr & 0x00FF); + IINCHIP_SpiSendData(data); + + IINCHIP_CSon(); + + IINCHIP_ISR_ENABLE(); +#else + #error "unknown bus type" +#endif + return 1; +} + + +/** +@brief This function reads the value from W5100 registers. +*/ +uint8 IINCHIP_READ(uint16 addr) +{ + uint8 data; + +// DIRECT MODE I/F + +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) + IINCHIP_ISR_DISABLE(); + data = *((vuint8*)(addr)); + IINCHIP_ISR_ENABLE(); +#elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) + IINCHIP_ISR_DISABLE(); + *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8); + *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF); + data = *((vuint8*)IDM_DR); + IINCHIP_ISR_ENABLE(); + +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) + IINCHIP_ISR_DISABLE(); + IINCHIP_SpiInit(); + IINCHIP_CSoff(); // CS=0, SPI start + + IINCHIP_SpiSendData(0x0F); + IINCHIP_SpiSendData((addr & 0xFF00) >> 8); + IINCHIP_SpiSendData(addr & 0x00FF); + + + IINCHIP_SpiSendData(0); + data = IINCHIP_SpiRecvData(); + + IINCHIP_CSon(); // SPI end + IINCHIP_ISR_ENABLE(); +#else + #error "unknown bus type" +#endif + return data; +} + + +/** +@brief This function writes into W5100 memory(Buffer) +*/ +uint16 wiz_write_buf(uint16 addr,uint8* buf,uint16 len) +{ +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) + IINCHIP_ISR_DISABLE(); + memcpy((uint8 *)addr, buf, len); + IINCHIP_ISR_ENABLE(); +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) + uint16 idx = 0; + IINCHIP_ISR_DISABLE(); + *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8); + *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF); + for (idx = 0; idx < len ; idx++) *((vuint8*)IDM_DR) = buf[idx]; + IINCHIP_ISR_ENABLE(); +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) + uint16 idx = 0; + + IINCHIP_ISR_DISABLE(); + IINCHIP_SpiInit(); + + //SPI MODE I/F + for(idx=0;idx> 8); + IINCHIP_SpiSendData((addr+idx) & 0x00FF); + IINCHIP_SpiSendData(buf[idx]); + + IINCHIP_CSon(); // CS=0, SPI end + } + + IINCHIP_ISR_ENABLE(); +#else + #error "unknown bus type" +#endif + return len; +} + + +/** +@brief This function reads into W5100 memory(Buffer) +*/ +uint16 wiz_read_buf(uint16 addr, uint8* buf,uint16 len) +{ +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) + IINCHIP_ISR_DISABLE(); + memcpy(buf, (uint8 *)addr, len); + IINCHIP_ISR_ENABLE(); +#elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) + uint16 idx = 0; + IINCHIP_ISR_DISABLE(); + *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8); + *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF); + for (idx = 0; idx < len ; idx++) buf[idx] = *((vuint8*)IDM_DR); + IINCHIP_ISR_ENABLE(); +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) + uint16 idx = 0; + IINCHIP_ISR_DISABLE(); + + IINCHIP_SpiInit(); + + for (idx=0; idx> 8); + IINCHIP_SpiSendData((addr+idx) & 0x00FF); + + + IINCHIP_SpiSendData(0); + buf[idx] = IINCHIP_SpiRecvData(); + + IINCHIP_CSon(); // CS=0, SPI end + } + + IINCHIP_ISR_ENABLE(); +#else + #error "unknown bus type" +#endif + return len; +} + + +/** +@brief Socket interrupt routine +*/ +#ifdef __DEF_IINCHIP_INT__ +ISR(INT4_vect) +{ + uint8 int_val; + IINCHIP_ISR_DISABLE(); + int_val = IINCHIP_READ(IR); + + /* +200801[bj] process all of interupt */ + do { + /*---*/ + + if (int_val & IR_CONFLICT) + { + printf("IP conflict : %.2x\r\n", int_val); + } + if (int_val & IR_UNREACH) + { + printf("INT Port Unreachable : %.2x\r\n", int_val); + printf("UIPR0 : %d.%d.%d.%d\r\n", IINCHIP_READ(UIPR0), IINCHIP_READ(UIPR0+1), IINCHIP_READ(UIPR0+2), IINCHIP_READ(UIPR0+3)); + printf("UPORT0 : %.2x %.2x\r\n", IINCHIP_READ(UPORT0), IINCHIP_READ(UPORT0+1)); + } + + /* +200801[bj] interrupt clear */ + IINCHIP_WRITE(IR, 0xf0); + /*---*/ + + if (int_val & IR_SOCK(0)) + { + /* +-200801[bj] save interrupt value*/ + I_STATUS[0] |= IINCHIP_READ(Sn_IR(0)); // can be come to over two times. + IINCHIP_WRITE(Sn_IR(0), I_STATUS[0]); + /*---*/ + } + if (int_val & IR_SOCK(1)) + { + /* +-200801[bj] save interrupt value*/ + I_STATUS[1] |= IINCHIP_READ(Sn_IR(1)); + IINCHIP_WRITE(Sn_IR(1), I_STATUS[1]); + /*---*/ + } + if (int_val & IR_SOCK(2)) + { + /* +-200801[bj] save interrupt value*/ + I_STATUS[2] |= IINCHIP_READ(Sn_IR(2)); + IINCHIP_WRITE(Sn_IR(2), I_STATUS[2]); + /*---*/ + } + if (int_val & IR_SOCK(3)) + { + /* +-200801[bj] save interrupt value*/ + I_STATUS[3] |= IINCHIP_READ(Sn_IR(3)); + IINCHIP_WRITE(Sn_IR(3), I_STATUS[3]); + /*---*/ + } + + /* +-200801[bj] re-read interrupt value*/ + int_val = IINCHIP_READ(IR); + + /* +200801[bj] if exist, contiue to process */ + } while (int_val != 0x00); + /*---*/ + + IINCHIP_ISR_ENABLE(); +} +#endif + +/** +@brief This function is for resetting of the iinchip. Initializes the iinchip to work in whether DIRECT or INDIRECT mode +*/ +void iinchip_init(void) +{ + setMR( MR_RST ); +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) + setMR( MR_IND | MR_AI ); +#ifdef __DEF_IINCHIP_DBG__ + printf("MR value is %d \r\n",IINCHIP_READ(MR)); +#endif +#endif +} + + +/** +@brief This function set the transmit & receive buffer size as per the channels is used + +Note for TMSR and RMSR bits are as follows\n +bit 1-0 : memory size of channel #0 \n +bit 3-2 : memory size of channel #1 \n +bit 5-4 : memory size of channel #2 \n +bit 7-6 : memory size of channel #3 \n\n +Maximum memory size for Tx, Rx in the W5100 is 8K Bytes,\n +In the range of 8KBytes, the memory size could be allocated dynamically by each channel.\n +Be attentive to sum of memory size shouldn't exceed 8Kbytes\n +and to data transmission and receiption from non-allocated channel may cause some problems.\n +If the 8KBytes memory is already assigned to centain channel, \n +other 3 channels couldn't be used, for there's no available memory.\n +If two 4KBytes memory are assigned to two each channels, \n +other 2 channels couldn't be used, for there's no available memory.\n +*/ +void sysinit( + uint8 tx_size, /**< tx_size Tx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte) */ + uint8 rx_size /**< rx_size Rx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte) */ + ) +{ + int16 i; + int16 ssum,rsum; + +#ifdef __DEF_IINCHIP_DBG__ + printf("sysinit()\r\n"); +#endif + + ssum = 0; + rsum = 0; + + IINCHIP_WRITE(TMSR,tx_size); /* Set Tx memory size for each channel */ + IINCHIP_WRITE(RMSR,rx_size); /* Set Rx memory size for each channel */ + + SBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_TXBUF__); /* Set base address of Tx memory for channel #0 */ + RBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_RXBUF__); /* Set base address of Rx memory for channel #0 */ + +#ifdef __DEF_IINCHIP_DBG__ + printf("Channel : SEND MEM SIZE : RECV MEM SIZE\r\n"); +#endif + + for (i = 0 ; i < MAX_SOCK_NUM; i++) // Set the size, masking and base address of Tx & Rx memory by each channel + { + SSIZE[i] = (int16)(0); + RSIZE[i] = (int16)(0); + if (ssum < 8192) + { + switch((tx_size >> i*2) & 0x03) // Set Tx memory size + { + case 0: + SSIZE[i] = (int16)(1024); + SMASK[i] = (uint16)(0x03FF); + break; + case 1: + SSIZE[i] = (int16)(2048); + SMASK[i] = (uint16)(0x07FF); + break; + case 2: + SSIZE[i] = (int16)(4096); + SMASK[i] = (uint16)(0x0FFF); + break; + case 3: + SSIZE[i] = (int16)(8192); + SMASK[i] = (uint16)(0x1FFF); + break; + } + } + if (rsum < 8192) + { + switch((rx_size >> i*2) & 0x03) // Set Rx memory size + { + case 0: + RSIZE[i] = (int16)(1024); + RMASK[i] = (uint16)(0x03FF); + break; + case 1: + RSIZE[i] = (int16)(2048); + RMASK[i] = (uint16)(0x07FF); + break; + case 2: + RSIZE[i] = (int16)(4096); + RMASK[i] = (uint16)(0x0FFF); + break; + case 3: + RSIZE[i] = (int16)(8192); + RMASK[i] = (uint16)(0x1FFF); + break; + } + } + ssum += SSIZE[i]; + rsum += RSIZE[i]; + + if (i != 0) // Sets base address of Tx and Rx memory for channel #1,#2,#3 + { + SBUFBASEADDRESS[i] = SBUFBASEADDRESS[i-1] + SSIZE[i-1]; + RBUFBASEADDRESS[i] = RBUFBASEADDRESS[i-1] + RSIZE[i-1]; + } +#ifdef __DEF_IINCHIP_DBG__ + printf("%d : %.4x : %.4x : %.4x : %.4x\r\n", i, (uint16)SBUFBASEADDRESS[i], (uint16)RBUFBASEADDRESS[i], SSIZE[i], RSIZE[i]); +#endif + } +} + + +void setMR(uint8 val) +{ + +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) + *((volatile uint8*)(MR)) = val; +#else + /* DIRECT ACCESS */ + IINCHIP_WRITE(MR,val); +#endif +} + + +/** +@brief This function sets up gateway IP address. +*/ +void setGAR( + uint8 * addr /**< a pointer to a 4 -byte array responsible to set the Gateway IP address. */ + ) +{ + IINCHIP_WRITE((GAR0 + 0),addr[0]); + IINCHIP_WRITE((GAR0 + 1),addr[1]); + IINCHIP_WRITE((GAR0 + 2),addr[2]); + IINCHIP_WRITE((GAR0 + 3),addr[3]); +} +void getGWIP(uint8 * addr) +{ + addr[0] = IINCHIP_READ((GAR0 + 0)); + addr[1] = IINCHIP_READ((GAR0 + 1)); + addr[2] = IINCHIP_READ((GAR0 + 2)); + addr[3] = IINCHIP_READ((GAR0 + 3)); +} + + +/** +@brief It sets up SubnetMask address +*/ +void setSUBR( + uint8 * addr /**< a pointer to a 4 -byte array responsible to set the SubnetMask address */ + ) +{ + IINCHIP_WRITE((SUBR0 + 0),addr[0]); + IINCHIP_WRITE((SUBR0 + 1),addr[1]); + IINCHIP_WRITE((SUBR0 + 2),addr[2]); + IINCHIP_WRITE((SUBR0 + 3),addr[3]); +} + + +/** +@brief This function sets up MAC address. +*/ +void setSHAR( + uint8 * addr /**< a pointer to a 6 -byte array responsible to set the MAC address. */ + ) +{ + IINCHIP_WRITE((SHAR0 + 0),addr[0]); + IINCHIP_WRITE((SHAR0 + 1),addr[1]); + IINCHIP_WRITE((SHAR0 + 2),addr[2]); + IINCHIP_WRITE((SHAR0 + 3),addr[3]); + IINCHIP_WRITE((SHAR0 + 4),addr[4]); + IINCHIP_WRITE((SHAR0 + 5),addr[5]); +} + + +/** +@brief This function sets up Source IP address. +*/ +void setSIPR( + uint8 * addr /**< a pointer to a 4 -byte array responsible to set the Source IP address. */ + ) +{ + IINCHIP_WRITE((SIPR0 + 0),addr[0]); + IINCHIP_WRITE((SIPR0 + 1),addr[1]); + IINCHIP_WRITE((SIPR0 + 2),addr[2]); + IINCHIP_WRITE((SIPR0 + 3),addr[3]); +} + + +/** +@brief This function gets Interrupt register in common register. + */ +uint8 getIR( void ) +{ + return IINCHIP_READ(IR); +} + + + +/** +@brief This function sets up Retransmission time. + +If there is no response from the peer or delay in response then retransmission +will be there as per RTR (Retry Time-value Register)setting +*/ +void setRTR(uint16 timeout) +{ + IINCHIP_WRITE(RTR0,(uint8)((timeout & 0xff00) >> 8)); + IINCHIP_WRITE((RTR0 + 1),(uint8)(timeout & 0x00ff)); +} + + +/** +@brief This function set the number of Retransmission. + +If there is no response from the peer or delay in response then recorded time +as per RTR & RCR register seeting then time out will occur. +*/ +void setRCR(uint8 retry) +{ + IINCHIP_WRITE(RCR,retry); +} + + +/** +@brief This function set the interrupt mask Enable/Disable appropriate Interrupt. ('1' : interrupt enable) + +If any bit in IMR is set as '0' then there is not interrupt signal though the bit is +set in IR register. +*/ +void setIMR(uint8 mask) +{ + IINCHIP_WRITE(IMR,mask); // must be setted 0x10. +} + + +/** +@brief These below functions are used to get the Gateway, SubnetMask + and Source Hardware Address (MAC Address) and Source IP address +*/ +void getGAR(uint8 * addr) +{ + addr[0] = IINCHIP_READ(GAR0); + addr[1] = IINCHIP_READ(GAR0+1); + addr[2] = IINCHIP_READ(GAR0+2); + addr[3] = IINCHIP_READ(GAR0+3); +} +void getSUBR(uint8 * addr) +{ + addr[0] = IINCHIP_READ(SUBR0); + addr[1] = IINCHIP_READ(SUBR0+1); + addr[2] = IINCHIP_READ(SUBR0+2); + addr[3] = IINCHIP_READ(SUBR0+3); +} +void getSHAR(uint8 * addr) +{ + addr[0] = IINCHIP_READ(SHAR0); + addr[1] = IINCHIP_READ(SHAR0+1); + addr[2] = IINCHIP_READ(SHAR0+2); + addr[3] = IINCHIP_READ(SHAR0+3); + addr[4] = IINCHIP_READ(SHAR0+4); + addr[5] = IINCHIP_READ(SHAR0+5); +} +void getSIPR(uint8 * addr) +{ + addr[0] = IINCHIP_READ(SIPR0); + addr[1] = IINCHIP_READ(SIPR0+1); + addr[2] = IINCHIP_READ(SIPR0+2); + addr[3] = IINCHIP_READ(SIPR0+3); +} + + +/** +@brief These below functions are used to get the Destination Hardware Address (MAC Address), Destination IP address and Destination Port. +*/ +void getSn_DHAR(SOCKET s, uint8 * addr) +{ + addr[0] = IINCHIP_READ(Sn_DHAR0(s)); + addr[1] = IINCHIP_READ(Sn_DHAR0(s)+1); + addr[2] = IINCHIP_READ(Sn_DHAR0(s)+2); + addr[3] = IINCHIP_READ(Sn_DHAR0(s)+3); + addr[4] = IINCHIP_READ(Sn_DHAR0(s)+4); + addr[5] = IINCHIP_READ(Sn_DHAR0(s)+5); +} +void setSn_DHAR(SOCKET s, uint8 * addr) +{ + IINCHIP_WRITE((Sn_DHAR0(s) + 0),addr[0]); + IINCHIP_WRITE((Sn_DHAR0(s) + 1),addr[1]); + IINCHIP_WRITE((Sn_DHAR0(s) + 2),addr[2]); + IINCHIP_WRITE((Sn_DHAR0(s) + 3),addr[3]); + IINCHIP_WRITE((Sn_DHAR0(s) + 4),addr[4]); + IINCHIP_WRITE((Sn_DHAR0(s) + 5),addr[5]); +} +void getSn_DIPR(SOCKET s, uint8 * addr) +{ + addr[0] = IINCHIP_READ(Sn_DIPR0(s)); + addr[1] = IINCHIP_READ(Sn_DIPR0(s)+1); + addr[2] = IINCHIP_READ(Sn_DIPR0(s)+2); + addr[3] = IINCHIP_READ(Sn_DIPR0(s)+3); +} +void setSn_DIPR(SOCKET s, uint8 * addr) +{ + IINCHIP_WRITE((Sn_DIPR0(s) + 0),addr[0]); + IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]); + IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]); + IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]); +} +void getSn_DPORT(SOCKET s, uint8 * addr) +{ + addr[0] = IINCHIP_READ(Sn_DPORT0(s)); + addr[1] = IINCHIP_READ(Sn_DPORT0(s)+1); +} +void setSn_DPORT(SOCKET s, uint8 * addr) +{ + IINCHIP_WRITE((Sn_DPORT0(s) + 0),addr[0]); + IINCHIP_WRITE((Sn_DPORT0(s) + 1),addr[1]); +} + + +/** +@brief This sets the maximum segment size of TCP in Active Mode), while in Passive Mode this is set by peer +*/ +void setSn_MSS(SOCKET s, uint16 Sn_MSSR0) +{ + IINCHIP_WRITE(Sn_MSSR0(s),(uint8)((Sn_MSSR0 & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_MSSR0(s) + 1),(uint8)(Sn_MSSR0 & 0x00ff)); +} + +void setSn_TTL(SOCKET s, uint8 ttl) +{ + IINCHIP_WRITE(Sn_TTL(s), ttl); +} + + +/** +@brief These below function is used to setup the Protocol Field of IP Header when + executing the IP Layer RAW mode. +*/ +void setSn_PROTO(SOCKET s, uint8 proto) +{ + IINCHIP_WRITE(Sn_PROTO(s),proto); +} + + +/** +@brief get socket interrupt status + +These below functions are used to read the Interrupt & Soket Status register +*/ +uint8 getSn_IR(SOCKET s) +{ + return IINCHIP_READ(Sn_IR(s)); +} + + +/** +@brief get socket status +*/ +uint8 getSn_SR(SOCKET s) +{ + return IINCHIP_READ(Sn_SR(s)); +} + + +/** +@brief get socket TX free buf size + +This gives free buffer size of transmit buffer. This is the data size that user can transmit. +User shuold check this value first and control the size of transmitting data +*/ +uint16 getSn_TX_FSR(SOCKET s) +{ + uint16 val=0,val1=0; + do + { + val1 = IINCHIP_READ(Sn_TX_FSR0(s)); + val1 = (val1 << 8) + IINCHIP_READ(Sn_TX_FSR0(s) + 1); + if (val1 != 0) + { + val = IINCHIP_READ(Sn_TX_FSR0(s)); + val = (val << 8) + IINCHIP_READ(Sn_TX_FSR0(s) + 1); + } + } while (val != val1); + return val; +} + + +/** +@brief get socket RX recv buf size + +This gives size of received data in receive buffer. +*/ +uint16 getSn_RX_RSR(SOCKET s) +{ + uint16 val=0,val1=0; + do + { + val1 = IINCHIP_READ(Sn_RX_RSR0(s)); + val1 = (val1 << 8) + IINCHIP_READ(Sn_RX_RSR0(s) + 1); + if(val1 != 0) + { + val = IINCHIP_READ(Sn_RX_RSR0(s)); + val = (val << 8) + IINCHIP_READ(Sn_RX_RSR0(s) + 1); + } + } while (val != val1); + return val; +} + + +/** +@brief This function is being called by send() and sendto() function also. + +This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer +register. User should read upper byte first and lower byte later to get proper value. +*/ +void send_data_processing(SOCKET s, uint8 *data, uint16 len) +{ + uint16 ptr; + ptr = IINCHIP_READ(Sn_TX_WR0(s)); + ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_TX_WR0(s) + 1); + write_data(s, data, (uint8 *)(ptr), len); + ptr += len; + IINCHIP_WRITE(Sn_TX_WR0(s),(uint8)((ptr & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_TX_WR0(s) + 1),(uint8)(ptr & 0x00ff)); +} + + +/** +@brief This function is being called by recv() also. + +This function read the Rx read pointer register +and after copy the data from receive buffer update the Rx write pointer register. +User should read upper byte first and lower byte later to get proper value. +*/ +void recv_data_processing(SOCKET s, uint8 *data, uint16 len) +{ + uint16 ptr; + ptr = IINCHIP_READ(Sn_RX_RD0(s)); + ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_RX_RD0(s) + 1); +#ifdef __DEF_IINCHIP_DBG__ + printf("ISR_RX: rd_ptr : %.4x\r\n", ptr); +#endif + read_data(s, (uint8 *)ptr, data, len); // read data + ptr += len; + IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8)); + IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff)); +} + + +/** +@brief for copy the data form application buffer to Transmite buffer of the chip. + +This function is being used for copy the data form application buffer to Transmite +buffer of the chip. It calculate the actual physical address where one has to write +the data in transmite buffer. Here also take care of the condition while it exceed +the Tx memory uper-bound of socket. +*/ +void write_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len) +{ + uint16 size; + uint16 dst_mask; + uint8 * dst_ptr; + + dst_mask = (uint16)dst & getIINCHIP_TxMASK(s); + dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s) + dst_mask); + + if (dst_mask + len > getIINCHIP_TxMAX(s)) + { + size = getIINCHIP_TxMAX(s) - dst_mask; + wiz_write_buf((uint16)dst_ptr, (uint8*)src, size); + src += size; + size = len - size; + dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s)); + wiz_write_buf((uint16)dst_ptr, (uint8*)src, size); + } + else + { + wiz_write_buf((uint16)dst_ptr, (uint8*)src, len); + } +} + + +/** +@brief This function is being used for copy the data form Receive buffer of the chip to application buffer. + +It calculate the actual physical address where one has to read +the data from Receive buffer. Here also take care of the condition while it exceed +the Rx memory uper-bound of socket. +*/ +void read_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len) +{ + uint16 size; + uint16 src_mask; + uint8 * src_ptr; + + src_mask = (uint16)src & getIINCHIP_RxMASK(s); + src_ptr = (uint8 *)(getIINCHIP_RxBASE(s) + src_mask); + + if( (src_mask + len) > getIINCHIP_RxMAX(s) ) + { + size = getIINCHIP_RxMAX(s) - src_mask; + wiz_read_buf((uint16)src_ptr, (uint8*)dst,size); + dst += size; + size = len - size; + src_ptr = (uint8 *)(getIINCHIP_RxBASE(s)); + wiz_read_buf((uint16)src_ptr, (uint8*) dst,size); + } + else + { + wiz_read_buf((uint16)src_ptr, (uint8*) dst,len); + } +} + + +#ifdef __DEF_IINCHIP_PPP__ +#define PPP_OPTION_BUF_LEN 64 + +uint8 pppinit_in(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen); + + +/** +@brief make PPPoE connection +@return 1 => success to connect, 2 => Auth fail, 3 => timeout, 4 => Auth type not support + +*/ +uint8 pppinit(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen) +{ + uint8 ret; + uint8 isr; + + // PHASE0. W5100 PPPoE(ADSL) setup + // enable pppoe mode + printf("-- PHASE 0. W5100 PPPoE(ADSL) setup process --\r\n"); + printf("\r\n"); + IINCHIP_WRITE(MR,IINCHIP_READ(MR) | MR_PPPOE); + + // open socket in pppoe mode + isr = IINCHIP_READ(Sn_IR(0));// first clear isr(0), W5100 at present time + IINCHIP_WRITE(Sn_IR(0),isr); + + IINCHIP_WRITE(PTIMER,200); // 5sec timeout + IINCHIP_WRITE(PMAGIC,0x01); // magic number + IINCHIP_WRITE(Sn_MR(0),Sn_MR_PPPOE); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_OPEN); + + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + + ret = pppinit_in(id, idlen, passwd, passwdlen); + + // close ppp connection socket + /* +200801 (hwkim) */ + close(0); + /* ------- */ + + return ret; +} + + +uint8 pppinit_in(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen) +{ + uint8 loop_idx = 0; + uint8 isr = 0; + uint8 buf[PPP_OPTION_BUF_LEN]; + uint16 len; + uint8 str[PPP_OPTION_BUF_LEN]; + uint8 str_idx,dst_idx; + + // PHASE1. PPPoE Discovery + // start to connect pppoe connection + printf("-- PHASE 1. PPPoE Discovery process --"); + printf(" ok\r\n"); + printf("\r\n"); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCON); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + + wait_10ms(100); + + loop_idx = 0; + //check whether PPPoE discovery end or not + while (!(IINCHIP_READ(Sn_IR(0)) & Sn_IR_PNEXT)) + { + printf("."); + if (loop_idx++ == 10) // timeout + { + printf("timeout before LCP\r\n"); + return 3; + } + wait_10ms(100); + } + + /* +200801[bj] clear interrupt value*/ + IINCHIP_WRITE(Sn_IR(0), 0xff); + /*---*/ + + // PHASE2. LCP process + printf("-- PHASE 2. LCP process --"); + + // send LCP Request + { + // Magic number option + // option format (type value + length value + data) + // write magic number value + buf[0] = 0x05; // type value + buf[1] = 0x06; // length value + buf[2] = 0x01; buf[3] = 0x01; buf[4] = 0x01; buf[5]= 0x01; // data + // for MRU option, 1492 0x05d4 + // buf[6] = 0x01; buf[7] = 0x04; buf[8] = 0x05; buf[9] = 0xD4; + } + send_data_processing(0, buf, 0x06); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send request + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + + wait_10ms(100); + + while (!((isr = IINCHIP_READ(Sn_IR(0))) & Sn_IR_PNEXT)) + { + if (isr & Sn_IR_PRECV) // Not support option + { + /* +200801[bj] clear interrupt value*/ + IINCHIP_WRITE(Sn_IR(0), Sn_IR_PRECV); + /*---*/ + len = getSn_RX_RSR(0); + if ( len > 0 ) + { + recv_data_processing(0, str, len); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + + // for debug + //printf("LCP proc\r\n"); for (i = 0; i < len; i++) printf ("%02x ", str[i]); printf("\r\n"); + // get option length + len = str[4]; len = ((len & 0x00ff) << 8) + str[5]; + len += 2; + str_idx = 6; dst_idx = 0; // ppp header is 6 byte, so starts at 6. + do + { + if ((str[str_idx] == 0x01) || (str[str_idx] == 0x02) || (str[str_idx] == 0x03) || (str[str_idx] == 0x05)) + { + // skip as length of support option. str_idx+1 is option's length. + str_idx += str[str_idx+1]; + } + else + { + // not support option , REJECT + memcpy((uint8 *)(buf+dst_idx), (uint8 *)(str+str_idx), str[str_idx+1]); + dst_idx += str[str_idx+1]; str_idx += str[str_idx+1]; + } + } while (str_idx != len); + // for debug + // printf("LCP dst proc\r\n"); for (i = 0; i < dst_idx; i++) printf ("%02x ", dst[i]); printf("\r\n"); + + // send LCP REJECT packet + send_data_processing(0, buf, dst_idx); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCJ); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + } + } + printf("."); + if (loop_idx++ == 10) // timeout + { + printf("timeout after LCP\r\n"); + return 3; + } + wait_10ms(100); + } + printf(" ok\r\n"); + printf("\r\n"); + + /* +200801[bj] clear interrupt value*/ + IINCHIP_WRITE(Sn_IR(0), 0xff); + /*---*/ + + printf("-- PHASE 3. PPPoE(ADSL) Authentication mode --\r\n"); + printf("Authentication protocol : %.2x %.2x, ", IINCHIP_READ(PATR0), IINCHIP_READ(PATR0+1)); + + loop_idx = 0; + if (IINCHIP_READ(PATR0) == 0xc0 && IINCHIP_READ(PATR0+1) == 0x23) + { + printf("PAP\r\n"); // in case of adsl normally supports PAP. + // send authentication data + // copy (idlen + id + passwdlen + passwd) + buf[loop_idx] = idlen; loop_idx++; + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(id), idlen); loop_idx += idlen; + buf[loop_idx] = passwdlen; loop_idx++; + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(passwd), passwdlen); loop_idx += passwdlen; + send_data_processing(0, buf, loop_idx); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + wait_10ms(100); + } + else if (IINCHIP_READ(PATR0) == 0xc2 && IINCHIP_READ(PATR0+1) == 0x23) + { + uint8 chal_len; + md5_ctx context; + uint8 digest[16]; + + len = getSn_RX_RSR(0); + if ( len > 0 ) + { + recv_data_processing(0, str, len); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ +#ifdef __DEF_IINCHIP_DBG__ + printf("recv CHAP\r\n"); + { + int16 i; + + for (i = 0; i < 32; i++) + printf ("%02x ", str[i]); + } + printf("\r\n"); +#endif +// str is C2 23 xx CHAL_ID xx xx CHAP_LEN CHAP_DATA +// index 0 1 2 3 4 5 6 7 ... + + memset(buf,0x00,64); + buf[loop_idx] = str[3]; loop_idx++; // chal_id + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(passwd), passwdlen); loop_idx += passwdlen; //passwd + chal_len = str[6]; // chal_id + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(str+7), chal_len); loop_idx += chal_len; //challenge + buf[loop_idx] = 0x80; +#ifdef __DEF_IINCHIP_DBG__ + printf("CHAP proc d1\r\n"); + { + int16 i; + for (i = 0; i < 64; i++) + printf ("%02x ", buf[i]); + } + printf("\r\n"); +#endif + + md5_init(&context); + md5_update(&context, buf, loop_idx); + md5_final(digest, &context); + +#ifdef __DEF_IINCHIP_DBG__ + printf("CHAP proc d1\r\n"); + { + int16 i; + for (i = 0; i < 16; i++) + printf ("%02x", digest[i]); + } + printf("\r\n"); +#endif + loop_idx = 0; + buf[loop_idx] = 16; loop_idx++; // hash_len + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(digest), 16); loop_idx += 16; // hashed value + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(id), idlen); loop_idx += idlen; // id + send_data_processing(0, buf, loop_idx); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + wait_10ms(100); + } + } + else + { + printf("Not support\r\n"); +#ifdef __DEF_IINCHIP_DBG__ + printf("Not support PPP Auth type: %.2x%.2x\r\n",IINCHIP_READ(PATR0), IINCHIP_READ(PATR0+1)); +#endif + return 4; + } + printf("\r\n"); + + printf("-- Waiting for PPPoE server's admission --"); + loop_idx = 0; + while (!((isr = IINCHIP_READ(Sn_IR(0))) & Sn_IR_PNEXT)) + { + if (isr & Sn_IR_PFAIL) + { + /* +200801[bj] clear interrupt value*/ + IINCHIP_WRITE(Sn_IR(0), 0xff); + /*---*/ + printf("failed\r\nReinput id, password..\r\n"); + return 2; + } + printf("."); + if (loop_idx++ == 10) // timeout + { + /* +200801[bj] clear interrupt value*/ + IINCHIP_WRITE(Sn_IR(0), 0xff); + /*---*/ + printf("timeout after PAP\r\n"); + return 3; + } + wait_10ms(100); + } + /* +200801[bj] clear interrupt value*/ + IINCHIP_WRITE(Sn_IR(0), 0xff); + /*---*/ + printf("ok\r\n"); + printf("\r\n"); + printf("-- PHASE 4. IPCP process --"); + // IP Address + buf[0] = 0x03; buf[1] = 0x06; buf[2] = 0x00; buf[3] = 0x00; buf[4] = 0x00; buf[5] = 0x00; + send_data_processing(0, buf, 6); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + wait_10ms(100); + + loop_idx = 0; + while (1) + { + if (IINCHIP_READ(Sn_IR(0)) & Sn_IR_PRECV) + { + /* +200801[bj] clear interrupt value*/ + IINCHIP_WRITE(Sn_IR(0), 0xff); + /*---*/ + len = getSn_RX_RSR(0); + if ( len > 0 ) + { + recv_data_processing(0, str, len); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + //for debug + //printf("IPCP proc\r\n"); for (i = 0; i < len; i++) printf ("%02x ", str[i]); printf("\r\n"); + str_idx = 6; dst_idx = 0; + if (str[2] == 0x03) // in case of NAK + { + do + { + if (str[str_idx] == 0x03) // request only ip information + { + memcpy((uint8 *)(buf+dst_idx), (uint8 *)(str+str_idx), str[str_idx+1]); + dst_idx += str[str_idx+1]; str_idx += str[str_idx+1]; + } + else + { + // skip byte + str_idx += str[str_idx+1]; + } + // for debug + //printf("s: %d, d: %d, l: %d", str_idx, dst_idx, len); + } while (str_idx != len); + send_data_processing(0, buf, dst_idx); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send ipcp request + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + wait_10ms(100); + break; + } + } + } + printf("."); + if (loop_idx++ == 10) // timeout + { + printf("timeout after IPCP\r\n"); + return 3; + } + wait_10ms(100); + send_data_processing(0, buf, 6); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); //ipcp re-request + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + } + + loop_idx = 0; + while (!(IINCHIP_READ(Sn_IR(0)) & Sn_IR_PNEXT)) + { + printf("."); + if (loop_idx++ == 10) // timeout + { + printf("timeout after IPCP NAK\r\n"); + return 3; + } + wait_10ms(100); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send ipcp request + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + } + /* +200801[bj] clear interrupt value*/ + IINCHIP_WRITE(Sn_IR(0), 0xff); + /*---*/ + printf("ok\r\n"); + printf("\r\n"); + return 1; + // after this function, User must save the pppoe server's mac address and pppoe session id in current connection +} + + +/** +@brief terminate PPPoE connection +*/ +uint8 pppterm(uint8 * mac, uint8 * sessionid) +{ + uint16 i; + uint8 isr; +#ifdef __DEF_IINCHIP_DBG__ + printf("pppterm()\r\n"); +#endif + /* Set PPPoE bit in MR(Common Mode Register) : enable socket0 pppoe */ + IINCHIP_WRITE(MR,IINCHIP_READ(MR) | MR_PPPOE); + + // write pppoe server's mac address and session id + // must be setted these value. + for (i = 0; i < 6; i++) IINCHIP_WRITE((Sn_DHAR0(0)+i),mac[i]); + for (i = 0; i < 2; i++) IINCHIP_WRITE((Sn_DPORT0(0)+i),sessionid[i]); + isr = IINCHIP_READ(Sn_IR(0)); + IINCHIP_WRITE(Sn_IR(0),isr); + + //open socket in pppoe mode + IINCHIP_WRITE(Sn_MR(0),Sn_MR_PPPOE); + IINCHIP_WRITE(Sn_CR(0),Sn_CR_OPEN); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + wait_1us(1); + // close pppoe connection + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PDISCON); + /* +20071122[chungs]:wait to process the command... */ + while( IINCHIP_READ(Sn_CR(0)) ) + ; + /* ------- */ + wait_10ms(100); + // close socket + /* +200801 (hwkim) */ + close(0); + /* ------- */ + + +#ifdef __DEF_IINCHIP_DBG__ + printf("pppterm() end ..\r\n"); +#endif + + return 1; +} +#endif diff --git a/libraries/Ethernet/utility/w5100.h b/libraries/Ethernet/utility/w5100.h new file mode 100755 index 0000000..6eddf91 --- /dev/null +++ b/libraries/Ethernet/utility/w5100.h @@ -0,0 +1,299 @@ +/* +@file w5100.h +*/ + +#ifndef _W5100_H_ +#define _W5100_H_ + + +#define MR __DEF_IINCHIP_MAP_BASE__ +#define IDM_OR ((__DEF_IINCHIP_MAP_BASE__ + 0x00)) +#define IDM_AR0 ((__DEF_IINCHIP_MAP_BASE__ + 0x01)) +#define IDM_AR1 ((__DEF_IINCHIP_MAP_BASE__ + 0x02)) +#define IDM_DR ((__DEF_IINCHIP_MAP_BASE__ + 0x03)) + + +/** + @brief Gateway IP Register address + */ +#define GAR0 (COMMON_BASE + 0x0001) +/** + @brief Subnet mask Register address + */ +#define SUBR0 (COMMON_BASE + 0x0005) +/** + @brief Source MAC Register address + */ +#define SHAR0 (COMMON_BASE + 0x0009) +/** + @brief Source IP Register address + */ +#define SIPR0 (COMMON_BASE + 0x000F) +/** + @brief Interrupt Register + */ +#define IR (COMMON_BASE + 0x0015) +/** + @brief Interrupt mask register + */ +#define IMR (COMMON_BASE + 0x0016) +/** + @brief Timeout register address( 1 is 100us ) + */ +#define RTR0 (COMMON_BASE + 0x0017) +/** + @brief Retry count reigster + */ +#define RCR (COMMON_BASE + 0x0019) +/** + @brief Receive memory size reigster + */ +#define RMSR (COMMON_BASE + 0x001A) +/** + @brief Transmit memory size reigster + */ +#define TMSR (COMMON_BASE + 0x001B) +/** + @brief Authentication type register address in PPPoE mode + */ +#define PATR0 (COMMON_BASE + 0x001C) +//#define PPPALGO (COMMON_BASE + 0x001D) +#define PTIMER (COMMON_BASE + 0x0028) +#define PMAGIC (COMMON_BASE + 0x0029) + +/** + @brief Unreachable IP register address in UDP mode + */ +#define UIPR0 (COMMON_BASE + 0x002A) +/** + @brief Unreachable Port register address in UDP mode + */ +#define UPORT0 (COMMON_BASE + 0x002E) + +/** + @brief socket register +*/ +#define CH_BASE (COMMON_BASE + 0x0400) +/** + @brief size of each channel register map + */ +#define CH_SIZE 0x0100 +/** + @brief socket Mode register + */ +#define Sn_MR(ch) (CH_BASE + ch * CH_SIZE + 0x0000) +/** + @brief channel Sn_CR register + */ +#define Sn_CR(ch) (CH_BASE + ch * CH_SIZE + 0x0001) +/** + @brief channel interrupt register + */ +#define Sn_IR(ch) (CH_BASE + ch * CH_SIZE + 0x0002) +/** + @brief channel status register + */ +#define Sn_SR(ch) (CH_BASE + ch * CH_SIZE + 0x0003) +/** + @brief source port register + */ +#define Sn_PORT0(ch) (CH_BASE + ch * CH_SIZE + 0x0004) +/** + @brief Peer MAC register address + */ +#define Sn_DHAR0(ch) (CH_BASE + ch * CH_SIZE + 0x0006) +/** + @brief Peer IP register address + */ +#define Sn_DIPR0(ch) (CH_BASE + ch * CH_SIZE + 0x000C) +/** + @brief Peer port register address + */ +#define Sn_DPORT0(ch) (CH_BASE + ch * CH_SIZE + 0x0010) +/** + @brief Maximum Segment Size(Sn_MSSR0) register address + */ +#define Sn_MSSR0(ch) (CH_BASE + ch * CH_SIZE + 0x0012) +/** + @brief Protocol of IP Header field register in IP raw mode + */ +#define Sn_PROTO(ch) (CH_BASE + ch * CH_SIZE + 0x0014) + +/** + @brief IP Type of Service(TOS) Register + */ +#define Sn_TOS(ch) (CH_BASE + ch * CH_SIZE + 0x0015) +/** + @brief IP Time to live(TTL) Register + */ +#define Sn_TTL(ch) (CH_BASE + ch * CH_SIZE + 0x0016) + +/** + @brief Transmit free memory size register + */ +#define Sn_TX_FSR0(ch) (CH_BASE + ch * CH_SIZE + 0x0020) +/** + @brief Transmit memory read pointer register address + */ +#define Sn_TX_RD0(ch) (CH_BASE + ch * CH_SIZE + 0x0022) +/** + @brief Transmit memory write pointer register address + */ +#define Sn_TX_WR0(ch) (CH_BASE + ch * CH_SIZE + 0x0024) +/** + @brief Received data size register + */ +#define Sn_RX_RSR0(ch) (CH_BASE + ch * CH_SIZE + 0x0026) +/** + @brief Read point of Receive memory + */ +#define Sn_RX_RD0(ch) (CH_BASE + ch * CH_SIZE + 0x0028) +/** + @brief Write point of Receive memory + */ +#define Sn_RX_WR0(ch) (CH_BASE + ch * CH_SIZE + 0x002A) + + + +/* MODE register values */ +#define MR_RST 0x80 /**< reset */ +#define MR_PB 0x10 /**< ping block */ +#define MR_PPPOE 0x08 /**< enable pppoe */ +#define MR_LB 0x04 /**< little or big endian selector in indirect mode */ +#define MR_AI 0x02 /**< auto-increment in indirect mode */ +#define MR_IND 0x01 /**< enable indirect mode */ + +/* IR register values */ +#define IR_CONFLICT 0x80 /**< check ip confict */ +#define IR_UNREACH 0x40 /**< get the destination unreachable message in UDP sending */ +#define IR_PPPoE 0x20 /**< get the PPPoE close message */ +#define IR_SOCK(ch) (0x01 << ch) /**< check socket interrupt */ + +/* Sn_MR values */ +#define Sn_MR_CLOSE 0x00 /**< unused socket */ +#define Sn_MR_TCP 0x01 /**< TCP */ +#define Sn_MR_UDP 0x02 /**< UDP */ +#define Sn_MR_IPRAW 0x03 /**< IP LAYER RAW SOCK */ +#define Sn_MR_MACRAW 0x04 /**< MAC LAYER RAW SOCK */ +#define Sn_MR_PPPOE 0x05 /**< PPPoE */ +#define Sn_MR_ND 0x20 /**< No Delayed Ack(TCP) flag */ +#define Sn_MR_MULTI 0x80 /**< support multicating */ + + +/* Sn_CR values */ +#define Sn_CR_OPEN 0x01 /**< initialize or open socket */ +#define Sn_CR_LISTEN 0x02 /**< wait connection request in tcp mode(Server mode) */ +#define Sn_CR_CONNECT 0x04 /**< send connection request in tcp mode(Client mode) */ +#define Sn_CR_DISCON 0x08 /**< send closing reqeuset in tcp mode */ +#define Sn_CR_CLOSE 0x10 /**< close socket */ +#define Sn_CR_SEND 0x20 /**< updata txbuf pointer, send data */ +#define Sn_CR_SEND_MAC 0x21 /**< send data with MAC address, so without ARP process */ +#define Sn_CR_SEND_KEEP 0x22 /**< send keep alive message */ +#define Sn_CR_RECV 0x40 /**< update rxbuf pointer, recv data */ + +#ifdef __DEF_IINCHIP_PPP__ + #define Sn_CR_PCON 0x23 + #define Sn_CR_PDISCON 0x24 + #define Sn_CR_PCR 0x25 + #define Sn_CR_PCN 0x26 + #define Sn_CR_PCJ 0x27 +#endif + +/* Sn_IR values */ +#ifdef __DEF_IINCHIP_PPP__ + #define Sn_IR_PRECV 0x80 + #define Sn_IR_PFAIL 0x40 + #define Sn_IR_PNEXT 0x20 +#endif +#define Sn_IR_SEND_OK 0x10 /**< complete sending */ +#define Sn_IR_TIMEOUT 0x08 /**< assert timeout */ +#define Sn_IR_RECV 0x04 /**< receiving data */ +#define Sn_IR_DISCON 0x02 /**< closed socket */ +#define Sn_IR_CON 0x01 /**< established connection */ + +/* Sn_SR values */ +#define SOCK_CLOSED 0x00 /**< closed */ +#define SOCK_INIT 0x13 /**< init state */ +#define SOCK_LISTEN 0x14 /**< listen state */ +#define SOCK_SYNSENT 0x15 /**< connection state */ +#define SOCK_SYNRECV 0x16 /**< connection state */ +#define SOCK_ESTABLISHED 0x17 /**< success to connect */ +#define SOCK_FIN_WAIT 0x18 /**< closing state */ +#define SOCK_CLOSING 0x1A /**< closing state */ +#define SOCK_TIME_WAIT 0x1B /**< closing state */ +#define SOCK_CLOSE_WAIT 0x1C /**< closing state */ +#define SOCK_LAST_ACK 0x1D /**< closing state */ +#define SOCK_UDP 0x22 /**< udp socket */ +#define SOCK_IPRAW 0x32 /**< ip raw mode socket */ +#define SOCK_MACRAW 0x42 /**< mac raw mode socket */ +#define SOCK_PPPOE 0x5F /**< pppoe socket */ + +/* IP PROTOCOL */ +#define IPPROTO_IP 0 /**< Dummy for IP */ +#define IPPROTO_ICMP 1 /**< Control message protocol */ +#define IPPROTO_IGMP 2 /**< Internet group management protocol */ +#define IPPROTO_GGP 3 /**< Gateway^2 (deprecated) */ +#define IPPROTO_TCP 6 /**< TCP */ +#define IPPROTO_PUP 12 /**< PUP */ +#define IPPROTO_UDP 17 /**< UDP */ +#define IPPROTO_IDP 22 /**< XNS idp */ +#define IPPROTO_ND 77 /**< UNOFFICIAL net disk protocol */ +#define IPPROTO_RAW 255 /**< Raw IP packet */ + + +/********************************************************* +* iinchip access function +*********************************************************/ +extern uint8 IINCHIP_READ(uint16 addr); +extern uint8 IINCHIP_WRITE(uint16 addr,uint8 data); +extern uint16 wiz_read_buf(uint16 addr, uint8* buf,uint16 len); +extern uint16 wiz_write_buf(uint16 addr,uint8* buf,uint16 len); + +extern void iinchip_init(void); // reset iinchip +extern void sysinit(uint8 tx_size, uint8 rx_size); // setting tx/rx buf size +extern uint8 getISR(uint8 s); +extern void putISR(uint8 s, uint8 val); +extern uint16 getIINCHIP_RxMAX(uint8 s); +extern uint16 getIINCHIP_TxMAX(uint8 s); +extern uint16 getIINCHIP_RxMASK(uint8 s); +extern uint16 getIINCHIP_TxMASK(uint8 s); +extern uint16 getIINCHIP_RxBASE(uint8 s); +extern uint16 getIINCHIP_TxBASE(uint8 s); +extern void setGAR(uint8 * addr); // set gateway address +extern void setSUBR(uint8 * addr); // set subnet mask address +extern void setSHAR(uint8 * addr); // set local MAC address +extern void setSIPR(uint8 * addr); // set local IP address +extern void setRTR(uint16 timeout); // set retry duration for data transmission, connection, closing ... +extern void setRCR(uint8 retry); // set retry count (above the value, assert timeout interrupt) +extern void setIMR(uint8 mask); // set interrupt mask. +extern void getGAR(uint8 * addr); +extern void getSUBR(uint8 * addr); +extern void getSHAR(uint8 * addr); +extern void getSIPR(uint8 * addr); +extern uint8 getIR( void ); +extern void setSn_MSS(SOCKET s, uint16 Sn_MSSR0); // set maximum segment size +extern void setSn_PROTO(SOCKET s, uint8 proto); // set IP Protocol value using IP-Raw mode +extern uint8 getSn_IR(SOCKET s); // get socket interrupt status +extern uint8 getSn_SR(SOCKET s); // get socket status +extern uint16 getSn_TX_FSR(SOCKET s); // get socket TX free buf size +extern uint16 getSn_RX_RSR(SOCKET s); // get socket RX recv buf size +extern void setSn_DHAR(SOCKET s, uint8 * addr); +extern void setSn_DIPR(SOCKET s, uint8 * addr); +extern void setSn_DPORT(SOCKET s, uint8 * addr); +extern void getSn_DHAR(SOCKET s, uint8 * addr); +extern void getSn_DIPR(SOCKET s, uint8 * addr); +extern void getSn_DPORT(SOCKET s, uint8 * addr); +extern void setSn_TTL(SOCKET s, uint8 ttl); +extern void setMR(uint8 val); + +#ifdef __DEF_IINCHIP_PPP__ +extern uint8 pppinit(uint8 *id, uint8 idlen, uint8 *passwd, uint8 passwdlen); +extern uint8 pppterm(uint8 *mac,uint8 *sessionid); +#endif + +extern void send_data_processing(SOCKET s, uint8 *data, uint16 len); +extern void recv_data_processing(SOCKET s, uint8 *data, uint16 len); +extern void read_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len); +extern void write_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len); + +#endif diff --git a/libraries/Firmata/Firmata.cpp b/libraries/Firmata/Firmata.cpp new file mode 100644 index 0000000..4e59d27 --- /dev/null +++ b/libraries/Firmata/Firmata.cpp @@ -0,0 +1,445 @@ +/* + Firmata.cpp - Firmata library + Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights 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. + + See file LICENSE.txt for further informations on licensing terms. +*/ + +//****************************************************************************** +//* Includes +//****************************************************************************** + +#include "WProgram.h" +#include "HardwareSerial.h" +#include "Firmata.h" + +extern "C" { +#include +#include +} + +//****************************************************************************** +//* Support Functions +//****************************************************************************** + +void sendValueAsTwo7bitBytes(int value) +{ + Serial.print(value & B01111111, BYTE); // LSB + Serial.print(value >> 7 & B01111111, BYTE); // MSB +} + +void startSysex(void) +{ + Serial.print(START_SYSEX, BYTE); +} + +void endSysex(void) +{ + Serial.print(END_SYSEX, BYTE); +} + +//****************************************************************************** +//* Constructors +//****************************************************************************** + +FirmataClass::FirmataClass(void) +{ + firmwareVersionCount = 0; + systemReset(); +} + +//****************************************************************************** +//* Public Methods +//****************************************************************************** + +/* begin method for overriding default serial bitrate */ +void FirmataClass::begin(void) +{ + Serial.begin(115200); + blinkVersion(); + delay(300); + printVersion(); +} + +/* begin method for overriding default serial bitrate */ +void FirmataClass::begin(long speed) +{ + blinkVersion(); +#if defined(__AVR_ATmega128__) // Wiring + Serial.begin((uint32_t)speed); +#else + Serial.begin(speed); +#endif + delay(300); + printVersion(); + printFirmwareVersion(); +} + +// output the protocol version message to the serial port +void FirmataClass::printVersion(void) { + Serial.print(REPORT_VERSION, BYTE); + Serial.print(FIRMATA_MAJOR_VERSION, BYTE); + Serial.print(FIRMATA_MINOR_VERSION, BYTE); +} + +void FirmataClass::blinkVersion(void) +{ + // flash the pin with the protocol version + pinMode(VERSION_BLINK_PIN,OUTPUT); + pin13strobe(FIRMATA_MAJOR_VERSION, 200, 400); + delay(300); + pin13strobe(2,1,4); // separator, a quick burst + delay(300); + pin13strobe(FIRMATA_MINOR_VERSION, 200, 400); +} + +void FirmataClass::printFirmwareVersion(void) +{ + byte i; + + if(firmwareVersionCount) { // make sure that the name has been set before reporting + startSysex(); + Serial.print(REPORT_FIRMWARE, BYTE); + Serial.print(firmwareVersionVector[0]); // major version number + Serial.print(firmwareVersionVector[1]); // minor version number + for(i=2; i 0) && (inputData < 128) ) { + waitForData--; + storedInputData[waitForData] = inputData; + if( (waitForData==0) && executeMultiByteCommand ) { // got the whole message + switch(executeMultiByteCommand) { + case ANALOG_MESSAGE: + if(currentAnalogCallback) { + (*currentAnalogCallback)(multiByteChannel, + (storedInputData[0] << 7) + + storedInputData[1]); + } + break; + case DIGITAL_MESSAGE: + if(currentDigitalCallback) { + (*currentDigitalCallback)(multiByteChannel, + (storedInputData[0] << 7) + + storedInputData[1]); + } + break; + case SET_PIN_MODE: + if(currentPinModeCallback) + (*currentPinModeCallback)(storedInputData[1], storedInputData[0]); + break; + case REPORT_ANALOG: + if(currentReportAnalogCallback) + (*currentReportAnalogCallback)(multiByteChannel,storedInputData[0]); + break; + case REPORT_DIGITAL: + if(currentReportDigitalCallback) + (*currentReportDigitalCallback)(multiByteChannel,storedInputData[0]); + break; + } + executeMultiByteCommand = 0; + } + } else { + // remove channel info from command byte if less than 0xF0 + if(inputData < 0xF0) { + command = inputData & 0xF0; + multiByteChannel = inputData & 0x0F; + } else { + command = inputData; + // commands in the 0xF* range don't use channel data + } + switch (command) { + case ANALOG_MESSAGE: + case DIGITAL_MESSAGE: + case SET_PIN_MODE: + waitForData = 2; // two data bytes needed + executeMultiByteCommand = command; + break; + case REPORT_ANALOG: + case REPORT_DIGITAL: + waitForData = 1; // two data bytes needed + executeMultiByteCommand = command; + break; + case START_SYSEX: + parsingSysex = true; + sysexBytesRead = 0; + break; + case SYSTEM_RESET: + systemReset(); + break; + case REPORT_VERSION: + Firmata.printVersion(); + break; + } + } +} + +//------------------------------------------------------------------------------ +// Serial Send Handling + +// send an analog message +void FirmataClass::sendAnalog(byte pin, int value) +{ + // pin can only be 0-15, so chop higher bits + Serial.print(ANALOG_MESSAGE | (pin & 0xF), BYTE); + sendValueAsTwo7bitBytes(value); +} + +// send a single digital pin in a digital message +void FirmataClass::sendDigital(byte pin, int value) +{ + /* TODO add single pin digital messages to the protocol, this needs to + * track the last digital data sent so that it can be sure to change just + * one bit in the packet. This is complicated by the fact that the + * numbering of the pins will probably differ on Arduino, Wiring, and + * other boards. The DIGITAL_MESSAGE sends 14 bits at a time, but it is + * probably easier to send 8 bit ports for any board with more than 14 + * digital pins. + */ + + // TODO: the digital message should not be sent on the serial port every + // time sendDigital() is called. Instead, it should add it to an int + // which will be sent on a schedule. If a pin changes more than once + // before the digital message is sent on the serial port, it should send a + // digital message for each change. + +// if(value == 0) +// sendDigitalPortPair(); +} + + +// send 14-bits in a single digital message (protocol v1) +// send an 8-bit port in a single digital message (protocol v2) +void FirmataClass::sendDigitalPort(byte portNumber, int portData) +{ + Serial.print(DIGITAL_MESSAGE | (portNumber & 0xF),BYTE); + Serial.print(portData % 128, BYTE); // Tx bits 0-6 + Serial.print(portData >> 7, BYTE); // Tx bits 7-13 +} + + +void FirmataClass::sendSysex(byte command, byte bytec, byte* bytev) +{ + byte i; + startSysex(); + Serial.print(command, BYTE); + for(i=0; i +#include + + +/* Version numbers for the protocol. The protocol is still changing, so these + * version numbers are important. This number can be queried so that host + * software can test whether it will be compatible with the currently + * installed firmware. */ +#define FIRMATA_MAJOR_VERSION 2 // for non-compatible changes +#define FIRMATA_MINOR_VERSION 0 // for backwards compatible changes +#define VERSION_BLINK_PIN 13 // digital pin to blink version on + +#define MAX_DATA_BYTES 32 // max number of data bytes in non-Sysex messages + +// message command bytes (128-255/0x80-0xFF) +#define DIGITAL_MESSAGE 0x90 // send data for a digital pin +#define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM) +#define REPORT_ANALOG 0xC0 // enable analog input by pin # +#define REPORT_DIGITAL 0xD0 // enable digital input by port pair +// +#define SET_PIN_MODE 0xF4 // set a pin to INPUT/OUTPUT/PWM/etc +// +#define REPORT_VERSION 0xF9 // report protocol version +#define SYSTEM_RESET 0xFF // reset from MIDI +// +#define START_SYSEX 0xF0 // start a MIDI Sysex message +#define END_SYSEX 0xF7 // end a MIDI Sysex message + +// extended command set using sysex (0-127/0x00-0x7F) +/* 0x00-0x0F reserved for custom commands */ +#define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq +#define FIRMATA_STRING 0x71 // a string message with 14-bits per char +#define REPORT_FIRMWARE 0x79 // report name and version of the firmware +#define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages +#define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages + +// pin modes +//#define INPUT 0x00 // defined in wiring.h +//#define OUTPUT 0x01 // defined in wiring.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 + + +extern "C" { +// callback function types + typedef void (*callbackFunction)(byte, int); + typedef void (*systemResetCallbackFunction)(void); + typedef void (*stringCallbackFunction)(char*); + typedef void (*sysexCallbackFunction)(byte command, byte argc, byte*argv); +} + + +// TODO make it a subclass of HardwareSerial +class FirmataClass +{ +public: + FirmataClass(); +/* Arduino constructors */ + void begin(); + void begin(long); +/* querying functions */ + void printVersion(void); + void blinkVersion(void); + void printFirmwareVersion(void); +// void setFirmwareVersion(byte major, byte minor); // see macro below + void setFirmwareNameAndVersion(const char *name, byte major, byte minor); +/* serial receive handling */ + int available(void); + void processInput(void); +/* serial send handling */ + void sendAnalog(byte pin, int value); + void sendDigital(byte pin, int value); + void sendDigitalPort(byte portNumber, int portData); + void sendString(const char* string); + void sendString(byte command, const char* string); + void sendSysex(byte command, byte bytec, byte* bytev); +// void print(); // TODO implement so it's compatible to Serial +// void println(); // TODO implement so it's compatible to Serial +/* attach & detach callback functions to messages */ + void attach(byte command, callbackFunction newFunction); + void attach(byte command, systemResetCallbackFunction newFunction); + void attach(byte command, stringCallbackFunction newFunction); + void attach(byte command, sysexCallbackFunction newFunction); + void detach(byte command); +// void flush(); // TODO implement flush, probably by subclassing + +private: +/* firmware name and version */ + byte firmwareVersionCount; + byte *firmwareVersionVector; +/* input message handling */ + byte waitForData; // this flag says the next serial input will be data + byte executeMultiByteCommand; // execute this after getting multi-byte data + byte multiByteChannel; // channel data for multiByteCommands + byte storedInputData[MAX_DATA_BYTES]; // multi-byte data +/* sysex */ + boolean parsingSysex; + int sysexBytesRead; +/* callback functions */ + callbackFunction currentAnalogCallback; + callbackFunction currentDigitalCallback; + callbackFunction currentReportAnalogCallback; + callbackFunction currentReportDigitalCallback; + callbackFunction currentPinModeCallback; + systemResetCallbackFunction currentSystemResetCallback; + stringCallbackFunction currentStringCallback; + sysexCallbackFunction currentSysexCallback; + +/* private methods ------------------------------ */ + void processSysexMessage(void); + void systemReset(void); + void pin13strobe(int count, int onInterval, int offInterval); +}; + +extern FirmataClass Firmata; + +/*============================================================================== + * MACROS + *============================================================================*/ + +/* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the + * firmware name. It needs to be a macro so that __FILE__ is included in the + * firmware source file rather than the library source file. + */ +#define setFirmwareVersion(x, y) setFirmwareNameAndVersion(__FILE__, x, y) + +// total number of pins currently supported +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) // Arduino NG and Diecimila +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_DIGITAL_PINS 22 // 14 digital + 8 analog +#define TOTAL_PORTS 3 // total number of ports for the board +#define ANALOG_PORT 2 // port# of analog used as digital +#elif defined(__AVR_ATmega8__) // old Arduinos +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_DIGITAL_PINS 20 // 14 digital + 6 analog +#define TOTAL_PORTS 3 // total number of ports for the board +#define ANALOG_PORT 2 // port# of analog used as digital +#elif defined(__AVR_ATmega128__)// Wiring +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_DIGITAL_PINS 51 +#define TOTAL_PORTS 6 // total number of ports for the board +#define ANALOG_PORT 2 // port# of analog used as digital +#else // anything else +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_DIGITAL_PINS 14 +#define TOTAL_PORTS 3 // total number of ports for the board +#define ANALOG_PORT 2 // port# of analog used as digital +#endif + + + +#endif /* Firmata_h */ + diff --git a/libraries/Firmata/LICENSE.txt b/libraries/Firmata/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/TODO.txt b/libraries/Firmata/TODO.txt new file mode 100644 index 0000000..86c9858 --- /dev/null +++ b/libraries/Firmata/TODO.txt @@ -0,0 +1,14 @@ + +- make Firmata a subclass of HardwareSerial + +- per-pin digital callback, since the per-port callback is a bit complicated + for beginners (maybe Firmata is not for beginners...) + +- simplify SimpleDigitalFirmata, take out the code that checks to see if the + data has changed, since it is a bit complicated for this example. Ideally + this example would be based on a call + +- turn current SimpleDigitalFirmata into DigitalPortFirmata for a more complex + example using the code which checks for changes before doing anything + +- test integration with Wiring diff --git a/libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.pde b/libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.pde new file mode 100644 index 0000000..fcd8e5e --- /dev/null +++ b/libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.pde @@ -0,0 +1,83 @@ +/* This firmware supports as many analog ports as possible, all analog inputs, + * four PWM outputs, and two with servo support. + * + * This example code is in the public domain. + */ +#include +#include + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +/* servos */ +Servo servo9, servo10; // one instance per pin +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting +int analogPin = 0; // counter for reading analog pins +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long nextExecuteMillis; // for comparison with currentMillis + + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void analogWriteCallback(byte pin, int value) +{ + switch(pin) { + case 9: servo9.write(value); break; + case 10: servo10.write(value); break; + case 3: + case 5: + case 6: + case 11: // PWM pins + analogWrite(pin, value); + break; + } +} +// ----------------------------------------------------------------------------- +// sets bits in a bit array (int) to toggle the reporting of the analogIns +void reportAnalogCallback(byte pin, int value) +{ + if(value == 0) { + analogInputsToReport = analogInputsToReport &~ (1 << pin); + } + else { // everything but 0 enables reporting of that pin + analogInputsToReport = analogInputsToReport | (1 << pin); + } + // TODO: save status to EEPROM here, if changed +} + +/*============================================================================== + * SETUP() + *============================================================================*/ +void setup() +{ + Firmata.setFirmwareVersion(0, 2); + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + + servo9.attach(9); + servo10.attach(10); + Firmata.begin(); +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + while(Firmata.available()) + Firmata.processInput(); + currentMillis = millis(); + if(currentMillis > nextExecuteMillis) { + nextExecuteMillis = currentMillis + 19; // run this every 20ms + for(analogPin=0;analogPin +# +# - Write prototypes for all your functions (or define them before you +# call them). A prototype declares the types of parameters a +# function will take and what type of value it will return. This +# means that you can have a call to a function before the definition +# of the function. A function prototype looks like the first line of +# the function, with a semi-colon at the end. For example: +# int digitalRead(int pin); +# +# Instructions for using the makefile: +# +# 1. Copy this file into the folder with your sketch. +# +# 2. Below, modify the line containing "TARGET" to refer to the name of +# of your program's file without an extension (e.g. TARGET = foo). +# +# 3. Modify the line containg "ARDUINO" to point the directory that +# contains the Arduino core (for normal Arduino installations, this +# is the hardware/cores/arduino sub-directory). +# +# 4. Modify the line containing "PORT" to refer to the filename +# representing the USB or serial connection to your Arduino board +# (e.g. PORT = /dev/tty.USB0). If the exact name of this file +# changes, you can use * as a wildcard (e.g. PORT = /dev/tty.USB*). +# +# 5. At the command line, change to the directory containing your +# program's file and the makefile. +# +# 6. Type "make" and press enter to compile/verify your program. +# +# 7. Type "make upload", reset your Arduino board, and press enter to +# upload your program to the Arduino board. +# +# $Id: Makefile,v 1.7 2007/04/13 05:28:23 eighthave Exp $ + +PORT = /dev/tty.usbserial-* +TARGET := $(shell pwd | sed 's|.*/\(.*\)|\1|') +ARDUINO = /Applications/arduino +ARDUINO_SRC = $(ARDUINO)/hardware/cores/arduino +ARDUINO_LIB_SRC = $(ARDUINO)/hardware/libraries +INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/hardware/tools/avr/avr/include \ + -I$(ARDUINO_LIB_SRC)/EEPROM \ + -I$(ARDUINO_LIB_SRC)/Firmata \ + -I$(ARDUINO_LIB_SRC)/Servo \ + -I$(ARDUINO_LIB_SRC) +SRC = $(wildcard $(ARDUINO_SRC)/*.c) +CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \ + $(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \ + $(ARDUINO_LIB_SRC)/Firmata/Firmata.cpp \ + $(ARDUINO_LIB_SRC)/Servo/Servo.cpp \ + $(ARDUINO_SRC)/WMath.cpp +HEADERS = $(wildcard $(ARDUINO_SRC)/*.h) $(wildcard $(ARDUINO_LIB_SRC)/*/*.h) + +MCU = atmega168 +#MCU = atmega8 +F_CPU = 16000000 +FORMAT = ihex +UPLOAD_RATE = 19200 + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +DEBUG = stabs + +OPT = s + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU) +CXXDEFS = -DF_CPU=$(F_CPU) + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +CSTANDARD = -std=gnu99 +CDEBUG = -g$(DEBUG) +CWARN = -Wall -Wstrict-prototypes +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) + +CFLAGS = $(CDEBUG) $(CDEFS) $(INCLUDE) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) +CXXFLAGS = $(CDEFS) $(INCLUDE) -O$(OPT) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = + + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PROGRAMMER = stk500 +AVRDUDE_PORT = $(PORT) +AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex +AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \ + -b $(UPLOAD_RATE) -q -V + +# Program settings +CC = avr-gcc +CXX = avr-g++ +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +NM = avr-nm +AVRDUDE = avrdude +REMOVE = rm -f +MV = mv -f + +# Define all object files. +OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o) + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) +ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) + + +# Default target. +all: build + +build: applet/$(TARGET).hex + +eep: applet/$(TARGET).eep +lss: applet/$(TARGET).lss +sym: applet/$(TARGET).sym + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ +--change-section-address .data-0x800000 \ +--change-section-address .bss-0x800000 \ +--change-section-address .noinit-0x800000 \ +--change-section-address .eeprom-0x810000 + + +coff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +extcoff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +.SUFFIXES: .elf .hex .eep .lss .sym .pde + +.elf.hex: + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +.elf.sym: + $(NM) -n $< > $@ + + +# Compile: create object files from C++ source files. +.cpp.o: $(HEADERS) + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +# Compile: create object files from C source files. +.c.o: $(HEADERS) + $(CC) -c $(ALL_CFLAGS) $< -o $@ + + +# Compile: create assembler files from C source files. +.c.s: + $(CC) -S $(ALL_CFLAGS) $< -o $@ + + +# Assemble: create object files from assembler source files. +.S.o: + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + + + +applet/$(TARGET).cpp: $(TARGET).pde + test -d applet || mkdir applet + echo '#include "WProgram.h"' > applet/$(TARGET).cpp + echo '#include "avr/interrupt.h"' >> applet/$(TARGET).cpp + sed -n 's|^\(void .*)\).*|\1;|p' $(TARGET).pde | grep -v 'setup()' | \ + grep -v 'loop()' >> applet/$(TARGET).cpp + cat $(TARGET).pde >> applet/$(TARGET).cpp + cat $(ARDUINO_SRC)/main.cxx >> applet/$(TARGET).cpp + +# Link: create ELF output file from object files. +applet/$(TARGET).elf: applet/$(TARGET).cpp $(OBJ) + $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) + +pd_close_serial: + echo 'close;' | /Applications/Pd-extended.app/Contents/Resources/bin/pdsend 34567 || true + +# Program the device. +upload: applet/$(TARGET).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) + + +pd_test: build pd_close_serial upload + +# Target: clean project. +clean: + $(REMOVE) -- applet/$(TARGET).hex applet/$(TARGET).eep \ + applet/$(TARGET).cof applet/$(TARGET).elf $(TARGET).map \ + applet/$(TARGET).sym applet/$(TARGET).lss applet/$(TARGET).cpp \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + rmdir -- applet + +depend: + if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ + then \ + sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \ + $(MAKEFILE).$$$$ && \ + $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \ + fi + echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ + >> $(MAKEFILE); \ + $(CC) -M -mmcu=$(MCU) $(CDEFS) $(INCLUDE) $(SRC) $(ASRC) >> $(MAKEFILE) + +.PHONY: all build eep lss sym coff extcoff clean depend pd_close_serial pd_test + +# for emacs +etags: + make etags_`uname -s` + etags *.pde \ + $(ARDUINO_SRC)/*.[ch] \ + $(ARDUINO_SRC)/*.cpp \ + $(ARDUINO_LIB_SRC)/*/*.[ch] \ + $(ARDUINO_LIB_SRC)/*/*.cpp \ + $(ARDUINO)/hardware/tools/avr/avr/include/avr/*.[ch] \ + $(ARDUINO)/hardware/tools/avr/avr/include/*.[ch] + +etags_Darwin: +# etags -a + +etags_Linux: +# etags -a /usr/include/*.h linux/input.h /usr/include/sys/*.h + +etags_MINGW: +# etags -a /usr/include/*.h /usr/include/sys/*.h + + + diff --git a/libraries/Firmata/examples/EchoString/EchoString.pde b/libraries/Firmata/examples/EchoString/EchoString.pde new file mode 100644 index 0000000..ed13ad8 --- /dev/null +++ b/libraries/Firmata/examples/EchoString/EchoString.pde @@ -0,0 +1,40 @@ +/* This sketch accepts strings and raw sysex messages and echos them back. + * + * This example code is in the public domain. + */ +#include + +byte analogPin; + +void stringCallback(char *myString) +{ + Firmata.sendString(myString); +} + + +void sysexCallback(byte command, byte argc, byte*argv) +{ + Serial.print(START_SYSEX, BYTE); + Serial.print(command, BYTE); + for(byte i=0; i +# +# - Write prototypes for all your functions (or define them before you +# call them). A prototype declares the types of parameters a +# function will take and what type of value it will return. This +# means that you can have a call to a function before the definition +# of the function. A function prototype looks like the first line of +# the function, with a semi-colon at the end. For example: +# int digitalRead(int pin); +# +# Instructions for using the makefile: +# +# 1. Copy this file into the folder with your sketch. +# +# 2. Below, modify the line containing "TARGET" to refer to the name of +# of your program's file without an extension (e.g. TARGET = foo). +# +# 3. Modify the line containg "ARDUINO" to point the directory that +# contains the Arduino core (for normal Arduino installations, this +# is the hardware/cores/arduino sub-directory). +# +# 4. Modify the line containing "PORT" to refer to the filename +# representing the USB or serial connection to your Arduino board +# (e.g. PORT = /dev/tty.USB0). If the exact name of this file +# changes, you can use * as a wildcard (e.g. PORT = /dev/tty.USB*). +# +# 5. At the command line, change to the directory containing your +# program's file and the makefile. +# +# 6. Type "make" and press enter to compile/verify your program. +# +# 7. Type "make upload", reset your Arduino board, and press enter to +# upload your program to the Arduino board. +# +# $Id: Makefile,v 1.7 2007/04/13 05:28:23 eighthave Exp $ + +PORT = /dev/tty.usbserial-* +TARGET := $(shell pwd | sed 's|.*/\(.*\)|\1|') +ARDUINO = /Applications/arduino +ARDUINO_SRC = $(ARDUINO)/hardware/cores/arduino +ARDUINO_LIB_SRC = $(ARDUINO)/hardware/libraries +INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/hardware/tools/avr/avr/include \ + -I$(ARDUINO_LIB_SRC)/EEPROM \ + -I$(ARDUINO_LIB_SRC)/Firmata \ + -I$(ARDUINO_LIB_SRC)/Servo \ + -I$(ARDUINO_LIB_SRC) +SRC = $(wildcard $(ARDUINO_SRC)/*.c) +CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \ + $(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \ + $(ARDUINO_LIB_SRC)/Firmata/Firmata.cpp \ + $(ARDUINO_LIB_SRC)/Servo/Servo.cpp \ + $(ARDUINO_SRC)/WMath.cpp +HEADERS = $(wildcard $(ARDUINO_SRC)/*.h) $(wildcard $(ARDUINO_LIB_SRC)/*/*.h) + +MCU = atmega168 +#MCU = atmega8 +F_CPU = 16000000 +FORMAT = ihex +UPLOAD_RATE = 19200 + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +DEBUG = stabs + +OPT = s + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU) +CXXDEFS = -DF_CPU=$(F_CPU) + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +CSTANDARD = -std=gnu99 +CDEBUG = -g$(DEBUG) +CWARN = -Wall -Wstrict-prototypes +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) + +CFLAGS = $(CDEBUG) $(CDEFS) $(INCLUDE) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) +CXXFLAGS = $(CDEFS) $(INCLUDE) -O$(OPT) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = + + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PROGRAMMER = stk500 +AVRDUDE_PORT = $(PORT) +AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex +AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \ + -b $(UPLOAD_RATE) -q -V + +# Program settings +CC = avr-gcc +CXX = avr-g++ +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +NM = avr-nm +AVRDUDE = avrdude +REMOVE = rm -f +MV = mv -f + +# Define all object files. +OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o) + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) +ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) + + +# Default target. +all: build + +build: applet/$(TARGET).hex + +eep: applet/$(TARGET).eep +lss: applet/$(TARGET).lss +sym: applet/$(TARGET).sym + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ +--change-section-address .data-0x800000 \ +--change-section-address .bss-0x800000 \ +--change-section-address .noinit-0x800000 \ +--change-section-address .eeprom-0x810000 + + +coff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +extcoff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +.SUFFIXES: .elf .hex .eep .lss .sym .pde + +.elf.hex: + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +.elf.sym: + $(NM) -n $< > $@ + + +# Compile: create object files from C++ source files. +.cpp.o: $(HEADERS) + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +# Compile: create object files from C source files. +.c.o: $(HEADERS) + $(CC) -c $(ALL_CFLAGS) $< -o $@ + + +# Compile: create assembler files from C source files. +.c.s: + $(CC) -S $(ALL_CFLAGS) $< -o $@ + + +# Assemble: create object files from assembler source files. +.S.o: + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + + + +applet/$(TARGET).cpp: $(TARGET).pde + test -d applet || mkdir applet + echo '#include "WProgram.h"' > applet/$(TARGET).cpp + echo '#include "avr/interrupt.h"' >> applet/$(TARGET).cpp + sed -n 's|^\(void .*)\).*|\1;|p' $(TARGET).pde | grep -v 'setup()' | \ + grep -v 'loop()' >> applet/$(TARGET).cpp + cat $(TARGET).pde >> applet/$(TARGET).cpp + cat $(ARDUINO_SRC)/main.cxx >> applet/$(TARGET).cpp + +# Link: create ELF output file from object files. +applet/$(TARGET).elf: applet/$(TARGET).cpp $(OBJ) + $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) + +pd_close_serial: + echo 'close;' | /Applications/Pd-extended.app/Contents/Resources/bin/pdsend 34567 || true + +# Program the device. +upload: applet/$(TARGET).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) + + +pd_test: build pd_close_serial upload + +# Target: clean project. +clean: + $(REMOVE) -- applet/$(TARGET).hex applet/$(TARGET).eep \ + applet/$(TARGET).cof applet/$(TARGET).elf $(TARGET).map \ + applet/$(TARGET).sym applet/$(TARGET).lss applet/$(TARGET).cpp \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + rmdir -- applet + +depend: + if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ + then \ + sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \ + $(MAKEFILE).$$$$ && \ + $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \ + fi + echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ + >> $(MAKEFILE); \ + $(CC) -M -mmcu=$(MCU) $(CDEFS) $(INCLUDE) $(SRC) $(ASRC) >> $(MAKEFILE) + +.PHONY: all build eep lss sym coff extcoff clean depend pd_close_serial pd_test + +# for emacs +etags: + make etags_`uname -s` + etags *.pde \ + $(ARDUINO_SRC)/*.[ch] \ + $(ARDUINO_SRC)/*.cpp \ + $(ARDUINO_LIB_SRC)/*/*.[ch] \ + $(ARDUINO_LIB_SRC)/*/*.cpp \ + $(ARDUINO)/hardware/tools/avr/avr/include/avr/*.[ch] \ + $(ARDUINO)/hardware/tools/avr/avr/include/*.[ch] + +etags_Darwin: +# etags -a + +etags_Linux: +# etags -a /usr/include/*.h linux/input.h /usr/include/sys/*.h + +etags_MINGW: +# etags -a /usr/include/*.h /usr/include/sys/*.h + + + diff --git a/libraries/Firmata/examples/ServoFirmata/Makefile b/libraries/Firmata/examples/ServoFirmata/Makefile new file mode 100644 index 0000000..e968c0a --- /dev/null +++ b/libraries/Firmata/examples/ServoFirmata/Makefile @@ -0,0 +1,263 @@ +# Arduino makefile +# +# This makefile allows you to build sketches from the command line +# without the Arduino environment (or Java). +# +# The Arduino environment does preliminary processing on a sketch before +# compiling it. If you're using this makefile instead, you'll need to do +# a few things differently: +# +# - Give your program's file a .cpp extension (e.g. foo.cpp). +# +# - Put this line at top of your code: #include +# +# - Write prototypes for all your functions (or define them before you +# call them). A prototype declares the types of parameters a +# function will take and what type of value it will return. This +# means that you can have a call to a function before the definition +# of the function. A function prototype looks like the first line of +# the function, with a semi-colon at the end. For example: +# int digitalRead(int pin); +# +# Instructions for using the makefile: +# +# 1. Copy this file into the folder with your sketch. +# +# 2. Below, modify the line containing "TARGET" to refer to the name of +# of your program's file without an extension (e.g. TARGET = foo). +# +# 3. Modify the line containg "ARDUINO" to point the directory that +# contains the Arduino core (for normal Arduino installations, this +# is the hardware/cores/arduino sub-directory). +# +# 4. Modify the line containing "PORT" to refer to the filename +# representing the USB or serial connection to your Arduino board +# (e.g. PORT = /dev/tty.USB0). If the exact name of this file +# changes, you can use * as a wildcard (e.g. PORT = /dev/tty.USB*). +# +# 5. At the command line, change to the directory containing your +# program's file and the makefile. +# +# 6. Type "make" and press enter to compile/verify your program. +# +# 7. Type "make upload", reset your Arduino board, and press enter to +# upload your program to the Arduino board. +# +# $Id: Makefile,v 1.7 2007/04/13 05:28:23 eighthave Exp $ + +PORT = /dev/tty.usbserial-* +TARGET := $(shell pwd | sed 's|.*/\(.*\)|\1|') +ARDUINO = /Applications/arduino +ARDUINO_SRC = $(ARDUINO)/hardware/cores/arduino +ARDUINO_LIB_SRC = $(ARDUINO)/hardware/libraries +INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/hardware/tools/avr/avr/include \ + -I$(ARDUINO_LIB_SRC)/EEPROM \ + -I$(ARDUINO_LIB_SRC)/Firmata \ + -I$(ARDUINO_LIB_SRC)/Servo \ + -I$(ARDUINO_LIB_SRC) +SRC = $(wildcard $(ARDUINO_SRC)/*.c) +CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \ + $(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \ + $(ARDUINO_LIB_SRC)/Firmata/Firmata.cpp \ + $(ARDUINO_LIB_SRC)/Servo/Servo.cpp \ + $(ARDUINO_SRC)/WMath.cpp +HEADERS = $(wildcard $(ARDUINO_SRC)/*.h) $(wildcard $(ARDUINO_LIB_SRC)/*/*.h) + +MCU = atmega168 +#MCU = atmega8 +F_CPU = 16000000 +FORMAT = ihex +UPLOAD_RATE = 19200 + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +DEBUG = stabs + +OPT = s + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU) +CXXDEFS = -DF_CPU=$(F_CPU) + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +CSTANDARD = -std=gnu99 +CDEBUG = -g$(DEBUG) +CWARN = -Wall -Wstrict-prototypes +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) + +CFLAGS = $(CDEBUG) $(CDEFS) $(INCLUDE) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) +CXXFLAGS = $(CDEFS) $(INCLUDE) -O$(OPT) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = + + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PROGRAMMER = stk500 +AVRDUDE_PORT = $(PORT) +AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex +AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \ + -b $(UPLOAD_RATE) -q -V + +# Program settings +CC = avr-gcc +CXX = avr-g++ +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +NM = avr-nm +AVRDUDE = avrdude +REMOVE = rm -f +MV = mv -f + +# Define all object files. +OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o) + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) +ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) + + +# Default target. +all: build + +build: applet/$(TARGET).hex + +eep: applet/$(TARGET).eep +lss: applet/$(TARGET).lss +sym: applet/$(TARGET).sym + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ +--change-section-address .data-0x800000 \ +--change-section-address .bss-0x800000 \ +--change-section-address .noinit-0x800000 \ +--change-section-address .eeprom-0x810000 + + +coff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +extcoff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +.SUFFIXES: .elf .hex .eep .lss .sym .pde + +.elf.hex: + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +.elf.sym: + $(NM) -n $< > $@ + + +# Compile: create object files from C++ source files. +.cpp.o: $(HEADERS) + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +# Compile: create object files from C source files. +.c.o: $(HEADERS) + $(CC) -c $(ALL_CFLAGS) $< -o $@ + + +# Compile: create assembler files from C source files. +.c.s: + $(CC) -S $(ALL_CFLAGS) $< -o $@ + + +# Assemble: create object files from assembler source files. +.S.o: + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + + + +applet/$(TARGET).cpp: $(TARGET).pde + test -d applet || mkdir applet + echo '#include "WProgram.h"' > applet/$(TARGET).cpp + echo '#include "avr/interrupt.h"' >> applet/$(TARGET).cpp + sed -n 's|^\(void .*)\).*|\1;|p' $(TARGET).pde | grep -v 'setup()' | \ + grep -v 'loop()' >> applet/$(TARGET).cpp + cat $(TARGET).pde >> applet/$(TARGET).cpp + cat $(ARDUINO_SRC)/main.cxx >> applet/$(TARGET).cpp + +# Link: create ELF output file from object files. +applet/$(TARGET).elf: applet/$(TARGET).cpp $(OBJ) + $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) + +pd_close_serial: + echo 'close;' | /Applications/Pd-extended.app/Contents/Resources/bin/pdsend 34567 || true + +# Program the device. +upload: applet/$(TARGET).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) + + +pd_test: build pd_close_serial upload + +# Target: clean project. +clean: + $(REMOVE) -- applet/$(TARGET).hex applet/$(TARGET).eep \ + applet/$(TARGET).cof applet/$(TARGET).elf $(TARGET).map \ + applet/$(TARGET).sym applet/$(TARGET).lss applet/$(TARGET).cpp \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + rmdir -- applet + +depend: + if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ + then \ + sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \ + $(MAKEFILE).$$$$ && \ + $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \ + fi + echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ + >> $(MAKEFILE); \ + $(CC) -M -mmcu=$(MCU) $(CDEFS) $(INCLUDE) $(SRC) $(ASRC) >> $(MAKEFILE) + +.PHONY: all build eep lss sym coff extcoff clean depend pd_close_serial pd_test + +# for emacs +etags: + make etags_`uname -s` + etags *.pde \ + $(ARDUINO_SRC)/*.[ch] \ + $(ARDUINO_SRC)/*.cpp \ + $(ARDUINO_LIB_SRC)/*/*.[ch] \ + $(ARDUINO_LIB_SRC)/*/*.cpp \ + $(ARDUINO)/hardware/tools/avr/avr/include/avr/*.[ch] \ + $(ARDUINO)/hardware/tools/avr/avr/include/*.[ch] + +etags_Darwin: +# etags -a + +etags_Linux: +# etags -a /usr/include/*.h linux/input.h /usr/include/sys/*.h + +etags_MINGW: +# etags -a /usr/include/*.h /usr/include/sys/*.h + + + diff --git a/libraries/Firmata/examples/ServoFirmata/ServoFirmata.pde b/libraries/Firmata/examples/ServoFirmata/ServoFirmata.pde new file mode 100644 index 0000000..a3c609c --- /dev/null +++ b/libraries/Firmata/examples/ServoFirmata/ServoFirmata.pde @@ -0,0 +1,39 @@ +/* This firmware supports as many servos as possible using the Servo" library + * included in Arduino 0012 + * + * TODO add message to configure minPulse/maxPulse/degrees + * + * This example code is in the public domain. + */ + +#include +#include + +Servo servo9; +Servo servo10; + +void analogWriteCallback(byte pin, int value) +{ + if(pin == 9) + servo9.write(value); + if(pin == 10) + servo10.write(value); +} + +void setup() +{ + Firmata.setFirmwareVersion(0, 2); + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + + servo9.attach(9); + servo10.attach(10); + + Firmata.begin(); +} + +void loop() +{ + while(Firmata.available()) + Firmata.processInput(); +} + diff --git a/libraries/Firmata/examples/SimpleAnalogFirmata/Makefile b/libraries/Firmata/examples/SimpleAnalogFirmata/Makefile new file mode 100644 index 0000000..e968c0a --- /dev/null +++ b/libraries/Firmata/examples/SimpleAnalogFirmata/Makefile @@ -0,0 +1,263 @@ +# Arduino makefile +# +# This makefile allows you to build sketches from the command line +# without the Arduino environment (or Java). +# +# The Arduino environment does preliminary processing on a sketch before +# compiling it. If you're using this makefile instead, you'll need to do +# a few things differently: +# +# - Give your program's file a .cpp extension (e.g. foo.cpp). +# +# - Put this line at top of your code: #include +# +# - Write prototypes for all your functions (or define them before you +# call them). A prototype declares the types of parameters a +# function will take and what type of value it will return. This +# means that you can have a call to a function before the definition +# of the function. A function prototype looks like the first line of +# the function, with a semi-colon at the end. For example: +# int digitalRead(int pin); +# +# Instructions for using the makefile: +# +# 1. Copy this file into the folder with your sketch. +# +# 2. Below, modify the line containing "TARGET" to refer to the name of +# of your program's file without an extension (e.g. TARGET = foo). +# +# 3. Modify the line containg "ARDUINO" to point the directory that +# contains the Arduino core (for normal Arduino installations, this +# is the hardware/cores/arduino sub-directory). +# +# 4. Modify the line containing "PORT" to refer to the filename +# representing the USB or serial connection to your Arduino board +# (e.g. PORT = /dev/tty.USB0). If the exact name of this file +# changes, you can use * as a wildcard (e.g. PORT = /dev/tty.USB*). +# +# 5. At the command line, change to the directory containing your +# program's file and the makefile. +# +# 6. Type "make" and press enter to compile/verify your program. +# +# 7. Type "make upload", reset your Arduino board, and press enter to +# upload your program to the Arduino board. +# +# $Id: Makefile,v 1.7 2007/04/13 05:28:23 eighthave Exp $ + +PORT = /dev/tty.usbserial-* +TARGET := $(shell pwd | sed 's|.*/\(.*\)|\1|') +ARDUINO = /Applications/arduino +ARDUINO_SRC = $(ARDUINO)/hardware/cores/arduino +ARDUINO_LIB_SRC = $(ARDUINO)/hardware/libraries +INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/hardware/tools/avr/avr/include \ + -I$(ARDUINO_LIB_SRC)/EEPROM \ + -I$(ARDUINO_LIB_SRC)/Firmata \ + -I$(ARDUINO_LIB_SRC)/Servo \ + -I$(ARDUINO_LIB_SRC) +SRC = $(wildcard $(ARDUINO_SRC)/*.c) +CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \ + $(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \ + $(ARDUINO_LIB_SRC)/Firmata/Firmata.cpp \ + $(ARDUINO_LIB_SRC)/Servo/Servo.cpp \ + $(ARDUINO_SRC)/WMath.cpp +HEADERS = $(wildcard $(ARDUINO_SRC)/*.h) $(wildcard $(ARDUINO_LIB_SRC)/*/*.h) + +MCU = atmega168 +#MCU = atmega8 +F_CPU = 16000000 +FORMAT = ihex +UPLOAD_RATE = 19200 + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +DEBUG = stabs + +OPT = s + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU) +CXXDEFS = -DF_CPU=$(F_CPU) + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +CSTANDARD = -std=gnu99 +CDEBUG = -g$(DEBUG) +CWARN = -Wall -Wstrict-prototypes +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) + +CFLAGS = $(CDEBUG) $(CDEFS) $(INCLUDE) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) +CXXFLAGS = $(CDEFS) $(INCLUDE) -O$(OPT) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = + + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PROGRAMMER = stk500 +AVRDUDE_PORT = $(PORT) +AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex +AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \ + -b $(UPLOAD_RATE) -q -V + +# Program settings +CC = avr-gcc +CXX = avr-g++ +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +NM = avr-nm +AVRDUDE = avrdude +REMOVE = rm -f +MV = mv -f + +# Define all object files. +OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o) + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) +ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) + + +# Default target. +all: build + +build: applet/$(TARGET).hex + +eep: applet/$(TARGET).eep +lss: applet/$(TARGET).lss +sym: applet/$(TARGET).sym + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ +--change-section-address .data-0x800000 \ +--change-section-address .bss-0x800000 \ +--change-section-address .noinit-0x800000 \ +--change-section-address .eeprom-0x810000 + + +coff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +extcoff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +.SUFFIXES: .elf .hex .eep .lss .sym .pde + +.elf.hex: + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +.elf.sym: + $(NM) -n $< > $@ + + +# Compile: create object files from C++ source files. +.cpp.o: $(HEADERS) + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +# Compile: create object files from C source files. +.c.o: $(HEADERS) + $(CC) -c $(ALL_CFLAGS) $< -o $@ + + +# Compile: create assembler files from C source files. +.c.s: + $(CC) -S $(ALL_CFLAGS) $< -o $@ + + +# Assemble: create object files from assembler source files. +.S.o: + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + + + +applet/$(TARGET).cpp: $(TARGET).pde + test -d applet || mkdir applet + echo '#include "WProgram.h"' > applet/$(TARGET).cpp + echo '#include "avr/interrupt.h"' >> applet/$(TARGET).cpp + sed -n 's|^\(void .*)\).*|\1;|p' $(TARGET).pde | grep -v 'setup()' | \ + grep -v 'loop()' >> applet/$(TARGET).cpp + cat $(TARGET).pde >> applet/$(TARGET).cpp + cat $(ARDUINO_SRC)/main.cxx >> applet/$(TARGET).cpp + +# Link: create ELF output file from object files. +applet/$(TARGET).elf: applet/$(TARGET).cpp $(OBJ) + $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) + +pd_close_serial: + echo 'close;' | /Applications/Pd-extended.app/Contents/Resources/bin/pdsend 34567 || true + +# Program the device. +upload: applet/$(TARGET).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) + + +pd_test: build pd_close_serial upload + +# Target: clean project. +clean: + $(REMOVE) -- applet/$(TARGET).hex applet/$(TARGET).eep \ + applet/$(TARGET).cof applet/$(TARGET).elf $(TARGET).map \ + applet/$(TARGET).sym applet/$(TARGET).lss applet/$(TARGET).cpp \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + rmdir -- applet + +depend: + if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ + then \ + sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \ + $(MAKEFILE).$$$$ && \ + $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \ + fi + echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ + >> $(MAKEFILE); \ + $(CC) -M -mmcu=$(MCU) $(CDEFS) $(INCLUDE) $(SRC) $(ASRC) >> $(MAKEFILE) + +.PHONY: all build eep lss sym coff extcoff clean depend pd_close_serial pd_test + +# for emacs +etags: + make etags_`uname -s` + etags *.pde \ + $(ARDUINO_SRC)/*.[ch] \ + $(ARDUINO_SRC)/*.cpp \ + $(ARDUINO_LIB_SRC)/*/*.[ch] \ + $(ARDUINO_LIB_SRC)/*/*.cpp \ + $(ARDUINO)/hardware/tools/avr/avr/include/avr/*.[ch] \ + $(ARDUINO)/hardware/tools/avr/avr/include/*.[ch] + +etags_Darwin: +# etags -a + +etags_Linux: +# etags -a /usr/include/*.h linux/input.h /usr/include/sys/*.h + +etags_MINGW: +# etags -a /usr/include/*.h /usr/include/sys/*.h + + + diff --git a/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde b/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde new file mode 100644 index 0000000..2950c01 --- /dev/null +++ b/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde @@ -0,0 +1,32 @@ +/* Supports as many analog inputs and analog PWM outputs as possible. + * + * This example code is in the public domain. + */ +#include + +byte analogPin; + +void analogWriteCallback(byte pin, int value) +{ + pinMode(pin,OUTPUT); + analogWrite(pin, value); +} + +void setup() +{ + Firmata.setFirmwareVersion(0, 1); + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.begin(); +} + +void loop() +{ + while(Firmata.available()) { + Firmata.processInput(); + } + for(analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } +} + + diff --git a/libraries/Firmata/examples/SimpleDigitalFirmata/Makefile b/libraries/Firmata/examples/SimpleDigitalFirmata/Makefile new file mode 100644 index 0000000..e968c0a --- /dev/null +++ b/libraries/Firmata/examples/SimpleDigitalFirmata/Makefile @@ -0,0 +1,263 @@ +# Arduino makefile +# +# This makefile allows you to build sketches from the command line +# without the Arduino environment (or Java). +# +# The Arduino environment does preliminary processing on a sketch before +# compiling it. If you're using this makefile instead, you'll need to do +# a few things differently: +# +# - Give your program's file a .cpp extension (e.g. foo.cpp). +# +# - Put this line at top of your code: #include +# +# - Write prototypes for all your functions (or define them before you +# call them). A prototype declares the types of parameters a +# function will take and what type of value it will return. This +# means that you can have a call to a function before the definition +# of the function. A function prototype looks like the first line of +# the function, with a semi-colon at the end. For example: +# int digitalRead(int pin); +# +# Instructions for using the makefile: +# +# 1. Copy this file into the folder with your sketch. +# +# 2. Below, modify the line containing "TARGET" to refer to the name of +# of your program's file without an extension (e.g. TARGET = foo). +# +# 3. Modify the line containg "ARDUINO" to point the directory that +# contains the Arduino core (for normal Arduino installations, this +# is the hardware/cores/arduino sub-directory). +# +# 4. Modify the line containing "PORT" to refer to the filename +# representing the USB or serial connection to your Arduino board +# (e.g. PORT = /dev/tty.USB0). If the exact name of this file +# changes, you can use * as a wildcard (e.g. PORT = /dev/tty.USB*). +# +# 5. At the command line, change to the directory containing your +# program's file and the makefile. +# +# 6. Type "make" and press enter to compile/verify your program. +# +# 7. Type "make upload", reset your Arduino board, and press enter to +# upload your program to the Arduino board. +# +# $Id: Makefile,v 1.7 2007/04/13 05:28:23 eighthave Exp $ + +PORT = /dev/tty.usbserial-* +TARGET := $(shell pwd | sed 's|.*/\(.*\)|\1|') +ARDUINO = /Applications/arduino +ARDUINO_SRC = $(ARDUINO)/hardware/cores/arduino +ARDUINO_LIB_SRC = $(ARDUINO)/hardware/libraries +INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/hardware/tools/avr/avr/include \ + -I$(ARDUINO_LIB_SRC)/EEPROM \ + -I$(ARDUINO_LIB_SRC)/Firmata \ + -I$(ARDUINO_LIB_SRC)/Servo \ + -I$(ARDUINO_LIB_SRC) +SRC = $(wildcard $(ARDUINO_SRC)/*.c) +CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \ + $(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \ + $(ARDUINO_LIB_SRC)/Firmata/Firmata.cpp \ + $(ARDUINO_LIB_SRC)/Servo/Servo.cpp \ + $(ARDUINO_SRC)/WMath.cpp +HEADERS = $(wildcard $(ARDUINO_SRC)/*.h) $(wildcard $(ARDUINO_LIB_SRC)/*/*.h) + +MCU = atmega168 +#MCU = atmega8 +F_CPU = 16000000 +FORMAT = ihex +UPLOAD_RATE = 19200 + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +DEBUG = stabs + +OPT = s + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU) +CXXDEFS = -DF_CPU=$(F_CPU) + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +CSTANDARD = -std=gnu99 +CDEBUG = -g$(DEBUG) +CWARN = -Wall -Wstrict-prototypes +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) + +CFLAGS = $(CDEBUG) $(CDEFS) $(INCLUDE) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) +CXXFLAGS = $(CDEFS) $(INCLUDE) -O$(OPT) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = + + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PROGRAMMER = stk500 +AVRDUDE_PORT = $(PORT) +AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex +AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \ + -b $(UPLOAD_RATE) -q -V + +# Program settings +CC = avr-gcc +CXX = avr-g++ +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +NM = avr-nm +AVRDUDE = avrdude +REMOVE = rm -f +MV = mv -f + +# Define all object files. +OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o) + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) +ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) + + +# Default target. +all: build + +build: applet/$(TARGET).hex + +eep: applet/$(TARGET).eep +lss: applet/$(TARGET).lss +sym: applet/$(TARGET).sym + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ +--change-section-address .data-0x800000 \ +--change-section-address .bss-0x800000 \ +--change-section-address .noinit-0x800000 \ +--change-section-address .eeprom-0x810000 + + +coff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +extcoff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +.SUFFIXES: .elf .hex .eep .lss .sym .pde + +.elf.hex: + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +.elf.sym: + $(NM) -n $< > $@ + + +# Compile: create object files from C++ source files. +.cpp.o: $(HEADERS) + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +# Compile: create object files from C source files. +.c.o: $(HEADERS) + $(CC) -c $(ALL_CFLAGS) $< -o $@ + + +# Compile: create assembler files from C source files. +.c.s: + $(CC) -S $(ALL_CFLAGS) $< -o $@ + + +# Assemble: create object files from assembler source files. +.S.o: + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + + + +applet/$(TARGET).cpp: $(TARGET).pde + test -d applet || mkdir applet + echo '#include "WProgram.h"' > applet/$(TARGET).cpp + echo '#include "avr/interrupt.h"' >> applet/$(TARGET).cpp + sed -n 's|^\(void .*)\).*|\1;|p' $(TARGET).pde | grep -v 'setup()' | \ + grep -v 'loop()' >> applet/$(TARGET).cpp + cat $(TARGET).pde >> applet/$(TARGET).cpp + cat $(ARDUINO_SRC)/main.cxx >> applet/$(TARGET).cpp + +# Link: create ELF output file from object files. +applet/$(TARGET).elf: applet/$(TARGET).cpp $(OBJ) + $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) + +pd_close_serial: + echo 'close;' | /Applications/Pd-extended.app/Contents/Resources/bin/pdsend 34567 || true + +# Program the device. +upload: applet/$(TARGET).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) + + +pd_test: build pd_close_serial upload + +# Target: clean project. +clean: + $(REMOVE) -- applet/$(TARGET).hex applet/$(TARGET).eep \ + applet/$(TARGET).cof applet/$(TARGET).elf $(TARGET).map \ + applet/$(TARGET).sym applet/$(TARGET).lss applet/$(TARGET).cpp \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + rmdir -- applet + +depend: + if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ + then \ + sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \ + $(MAKEFILE).$$$$ && \ + $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \ + fi + echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ + >> $(MAKEFILE); \ + $(CC) -M -mmcu=$(MCU) $(CDEFS) $(INCLUDE) $(SRC) $(ASRC) >> $(MAKEFILE) + +.PHONY: all build eep lss sym coff extcoff clean depend pd_close_serial pd_test + +# for emacs +etags: + make etags_`uname -s` + etags *.pde \ + $(ARDUINO_SRC)/*.[ch] \ + $(ARDUINO_SRC)/*.cpp \ + $(ARDUINO_LIB_SRC)/*/*.[ch] \ + $(ARDUINO_LIB_SRC)/*/*.cpp \ + $(ARDUINO)/hardware/tools/avr/avr/include/avr/*.[ch] \ + $(ARDUINO)/hardware/tools/avr/avr/include/*.[ch] + +etags_Darwin: +# etags -a + +etags_Linux: +# etags -a /usr/include/*.h linux/input.h /usr/include/sys/*.h + +etags_MINGW: +# etags -a /usr/include/*.h /usr/include/sys/*.h + + + diff --git a/libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.pde b/libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.pde new file mode 100644 index 0000000..1104a92 --- /dev/null +++ b/libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.pde @@ -0,0 +1,58 @@ +/* Supports as many digital inputs and outputs as possible. + * + * This example code is in the public domain. + */ +#include + +byte previousPIN[2]; // PIN means PORT for input +byte previousPORT[2]; + +void outputPort(byte portNumber, byte portValue) +{ +// only send the data when it changes, otherwise you get too many messages! + if(previousPIN[portNumber] != portValue) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPIN[portNumber] = portValue; + Firmata.sendDigitalPort(portNumber, portValue); + } +} + +void setPinModeCallback(byte pin, int mode) { + if(pin > 1) { // don't touch RxTx pins (0,1) + pinMode(pin, mode); + } +} + +void digitalWriteCallback(byte port, int value) +{ + byte i; + byte currentPinValue, previousPinValue; + + if(value != previousPORT[port]) { + for(i=0; i<8; i++) { + currentPinValue = (byte) value & (1 << i); + previousPinValue = previousPORT[port] & (1 << i); + if(currentPinValue != previousPinValue) { + digitalWrite(i + (port*8), currentPinValue); + } + } + previousPORT[port] = value; + } +} + +void setup() +{ + Firmata.setFirmwareVersion(0, 1); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.begin(); +} + +void loop() +{ + outputPort(0, PIND &~ B00000011); // pins 0-7, ignoring Rx/Tx pins (0/1) + outputPort(1, PINB); // pins 8-13 + while(Firmata.available()) { + Firmata.processInput(); + } +} diff --git a/libraries/Firmata/examples/StandardFirmata/LICENSE.txt b/libraries/Firmata/examples/StandardFirmata/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmata/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/StandardFirmata/Makefile b/libraries/Firmata/examples/StandardFirmata/Makefile new file mode 100644 index 0000000..55ca8c2 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmata/Makefile @@ -0,0 +1,261 @@ +# Arduino makefile +# +# This makefile allows you to build sketches from the command line +# without the Arduino environment (or Java). +# +# The Arduino environment does preliminary processing on a sketch before +# compiling it. If you're using this makefile instead, you'll need to do +# a few things differently: +# +# - Give your program's file a .cpp extension (e.g. foo.cpp). +# +# - Put this line at top of your code: #include +# +# - Write prototypes for all your functions (or define them before you +# call them). A prototype declares the types of parameters a +# function will take and what type of value it will return. This +# means that you can have a call to a function before the definition +# of the function. A function prototype looks like the first line of +# the function, with a semi-colon at the end. For example: +# int digitalRead(int pin); +# +# Instructions for using the makefile: +# +# 1. Copy this file into the folder with your sketch. +# +# 2. Below, modify the line containing "TARGET" to refer to the name of +# of your program's file without an extension (e.g. TARGET = foo). +# +# 3. Modify the line containg "ARDUINO" to point the directory that +# contains the Arduino core (for normal Arduino installations, this +# is the hardware/cores/arduino sub-directory). +# +# 4. Modify the line containing "PORT" to refer to the filename +# representing the USB or serial connection to your Arduino board +# (e.g. PORT = /dev/tty.USB0). If the exact name of this file +# changes, you can use * as a wildcard (e.g. PORT = /dev/tty.USB*). +# +# 5. At the command line, change to the directory containing your +# program's file and the makefile. +# +# 6. Type "make" and press enter to compile/verify your program. +# +# 7. Type "make upload", reset your Arduino board, and press enter to +# upload your program to the Arduino board. +# +# $Id: Makefile,v 1.7 2007/04/13 05:28:23 eighthave Exp $ + +PORT = /dev/tty.usbserial-* +TARGET := $(shell pwd | sed 's|.*/\(.*\)|\1|') +ARDUINO = /Applications/arduino +ARDUINO_SRC = $(ARDUINO)/hardware/cores/arduino +ARDUINO_LIB_SRC = $(ARDUINO)/hardware/libraries +INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/hardware/tools/avr/avr/include \ + -I$(ARDUINO_LIB_SRC)/EEPROM \ + -I$(ARDUINO_LIB_SRC)/Firmata \ + -I$(ARDUINO_LIB_SRC) +SRC = $(wildcard $(ARDUINO_SRC)/*.c) +CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \ + $(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \ + $(ARDUINO_LIB_SRC)/Firmata/Firmata.cpp \ + $(ARDUINO_SRC)/WMath.cpp +HEADERS = $(wildcard $(ARDUINO_SRC)/*.h) $(wildcard $(ARDUINO_LIB_SRC)/*/*.h) + +MCU = atmega168 +#MCU = atmega8 +F_CPU = 16000000 +FORMAT = ihex +UPLOAD_RATE = 19200 + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +DEBUG = stabs + +OPT = s + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU) +CXXDEFS = -DF_CPU=$(F_CPU) + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +CSTANDARD = -std=gnu99 +CDEBUG = -g$(DEBUG) +CWARN = -Wall -Wstrict-prototypes +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) + +CFLAGS = $(CDEBUG) $(CDEFS) $(INCLUDE) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) +CXXFLAGS = $(CDEFS) $(INCLUDE) -O$(OPT) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = + + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PROGRAMMER = stk500 +AVRDUDE_PORT = $(PORT) +AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex +AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \ + -b $(UPLOAD_RATE) -q -V + +# Program settings +CC = avr-gcc +CXX = avr-g++ +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +NM = avr-nm +AVRDUDE = avrdude +REMOVE = rm -f +MV = mv -f + +# Define all object files. +OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o) + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) +ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) + + +# Default target. +all: build + +build: applet/$(TARGET).hex + +eep: applet/$(TARGET).eep +lss: applet/$(TARGET).lss +sym: applet/$(TARGET).sym + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ +--change-section-address .data-0x800000 \ +--change-section-address .bss-0x800000 \ +--change-section-address .noinit-0x800000 \ +--change-section-address .eeprom-0x810000 + + +coff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +extcoff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +.SUFFIXES: .elf .hex .eep .lss .sym .pde + +.elf.hex: + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +.elf.sym: + $(NM) -n $< > $@ + + +# Compile: create object files from C++ source files. +.cpp.o: $(HEADERS) + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +# Compile: create object files from C source files. +.c.o: $(HEADERS) + $(CC) -c $(ALL_CFLAGS) $< -o $@ + + +# Compile: create assembler files from C source files. +.c.s: + $(CC) -S $(ALL_CFLAGS) $< -o $@ + + +# Assemble: create object files from assembler source files. +.S.o: + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + + + +applet/$(TARGET).cpp: $(TARGET).pde + test -d applet || mkdir applet + echo '#include "WProgram.h"' > applet/$(TARGET).cpp + echo '#include "avr/interrupt.h"' >> applet/$(TARGET).cpp + sed -n 's|^\(void .*)\).*|\1;|p' $(TARGET).pde | grep -v 'setup()' | \ + grep -v 'loop()' >> applet/$(TARGET).cpp + cat $(TARGET).pde >> applet/$(TARGET).cpp + cat $(ARDUINO_SRC)/main.cxx >> applet/$(TARGET).cpp + +# Link: create ELF output file from object files. +applet/$(TARGET).elf: applet/$(TARGET).cpp $(OBJ) + $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) + +pd_close_serial: + echo 'close;' | /Applications/Pd-extended.app/Contents/Resources/bin/pdsend 34567 || true + +# Program the device. +upload: applet/$(TARGET).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) + + +pd_test: build pd_close_serial upload + +# Target: clean project. +clean: + $(REMOVE) -- applet/$(TARGET).hex applet/$(TARGET).eep \ + applet/$(TARGET).cof applet/$(TARGET).elf $(TARGET).map \ + applet/$(TARGET).sym applet/$(TARGET).lss applet/$(TARGET).cpp \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + rmdir -- applet + +depend: + if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ + then \ + sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \ + $(MAKEFILE).$$$$ && \ + $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \ + fi + echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ + >> $(MAKEFILE); \ + $(CC) -M -mmcu=$(MCU) $(CDEFS) $(INCLUDE) $(SRC) $(ASRC) >> $(MAKEFILE) + +.PHONY: all build eep lss sym coff extcoff clean depend pd_close_serial pd_test + +# for emacs +etags: + make etags_`uname -s` + etags *.pde \ + $(ARDUINO_SRC)/*.[ch] \ + $(ARDUINO_SRC)/*.cpp \ + $(ARDUINO_LIB_SRC)/*/*.[ch] \ + $(ARDUINO_LIB_SRC)/*/*.cpp \ + $(ARDUINO)/hardware/tools/avr/avr/include/avr/*.[ch] \ + $(ARDUINO)/hardware/tools/avr/avr/include/*.[ch] + +etags_Darwin: +# etags -a + +etags_Linux: +# etags -a /usr/include/*.h linux/input.h /usr/include/sys/*.h + +etags_MINGW: +# etags -a /usr/include/*.h /usr/include/sys/*.h + + + diff --git a/libraries/Firmata/examples/StandardFirmata/StandardFirmata.pde b/libraries/Firmata/examples/StandardFirmata/StandardFirmata.pde new file mode 100644 index 0000000..4cc8539 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmata/StandardFirmata.pde @@ -0,0 +1,226 @@ +/* + Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights 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. + + See file LICENSE.txt for further informations on licensing terms. + */ + +/* + * TODO: add Servo support using setPinMode(pin, SERVO); + * TODO: use Program Control to load stored profiles from EEPROM + */ + +#include +#include + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting +int analogPin = 0; // counter for reading analog pins + +/* digital pins */ +byte reportPINs[TOTAL_PORTS]; // PIN == input port +byte previousPINs[TOTAL_PORTS]; // PIN == input port +byte pinStatus[TOTAL_DIGITAL_PINS]; // store pin status, default OUTPUT +byte portStatus[TOTAL_PORTS]; + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long nextExecuteMillis; // for comparison with currentMillis + + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void outputPort(byte portNumber, byte portValue) +{ + portValue = portValue &~ portStatus[portNumber]; + if(previousPINs[portNumber] != portValue) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + Firmata.sendDigitalPort(portNumber, portValue); + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void checkDigitalInputs(void) +{ + byte i, tmp; + for(i=0; i < TOTAL_PORTS; i++) { + if(reportPINs[i]) { + switch(i) { + case 0: outputPort(0, PIND &~ B00000011); break; // ignore Rx/Tx 0/1 + case 1: outputPort(1, PINB); break; + case ANALOG_PORT: outputPort(ANALOG_PORT, PINC); break; + } + } + } +} + +// ----------------------------------------------------------------------------- +/* sets the pin mode to the correct state and sets the relevant bits in the + * two bit-arrays that track Digital I/O and PWM status + */ +void setPinModeCallback(byte pin, int mode) { + byte port = 0; + byte offset = 0; + + if (pin < 8) { + port = 0; + offset = 0; + } else if (pin < 14) { + port = 1; + offset = 8; + } else if (pin < 22) { + port = 2; + offset = 14; + } + + if(pin > 1) { // ignore RxTx (pins 0 and 1) + pinStatus[pin] = mode; + switch(mode) { + case INPUT: + pinMode(pin, INPUT); + portStatus[port] = portStatus[port] &~ (1 << (pin - offset)); + break; + case OUTPUT: + digitalWrite(pin, LOW); // disable PWM + case PWM: + pinMode(pin, OUTPUT); + portStatus[port] = portStatus[port] | (1 << (pin - offset)); + break; + //case ANALOG: // TODO figure this out + default: + Firmata.sendString(""); + } + // TODO: save status to EEPROM here, if changed + } +} + +void analogWriteCallback(byte pin, int value) +{ + setPinModeCallback(pin,PWM); + analogWrite(pin, value); +} + +void digitalWriteCallback(byte port, int value) +{ + switch(port) { + case 0: // pins 2-7 (don't change Rx/Tx, pins 0 and 1) + // 0xFF03 == B1111111100000011 0x03 == B00000011 + PORTD = (value &~ 0xFF03) | (PORTD & 0x03); + break; + case 1: // pins 8-13 (14,15 are disabled for the crystal) + PORTB = (byte)value; + break; + case 2: // analog pins used as digital + PORTC = (byte)value; + break; + } +} + +// ----------------------------------------------------------------------------- +/* sets bits in a bit array (int) to toggle the reporting of the analogIns + */ +//void FirmataClass::setAnalogPinReporting(byte pin, byte state) { +//} +void reportAnalogCallback(byte pin, int value) +{ + if(value == 0) { + analogInputsToReport = analogInputsToReport &~ (1 << pin); + } + else { // everything but 0 enables reporting of that pin + analogInputsToReport = analogInputsToReport | (1 << pin); + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + reportPINs[port] = (byte)value; + if(port == ANALOG_PORT) // turn off analog reporting when used as digital + analogInputsToReport = 0; +} + +/*============================================================================== + * SETUP() + *============================================================================*/ +void setup() +{ + byte i; + + Firmata.setFirmwareVersion(2, 0); + + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + + portStatus[0] = B00000011; // ignore Tx/RX pins + portStatus[1] = B11000000; // ignore 14/15 pins + portStatus[2] = B00000000; + +// for(i=0; i nextExecuteMillis) { + nextExecuteMillis = currentMillis + 19; // run this every 20ms + /* SERIALREAD - Serial.read() uses a 128 byte circular buffer, so handle + * all serialReads at once, i.e. empty the buffer */ + while(Firmata.available()) + Firmata.processInput(); + /* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over + * 60 bytes. use a timer to sending an event character every 4 ms to + * trigger the buffer to dump. */ + + /* ANALOGREAD - right after the event character, do all of the + * analogReads(). These only need to be done every 4ms. */ + for(analogPin=0;analogPin +#include +#include +#include "WProgram.h" + +// When the display powers up, it is configured as follows: +// +// 1. Display clear +// 2. Function set: +// DL = 1; 8-bit interface data +// N = 0; 1-line display +// F = 0; 5x8 dot character font +// 3. Display on/off control: +// D = 0; Display off +// C = 0; Cursor off +// B = 0; Blinking off +// 4. Entry mode set: +// I/D = 1; Increment by 1 +// S = 0; No shift +// +// Note, however, that resetting the Arduino doesn't reset the LCD, so we +// can't assume that its in that state when a sketch starts (and the +// LiquidCrystal constructor is called). + +LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, + uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) : + _four_bit_mode(0), _rs_pin(rs), _rw_pin(rw), _enable_pin(enable) +{ + _data_pins[0] = d0; + _data_pins[1] = d1; + _data_pins[2] = d2; + _data_pins[3] = d3; + _data_pins[4] = d4; + _data_pins[5] = d5; + _data_pins[6] = d6; + _data_pins[7] = d7; + + pinMode(_rs_pin, OUTPUT); + pinMode(_rw_pin, OUTPUT); + pinMode(_enable_pin, OUTPUT); + + for (int i = 0; i < 8; i++) + pinMode(_data_pins[i], OUTPUT); + + command(0x38); // function set: 8 bits, 1 line, 5x8 dots + command(0x0C); // display control: turn display on, cursor off, no blinking + command(0x06); // entry mode set: increment automatically, display shift, right shift + clear(); +} + +LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) : + _four_bit_mode(1), _rs_pin(rs), _rw_pin(rw), _enable_pin(enable) +{ + _data_pins[0] = d0; + _data_pins[1] = d1; + _data_pins[2] = d2; + _data_pins[3] = d3; + + pinMode(_rs_pin, OUTPUT); + pinMode(_rw_pin, OUTPUT); + pinMode(_enable_pin, OUTPUT); + + for (int i = 0; i < 4; i++) + pinMode(_data_pins[i], OUTPUT); + + command(0x28); // function set: 4 bits, 1 line, 5x8 dots + command(0x0C); // display control: turn display on, cursor off, no blinking + command(0x06); // entry mode set: increment automatically, display shift, right shift + clear(); +} + +void LiquidCrystal::clear() +{ + command(0x01); // clear display, set cursor position to zero + delayMicroseconds(2000); +} + +void LiquidCrystal::home() +{ + command(0x02); // set cursor position to zero + delayMicroseconds(2000); +} + +void LiquidCrystal::setCursor(int col, int row) +{ + int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; + command(0x80 | (col + row_offsets[row])); +} + +void LiquidCrystal::command(uint8_t value) { + send(value, LOW); +} + +void LiquidCrystal::write(uint8_t value) { + send(value, HIGH); +} + +void LiquidCrystal::send(uint8_t value, uint8_t mode) { + digitalWrite(_rs_pin, mode); + digitalWrite(_rw_pin, LOW); + + if (_four_bit_mode) { + for (int i = 0; i < 4; i++) { + digitalWrite(_data_pins[i], (value >> (i + 4)) & 0x01); + } + + digitalWrite(_enable_pin, HIGH); + digitalWrite(_enable_pin, LOW); + + for (int i = 0; i < 4; i++) { + digitalWrite(_data_pins[i], (value >> i) & 0x01); + } + + digitalWrite(_enable_pin, HIGH); + digitalWrite(_enable_pin, LOW); + } else { + for (int i = 0; i < 8; i++) { + digitalWrite(_data_pins[i], (value >> i) & 0x01); + } + + digitalWrite(_enable_pin, HIGH); + digitalWrite(_enable_pin, LOW); + } +} diff --git a/libraries/LiquidCrystal/LiquidCrystal.h b/libraries/LiquidCrystal/LiquidCrystal.h new file mode 100755 index 0000000..a5edc5f --- /dev/null +++ b/libraries/LiquidCrystal/LiquidCrystal.h @@ -0,0 +1,31 @@ +#ifndef LiquidCrystal_h +#define LiquidCrystal_h + +#include +#include "Print.h" + +class LiquidCrystal : public Print { +public: + LiquidCrystal(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t); + LiquidCrystal(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, + uint8_t, uint8_t, uint8_t, uint8_t); + void clear(); + void home(); + void setCursor(int, int); + /* + void shiftDisplayLeft(); + void shiftDisplayRight(); + */ + virtual void write(uint8_t); + void command(uint8_t); +private: + void send(uint8_t, uint8_t); + + uint8_t _four_bit_mode; + uint8_t _rs_pin; // LOW: command. HIGH: character. + uint8_t _rw_pin; // LOW: write to LCD. HIGH: read from LCD. + uint8_t _enable_pin; // activated by a HIGH pulse. + uint8_t _data_pins[8]; +}; + +#endif diff --git a/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.pde b/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.pde new file mode 100644 index 0000000..2f244d0 --- /dev/null +++ b/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.pde @@ -0,0 +1,18 @@ +#include + +// LiquidCrystal display with: +// rs on pin 12 +// rw on pin 11 +// enable on pin 10 +// d4, d5, d6, d7 on pins 5, 4, 3, 2 +LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); + +void setup() +{ + // Print a message to the LCD. + lcd.print("hello, world!"); +} + +void loop() +{ +} diff --git a/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.pde b/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.pde new file mode 100644 index 0000000..0c4ce35 --- /dev/null +++ b/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.pde @@ -0,0 +1,34 @@ +/* + * Displays text sent over the serial port (e.g. from the Serial Monitor) on + * an attached LCD. + */ + +#include + +// LiquidCrystal display with: +// rs on pin 12 +// rw on pin 11 +// enable on pin 10 +// d4, d5, d6, d7 on pins 5, 4, 3, 2 +LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + // when characters arrive over the serial port... + if (Serial.available()) { + // wait a bit for the entire message to arrive + delay(100); + // clear the screen + lcd.clear(); + // read all the available characters + while (Serial.available() > 0) { + // display each character to the LCD + lcd.write(Serial.read()); + } + } +} diff --git a/libraries/LiquidCrystal/keywords.txt b/libraries/LiquidCrystal/keywords.txt new file mode 100755 index 0000000..367ab1f --- /dev/null +++ b/libraries/LiquidCrystal/keywords.txt @@ -0,0 +1,23 @@ +####################################### +# Syntax Coloring Map For LiquidCrystal +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +LiquidCrystal KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +clear KEYWORD2 +home KEYWORD2 +print KEYWORD2 +setCursor KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/Matrix/Matrix.cpp b/libraries/Matrix/Matrix.cpp new file mode 100755 index 0000000..2eb3e25 --- /dev/null +++ b/libraries/Matrix/Matrix.cpp @@ -0,0 +1,229 @@ +/* + 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 + #include + + // 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); + } +} + diff --git a/libraries/Matrix/Matrix.h b/libraries/Matrix/Matrix.h new file mode 100755 index 0000000..7c6be91 --- /dev/null +++ b/libraries/Matrix/Matrix.h @@ -0,0 +1,54 @@ +/* + 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 + +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 + diff --git a/libraries/Matrix/examples/hello_matrix/hello_matrix.pde b/libraries/Matrix/examples/hello_matrix/hello_matrix.pde new file mode 100644 index 0000000..127917f --- /dev/null +++ b/libraries/Matrix/examples/hello_matrix/hello_matrix.pde @@ -0,0 +1,42 @@ +#include +#include + +// Hello Matrix +// by Nicholas Zambetti + +// 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); +} + diff --git a/libraries/Matrix/examples/sprite_animation/sprite_animation.pde b/libraries/Matrix/examples/sprite_animation/sprite_animation.pde new file mode 100644 index 0000000..bf7c6f5 --- /dev/null +++ b/libraries/Matrix/examples/sprite_animation/sprite_animation.pde @@ -0,0 +1,48 @@ +#include +#include + +// Sprite Animation +// by Nicholas Zambetti + +// 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 +} + diff --git a/libraries/Matrix/keywords.txt b/libraries/Matrix/keywords.txt new file mode 100644 index 0000000..b784f87 --- /dev/null +++ b/libraries/Matrix/keywords.txt @@ -0,0 +1,22 @@ +####################################### +# Syntax Coloring Map For Matrix +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Matrix KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +setBrightness KEYWORD2 +write KEYWORD2 +clear KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/Servo/Servo.cpp b/libraries/Servo/Servo.cpp new file mode 100755 index 0000000..8578fef --- /dev/null +++ b/libraries/Servo/Servo.cpp @@ -0,0 +1,133 @@ +#include +#include +#include + +/* + Servo.h - Hardware Servo Timer Library + Author: Jim Studt, jim@federated.com + Copyright (c) 2007 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 +*/ + + +uint8_t Servo::attached9 = 0; +uint8_t Servo::attached10 = 0; + +void Servo::seizeTimer1() +{ + uint8_t oldSREG = SREG; + + cli(); + TCCR1A = _BV(WGM11); /* Fast PWM, ICR1 is top */ + TCCR1B = _BV(WGM13) | _BV(WGM12) /* Fast PWM, ICR1 is top */ + | _BV(CS11) /* div 8 clock prescaler */ + ; + OCR1A = 3000; + OCR1B = 3000; + ICR1 = clockCyclesPerMicrosecond()*(20000L/8); // 20000 uS is a bit fast for the refresh, 20ms, but + // it keeps us from overflowing ICR1 at 20MHz clocks + // That "/8" at the end is the prescaler. +#if defined(__AVR_ATmega8__) + TIMSK &= ~(_BV(TICIE1) | _BV(OCIE1A) | _BV(OCIE1B) | _BV(TOIE1) ); +#else + TIMSK1 &= ~(_BV(OCIE1A) | _BV(OCIE1B) | _BV(TOIE1) ); +#endif + + SREG = oldSREG; // undo cli() +} + +void Servo::releaseTimer1() {} + +#define NO_ANGLE (0xff) + +Servo::Servo() : pin(0), angle(NO_ANGLE) {} + +uint8_t Servo::attach(int pinArg) +{ + return attach(pinArg, 544, 2400); +} + +uint8_t Servo::attach(int pinArg, int min, int max) +{ + if (pinArg != 9 && pinArg != 10) return 0; + + min16 = min / 16; + max16 = max / 16; + + pin = pinArg; + angle = NO_ANGLE; + digitalWrite(pin, LOW); + pinMode(pin, OUTPUT); + + if (!attached9 && !attached10) seizeTimer1(); + + if (pin == 9) { + attached9 = 1; + TCCR1A = (TCCR1A & ~_BV(COM1A0)) | _BV(COM1A1); + } + + if (pin == 10) { + attached10 = 1; + TCCR1A = (TCCR1A & ~_BV(COM1B0)) | _BV(COM1B1); + } + return 1; +} + +void Servo::detach() +{ + // muck with timer flags + if (pin == 9) { + attached9 = 0; + TCCR1A = TCCR1A & ~_BV(COM1A0) & ~_BV(COM1A1); + pinMode(pin, INPUT); + } + + if (pin == 10) { + attached10 = 0; + TCCR1A = TCCR1A & ~_BV(COM1B0) & ~_BV(COM1B1); + pinMode(pin, INPUT); + } + + if (!attached9 && !attached10) releaseTimer1(); +} + +void Servo::write(int angleArg) +{ + uint16_t p; + + if (angleArg < 0) angleArg = 0; + if (angleArg > 180) angleArg = 180; + angle = angleArg; + + // bleh, have to use longs to prevent overflow, could be tricky if always a 16MHz clock, but not true + // That 8L on the end is the TCNT1 prescaler, it will need to change if the clock's prescaler changes, + // but then there will likely be an overflow problem, so it will have to be handled by a human. + p = (min16*16L*clockCyclesPerMicrosecond() + (max16-min16)*(16L*clockCyclesPerMicrosecond())*angle/180L)/8L; + if (pin == 9) OCR1A = p; + if (pin == 10) OCR1B = p; +} + +uint8_t Servo::read() +{ + return angle; +} + +uint8_t Servo::attached() +{ + if (pin == 9 && attached9) return 1; + if (pin == 10 && attached10) return 1; + return 0; +} diff --git a/libraries/Servo/Servo.h b/libraries/Servo/Servo.h new file mode 100755 index 0000000..0b0e8db --- /dev/null +++ b/libraries/Servo/Servo.h @@ -0,0 +1,52 @@ +#ifndef Servo_h +#define Servo_h + +/* + Servo.h - Hardware Servo Timer Library + Author: Jim Studt, jim@federated.com + Copyright (c) 2007 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 + +class Servo +{ + private: + uint8_t pin; + uint8_t angle; // in degrees + uint8_t min16; // minimum pulse, 16uS units (default is 34) + uint8_t max16; // maximum pulse, 16uS units, 0-4ms range (default is 150) + static void seizeTimer1(); + static void releaseTimer1(); + static uint8_t attached9; + static uint8_t attached10; + public: + Servo(); + uint8_t attach(int); + // pulse length for 0 degrees in microseconds, 544uS default + // pulse length for 180 degrees in microseconds, 2400uS default + uint8_t attach(int, int, int); + // attach to a pin, sets pinMode, returns 0 on failure, won't + // position the servo until a subsequent write() happens + // Only works for 9 and 10. + void detach(); + void write(int); // specify the angle in degrees, 0 to 180 + uint8_t read(); + uint8_t attached(); +}; + +#endif diff --git a/libraries/Servo/examples/Knob/Knob.pde b/libraries/Servo/examples/Knob/Knob.pde new file mode 100644 index 0000000..886e107 --- /dev/null +++ b/libraries/Servo/examples/Knob/Knob.pde @@ -0,0 +1,22 @@ +// Controlling a servo position using a potentiometer (variable resistor) +// by Michal Rinott + +#include + +Servo myservo; // create servo object to control a servo + +int potpin = 0; // analog pin used to connect the potentiometer +int val; // variable to read the value from the analog pin + +void setup() +{ + myservo.attach(9); // attaches the servo on pin 9 to the servo object +} + +void loop() +{ + val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) + val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) + myservo.write(val); // sets the servo position according to the scaled value + delay(15); // waits for the servo to get there +} diff --git a/libraries/Servo/examples/Sweep/Sweep.pde b/libraries/Servo/examples/Sweep/Sweep.pde new file mode 100644 index 0000000..52e6056 --- /dev/null +++ b/libraries/Servo/examples/Sweep/Sweep.pde @@ -0,0 +1,29 @@ +// Sweep +// by BARRAGAN + +#include + +Servo myservo; // create servo object to control a servo + // a maximum of eight servo objects can be created + +int pos = 0; // variable to store the servo position + +void setup() +{ + myservo.attach(9); // attaches the servo on pin 9 to the servo object +} + + +void loop() +{ + for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees + { // in steps of 1 degree + myservo.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } + for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees + { + myservo.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } +} diff --git a/libraries/Servo/keywords.txt b/libraries/Servo/keywords.txt new file mode 100755 index 0000000..918c46a --- /dev/null +++ b/libraries/Servo/keywords.txt @@ -0,0 +1,22 @@ +####################################### +# Syntax Coloring Map Servo +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Servo KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +attach KEYWORD2 +detach KEYWORD2 +write KEYWORD2 +read KEYWORD2 +attached KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/libraries/SoftwareSerial/SoftwareSerial.cpp b/libraries/SoftwareSerial/SoftwareSerial.cpp new file mode 100755 index 0000000..6df04d2 --- /dev/null +++ b/libraries/SoftwareSerial/SoftwareSerial.cpp @@ -0,0 +1,227 @@ +/* + 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)); +} diff --git a/libraries/SoftwareSerial/SoftwareSerial.h b/libraries/SoftwareSerial/SoftwareSerial.h new file mode 100755 index 0000000..95753fc --- /dev/null +++ b/libraries/SoftwareSerial/SoftwareSerial.h @@ -0,0 +1,56 @@ +/* + 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 + +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 + diff --git a/libraries/SoftwareSerial/keywords.txt b/libraries/SoftwareSerial/keywords.txt new file mode 100644 index 0000000..de5a74c --- /dev/null +++ b/libraries/SoftwareSerial/keywords.txt @@ -0,0 +1,18 @@ +####################################### +# Syntax Coloring Map For Ultrasound +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +SoftwareSerial KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/Sprite/Sprite.cpp b/libraries/Sprite/Sprite.cpp new file mode 100644 index 0000000..6055876 --- /dev/null +++ b/libraries/Sprite/Sprite.cpp @@ -0,0 +1,95 @@ +/* + 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 +#include +//#include + +#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; +} diff --git a/libraries/Sprite/Sprite.h b/libraries/Sprite/Sprite.h new file mode 100644 index 0000000..bdcfdb8 --- /dev/null +++ b/libraries/Sprite/Sprite.h @@ -0,0 +1,48 @@ +/* + 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 + +#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 diff --git a/libraries/Sprite/binary.h b/libraries/Sprite/binary.h new file mode 100644 index 0000000..af14980 --- /dev/null +++ b/libraries/Sprite/binary.h @@ -0,0 +1,515 @@ +#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 diff --git a/libraries/Sprite/keywords.txt b/libraries/Sprite/keywords.txt new file mode 100644 index 0000000..73cd8d9 --- /dev/null +++ b/libraries/Sprite/keywords.txt @@ -0,0 +1,534 @@ +####################################### +# 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 + diff --git a/libraries/Stepper/Stepper.cpp b/libraries/Stepper/Stepper.cpp new file mode 100644 index 0000000..d5c16a3 --- /dev/null +++ b/libraries/Stepper/Stepper.cpp @@ -0,0 +1,220 @@ +/* + Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4 + + Original library (0.1) by Tom Igoe. + Two-wire modifications (0.2) by Sebastian Gassner + Combination version (0.3) by Tom Igoe and David Mellis + Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley + + Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires + + When wiring multiple stepper motors to a microcontroller, + you quickly run out of output pins, with each motor requiring 4 connections. + + By making use of the fact that at any time two of the four motor + coils are the inverse of the other two, the number of + control connections can be reduced from 4 to 2. + + A slightly modified circuit around a Darlington transistor array or an L293 H-bridge + connects to only 2 microcontroler pins, inverts the signals received, + and delivers the 4 (2 plus 2 inverted ones) output signals required + for driving a stepper motor. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 + + The circuits can be found at + +http://www.arduino.cc/en/Tutorial/Stepper + + + */ + + +#include "WProgram.h" +#include "Stepper.h" + +/* + * two-wire constructor. + * Sets which wires should control the motor. + */ +Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2) +{ + this->step_number = 0; // which step the motor is on + this->speed = 0; // the motor speed, in revolutions per minute + this->direction = 0; // motor direction + this->last_step_time = 0; // time stamp in ms of the last step taken + this->number_of_steps = number_of_steps; // total number of steps for this motor + + // Arduino pins for the motor control connection: + this->motor_pin_1 = motor_pin_1; + this->motor_pin_2 = motor_pin_2; + + // setup the pins on the microcontroller: + pinMode(this->motor_pin_1, OUTPUT); + pinMode(this->motor_pin_2, OUTPUT); + + // When there are only 2 pins, set the other two to 0: + this->motor_pin_3 = 0; + this->motor_pin_4 = 0; + + // pin_count is used by the stepMotor() method: + this->pin_count = 2; +} + + +/* + * constructor for four-pin version + * Sets which wires should control the motor. + */ + +Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4) +{ + this->step_number = 0; // which step the motor is on + this->speed = 0; // the motor speed, in revolutions per minute + this->direction = 0; // motor direction + this->last_step_time = 0; // time stamp in ms of the last step taken + this->number_of_steps = number_of_steps; // total number of steps for this motor + + // Arduino pins for the motor control connection: + this->motor_pin_1 = motor_pin_1; + this->motor_pin_2 = motor_pin_2; + this->motor_pin_3 = motor_pin_3; + this->motor_pin_4 = motor_pin_4; + + // setup the pins on the microcontroller: + pinMode(this->motor_pin_1, OUTPUT); + pinMode(this->motor_pin_2, OUTPUT); + pinMode(this->motor_pin_3, OUTPUT); + pinMode(this->motor_pin_4, OUTPUT); + + // pin_count is used by the stepMotor() method: + this->pin_count = 4; +} + +/* + Sets the speed in revs per minute + +*/ +void Stepper::setSpeed(long whatSpeed) +{ + this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed; +} + +/* + Moves the motor steps_to_move steps. If the number is negative, + the motor moves in the reverse direction. + */ +void Stepper::step(int steps_to_move) +{ + int steps_left = abs(steps_to_move); // how many steps to take + + // determine direction based on whether steps_to_mode is + or -: + if (steps_to_move > 0) {this->direction = 1;} + if (steps_to_move < 0) {this->direction = 0;} + + + // decrement the number of steps, moving one step each time: + while(steps_left > 0) { + // move only if the appropriate delay has passed: + if (millis() - this->last_step_time >= this->step_delay) { + // get the timeStamp of when you stepped: + this->last_step_time = millis(); + // increment or decrement the step number, + // depending on direction: + if (this->direction == 1) { + this->step_number++; + if (this->step_number == this->number_of_steps) { + this->step_number = 0; + } + } + else { + if (this->step_number == 0) { + this->step_number = this->number_of_steps; + } + this->step_number--; + } + // decrement the steps left: + steps_left--; + // step the motor to step number 0, 1, 2, or 3: + stepMotor(this->step_number % 4); + } + } +} + +/* + * Moves the motor forward or backwards. + */ +void Stepper::stepMotor(int thisStep) +{ + if (this->pin_count == 2) { + switch (thisStep) { + case 0: /* 01 */ + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + break; + case 1: /* 11 */ + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, HIGH); + break; + case 2: /* 10 */ + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + break; + case 3: /* 00 */ + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + break; + } + } + if (this->pin_count == 4) { + switch (thisStep) { + case 0: // 1010 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 1: // 0110 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 2: //0101 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + case 3: //1001 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + } + } +} + +/* + version() returns the version of the library: +*/ +int Stepper::version(void) +{ + return 4; +} diff --git a/libraries/Stepper/Stepper.h b/libraries/Stepper/Stepper.h new file mode 100644 index 0000000..4094aee --- /dev/null +++ b/libraries/Stepper/Stepper.h @@ -0,0 +1,83 @@ +/* + Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4 + + Original library (0.1) by Tom Igoe. + Two-wire modifications (0.2) by Sebastian Gassner + Combination version (0.3) by Tom Igoe and David Mellis + Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley + + Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires + + When wiring multiple stepper motors to a microcontroller, + you quickly run out of output pins, with each motor requiring 4 connections. + + By making use of the fact that at any time two of the four motor + coils are the inverse of the other two, the number of + control connections can be reduced from 4 to 2. + + A slightly modified circuit around a Darlington transistor array or an L293 H-bridge + connects to only 2 microcontroler pins, inverts the signals received, + and delivers the 4 (2 plus 2 inverted ones) output signals required + for driving a stepper motor. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 + + The circuits can be found at + http://www.arduino.cc/en/Tutorial/Stepper +*/ + +// ensure this library description is only included once +#ifndef Stepper_h +#define Stepper_h + +// library interface description +class Stepper { + public: + // constructors: + Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2); + Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4); + + // speed setter method: + void setSpeed(long whatSpeed); + + // mover method: + void step(int number_of_steps); + + int version(void); + + private: + void stepMotor(int this_step); + + int direction; // Direction of rotation + int speed; // Speed in RPMs + unsigned long step_delay; // delay between steps, in ms, based on speed + int number_of_steps; // total number of steps this motor can take + int pin_count; // whether you're driving the motor with 2 or 4 pins + int step_number; // which step the motor is on + + // motor pin numbers: + int motor_pin_1; + int motor_pin_2; + int motor_pin_3; + int motor_pin_4; + + long last_step_time; // time stamp in ms of when the last step was taken +}; + +#endif + diff --git a/libraries/Stepper/examples/MotorKnob/MotorKnob.pde b/libraries/Stepper/examples/MotorKnob/MotorKnob.pde new file mode 100644 index 0000000..062cac9 --- /dev/null +++ b/libraries/Stepper/examples/MotorKnob/MotorKnob.pde @@ -0,0 +1,40 @@ +/* + * MotorKnob + * + * A stepper motor follows the turns of a potentiometer + * (or other sensor) on analog input 0. + * + * http://www.arduino.cc/en/Reference/Stepper + */ + +#include + +// change this to the number of steps on your motor +#define STEPS 100 + +// create an instance of the stepper class, specifying +// the number of steps of the motor and the pins it's +// attached to +Stepper stepper(STEPS, 8, 9, 10, 11); + +// the previous reading from the analog input +int previous = 0; + +void setup() +{ + // set the speed of the motor to 30 RPMs + stepper.setSpeed(30); +} + +void loop() +{ + // get the sensor value + int val = analogRead(0); + + // move a number of steps equal to the change in the + // sensor reading + stepper.step(val - previous); + + // remember the previous value of the sensor + previous = val; +} \ No newline at end of file diff --git a/libraries/Stepper/keywords.txt b/libraries/Stepper/keywords.txt new file mode 100644 index 0000000..19a0fad --- /dev/null +++ b/libraries/Stepper/keywords.txt @@ -0,0 +1,28 @@ +####################################### +# Syntax Coloring Map For Test +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Stepper KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +step KEYWORD2 +setSpeed KEYWORD2 +version KEYWORD2 + +###################################### +# Instances (KEYWORD2) +####################################### +direction KEYWORD2 +speed KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp new file mode 100755 index 0000000..0ee3012 --- /dev/null +++ b/libraries/Wire/Wire.cpp @@ -0,0 +1,265 @@ +/* + TwoWire.cpp - TWI/I2C library for Wiring & Arduino + 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 +*/ + +extern "C" { + #include + #include + #include + #include "twi.h" +} + +#include "Wire.h" + +// Initialize Class Variables ////////////////////////////////////////////////// + +uint8_t* TwoWire::rxBuffer = 0; +uint8_t TwoWire::rxBufferIndex = 0; +uint8_t TwoWire::rxBufferLength = 0; + +uint8_t TwoWire::txAddress = 0; +uint8_t* TwoWire::txBuffer = 0; +uint8_t TwoWire::txBufferIndex = 0; +uint8_t TwoWire::txBufferLength = 0; + +uint8_t TwoWire::transmitting = 0; +void (*TwoWire::user_onRequest)(void); +void (*TwoWire::user_onReceive)(int); + +// Constructors //////////////////////////////////////////////////////////////// + +TwoWire::TwoWire() +{ +} + +// Public Methods ////////////////////////////////////////////////////////////// + +void TwoWire::begin(void) +{ + // init buffer for reads + rxBuffer = (uint8_t*) calloc(BUFFER_LENGTH, sizeof(uint8_t)); + rxBufferIndex = 0; + rxBufferLength = 0; + + // init buffer for writes + txBuffer = (uint8_t*) calloc(BUFFER_LENGTH, sizeof(uint8_t)); + txBufferIndex = 0; + txBufferLength = 0; + + twi_init(); +} + +void TwoWire::begin(uint8_t address) +{ + twi_setAddress(address); + twi_attachSlaveTxEvent(onRequestService); + twi_attachSlaveRxEvent(onReceiveService); + begin(); +} + +void TwoWire::begin(int address) +{ + begin((uint8_t)address); +} + +uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) +{ + // clamp to buffer length + if(quantity > BUFFER_LENGTH){ + quantity = BUFFER_LENGTH; + } + // perform blocking read into buffer + uint8_t read = twi_readFrom(address, rxBuffer, quantity); + // set rx buffer iterator vars + rxBufferIndex = 0; + rxBufferLength = read; + + return read; +} + +uint8_t TwoWire::requestFrom(int address, int quantity) +{ + return requestFrom((uint8_t)address, (uint8_t)quantity); +} + +void TwoWire::beginTransmission(uint8_t address) +{ + // indicate that we are transmitting + transmitting = 1; + // set address of targeted slave + txAddress = address; + // reset tx buffer iterator vars + txBufferIndex = 0; + txBufferLength = 0; +} + +void TwoWire::beginTransmission(int address) +{ + beginTransmission((uint8_t)address); +} + +uint8_t TwoWire::endTransmission(void) +{ + // transmit buffer (blocking) + int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1); + // reset tx buffer iterator vars + txBufferIndex = 0; + txBufferLength = 0; + // indicate that we are done transmitting + transmitting = 0; + return ret; +} + +// must be called in: +// slave tx event callback +// or after beginTransmission(address) +void TwoWire::send(uint8_t data) +{ + if(transmitting){ + // in master transmitter mode + // don't bother if buffer is full + if(txBufferLength >= BUFFER_LENGTH){ + return; + } + // put byte in tx buffer + txBuffer[txBufferIndex] = data; + ++txBufferIndex; + // update amount in buffer + txBufferLength = txBufferIndex; + }else{ + // in slave send mode + // reply to master + twi_transmit(&data, 1); + } +} + +// must be called in: +// slave tx event callback +// or after beginTransmission(address) +void TwoWire::send(uint8_t* data, uint8_t quantity) +{ + if(transmitting){ + // in master transmitter mode + for(uint8_t i = 0; i < quantity; ++i){ + send(data[i]); + } + }else{ + // in slave send mode + // reply to master + twi_transmit(data, quantity); + } +} + +// must be called in: +// slave tx event callback +// or after beginTransmission(address) +void TwoWire::send(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); +} + +// must be called in: +// slave rx event callback +// or after requestFrom(address, numBytes) +uint8_t TwoWire::available(void) +{ + return rxBufferLength - rxBufferIndex; +} + +// must be called in: +// slave rx event callback +// or after requestFrom(address, numBytes) +uint8_t TwoWire::receive(void) +{ + // default to returning null char + // for people using with char strings + uint8_t value = '\0'; + + // get each successive byte on each call + if(rxBufferIndex < rxBufferLength){ + value = rxBuffer[rxBufferIndex]; + ++rxBufferIndex; + } + + return value; +} + +// behind the scenes function that is called when data is received +void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes) +{ + // don't bother if user hasn't registered a callback + if(!user_onReceive){ + return; + } + // don't bother if rx buffer is in use by a master requestFrom() op + // i know this drops data, but it allows for slight stupidity + // meaning, they may not have read all the master requestFrom() data yet + if(rxBufferIndex < rxBufferLength){ + return; + } + // copy twi rx buffer into local read buffer + // this enables new reads to happen in parallel + for(uint8_t i = 0; i < numBytes; ++i){ + rxBuffer[i] = inBytes[i]; + } + // set rx iterator vars + rxBufferIndex = 0; + rxBufferLength = numBytes; + // alert user program + user_onReceive(numBytes); +} + +// behind the scenes function that is called when data is requested +void TwoWire::onRequestService(void) +{ + // don't bother if user hasn't registered a callback + if(!user_onRequest){ + return; + } + // reset tx buffer iterator vars + // !!! this will kill any pending pre-master sendTo() activity + txBufferIndex = 0; + txBufferLength = 0; + // alert user program + user_onRequest(); +} + +// sets function called on slave write +void TwoWire::onReceive( void (*function)(int) ) +{ + user_onReceive = function; +} + +// sets function called on slave read +void TwoWire::onRequest( void (*function)(void) ) +{ + user_onRequest = function; +} + +// Preinstantiate Objects ////////////////////////////////////////////////////// + +TwoWire Wire = TwoWire(); + diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h new file mode 100755 index 0000000..9e849d5 --- /dev/null +++ b/libraries/Wire/Wire.h @@ -0,0 +1,67 @@ +/* + TwoWire.h - TWI/I2C 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 TwoWire_h +#define TwoWire_h + +#include + +#define BUFFER_LENGTH 32 + +class TwoWire +{ + private: + static uint8_t* rxBuffer; + static uint8_t rxBufferIndex; + static uint8_t rxBufferLength; + + static uint8_t txAddress; + static uint8_t* txBuffer; + static uint8_t txBufferIndex; + static uint8_t txBufferLength; + + static uint8_t transmitting; + static void (*user_onRequest)(void); + static void (*user_onReceive)(int); + static void onRequestService(void); + static void onReceiveService(uint8_t*, int); + public: + TwoWire(); + void begin(); + void begin(uint8_t); + void begin(int); + void beginTransmission(uint8_t); + void beginTransmission(int); + 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); + void onReceive( void (*)(int) ); + void onRequest( void (*)(void) ); +}; + +extern TwoWire Wire; + +#endif + diff --git a/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.pde b/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.pde new file mode 100755 index 0000000..c89b0f0 --- /dev/null +++ b/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.pde @@ -0,0 +1,84 @@ +// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder +// by Nicholas Zambetti +// and James Tichenor + +// Demonstrates use of the Wire library reading data from the +// Devantech Utrasonic Rangers SFR08 and SFR10 + +// Created 29 April 2006 + +#include + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) + Serial.begin(9600); // start serial communication at 9600bps +} + +int reading = 0; + +void loop() +{ + // step 1: instruct sensor to read echoes + 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) + // use 0x51 for centimeters + // use 0x52 for ping microseconds + Wire.endTransmission(); // stop transmitting + + // step 2: wait for readings to happen + delay(70); // datasheet suggests at least 65 milliseconds + + // 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.endTransmission(); // stop transmitting + + // step 4: request reading from sensor + Wire.requestFrom(112, 2); // request 2 bytes from slave device #112 + + // 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 = reading << 8; // shift high byte to be high 8 bits + reading |= Wire.receive(); // receive low byte as lower 8 bits + Serial.println(reading); // print the reading + } + + delay(250); // wait a bit since people have to read the output :) +} + + +/* + +// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08) +// usage: changeAddress(0x70, 0xE6); + +void changeAddress(byte oldAddress, byte newAddress) +{ + Wire.beginTransmission(oldAddress); + Wire.send(0x00); + Wire.send(0xA0); + Wire.endTransmission(); + + Wire.beginTransmission(oldAddress); + Wire.send(0x00); + Wire.send(0xAA); + Wire.endTransmission(); + + Wire.beginTransmission(oldAddress); + Wire.send(0x00); + Wire.send(0xA5); + Wire.endTransmission(); + + Wire.beginTransmission(oldAddress); + Wire.send(0x00); + Wire.send(newAddress); + Wire.endTransmission(); +} + +*/ diff --git a/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.pde b/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.pde new file mode 100644 index 0000000..35ee5d6 --- /dev/null +++ b/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.pde @@ -0,0 +1,34 @@ +// I2C Digital Potentiometer +// by Nicholas Zambetti +// and Shawn Bonkowski + +// Demonstrates use of the Wire library +// Controls AD5171 digital potentiometer via I2C/TWI + +// Created 31 March 2006 + +#include + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) +} + +byte val = 0; + +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.endTransmission(); // stop transmitting + + val++; // increment value + if(val == 64) // if reached 64th position (max) + { + val = 0; // start over from lowest value + } + delay(500); +} + diff --git a/libraries/Wire/examples/master_reader/master_reader.pde b/libraries/Wire/examples/master_reader/master_reader.pde new file mode 100644 index 0000000..1b139de --- /dev/null +++ b/libraries/Wire/examples/master_reader/master_reader.pde @@ -0,0 +1,29 @@ +// Wire Master Reader +// by Nicholas Zambetti + +// Demonstrates use of the Wire library +// Reads data from an I2C/TWI slave device +// Refer to the "Wire Slave Sender" example for use with this + +// Created 29 March 2006 + +#include + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) + Serial.begin(9600); // start serial for output +} + +void loop() +{ + Wire.requestFrom(2, 6); // request 6 bytes from slave device #2 + + while(Wire.available()) // slave may send less than requested + { + char c = Wire.receive(); // receive a byte as character + Serial.print(c); // print the character + } + + delay(500); +} diff --git a/libraries/Wire/examples/master_writer/master_writer.pde b/libraries/Wire/examples/master_writer/master_writer.pde new file mode 100644 index 0000000..d0ff9fa --- /dev/null +++ b/libraries/Wire/examples/master_writer/master_writer.pde @@ -0,0 +1,28 @@ +// Wire Master Writer +// by Nicholas Zambetti + +// Demonstrates use of the Wire library +// Writes data to an I2C/TWI slave device +// Refer to the "Wire Slave Receiver" example for use with this + +// Created 29 March 2006 + +#include + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) +} + +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.endTransmission(); // stop transmitting + + x++; + delay(500); +} diff --git a/libraries/Wire/examples/slave_receiver/slave_receiver.pde b/libraries/Wire/examples/slave_receiver/slave_receiver.pde new file mode 100644 index 0000000..53c86b5 --- /dev/null +++ b/libraries/Wire/examples/slave_receiver/slave_receiver.pde @@ -0,0 +1,35 @@ +// Wire Slave Receiver +// by Nicholas Zambetti + +// Demonstrates use of the Wire library +// Receives data as an I2C/TWI slave device +// Refer to the "Wire Master Writer" example for use with this + +// Created 29 March 2006 + +#include + +void setup() +{ + Wire.begin(4); // join i2c bus with address #4 + Wire.onReceive(receiveEvent); // register event + Serial.begin(9600); // start serial for output +} + +void loop() +{ + delay(100); +} + +// function that executes whenever data is received from master +// this function is registered as an event, see setup() +void receiveEvent(int howMany) +{ + while(1 < Wire.available()) // loop through all but the last + { + char c = Wire.receive(); // receive byte as a character + Serial.print(c); // print the character + } + int x = Wire.receive(); // receive byte as an integer + Serial.println(x); // print the integer +} diff --git a/libraries/Wire/examples/slave_sender/slave_sender.pde b/libraries/Wire/examples/slave_sender/slave_sender.pde new file mode 100644 index 0000000..f500644 --- /dev/null +++ b/libraries/Wire/examples/slave_sender/slave_sender.pde @@ -0,0 +1,29 @@ +// Wire Slave Sender +// by Nicholas Zambetti + +// Demonstrates use of the Wire library +// Sends data as an I2C/TWI slave device +// Refer to the "Wire Master Reader" example for use with this + +// Created 29 March 2006 + +#include + +void setup() +{ + Wire.begin(2); // join i2c bus with address #2 + Wire.onRequest(requestEvent); // register event +} + +void loop() +{ + delay(100); +} + +// function that executes whenever data is requested by master +// this function is registered as an event, see setup() +void requestEvent() +{ + Wire.send("hello "); // respond with message of 6 bytes + // as expected by master +} diff --git a/libraries/Wire/keywords.txt b/libraries/Wire/keywords.txt new file mode 100644 index 0000000..12f129b --- /dev/null +++ b/libraries/Wire/keywords.txt @@ -0,0 +1,31 @@ +####################################### +# Syntax Coloring Map For Wire +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +beginTransmission KEYWORD2 +endTransmission KEYWORD2 +requestFrom KEYWORD2 +send KEYWORD2 +receive KEYWORD2 +onReceive KEYWORD2 +onRequest KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + +Wire KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/Wire/utility/twi.c b/libraries/Wire/utility/twi.c new file mode 100644 index 0000000..82a25c0 --- /dev/null +++ b/libraries/Wire/utility/twi.c @@ -0,0 +1,474 @@ +/* + twi.c - TWI/I2C library for Wiring & Arduino + 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 +*/ + +#include +#include +#include +#include +#include +#include + +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif + +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +#include "twi.h" + +static volatile uint8_t twi_state; +static uint8_t twi_slarw; + +static void (*twi_onSlaveTransmit)(void); +static void (*twi_onSlaveReceive)(uint8_t*, int); + +static uint8_t* twi_masterBuffer; +static volatile uint8_t twi_masterBufferIndex; +static uint8_t twi_masterBufferLength; + +static uint8_t* twi_txBuffer; +static volatile uint8_t twi_txBufferIndex; +static volatile uint8_t twi_txBufferLength; + +static uint8_t* twi_rxBuffer; +static volatile uint8_t twi_rxBufferIndex; + +static volatile uint8_t twi_error; + +/* + * Function twi_init + * Desc readys twi pins and sets twi bitrate + * Input none + * Output none + */ +void twi_init(void) +{ + // initialize state + twi_state = TWI_READY; + + #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__) + // activate internal pull-ups for twi + // as per note from atmega8 manual pg167 + sbi(PORTC, 4); + sbi(PORTC, 5); + #else + // activate internal pull-ups for twi + // as per note from atmega128 manual pg204 + sbi(PORTD, 0); + sbi(PORTD, 1); + #endif + + // initialize twi prescaler and bit rate + cbi(TWSR, TWPS0); + cbi(TWSR, TWPS1); + TWBR = ((CPU_FREQ / TWI_FREQ) - 16) / 2; + + /* twi bit rate formula from atmega128 manual pg 204 + SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR)) + note: TWBR should be 10 or higher for master mode + It is 72 for a 16mhz Wiring board with 100kHz TWI */ + + // enable twi module, acks, and twi interrupt + TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); + + // allocate buffers + twi_masterBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t)); + twi_txBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t)); + twi_rxBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t)); +} + +/* + * Function twi_slaveInit + * Desc sets slave address and enables interrupt + * Input none + * Output none + */ +void twi_setAddress(uint8_t address) +{ + // set twi slave address (skip over TWGCE bit) + TWAR = address << 1; +} + +/* + * Function twi_readFrom + * Desc attempts to become twi bus master and read a + * series of bytes from a device on the bus + * Input address: 7bit i2c device address + * data: pointer to byte array + * length: number of bytes to read into array + * Output number of bytes read + */ +uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length) +{ + uint8_t i; + + // ensure data will fit into buffer + if(TWI_BUFFER_LENGTH < length){ + return 0; + } + + // wait until twi is ready, become master receiver + while(TWI_READY != twi_state){ + continue; + } + twi_state = TWI_MRX; + // reset error state (0xFF.. no error occured) + twi_error = 0xFF; + + // initialize buffer iteration vars + twi_masterBufferIndex = 0; + twi_masterBufferLength = length; + + // build sla+w, slave device address + w bit + twi_slarw = TW_READ; + twi_slarw |= address << 1; + + // send start condition + TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); + + // wait for read operation to complete + while(TWI_MRX == twi_state){ + continue; + } + + if (twi_masterBufferIndex < length) + length = twi_masterBufferIndex; + + // copy twi buffer to data + for(i = 0; i < length; ++i){ + data[i] = twi_masterBuffer[i]; + } + + return length; +} + +/* + * Function twi_writeTo + * Desc attempts to become twi bus master and write a + * series of bytes to a device on the bus + * Input address: 7bit i2c device address + * data: pointer to byte array + * length: number of bytes in array + * wait: boolean indicating to wait for write or not + * Output 0 .. success + * 1 .. length to long for buffer + * 2 .. address send, NACK received + * 3 .. data send, NACK received + * 4 .. other twi error (lost bus arbitration, bus error, ..) + */ +uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait) +{ + uint8_t i; + + // ensure data will fit into buffer + if(TWI_BUFFER_LENGTH < length){ + return 1; + } + + // wait until twi is ready, become master transmitter + while(TWI_READY != twi_state){ + continue; + } + twi_state = TWI_MTX; + // reset error state (0xFF.. no error occured) + twi_error = 0xFF; + + // initialize buffer iteration vars + twi_masterBufferIndex = 0; + twi_masterBufferLength = length; + + // copy data to twi buffer + for(i = 0; i < length; ++i){ + twi_masterBuffer[i] = data[i]; + } + + // build sla+w, slave device address + w bit + twi_slarw = TW_WRITE; + twi_slarw |= address << 1; + + // send start condition + TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); + + // wait for write operation to complete + while(wait && (TWI_MTX == twi_state)){ + continue; + } + + if (twi_error == 0xFF) + return 0; // success + else if (twi_error == TW_MT_SLA_NACK) + return 2; // error: address send, nack received + else if (twi_error == TW_MT_DATA_NACK) + return 3; // error: data send, nack received + else + return 4; // other twi error +} + +/* + * Function twi_transmit + * Desc fills slave tx buffer with data + * must be called in slave tx event callback + * Input data: pointer to byte array + * length: number of bytes in array + * Output 1 length too long for buffer + * 2 not slave transmitter + * 0 ok + */ +uint8_t twi_transmit(uint8_t* data, uint8_t length) +{ + uint8_t i; + + // ensure data will fit into buffer + if(TWI_BUFFER_LENGTH < length){ + return 1; + } + + // ensure we are currently a slave transmitter + if(TWI_STX != twi_state){ + return 2; + } + + // set length and copy data into tx buffer + twi_txBufferLength = length; + for(i = 0; i < length; ++i){ + twi_txBuffer[i] = data[i]; + } + + return 0; +} + +/* + * Function twi_attachSlaveRxEvent + * Desc sets function called before a slave read operation + * Input function: callback function to use + * Output none + */ +void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) ) +{ + twi_onSlaveReceive = function; +} + +/* + * Function twi_attachSlaveTxEvent + * Desc sets function called before a slave write operation + * Input function: callback function to use + * Output none + */ +void twi_attachSlaveTxEvent( void (*function)(void) ) +{ + twi_onSlaveTransmit = function; +} + +/* + * Function twi_reply + * Desc sends byte or readys receive line + * Input ack: byte indicating to ack or to nack + * Output none + */ +void twi_reply(uint8_t ack) +{ + // transmit master read ready signal, with or without ack + if(ack){ + TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); + }else{ + TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); + } +} + +/* + * Function twi_stop + * Desc relinquishes bus master status + * Input none + * Output none + */ +void twi_stop(void) +{ + // send stop condition + TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); + + // wait for stop condition to be exectued on bus + // TWINT is not set after a stop condition! + while(TWCR & _BV(TWSTO)){ + continue; + } + + // update twi state + twi_state = TWI_READY; +} + +/* + * Function twi_releaseBus + * Desc releases bus control + * Input none + * Output none + */ +void twi_releaseBus(void) +{ + // release bus + TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); + + // update twi state + twi_state = TWI_READY; +} + +SIGNAL(TWI_vect) +{ + switch(TW_STATUS){ + // All Master + case TW_START: // sent start condition + case TW_REP_START: // sent repeated start condition + // copy device address and r/w bit to output register and ack + TWDR = twi_slarw; + twi_reply(1); + break; + + // Master Transmitter + case TW_MT_SLA_ACK: // slave receiver acked address + case TW_MT_DATA_ACK: // slave receiver acked data + // if there is data to send, send it, otherwise stop + if(twi_masterBufferIndex < twi_masterBufferLength){ + // copy data to output register and ack + TWDR = twi_masterBuffer[twi_masterBufferIndex++]; + twi_reply(1); + }else{ + twi_stop(); + } + break; + case TW_MT_SLA_NACK: // address sent, nack received + twi_error = TW_MT_SLA_NACK; + twi_stop(); + break; + case TW_MT_DATA_NACK: // data sent, nack received + twi_error = TW_MT_DATA_NACK; + twi_stop(); + break; + case TW_MT_ARB_LOST: // lost bus arbitration + twi_error = TW_MT_ARB_LOST; + twi_releaseBus(); + break; + + // Master Receiver + case TW_MR_DATA_ACK: // data received, ack sent + // put byte into buffer + twi_masterBuffer[twi_masterBufferIndex++] = TWDR; + case TW_MR_SLA_ACK: // address sent, ack received + // ack if more bytes are expected, otherwise nack + if(twi_masterBufferIndex < twi_masterBufferLength){ + twi_reply(1); + }else{ + twi_reply(0); + } + break; + case TW_MR_DATA_NACK: // data received, nack sent + // put final byte into buffer + twi_masterBuffer[twi_masterBufferIndex++] = TWDR; + case TW_MR_SLA_NACK: // address sent, nack received + twi_stop(); + break; + // TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case + + // Slave Receiver + case TW_SR_SLA_ACK: // addressed, returned ack + case TW_SR_GCALL_ACK: // addressed generally, returned ack + case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack + case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack + // enter slave receiver mode + twi_state = TWI_SRX; + // indicate that rx buffer can be overwritten and ack + twi_rxBufferIndex = 0; + twi_reply(1); + break; + case TW_SR_DATA_ACK: // data received, returned ack + case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack + // if there is still room in the rx buffer + if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ + // put byte in buffer and ack + twi_rxBuffer[twi_rxBufferIndex++] = TWDR; + twi_reply(1); + }else{ + // otherwise nack + twi_reply(0); + } + break; + case TW_SR_STOP: // stop or repeated start condition received + // put a null char after data if there's room + if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ + twi_rxBuffer[twi_rxBufferIndex] = '\0'; + } + // callback to user defined callback + twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex); + // ack future responses + twi_reply(1); + // leave slave receiver state + twi_state = TWI_READY; + break; + case TW_SR_DATA_NACK: // data received, returned nack + case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack + // nack back at master + twi_reply(0); + break; + + // Slave Transmitter + case TW_ST_SLA_ACK: // addressed, returned ack + case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack + // enter slave transmitter mode + twi_state = TWI_STX; + // ready the tx buffer index for iteration + twi_txBufferIndex = 0; + // set tx buffer length to be zero, to verify if user changes it + twi_txBufferLength = 0; + // request for txBuffer to be filled and length to be set + // note: user must call twi_transmit(bytes, length) to do this + twi_onSlaveTransmit(); + // if they didn't change buffer & length, initialize it + if(0 == twi_txBufferLength){ + twi_txBufferLength = 1; + twi_txBuffer[0] = 0x00; + } + // transmit first byte from buffer, fall + case TW_ST_DATA_ACK: // byte sent, ack returned + // copy data to output register + TWDR = twi_txBuffer[twi_txBufferIndex++]; + // if there is more to send, ack, otherwise nack + if(twi_txBufferIndex < twi_txBufferLength){ + twi_reply(1); + }else{ + twi_reply(0); + } + break; + case TW_ST_DATA_NACK: // received nack, we are done + case TW_ST_LAST_DATA: // received ack, but we are done already! + // ack future responses + twi_reply(1); + // leave slave receiver state + twi_state = TWI_READY; + break; + + // All + case TW_NO_INFO: // no state information + break; + case TW_BUS_ERROR: // bus error, illegal stop/start + twi_error = TW_BUS_ERROR; + twi_stop(); + break; + } +} + diff --git a/libraries/Wire/utility/twi.h b/libraries/Wire/utility/twi.h new file mode 100755 index 0000000..1258d8d --- /dev/null +++ b/libraries/Wire/utility/twi.h @@ -0,0 +1,57 @@ +/* + twi.h - TWI/I2C library for Wiring & Arduino + 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 twi_h +#define twi_h + + #include + + //#define ATMEGA8 + + #ifndef CPU_FREQ + #define CPU_FREQ 16000000L + #endif + + #ifndef TWI_FREQ + #define TWI_FREQ 100000L + #endif + + #ifndef TWI_BUFFER_LENGTH + #define TWI_BUFFER_LENGTH 32 + #endif + + #define TWI_READY 0 + #define TWI_MRX 1 + #define TWI_MTX 2 + #define TWI_SRX 3 + #define TWI_STX 4 + + void twi_init(void); + 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); + void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) ); + void twi_attachSlaveTxEvent( void (*)(void) ); + void twi_reply(uint8_t); + void twi_stop(void); + void twi_releaseBus(void); + +#endif + -- cgit v1.2.3-18-g5258