Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial Commit

  • Loading branch information...
commit f0028328b147fe253f6d5dc87fa68647ca823ba4 1 parent c63f30f
J.P. McGlinn authored May 08, 2012
154  E2PROM.cpp
... ...
@@ -0,0 +1,154 @@
  1
+/*
  2
+  E2PROM.cpp - Extended EEPROM library for Arduino
  3
+  Copyright (c) 2006 David A. Mellis.  All right reserved.
  4
+  Copyright (c) 2012 J.P. McGlinn. All Rights reserved
  5
+
  6
+  This library is free software; you can redistribute it and/or
  7
+  modify it under the terms of the GNU Lesser General Public
  8
+  License as published by the Free Software Foundation; either
  9
+  version 2.1 of the License, or (at your option) any later version.
  10
+
  11
+  This library is distributed in the hope that it will be useful,
  12
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14
+  Lesser General Public License for more details.
  15
+
  16
+  You should have received a copy of the GNU Lesser General Public
  17
+  License along with this library; if not, write to the Free Software
  18
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  19
+*/
  20
+
  21
+/******************************************************************************
  22
+ * Includes
  23
+ ******************************************************************************/
  24
+
  25
+#include <avr/eeprom.h>
  26
+#include "E2PROM.h"
  27
+
  28
+/******************************************************************************
  29
+ * Definitions
  30
+ ******************************************************************************/
  31
+
  32
+/******************************************************************************
  33
+ * Constructors
  34
+ ******************************************************************************/
  35
+E2PROM::E2PROM(){
  36
+    setLineEnding(0);
  37
+}
  38
+
  39
+/******************************************************************************
  40
+ * Private Methods
  41
+ ******************************************************************************/
  42
+
  43
+void E2PROM::printASCII(char * buffer, Stream &output){
  44
+    for(int i = 0; i < 16; i++){
  45
+        if(i == 8)
  46
+            output.print(" ");
  47
+        
  48
+        if(buffer[i] > 31 and buffer[i] < 127){
  49
+            output.print(buffer[i]);
  50
+        }else{
  51
+            output.print(".");
  52
+        }
  53
+    }
  54
+}
  55
+
  56
+/******************************************************************************
  57
+ * User API
  58
+ ******************************************************************************/
  59
+
  60
+void E2PROM::setLineEnding(char *lineEnding){
  61
+    if(lineEnding){
  62
+        strncpy(_lineEnding, lineEnding, MAXLENLINEENDING);
  63
+    }else{
  64
+        strncpy(_lineEnding, "\n", MAXLENLINEENDING);
  65
+    }
  66
+}
  67
+
  68
+uint8_t E2PROM::read(int address)
  69
+{
  70
+	return eeprom_read_byte((unsigned char *) address);
  71
+}
  72
+
  73
+void E2PROM::write(int address, uint8_t value)
  74
+{
  75
+	eeprom_write_byte((unsigned char *) address, value);
  76
+}
  77
+
  78
+int E2PROM::set(byte value, unsigned int start, unsigned int count){
  79
+    if((start + count) - 1 > E2END)
  80
+        return 0;
  81
+    
  82
+    for(unsigned int i = start; i < start + count; i++)
  83
+        write(i, value);
  84
+    
  85
+    return count;
  86
+};
  87
+
  88
+void E2PROM::print(Stream &output){
  89
+    char buffer[16];
  90
+    char valuePrint[4];
  91
+    byte value;
  92
+    unsigned int address;
  93
+    uint8_t trailingSpace = 2;
  94
+    
  95
+    output.print("Dumping "); output.print(E2END + 1);
  96
+    output.print(" bytes from EEPROM.");
  97
+    output.print(_lineEnding);
  98
+    output.print("baseAddr ");
  99
+    for(int x = 0; x < 2; x++){
  100
+        output.print(" ");
  101
+        for(int y = 0; y < 25; y++)
  102
+            output.print("=");
  103
+    }
  104
+    
  105
+    // E2END is a macro defined as the last EEPROM address
  106
+    // (1023 for ATMEGA328P)
  107
+    for(address = 0; address <= E2END; address++){
  108
+        // read a byte from the current address of the EEPROM
  109
+        value = EEPROM.read(address);
  110
+        
  111
+        // add space between two sets of 8 bytes
  112
+        if(address % 8 == 0)
  113
+            output.print("  ");
  114
+        
  115
+        // newline and address for every 16 bytes
  116
+        if(address % 16 == 0){
  117
+            //print the buffer
  118
+            if(address > 0 && address % 16 == 0)
  119
+                printASCII(buffer, output);
  120
+            
  121
+            sprintf(buffer, "%s 0x%05X: ",_lineEnding, address);
  122
+            output.print(buffer);
  123
+            
  124
+            //clear the buffer for the next data block
  125
+            memset (buffer, 32, 16);
  126
+        }
  127
+        
  128
+        // save the value in temporary storage
  129
+        buffer[address%16] = value;
  130
+        
  131
+        // print the formatted value
  132
+        sprintf(valuePrint, " %02X", value);
  133
+        output.print(valuePrint);
  134
+    }
  135
+    
  136
+    if(address % 16 > 0){
  137
+        if(address % 16 < 9)
  138
+            trailingSpace += 2;
  139
+        
  140
+        trailingSpace += (16 - address % 16) * 3;
  141
+    }
  142
+    
  143
+    for(int i = trailingSpace; i > 0; i--)
  144
+        output.print(" ");
  145
+    
  146
+    //last line of data and a new line
  147
+    printASCII(buffer, output);
  148
+    output.print(_lineEnding);
  149
+}
  150
