Permalink
Browse files

Initial Commit

  • Loading branch information...
1 parent c63f30f commit f0028328b147fe253f6d5dc87fa68647ca823ba4 @jpbot jpbot committed May 9, 2012
View
@@ -0,0 +1,154 @@
+/*
+ E2PROM.cpp - Extended EEPROM library for Arduino
+ Copyright (c) 2006 David A. Mellis. All right reserved.
+ Copyright (c) 2012 J.P. McGlinn. All Rights reserved
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ 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 "E2PROM.h"
+
+/******************************************************************************
+ * Definitions
+ ******************************************************************************/
+
+/******************************************************************************
+ * Constructors
+ ******************************************************************************/
+E2PROM::E2PROM(){
+ setLineEnding(0);
+}
+
+/******************************************************************************
+ * Private Methods
+ ******************************************************************************/
+
+void E2PROM::printASCII(char * buffer, Stream &output){
+ for(int i = 0; i < 16; i++){
+ if(i == 8)
+ output.print(" ");
+
+ if(buffer[i] > 31 and buffer[i] < 127){
+ output.print(buffer[i]);
+ }else{
+ output.print(".");
+ }
+ }
+}
+
+/******************************************************************************
+ * User API
+ ******************************************************************************/
+
+void E2PROM::setLineEnding(char *lineEnding){
+ if(lineEnding){
+ strncpy(_lineEnding, lineEnding, MAXLENLINEENDING);
+ }else{
+ strncpy(_lineEnding, "\n", MAXLENLINEENDING);
+ }
+}
+
+uint8_t E2PROM::read(int address)
+{
+ return eeprom_read_byte((unsigned char *) address);
+}
+
+void E2PROM::write(int address, uint8_t value)
+{
+ eeprom_write_byte((unsigned char *) address, value);
+}
+
+int E2PROM::set(byte value, unsigned int start, unsigned int count){
+ if((start + count) - 1 > E2END)
+ return 0;
+
+ for(unsigned int i = start; i < start + count; i++)
+ write(i, value);
+
+ return count;
+};
+
+void E2PROM::print(Stream &output){
+ char buffer[16];
+ char valuePrint[4];
+ byte value;
+ unsigned int address;
+ uint8_t trailingSpace = 2;
+
+ output.print("Dumping "); output.print(E2END + 1);
+ output.print(" bytes from EEPROM.");
+ output.print(_lineEnding);
+ output.print("baseAddr ");
+ for(int x = 0; x < 2; x++){
+ output.print(" ");
+ for(int y = 0; y < 25; y++)
+ output.print("=");
+ }
+
+ // E2END is a macro defined as the last EEPROM address
+ // (1023 for ATMEGA328P)
+ for(address = 0; address <= E2END; address++){
+ // read a byte from the current address of the EEPROM
+ value = EEPROM.read(address);
+
+ // add space between two sets of 8 bytes
+ if(address % 8 == 0)
+ output.print(" ");
+
+ // newline and address for every 16 bytes
+ if(address % 16 == 0){
+ //print the buffer
+ if(address > 0 && address % 16 == 0)
+ printASCII(buffer, output);
+
+ sprintf(buffer, "%s 0x%05X: ",_lineEnding, address);
+ output.print(buffer);
+
+ //clear the buffer for the next data block
+ memset (buffer, 32, 16);
+ }
+
+ // save the value in temporary storage
+ buffer[address%16] = value;
+
+ // print the formatted value
+ sprintf(valuePrint, " %02X", value);
+ output.print(valuePrint);
+ }
+
+ if(address % 16 > 0){
+ if(address % 16 < 9)
+ trailingSpace += 2;
+
+ trailingSpace += (16 - address % 16) * 3;
+ }
+
+ for(int i = trailingSpace; i > 0; i--)
+ output.print(" ");
+
+ //last line of data and a new line
+ printASCII(buffer, output);
+ output.print(_lineEnding);
+}
+
+
+
+
+E2PROM EEPROM;
View
@@ -0,0 +1,54 @@
+/*
+ E2PROM.h - Extended EEPROM library for Arduino
+ Copyright (c) 2006 David A. Mellis. All right reserved.
+ Copyright (c) 2012 J.P. McGlinn. All Rights reserved
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ 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 E2PROM_h
+#define E2PROM_h
+
+#include <Arduino.h>
+#include <inttypes.h>
+#include <Stream.h>
+
+#define MAXLENLINEENDING 7
+
+class E2PROM
+{
+ public:
+ E2PROM();
+
+ uint8_t read(int);
+ void write(int, uint8_t);
+
+ int set(byte value, unsigned int count){ return set(value, 0, count); }
+ int set(byte value, unsigned int start, unsigned int count);
+
+ void setLineEnding(char *lineEnding);
+
+ void print() { print(Serial); }
+ void print(Stream &output);
+ private:
+ void printASCII(char *buffer, Stream &output);
+
+ char _lineEnding[MAXLENLINEENDING + 1];
+};
+
+extern E2PROM EEPROM;
+
+#endif
+
View
Binary file not shown.
@@ -0,0 +1,30 @@
+/*
+ * EEPROM Clear
+ *
+ * Sets all of the bytes of the EEPROM to 0.
+ * This example code is in the public domain.
+ *
+ * Example part of E2PROM, Extended EEPROM library.
+ *
+ */
+
+#include <E2PROM.h>
+
+//Light the LED on digital 13
+#define FINISHLED 13
+
+void setup()
+{
+ pinMode(FINISHLED, OUTPUT);
+
+ // write a 0 to all bytes of the EEPROM
+ EEPROM.set(0, E2END + 1);
+
+ // turn the LED on when we're done
+ digitalWrite(FINISHLED, HIGH);
+
+}
+
+void loop()
+{
+}
@@ -0,0 +1,40 @@
+/*
+ * EEPROM Dump
+ *
+ * Prints the output of all bytes of EEPROM memory
+ *
+ * Example part of E2PROM, Extended EEPROM library.
+ * Written by J.P. McGlinn for Black Cat Microelectronics.
+ *
+ * EEPROM Dump is meant as a debugging tool. This is an expensive
+ * function to include, taking about 2.5k of flash above just
+ * whatever including the Serial interface.
+ *
+ * E2PROM.print can print to different Streams, so it should
+ * work with software serial ports and I even used it with
+ * the EthernetClient. Just pass the Stream as a parameter
+ * as seen in the example. If no Stream is passed, Serial is assumed.
+ *
+ */
+
+
+#include <E2PROM.h>
+
+void setup(){
+ // Should open the stream you will be using, in this case the first
+ // hardware serial port. I like fast, so 57,600 baud!
+ Serial.begin(57600);
+
+ // If you don't like the default line endings, change them. If you
+ // need a really long line ending you might have to adjust the max
+ // defined size in E2EPROM.h. Factory setting allows 7 characters.
+ //EEPROM.setLineEnding(" BOB\n");
+
+ // now just tell the EEPROM to print, optionally include a stream
+ // to send the printed output to. In this example we specify Serial,
+ // even though it is the default.
+ EEPROM.print(Serial);
+}
+
+void loop(){
+}
@@ -0,0 +1,42 @@
+/*
+ * EEPROM Read
+ *
+ * Reads the value of each byte of the EEPROM and prints it
+ * to the computer.
+ * This example code is in the public domain.
+ *
+ * Example part of E2PROM, Extended EEPROM library.
+ *
+ */
+
+#include <E2PROM.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 E2END + 1 bytes of EEPROM, from 0 to E2END, so if we're
+ // on address E2END + 1, wrap around to address 0
+ if (address == E2END + 1)
+ address = 0;
+
+ delay(500);
+}
@@ -0,0 +1,51 @@
+/*
+ * 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 <E2PROM.h>
+
+//Light the LED on digital 13
+#define FINISHLED 13
+
+// the current address in the EEPROM (i.e. which byte
+// we're going to write to next)
+int addr = 0;
+
+void setup()
+{
+ pinMode(FINISHLED, OUTPUT);
+}
+
+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.
+
+ addr = addr + 1;
+
+ // there are E2END + 1 bytes in the EEPROM, so stop when
+ // we get to E2END + 1. No need to wear out the EEPROM
+ if (addr == E2END + 1){
+ digitalWrite(FINISHLED, HIGH); //indicate that we made it
+ while(1){}
+ digitalWrite(FINISHLED, LOW); //prove that it's really not getting here
+ }
+
+ delay(100);
+
+ //blink the LED while we're working
+ digitalWrite(FINISHLED, addr % 2);
+}
Oops, something went wrong.

0 comments on commit f002832

Please sign in to comment.