Jeroen Lodder / Mbed 2 deprecated xIFO_example

Dependencies:   xIFO mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "xIFO.h"
00003 
00004 DigitalOut myled(LED1);
00005 
00006 // Create a buffer (note that this is a C module, not an object)
00007 xifo_t buffer;
00008 // Allocate memory for buffer
00009 xifo_pool_t buffer_memory[16]; 
00010 
00011 int main() {
00012     uint32_t data, succes;
00013 
00014     // Initialise the created buffer
00015     xifo_init(&buffer, 128, (uint32_t *)&buffer_memory); 
00016     
00017     // Erase buffer contents
00018     xifo_clear(&buffer);
00019 
00020     // Write some data
00021     succes = xifo_write(&buffer, 0x1); 
00022     succes = xifo_write(&buffer, 0x2); 
00023     succes = xifo_write(&buffer, 0x3);
00024     succes = xifo_write(&buffer, 0x4); 
00025     succes = xifo_write(&buffer, 0x5); 
00026     // Are we full yet?
00027     if( xifo_get_free(&buffer) > 0 )
00028     {
00029         succes = xifo_write(&buffer, 0x6); 
00030     }
00031             
00032     /* Read from the buffer, read-only! */
00033     // Read the least recent element        : 0x1
00034     data = xifo_read_lr(&buffer, 0);  
00035     
00036     // Read the 2nd least recent element    : 0x2
00037     data = xifo_read_lr(&buffer, 1);
00038     
00039     // Read the most recent element         : 0x6
00040     data = xifo_read_mr(&buffer, 0);
00041     
00042     // Read the 2nd most recent element     : 0x5
00043     data = xifo_read_mr(&buffer, 1);
00044     
00045     /* Pop from the buffer, removes elements */
00046     // Remove the least recent element      : 0x1
00047     data = xifo_pop_lr(&buffer);
00048     
00049     // Remove the most recent element       : 0x6
00050     data = xifo_pop_mr(&buffer);
00051     
00052     /* Done 
00053      * Buffer should have this: 0x2, 0x3, 0x4, 0x5
00054      */
00055         
00056     while(1) {
00057         myled = 1;
00058         wait(0.2);
00059         myled = 0;
00060         wait(0.2);
00061     }
00062 }