Microbug / MicroBitDAL_SB2_TEST

Fork of MicroBitDALImageRewrite by Joe Finney

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main_fiber_test.cpp Source File

main_fiber_test.cpp

00001 #include "inc/MicroBitTest.h"
00002 
00003 #ifdef MAIN_FIBER_TEST
00004 
00005 #include "MicroBit.h"
00006 
00007 MicroBit uBit;
00008 Serial pc(USBTX, USBRX);
00009 
00010 void handler1()
00011 {
00012     fiber_sleep(5000);
00013     pc.printf("handler1: X\n");   
00014 }
00015 
00016 void thread1()
00017 {
00018     pc.printf("Thread1: Started\n");
00019  
00020     while(1)
00021     {
00022         pc.printf ("Thread1: Running...\n");
00023         fiber_sleep(10000);        
00024     }    
00025 }
00026 
00027 void thread2()
00028 {
00029     pc.printf("Thread2: Started\n");
00030  
00031     while(1)
00032     {
00033         pc.printf ("Thread2: Running...\n");
00034         fiber_sleep(5000);        
00035     }    
00036 }
00037 
00038 
00039 int main()
00040 { 
00041     // Set up debug console.
00042     pc.baud (115200);
00043     
00044     // Initialize the Fiber scheduler
00045     scheduler_init();
00046 
00047     // 10 second delay just to let me get the terminal console window up. :-)
00048     fiber_sleep(10000);    
00049 
00050     // Create a few simple fibers, just for demonstration purposes.
00051     create_fiber(thread1);
00052     create_fiber(thread2);
00053 
00054     // Register an event handler, decoupled from a hard interrupt using a fiber.
00055     MicroBit::MessageBus.listen(MICROBIT_ID_LEFT_BUTTON, MICROBIT_BUTTON_EVT_DOWN, handler1);
00056     
00057     // Just hang out.
00058     // after scheduler_init() the main thread does have a fiber context, so can also sleep on the fiber scheduler.
00059     // power down will be handled by the idle task, in MicroBitFiber.cpp (detail still to do!).
00060     
00061     while(1)
00062     {
00063         fiber_sleep(10000);
00064         schedule();
00065     }
00066 }
00067 
00068 
00069 #endif