SlicingBlockDevice example that showcases how to slice a larger block device into sub units.

Fork of ChainingBlockDevice_ex_2 by mbed_example

Revision:
3:62c01cd06ff7
Parent:
2:70419b9d778a
diff -r 70419b9d778a -r 62c01cd06ff7 main.cpp
--- a/main.cpp	Tue Oct 24 23:32:54 2017 +0000
+++ b/main.cpp	Wed Oct 25 20:18:23 2017 +0000
@@ -1,37 +1,44 @@
 #include "mbed.h"
 #include "HeapBlockDevice.h"
-#include "ChainingBlockDevice.h"
+#include "SlicingBlockDevice.h"
 
-#define BLOCKSIZE 512
-char buffer1[512];
-char buffer2[512];
+#define BLOCK 512
+#define NUMSLICES 3
+
+char buff[BLOCK];
 
 int main(void) {
-    // Create two smaller block devices with
-    // 64 and 32 blocks of size 512 bytes
-    HeapBlockDevice mem1(64*BLOCKSIZE, BLOCKSIZE);
-    HeapBlockDevice mem2(32*BLOCKSIZE, BLOCKSIZE);
-    
-    // Create a block device backed by mem1 and mem2
-    // contains 96 blocks of size 512 bytes
-    BlockDevice *bds[] = {&mem1, &mem2};
-    ChainingBlockDevice chainmem(bds);
-    
-    // Initialize the block devices
-    chainmem.init();
+    // Create a block device with 64 blocks of size 512
+    HeapBlockDevice mem(64*BLOCK, BLOCK);
+
+    SlicingBlockDevice slices[NUMSLICES] = {
+        // Create a block device that maps to the first 32 blocks
+        SlicingBlockDevice(&mem, 0 *BLOCK, 32*BLOCK),
     
-    // Erase the block device to prepare for programming. 64 and 32 refer to
-    // the respective number of blocks in mem1 and mem2
-    chainmem.erase(0, (BLOCKSIZE * (64 + 32)));
-    
-    // Program strings to the block device at byte-addressable locations that
-    // span both sub blocks. The second program will write past the end of the
-    // first block
-    chainmem.program("data for block", 0, BLOCKSIZE);
-    chainmem.program("Some more data", (65 * BLOCKSIZE), BLOCKSIZE);
-    
-    // Readback the written values
-    chainmem.read(&buffer1, 0, BLOCKSIZE);
-    chainmem.read(&buffer2, (65 * BLOCKSIZE), BLOCKSIZE);
-    printf("Read back: %s, %s\r\n", buffer1, buffer2);
+        // Create a block device that maps to the middle 32 blocks
+        SlicingBlockDevice(&mem, 16*BLOCK, -16*BLOCK),
+
+        // Create a block device that maps to the last 32 blocks
+        SlicingBlockDevice(&mem, 32*BLOCK)
+    };
+
+    for (int i = 0; i < NUMSLICES; i++) {
+        // Initialize and erase the slice to prepar for programming
+        slices[i].init();
+        slices[i].erase(0, 32*BLOCK);
+        
+        // Construct the message for the block and write to the slice
+        sprintf((char *)&buff, "test: %d", i);
+        slices[i].program(&buff, 0, BLOCK);
+    }
+
+    for (int i = 0; i < NUMSLICES; i++) {
+        // Read back the programmed blocks through the underlying block device
+        mem.read(&buff, (i * 16 * BLOCK), BLOCK);
+        printf("%s  --> ", buff);
+        
+        // Read back the programmed blocks through the sliced block device.
+        slices[i].read(&buff,0, BLOCK);
+        printf("%s\r\n", buff);
+    }
 }
\ No newline at end of file