diff options
author | David A. Mellis <d.mellis@arduino.cc> | 2007-10-06 13:04:46 +0000 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2007-10-06 13:04:46 +0000 |
commit | 9e48dfc4fd53a2bc53367568077fbea44f88eda2 (patch) | |
tree | 356854e8239e0f0676bcf6105b7ea6a53cb9a880 /libraries | |
parent | 179fcdbda432ff33a921a70994087b08b2a79caa (diff) |
Moving libraries out from inside targets and creating bootloaders directory.
Diffstat (limited to 'libraries')
36 files changed, 3802 insertions, 0 deletions
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 <avr/eeprom.h> +#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 <inttypes.h> + +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 <EEPROM.h> + +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 <EEPROM.h> + +// 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 <EEPROM.h> + +// 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/Firmata/Firmata.cpp b/libraries/Firmata/Firmata.cpp new file mode 100644 index 0000000..de58a41 --- /dev/null +++ b/libraries/Firmata/Firmata.cpp @@ -0,0 +1,128 @@ +/* + Firmata.cpp - Firmata library + Copyright (c) 2007 Free Software Foundation. All right reserved. + Written by Hans-Christoph Steiner <hans@at.or.at> + + 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 +//****************************************************************************** + +extern "C" { + // AVR LibC Includes + #include <inttypes.h> + #include <stdlib.h> + + // Wiring Core Includes + #include "WConstants.h" +} + +#include "Firmata.h" +#include "EEPROM.h" +#include "HardwareSerial.h" + +//****************************************************************************** +//* Definitions +//****************************************************************************** + +//****************************************************************************** +//* Constructors +//****************************************************************************** + +FirmataClass::FirmataClass() +{ + // TODO: init serial here + // TODO: printVersion +} + +//****************************************************************************** +//* Private Methods +//****************************************************************************** +// resets the system state upon a SYSTEM_RESET message from the host software +void FirmataClass::systemReset(void) +{ + // TODO automatically call this in response to SYSTEM_RESET + // TODO reset EEPROM to 0 here +} + +//****************************************************************************** +//* Public Methods +//****************************************************************************** + +// output type of message that is next on the queue +int FirmataClass::available(void) +{ + // TODO output next available message type, or -1 if nothing +} + + +// output the protocol version message to the serial port +void FirmataClass::printVersion() { + Serial.print(REPORT_VERSION, BYTE); + Serial.print(FIRMATA_MINOR_VERSION, BYTE); + Serial.print(FIRMATA_MAJOR_VERSION, BYTE); +} + +// send an analog message +void FirmataClass::sendAnalog(int pin, int value) +{ + // pin can only be 0-15, so chop higher bits + Serial.print(ANALOG_MESSAGE | (pin & 0xF), BYTE); + Serial.print(value % 128, BYTE); + Serial.print(value >> 7, BYTE); +} + +// send a single digital pin in a digital message +void FirmataClass::sendDigital(int pin, int value) +{ + // TODO add single pin digital messages to the protocol +} + +// send 14-bits in a single digital message +void FirmataClass::sendDigitalPortPair(int port, int value) +{ + // 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. + + // TODO: some math needs to happen for pin > 14 since MIDI channels are used + Serial.print(DIGITAL_MESSAGE | (port & 0xF),BYTE); + Serial.print(value % 128, BYTE); // Tx pins 0-6 + Serial.print(value >> 7, BYTE); // Tx pins 7-13 +} + +// Internal Actions///////////////////////////////////////////////////////////// + +void FirmataClass::loadState(void) +{ + // TODO load state from EEPROM +} + +void FirmataClass::saveState(void) +{ + // TODO save state to EEPROM +} + +void FirmataClass::resetState(void) +{ + // TODO reset state bytes in EEPROM +} + +// make one instance for the user to use +FirmataClass Firmata; diff --git a/libraries/Firmata/Firmata.h b/libraries/Firmata/Firmata.h new file mode 100644 index 0000000..1c6604b --- /dev/null +++ b/libraries/Firmata/Firmata.h @@ -0,0 +1,95 @@ +/* + Firmata.h - Firmata library + Copyright (c) 2007 Free Software Foundation. All right reserved. + Written by Hans-Christoph Steiner <hans@at.or.at> + + 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 Firmata_h +#define Firmata_h + +#include <inttypes.h> + +class FirmataClass +{ +private: + void systemReset(void); +public: + FirmataClass(); + int available(void); + // serial receive actions + // send serial messages + void printVersion(void); + void sendAnalog(int, int); + void sendDigital(int, int); + void sendDigitalPortPair(int, int); + // internal actions + void loadState(void); + void saveState(void); + void resetState(void); +}; + +extern FirmataClass Firmata; + +/*============================================================================== + * MACROS + *============================================================================*/ + +/* 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 1 // for non-compatible changes +#define FIRMATA_MINOR_VERSION 0 // for backwards compatible changes + +// total number of pins currently supported +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_DIGITAL_PINS 14 + +// for comparing along with INPUT and OUTPUT +#define PWM 2 + +// for selecting digital inputs +#define PB 2 // digital input, pins 8-13 +#define PC 3 // analog input port +#define PD 4 // digital input, pins 0-7 + +#define MAX_DATA_BYTES 2 // max number of data bytes in non-SysEx messages +// message command bytes +#define DIGITAL_MESSAGE 0x90 // send data for a digital pin +#define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM) +//#define PULSE_MESSAGE 0xA0 // proposed pulseIn/Out message (SysEx) +//#define SHIFTOUT_MESSAGE 0xB0 // proposed shiftOut message (SysEx) +#define REPORT_ANALOG_PIN 0xC0 // enable analog input by pin # +#define REPORT_DIGITAL_PORTS 0xD0 // enable digital input by port pair +#define START_SYSEX 0xF0 // start a MIDI SysEx message +#define SET_DIGITAL_PIN_MODE 0xF4 // set a digital pin to INPUT or OUTPUT +#define END_SYSEX 0xF7 // end a MIDI SysEx message +#define REPORT_VERSION 0xF9 // report firmware version +#define SYSTEM_RESET 0xFF // reset from MIDI + +// these are used for EEPROM reading and writing +#define ANALOGINPUTSTOREPORT_LOW_BYTE 0x1F0 // analogInputsToReport is an int +#define ANALOGINPUTSTOREPORT_HIGH_BYTE 0x1F1 // analogInputsToReport is an int +#define REPORTDIGITALINPUTS_BYTE 0x1F2 // +#define DIGITALPINSTATUS_LOW_BYTE 0x1F3 // digitalPinStatus is an int +#define DIGITALPINSTATUS_HIGH_BYTE 0x1F4 // digitalPinStatus is an int +#define PWMSTATUS_LOW_BYTE 0x1F5 // pwmStatus is an int +#define PWMSTATUS_HIGH_BYTE 0x1F6 // pwmStatus is an int + + +#endif /* Firmata_h */ + diff --git a/libraries/Firmata/keywords.txt b/libraries/Firmata/keywords.txt new file mode 100644 index 0000000..a241593 --- /dev/null +++ b/libraries/Firmata/keywords.txt @@ -0,0 +1,18 @@ +####################################### +# Syntax Coloring Map For Firmata +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Firmata KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/Matrix/Matrix.cpp b/libraries/Matrix/Matrix.cpp new file mode 100755 index 0000000..c86c4c9 --- /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 <inttypes.h> + #include <stdlib.h> + + // Wiring Core Includes + #undef abs + #include "WConstants.h" + + // Wiring Core Prototypes + //void pinMode(uint8_t, uint8_t); + //void digitalWrite(int, uint8_t); +} + +#include "Sprite.h" +#include "Matrix.h" + +/****************************************************************************** + * Definitions + ******************************************************************************/ + +// Matrix registers +#define REG_NOOP 0x00 +#define REG_DIGIT0 0x01 +#define REG_DIGIT1 0x02 +#define REG_DIGIT2 0x03 +#define REG_DIGIT3 0x04 +#define REG_DIGIT4 0x05 +#define REG_DIGIT5 0x06 +#define REG_DIGIT6 0x07 +#define REG_DIGIT7 0x08 +#define REG_DECODEMODE 0x09 +#define REG_INTENSITY 0x0A +#define REG_SCANLIMIT 0x0B +#define REG_SHUTDOWN 0x0C +#define REG_DISPLAYTEST 0x0F + +/****************************************************************************** + * Constructors + ******************************************************************************/ + +Matrix::Matrix(uint8_t data, uint8_t clock, uint8_t load, uint8_t screens /* = 1 */) +{ + // record pins for sw spi + _pinData = data; + _pinClock = clock; + _pinLoad = load; + + // set ddr for sw spi pins + pinMode(_pinClock, OUTPUT); + pinMode(_pinData, OUTPUT); + pinMode(_pinLoad, OUTPUT); + + // allocate screenbuffers + _screens = screens; + _buffer = (uint8_t*)calloc(_screens, 64); + _maximumX = (_screens * 8); + + // initialize registers + clear(); // clear display + setScanLimit(0x07); // use all rows/digits + setBrightness(0x0F); // maximum brightness + setRegister(REG_SHUTDOWN, 0x01); // normal operation + setRegister(REG_DECODEMODE, 0x00); // pixels not integers + setRegister(REG_DISPLAYTEST, 0x00); // not in test mode +} + +/****************************************************************************** + * MAX7219 SPI + ******************************************************************************/ + +// sends a single byte by sw spi (no latching) +void Matrix::putByte(uint8_t data) +{ + uint8_t i = 8; + uint8_t mask; + while(i > 0) { + mask = 0x01 << (i - 1); // get bitmask + digitalWrite(_pinClock, LOW); // tick + if (data & mask){ // choose bit + digitalWrite(_pinData, HIGH); // set 1 + }else{ + digitalWrite(_pinData, LOW); // set 0 + } + digitalWrite(_pinClock, HIGH); // tock + --i; // move to lesser bit + } +} + +// sets register to a byte value for all screens +void Matrix::setRegister(uint8_t reg, uint8_t data) +{ + digitalWrite(_pinLoad, HIGH); // begin + for(uint8_t i = 0; i < _screens; ++i){ + putByte(reg); // specify register + putByte(data); // send data + } + digitalWrite(_pinLoad, LOW); // latch in data + digitalWrite(_pinLoad, HIGH); // 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, HIGH); // begin + for(uint8_t i = 0; i < _screens; ++i){ + putByte(8 - row); // specify register + putByte(_buffer[row + (8 * i)]); // send data + } + digitalWrite(_pinLoad, LOW); // latch in data + digitalWrite(_pinLoad, HIGH); // 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 <inttypes.h> + +class Sprite; + +class Matrix +{ + private: + uint8_t _pinData; + uint8_t _pinClock; + uint8_t _pinLoad; + + uint8_t* _buffer; + uint8_t _screens; + uint8_t _maximumX; + + void putByte(uint8_t); + void setRegister(uint8_t, uint8_t); + void syncRow(uint8_t); + + void setScanLimit(uint8_t); + + void buffer(uint8_t, uint8_t, uint8_t); + public: + Matrix(uint8_t, uint8_t, uint8_t, uint8_t = 1); + void setBrightness(uint8_t); + void write(uint8_t, uint8_t, uint8_t); + void write(uint8_t, uint8_t, Sprite); + void clear(void); +}; + +#endif + 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..b8e1971 --- /dev/null +++ b/libraries/Matrix/examples/hello_matrix/hello_matrix.pde @@ -0,0 +1,39 @@ +// Hello Matrix +// by Nicholas Zambetti <http://www.zambetti.com> + +// Demonstrates the use of the Matrix library +// For MAX7219 LED Matrix Controllers +// Blinks welcoming face on screen + +// Created 13 February 2006 + +/* create a new Matrix instance + pin 0: data (din) + pin 1: load (load) + pin 2: clock (clk) +*/ +Matrix myMatrix = Matrix(0, 2, 1); + +void setup() +{ +} + +void loop() +{ + myMatrix.clear(); // clear display + + delay(1000); + + // turn some pixels on + myMatrix.write(1, 5, HIGH); + myMatrix.write(2, 2, HIGH); + myMatrix.write(2, 6, HIGH); + myMatrix.write(3, 6, HIGH); + myMatrix.write(4, 6, HIGH); + myMatrix.write(5, 2, HIGH); + myMatrix.write(5, 6, HIGH); + myMatrix.write(6, 5, HIGH); + + delay(1000); +} + 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..f6099fc --- /dev/null +++ b/libraries/Matrix/examples/sprite_animation/sprite_animation.pde @@ -0,0 +1,45 @@ +// Sprite Animation +// by Nicholas Zambetti <http://www.zambetti.com> + +// Demonstrates the use of the Matrix & Sprite libraries +// Displays animated waveform graphic on screen + +// Created 29 March 2006 + +/* create a new Matrix instance + pin 0: data (din) + pin 1: load (load) + pin 2: clock (clk) +*/ +Matrix myMatrix = Matrix(0, 2, 1); + +/* create a new Sprite instance + 8 pixels wide, 4 pixels tall +*/ +Sprite wave = Sprite( + 8, 4, + B00011000, + B00100100, + B01000010, + B10000001 +); + +void setup() +{ +} + +int x = 0; + +void loop() +{ + myMatrix.write(x, 2, wave); // place sprite on screen + myMatrix.write(x - 8, 2, wave); // place sprite again, elsewhere on screen + delay(75); // wait a little bit + myMatrix.clear(); // clear the screen for next animation frame + if(x == 8) // if reached end of animation sequence + { + x = 0; // start from beginning + } + x++; // advance x coordinate to the right +} + 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/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 <inttypes.h> + +class SoftwareSerial +{ + private: + uint8_t _receivePin; + uint8_t _transmitPin; + long _baudRate; + int _bitPeriod; + void printNumber(unsigned long, uint8_t); + public: + SoftwareSerial(uint8_t, uint8_t); + void begin(long); + int read(); + void print(char); + void print(const char[]); + void print(uint8_t); + void print(int); + void print(unsigned int); + void print(long); + void print(unsigned long); + void print(long, int); + void println(void); + void println(char); + void println(const char[]); + void println(uint8_t); + void println(int); + void println(long); + void println(unsigned long); + void println(long, int); +}; + +#endif + 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 <stdlib.h> +#include <stdarg.h> +//#include <stdio.h> + +#include "Sprite.h" + +void Sprite::init(uint8_t width, uint8_t height) +{ + _width = width >= 8 ? 8 : width; + _height = height >= 8 ? 8 : height; + + // for now, do nothing if this allocation fails. methods that require it + // should silently fail if _buffer is null. + _buffer = (uint8_t *) calloc(_height, 1); +} + +Sprite::Sprite(uint8_t width, uint8_t height) +{ + init(width, height); +} + +Sprite::Sprite(uint8_t width, uint8_t height, uint8_t row, ...) +{ + init(width, height); + + if (!_buffer) return; + + va_list ap; + va_start(ap, row); + + int y = 0; + + for (y = 0; ; y++) { + for (int x = 0; x < width && x < 8; x++) + write(x, y, (row >> (width - x - 1)) & 0x01); + + if (y == height - 1) + break; + + row = va_arg(ap, int); // using '...' promotes uint8_t to int + } + + va_end(ap); +} + +uint8_t Sprite::width() const +{ + return _width; +} + +uint8_t Sprite::height() const +{ + return _height; +} + +void Sprite::write(uint8_t x, uint8_t y, uint8_t value) +{ + if (!_buffer) return; + + // uint8_t's can't be negative, so don't test for negative x and y. + if (x >= _width || y >= _height) return; + + // we need to bitwise-or the value of the other pixels in the byte with + // the new value, masked and shifted into the proper bits. + _buffer[y] = (_buffer[y] & ~(0x01 << x)) | ((value & 0x01) << x); +} + +uint8_t Sprite::read(uint8_t x, uint8_t y) const +{ + if (!_buffer) return 0; + + // uint8_t's can't be negative, so don't test for negative x and y. + if (x >= _width || y >= _height) return 0; + + return (_buffer[y] >> x) & 0x01; +} 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 <inttypes.h> + +#include "binary.h" + +class Sprite +{ + private: + uint8_t _width; + uint8_t _height; + uint8_t _depth; + uint8_t _ppb; + uint8_t _bpr; + uint8_t _mask; + uint8_t *_buffer; + + void init(uint8_t width, uint8_t height); + public: + Sprite(uint8_t width, uint8_t height); + Sprite(uint8_t width, uint8_t height, uint8_t row, ...); + uint8_t width() const; + uint8_t height() const; + void write(uint8_t x, uint8_t y, uint8_t value); + uint8_t read(uint8_t x, uint8_t y) const; +}; + +#endif 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..b10f94c --- /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) { + // step the motor to step number 0, 1, 2, or 3: + stepMotor(this->step_number % 4); + // 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--; + } + } +} + +/* + * 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..a3aa180 --- /dev/null +++ b/libraries/Stepper/Stepper.h @@ -0,0 +1,86 @@ +/* + 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 + +// include types & constants of Wiring core API +#include "WConstants.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 <Stepper.h> + +// 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..73369b3 --- /dev/null +++ b/libraries/Wire/Wire.cpp @@ -0,0 +1,262 @@ +/* + 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 <stdlib.h> + #include <string.h> + #include <inttypes.h> + #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); +} + +void TwoWire::requestFrom(uint8_t address, uint8_t quantity) +{ + // clamp to buffer length + if(quantity > BUFFER_LENGTH){ + quantity = BUFFER_LENGTH; + } + // perform blocking read into buffer + twi_readFrom(address, rxBuffer, quantity); + // set rx buffer iterator vars + rxBufferIndex = 0; + rxBufferLength = quantity; +} + +void TwoWire::requestFrom(int address, int quantity) +{ + 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); +} + +void TwoWire::endTransmission(void) +{ + // transmit buffer (blocking) + twi_writeTo(txAddress, txBuffer, txBufferLength, 1); + // reset tx buffer iterator vars + txBufferIndex = 0; + txBufferLength = 0; + // indicate that we are done transmitting + transmitting = 0; +} + +// 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..9c91b9b --- /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 <inttypes.h> + +#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); + void endTransmission(void); + void requestFrom(uint8_t, uint8_t); + void 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 <http://www.zambetti.com> +// and James Tichenor <http://www.jamestichenor.net> + +// Demonstrates use of the Wire library reading data from the +// Devantech Utrasonic Rangers SFR08 and SFR10 + +// Created 29 April 2006 + +#include <Wire.h> + +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 <http://www.zambetti.com> +// and Shawn Bonkowski <http://people.interaction-ivrea.it/s.bonkowski/> + +// Demonstrates use of the Wire library +// Controls AD5171 digital potentiometer via I2C/TWI + +// Created 31 March 2006 + +#include <Wire.h> + +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 <http://www.zambetti.com> + +// 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 <Wire.h> + +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 <http://www.zambetti.com> + +// 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 <Wire.h> + +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 <http://www.zambetti.com> + +// 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 <Wire.h> + +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 <http://www.zambetti.com> + +// 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 <Wire.h> + +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..2235b0b --- /dev/null +++ b/libraries/Wire/utility/twi.c @@ -0,0 +1,449 @@ +/* + 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 <math.h> +#include <stdlib.h> +#include <inttypes.h> +#include <avr/io.h> +#include <avr/interrupt.h> +#include <avr/signal.h> +#include <compat/twi.h> + +#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; + +/* + * 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__) + // 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 byte: 0 ok, 1 length too long for buffer + */ +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 1; + } + + // wait until twi is ready, become master receiver + while(TWI_READY != twi_state){ + continue; + } + twi_state = TWI_MRX; + + // 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; + } + + // copy twi buffer to data + for(i = 0; i < length; ++i){ + data[i] = twi_masterBuffer[i]; + } + + return 0; +} + +/* + * 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 byte: 0 ok, 1 length too long for buffer + */ +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; + + // 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; + } + + return 0; +} + +/* + * 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(SIG_2WIRE_SERIAL) +{ + 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 + case TW_MT_DATA_NACK: // data sent, nack received + twi_stop(); + break; + case TW_MT_ARB_LOST: // lost bus arbitration + 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_stop(); + break; + } +} + diff --git a/libraries/Wire/utility/twi.h b/libraries/Wire/utility/twi.h new file mode 100755 index 0000000..7ccbf5c --- /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 <inttypes.h> + + //#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 + |