mbed I2C slave memory device emulator for use with Raspberry PI

Dependencies:   mbed

Revision:
4:f537311ddc53
Child:
5:b73ea174e997
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/memory.cpp	Tue Oct 14 09:10:45 2014 +0000
@@ -0,0 +1,55 @@
+#include "mbed.h"
+#include "memory.h"
+
+Memory::Memory()
+{
+    reset();
+}
+
+/*
+ * Reset all memory locations to 0.
+ */
+void Memory::reset()
+{
+    for (int i = 0; i < Memory::MEMORY_SIZE; i++) {
+        this->memory[i] = -1;
+    }
+}
+
+/*
+ * Store value in memory
+ */
+void Memory::set(int address, int value)
+{
+    if (address < Memory::MEMORY_SIZE) {
+        this->memory[address] = value;   
+    }
+}
+
+/*
+ * Retrieve value from memory
+ */
+int Memory::get(int address)
+{
+    if (address < Memory::MEMORY_SIZE) {
+        return this->memory[address];   
+    } else {
+        return 0;
+    }
+}
+
+/*
+ * Print current memory content to console
+ */
+void Memory::print()
+{
+    int i = 0, c = 0;
+    while (i < Memory::MEMORY_SIZE) {
+        c = (c + 1) % 4;
+        printf("\t[%#04x]: %6d", i, this->memory[i]);
+        if (!c) {
+            printf("\r\n");
+        }
+        i++;
+    }
+}
\ No newline at end of file