+
  151
+
  152
+
  153
+
  154
+E2PROM EEPROM;
54  E2PROM.h
... ...
@@ -0,0 +1,54 @@
  1
+/*
  2
+  E2PROM.h - Extended EEPROM library for Arduino
  3
+  Copyright (c) 2006 David A. Mellis.  All right reserved.
  4
+  Copyright (c) 2012 J.P. McGlinn. All Rights reserved
  5
+
  6
+  This library is free software; you can redistribute it and/or
  7
+  modify it under the terms of the GNU Lesser General Public
  8
+  License as published by the Free Software Foundation; either
  9
+  version 2.1 of the License, or (at your option) any later version.
  10
+
  11
+  This library is distributed in the hope that it will be useful,
  12
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14
+  Lesser General Public License for more details.
  15
+
  16
+  You should have received a copy of the GNU Lesser General Public
  17
+  License along with this library; if not, write to the Free Software
  18
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  19
+*/
  20
+
  21
+#ifndef E2PROM_h
  22
+#define E2PROM_h
  23
+
  24
+#include <Arduino.h>
  25
+#include <inttypes.h>
  26
+#include <Stream.h>
  27
+
  28
+#define MAXLENLINEENDING 7
  29
+
  30
+class E2PROM
  31
+{
  32
+  public:
  33
+    E2PROM();    
  34
+    
  35
+    uint8_t read(int);
  36
+    void write(int, uint8_t);
  37
+
  38
+    int set(byte value, unsigned int count){ return set(value, 0, count); }
  39
+    int set(byte value, unsigned int start, unsigned int count);
  40
+
  41
+    void setLineEnding(char *lineEnding);
  42
+    
  43
+    void print() { print(Serial); }
  44
+    void print(Stream &output);
  45
+  private:
  46
+    void printASCII(char *buffer, Stream &output);
  47
+    
  48
+    char _lineEnding[MAXLENLINEENDING + 1];
  49
+};
  50
+
  51
+extern E2PROM EEPROM;
  52
+
  53
+#endif
  54
+
BIN  examples/.DS_Store
Binary file not shown
30  examples/e2prom_clear/e2prom_clear.ino
... ...
@@ -0,0 +1,30 @@
  1
+/*
  2
+ * EEPROM Clear
  3
+ *
  4
+ * Sets all of the bytes of the EEPROM to 0.
  5
+ * This example code is in the public domain.
  6
+ *
  7
+ * Example part of E2PROM, Extended EEPROM library.
  8
+ * 
  9
+ */
  10
+
  11
+#include <E2PROM.h>
  12
+
  13
+//Light the LED on digital 13
  14
+#define FINISHLED 13
  15
+
  16
+void setup()
  17
+{
  18
+  pinMode(FINISHLED, OUTPUT);
  19
+  
  20
+  // write a 0 to all bytes of the EEPROM
  21
+  EEPROM.set(0, E2END + 1);
  22
+    
  23
+  // turn the LED on when we're done
  24
+  digitalWrite(FINISHLED, HIGH);
  25
+  
  26
+}
  27
+
  28
+void loop()
  29
+{
  30
+}
40  examples/e2prom_dump/e2prom_dump.ino
... ...
@@ -0,0 +1,40 @@
  1
+/*
  2
+ * EEPROM Dump
  3
+ *
  4
+ * Prints the output of all bytes of EEPROM memory
  5
+ *
  6
+ * Example part of E2PROM, Extended EEPROM library.
  7
+ * Written by J.P. McGlinn for Black Cat Microelectronics.  
  8
+ *
  9
+ * EEPROM Dump is meant as a debugging tool. This is an expensive
  10
+ * function to include, taking about 2.5k of flash above just
  11
+ * whatever including the Serial interface.
  12
+ *
  13
+ * E2PROM.print can print to different Streams, so it should
  14
+ * work with software serial ports and I even used it with
  15
+ * the EthernetClient. Just pass the Stream as a parameter
  16
+ * as seen in the example. If no Stream is passed, Serial is assumed.
  17
+ * 
  18
+ */
  19
+
  20
+
  21
+#include <E2PROM.h>
  22
+
  23
