Sonder Design Team / Mbed 2 deprecated BlackBoard_Firmware_Fast_read_not_test

Dependencies:   Memory25L16_fast USBDevice mbed

Fork of BlackBoard_Firmware_MVP by Sonder Design Team

Revision:
2:8b66a3f7e202
Parent:
1:1ec8bcd31b27
Child:
3:d4e1892846fb
--- a/BB_Basic.cpp	Sun Jun 26 23:01:49 2016 +0000
+++ b/BB_Basic.cpp	Thu Jun 30 01:08:45 2016 +0000
@@ -327,40 +327,25 @@
 
     //Checks if the block has been erased. only virgin bytes can be written to, so if a block has not been errased it is write
     //protected. this erases a block before writung to it if nessisary.
-    if (Address<lastErasedBlock) {
+    int currentSlot = Address/0x20000;
+    pc.printf("\nCurrent slot: %i", currentSlot);
+    if (erasedSlots[currentSlot]) {
         pc.printf("\nNE");
         Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
     } else {
-        pc.printf("\nFUUL");
-        mem.blockErase(my_spi, Address);
-        lastErasedBlock=Address+0x10000;
-        Address = mem.writeData(my_spi, memoryBuffer, Address, bufferSize);
-    }
+        pc.printf("\nCan not write to unerased slot.");
+            }
     /*t.stop();
     printf("\n%i ", t.read_ms());
     t.reset();*/
     return Address;
 }
 
-/**
- * Sends three reports containing the list of layout names stored in memory.
- */
-/*char getNameList()
-{
-    int temp1[]= {0x31, nameList[0], nameList[1], nameList[2], nameList[3], nameList[4], nameList[5], nameList[6]};
-    sendUSB(temp1);
-    int temp2[]= {0x32, nameList[7], nameList[8], nameList[9], nameList[10], nameList[11], nameList[12], nameList[13]};
-    sendUSB(temp2);
-    int temp3[]= {0x33, nameList[14], nameList[15], 0, 0 ,0 ,0 ,0};
-    sendUSB(temp3);
-    return (char)temp1[1];
-}
-*/
 
 /**
  * Sends a report containing a the name of the layout stored at a given slot
  */
-void getSlotName(int slot)
+void getLayoutName(int slot)
 {
     int temp [] = {0x31,slot,nameList[slot][0],nameList[slot][1],nameList[slot][2],nameList[slot][3],nameList[slot][4],0};
     sendUSB(temp);
@@ -373,7 +358,7 @@
 void getNameList()
 {
     for(int slot =0; slot< 16; slot++) {
-        getSlotName(slot);
+        getLayoutName(slot);
     }
 }
 
@@ -382,17 +367,41 @@
  */
 void getCurrentLayout()
 {
-    getSlotName(currentName);
+    getLayoutName(currentName);
 }
 
 /**
  * Writes the name of a given layout to the top memory address in its reserved block
  * name[] is a 5 bytes char array with the most significant byte at name[0]
  */
-void nameBlock(SPI my_spi, char name[], int address)
+void nameBlock(SPI my_spi, char name[], int slot)
 {
     //char temp[]= {name};
-    mem.writeData(my_spi, name, address, 5);
+    mem.writeData(my_spi, name, slots[slot]+0x1fff9, 5);
+}
+
+/**
+ * Reads memory to find the names of the layouts stored. puts the names into nameList
+ */
+void populateNameList(SPI my_spi)
+{
+   for(int slot=0; slot<16; slot++){
+       char name[5];
+       mem.readData(my_spi, name, slots[slot]+0x1FFF9, 5); //Read five bytes from the end of a slot into the name array
+       for( int i = 0; i<5;i++){
+           nameList[slot][i]=name[i];
+        }    }   
+}
+
+/**
+ * Prepares the memory for a new Layout, writes the layout's name, and erases the blocks
+ */
+void writeImage(SPI my_spi, int slot, char name[])
+{
+     mem.blockErase(my_spi, slots[slot]);           //erase the bottom block of the slot    
+     mem.blockErase(my_spi, slots[slot]+0x10000);   //erase the top block of the slot     
+     nameBlock(my_spi, name,  slots[slot]+0x1FFF9); //Write the name of the layout to memory
+     erasedSlots[slot]=true;                        //Mark the erased slot as true
 }
 
 
@@ -468,7 +477,12 @@
     return 1;
 }
 
-
+/*###### EPD Write ######*/
+//Update the whole display with data from a reserved slot
+int EPD_Swap(SPI mem_spi, int slot)
+{
+    return EPD_Write(mem_spi, slots[slot]);
+}
 /*******************************************************
  * Main Function
  */
@@ -478,6 +492,8 @@
 
     int USBDataBuffer[64];
     SPI my_spi = setupSPI();        //Creates an SPI object to comunicate with the external memory
+    populateNameList(my_spi);       //Reads the names  of layouts stored in external memory into RAM
+    
     while(1) {
         //pc.printf("Loop");