Sille Van Landschoot / Mbed 2 deprecated mbed_slave_full

Dependencies:   mbed

Revision:
4:f537311ddc53
Parent:
3:01b322df3731
Child:
5:b73ea174e997
--- a/main.cpp	Mon Oct 13 14:50:01 2014 +0000
+++ b/main.cpp	Tue Oct 14 09:10:45 2014 +0000
@@ -1,42 +1,22 @@
 #include "mbed.h"
+#include "memory.h"
 
 DigitalOut aLED(LED1);
 Serial pc(USBTX, USBRX); // tx, rx
 I2CSlave slave(p28, p27);
 
 const int SLAVE_ADDRESS = 0x84;
-const int MEMORY_SIZE = 16;
 const int I2C_FREQUENCY = 100000;
-const int I2C_BUFFER_SIZE = 10;
+const int I2C_BUFFER_SIZE = 5;
 
 enum COMMAND { PUSH, PULL, CLEAR, PRINT };
 
-int memory[MEMORY_SIZE];
-
-/*
- * Set all memory locations to 0x55
- */
-void initializeMemory(void)
-{
-    for (int i = 0; i < MEMORY_SIZE; i++) {
-        memory[i] = -1;
-    }
-}
-
-/*
- * Print current memory content
- */
-void printMemory(void)
-{
-    for (int i = 0; i < MEMORY_SIZE; i++) {
-        pc.printf("%d@0x%x\r\n", memory[i], i);
-    }
-}
+#define intToByte(pbytebuff,intval) (*((int*)(pbytebuff)) = intval)
+#define byteToInt(pbytebuff,pintval) (*(pintval) = *((int*)(pbytebuff)))
 
 int main() {
     pc.baud(115200);
     pc.printf("Size of integer is %d bytes\r\n", sizeof(int));
-    pc.printf("Size of memory buffer is %d elements\r\n", MEMORY_SIZE);
     
     // Alive LED
     int cAlive = 0;
@@ -47,49 +27,75 @@
     slave.address(SLAVE_ADDRESS);
     pc.printf("Slave is working @ SLAVE_ADDRESS = 0x%x\r\n", SLAVE_ADDRESS);
 
-    // Make sure memory buffer is zeroed
-    initializeMemory();
-    printMemory();
+    // Setup memory
+    Memory memory;
+    pc.printf("Size of memory buffer is %d elements\r\n", Memory::MEMORY_SIZE);
+    memory.print();
+    pc.printf("Awaiting commands from master ...\r\n");
 
     // I2C buffer
     char buffer[I2C_BUFFER_SIZE];
     
+    // Internal address pointer
+    int pointer = 0;
+    
     while (1) {
         int i = slave.receive();
         switch (i) {
-            /*case I2CSlave::ReadAddressed:
-                pc.printf("Slave received command: %d [ReadAddressed]\r\n", i);
-
-                if (slave.write(msg, strlen(msg) + 1) == 0) {
-                    printf("Sending %s to master\r\n", msg);
+            case I2CSlave::ReadAddressed:
+            {
+                int value = memory.get(pointer);
+                buffer[0] = pointer;
+                intToByte(buffer+1, value);
+                if (!slave.write(buffer, I2C_BUFFER_SIZE)) {
+                    pc.printf("Retrieving and sending to master %d@%d\r\n", value, pointer);
+                } else {
+                    pc.printf("Failed to send to master %d@%d\r\n", value, pointer);
                 }
-                break;*/
-    
+                break;
+            }
             case I2CSlave::WriteAddressed:
-                
+            {
                 if (!slave.read(buffer, I2C_BUFFER_SIZE)) {
                     // Check command byte
                     switch (buffer[0]) {
                         case PUSH:
-                            pc.printf("Storing %d@%d\r\n", *((int*)(&(buffer[2]))), buffer[1]);
-                            if (buffer[1] < MEMORY_SIZE) {
-                                memory[buffer[1]] = *((int*)(&(buffer[2])));
+                        {
+                            int address = buffer[1];
+                            int value;
+                            byteToInt(buffer+2, &value);
+                            pc.printf("Storing %d@%d\r\n", value, address);
+                            memory.set(address, value);
+                            break;
+                        }
+                        
+                        case PRINT:
+                        {
+                            memory.print();
+                            break;
+                        }
+                            
+                        case PULL:
+                        {
+                            pc.printf("Setting pointer to %d\r\n", buffer[1]);
+                            if (buffer[1] < Memory::MEMORY_SIZE) {
+                                pointer = buffer[1];
                             } else {
                                 pc.printf("Address out of boundary\r\n");    
                             }
                             break;
-                        
-                        case PRINT:
-                            printMemory();
-                            break;
+                        }
                         
                         default:
+                        {
                             pc.printf("Unknown command byte\r\n");
+                        }
                     }
                 } else {
                     pc.printf("Received WriteAddressed] without data\r\n");
                 }
                 break;
+            }
         }
         
         // Clear buffer