+void setup(){
  24
+  // Should open the stream you will be using, in this case the first
  25
+  // hardware serial port. I like fast, so 57,600 baud!
  26
+  Serial.begin(57600);
  27
+  
  28
+  // If you don't like the default line endings, change them. If you
  29
+  // need a really long line ending you might have to adjust the max
  30
+  // defined size in E2EPROM.h. Factory setting allows 7 characters.
  31
+  //EEPROM.setLineEnding(" BOB\n");
  32
+  
  33
+  // now just tell the EEPROM to print, optionally include a stream
  34
+  // to send the printed output to. In this example we specify Serial,
  35
+  // even though it is the default.
  36
+  EEPROM.print(Serial);
  37
+}
  38
+
  39
+void loop(){
  40
+}
42  examples/e2prom_read/e2prom_read.ino
... ...
@@ -0,0 +1,42 @@
  1
+/*
  2
+ * EEPROM Read
  3
+ *
  4
+ * Reads the value of each byte of the EEPROM and prints it 
  5
+ * to the computer.
  6
+ * This example code is in the public domain.
  7
+ *
  8
+ * Example part of E2PROM, Extended EEPROM library.
  9
+ * 
  10
+ */
  11
+
  12
+#include <E2PROM.h>
  13
+
  14
+// start reading from the first byte (address 0) of the EEPROM
  15
+int address = 0;
  16
+byte value;
  17
+
  18
+void setup()
  19
+{
  20
+  Serial.begin(9600);
  21
+}
  22
+
  23
+void loop()
  24
+{
  25
+  // read a byte from the current address of the EEPROM
  26
+  value = EEPROM.read(address);
  27
+  
  28
+  Serial.print(address);
  29
+  Serial.print("\t");
  30
+  Serial.print(value, DEC);
  31
+  Serial.println();
  32
+  
  33
+  // advance to the next address of the EEPROM
  34
+  address = address + 1;
  35
+  
  36
+  // there are only E2END + 1 bytes of EEPROM, from 0 to E2END, so if we're
  37
+  // on address E2END + 1, wrap around to address 0
  38
+  if (address == E2END + 1)
  39
+    address = 0;
  40
+    
  41
+  delay(500);
  42
+}
51  examples/e2prom_write/e2prom_write.ino
... ...
@@ -0,0 +1,51 @@
  1
+/*
  2
+ * EEPROM Write
  3
+ *
  4
+ * Stores values read from analog input 0 into the EEPROM.
  5
+ * These values will stay in the EEPROM when the board is
  6
+ * turned off and may be retrieved later by another sketch.
  7
+ */
  8
+
  9
+#include <E2PROM.h>
  10
+
  11
+//Light the LED on digital 13
  12
+#define FINISHLED 13
  13
+
  14
+// the current address in the EEPROM (i.e. which byte
  15
+// we're going to write to next)
  16
+int addr = 0;
  17
+
  18
+void setup()
  19
+{
  20
+  pinMode(FINISHLED, OUTPUT);
  21
+}
  22
+
  23
+void loop()
  24
+{
  25
+  // need to divide by 4 because analog inputs range from
  26
+  // 0 to 1023 and each byte of the EEPROM can only hold a
  27
+  // value from 0 to 255.
  28
+  int val = analogRead(0) / 4;
  29
+  
  30
+  // write the value to the appropriate byte of the EEPROM.
  31
+  // these values will remain there when the board is
  32
+  // turned off.
  33
+  EEPROM.write(addr, val);
  34
+ 
  35
+  // advance to the next address.
  36
+  
  37
+  addr = addr + 1;
  38
+  
  39
+  // there are E2END + 1 bytes in the EEPROM, so stop when
  40
+  // we get to E2END + 1. No need to wear out the EEPROM
  41
+  if (addr == E2END + 1){
  42
+    digitalWrite(FINISHLED, HIGH);  //indicate that we made it
  43
+    while(1){}
  44
+    digitalWrite(FINISHLED, LOW); //prove that it's really not getting here 
  45
+  }
  46
+  
  47
+  delay(100);
  48
+  
  49
+  //blink the LED while we're working
  50
+  digitalWrite(FINISHLED, addr % 2);
  51
+}
23  keywords.txt
... ...
@@ -0,0 +1,23 @@
  1
+#######################################
  2
+# Syntax Coloring Map For Ultrasound
  3
+#######################################
  4
+
  5
+#######################################
  6
+# Datatypes (KEYWORD1)
  7
+#######################################
  8
+
  9
+EEPROM	KEYWORD1
  10
+E2PROM	KEYWORD1
  11
+
  12
+#######################################
  13
+# Methods and Functions (KEYWORD2)
  14
+#######################################
  15
+read	KEYWORD2
  16
+write	KEYWORD2
  17
+set	KEYWORD2
  18
+print	KEYWORD2
  19
+setLineEnding	KEYWORD2
  20
+#######################################
  21
+# Constants (LITERAL1)
  22
+#######################################
  23
+

0 notes on commit f002832

Please sign in to comment.
Something went wrong with that request. Please try again.