Well, I admit that etimer is a little complicated, mainly because it seems to be mixed with the contiki process, which is everywhere in call_process. Search for the etimer example under contiki and try to write a demo.
Before writing the demo, let's talk about how we found the etimer example file.
In core/sys/etimer. in the H file, when describing the etimer data structure, the author points out that if you want to use etimer, you must first use the etimer_set () function for some work,
Yes, before the definition of the data structure, the comment has already been very clear --- etimer_set () is required. Of course, I also found the example using etimer based on this information. Specifically, the following Linux Command is run in the contiki/directory:
1 find -name "*.c" | xargs grep "etimer_set"
---------------------------------------------------------------
Well, write your own etimer demo in the native environment according to the example provided in contiki.
In contiki/examples/Hello-World/write your desired hello-world.c file:
1 #include "contiki.h" 2 3 #include <stdio.h> /* For printf() */ 4 /*---------------------------------------------------------------------------*/ 5 6 struct etimer timeout; 7 8 PROCESS(hello_world_process, "Hello world process"); 9 AUTOSTART_PROCESSES(&hello_world_process /*,&led_process*/);10 /*---------------------------------------------------------------------------*/11 PROCESS_THREAD(hello_world_process, ev, data)12 {13 PROCESS_BEGIN();14 printf("-----------\n");15 etimer_set(&timeout, 3*CLOCK_SECOND);16 while(1) {17 PROCESS_WAIT_EVENT();18 // PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timeout));19 if(ev == PROCESS_EVENT_TIMER) {20 printf("LED on or off\n");21 }22 etimer_reset(&timeout);23 }24 25 printf("Hello, world\n");26 PROCESS_END();27 }
Simple comments:
Row 6th: The entire sturct etimer {} type variable <That's to say, maybe not accurate>. According to the display of many examples, this can also be taken into the following function body, but it is added with a static keyword-in this case, of course, can be taken.
Row 3: Write according to the comments of the author implementing etimer, and use etimer_set (). The second parameter 3 * clok_second of etime_set () indicates 3X1 s, that is, 3 seconds. This line of code probably means setting a 3 s timeout item-what is it, and I will not investigate it for the moment.
Rows 3 to 23: I wrote an endless loop myself-I will never print "Hello World.
Row 17th: process_wait_event () Macro. In fact, it is a process_yield () Macro, which I have learned before and written in <contiki 7>
Process_yield () macro and switch statement in C Language <7 of contiki Study Notes>
Row 18th: process_wait_event_until (etimer_expired (& timeout); its function is similar to process_wait_event (). It is unknown whether it is equivalent.
It is defined in the./CORE/sys/process. h file, and will eventually be pt_yield_until (PT, Cond) in the./CORE/sys/Pt. h file)
The pt_yield_until (PT, Cond) macro is defined as follows:
1 #define PT_YIELD_UNTIL(pt, cond) 2 do { 3 PT_YIELD_FLAG = 0; 4 LC_SET((pt)->lc); 5 if((PT_YIELD_FLAG == 0) || !(cond)) { 6 return PT_YIELDED; 7 } 8 } while(0)
Similar to process_yield.
Row 3: determines whether or not an etimer event occurs. If so, execute line 3 of code and print it.
-------------------------------
Write down main () in your contiki-main.c below ()
Modify the contiki-main.c under the directory contiki/platform/native/as follows:
1 #include <stdio.h> 2 #include <string.h> 3 #include "contiki.h" 4 5 int 6 main(int argc, char **argv) 7 { 8 9 process_init();10 11 process_start(&etimer_process, NULL); 12 autostart_start(autostart_processes);13 14 while(1) {15 process_run();16 etimer_request_poll();17 }18 return 0;19 }
Of course, to use process, there must be 9th rows.
Row 3: Start etimer_process. Note that starting a process does not mean that you must use this process.
Row 3: Start the self-compiled process.
Rows from 14th to 17th have an endless loop.
After testing, the simplest and simplest functions are left in the while (1) {}. <currently, I do not know whether there are any irreplaceable functions.> otherwise, the etimer test could not be completed-that is, the code in the hello-world.c file that you modified may not be executed as you meant. The order of the two lines of code can be any one after testing.
Then make in the./examples/Hello-World/directory
/Hello-world.native
The phenomenon is that "led on or off" is printed every 3 s ". It is similar that sleep () takes 3 seconds between the last printing and the next printing.
Then, another test: annotate the while (1) loop in the contiki-main.c just compiled, as shown below:
1 #include <stdio.h> 2 #include <string.h> 3 #include "contiki.h" 4 5 int 6 main(int argc, char **argv) 7 { 8 9 process_init();10 11 process_start(&etimer_process, NULL); 12 autostart_start(autostart_processes);13 14 // while(1) {15 etimer_request_poll();16 process_run();17 // }18 return 0;19 }
Then make again and execute the hello-world.native, the result does not print "led on or off", and does not print the following "Hello world! ". Well, this is an interesting phenomenon. The specific reason is that we will share it next time.
Email: [email protected]