当前位置:首页 > 嵌入式培训 > 嵌入式学习 > 讲师博文 > 第1章 Contiki Hello World实验

第1章 Contiki Hello World实验 时间:2014-09-18      来源:未知

本实验定期的打印“hello world”,并且计数打印的个数。第二线程被称作“blink”,实现LEDs的高速闪烁(如果在Hello world线程里,可能实现不了这么快的速度)。

C++ Code

#include "contiki.h"
        #include "dev/leds.h"
        
        #include <stdio.h> /* For printf() */
        /*---------------------------------------------------------------------------*/
        /* We declare the two processes */
        PROCESS(hello_world_process, "Hello world process");
        PROCESS(blink_process, "LED blink process");
        
        /* We require the processes to be started automatically */
        AUTOSTART_PROCESSES(&hello_world_process, &blink_process);
        /*---------------------------------------------------------------------------*/
        /* Implementation of the first process */
        PROCESS_THREAD(hello_world_process, ev, data)
        {
                // variables are declared static to ensure their values are kept
                // between kernel calls.
                static struct etimer timer;
                static int count = 0;
        
                // any process mustt start with this.
                PROCESS_BEGIN();
                // set the etimer module to generate an event in one second.
                etimer_set(&timer, CLOCK_CONF_SECOND);
        
                while (1)
                {
                        // wait here for an event to happen
                        PROCESS_WAIT_EVENT();
        
                        // if the event is the timer event as expected...
                        if(ev == PROCESS_EVENT_TIMER)
                        {
                                // do the process work
                                printf("Hello, world #%i\n", count);
                                count ++;
        
                                // reset the timer so it will generate an other event
                                // the exact same time after it expired (periodicity guaranteed)
                                etimer_reset(&timer);
                        }
        
                        // and loop
                }
                // any process must end with this, even if it is never reached.
                PROCESS_END();
        }
        
        /*---------------------------------------------------------------------------*/
        /* Implementation of the second process */
        PROCESS_THREAD(blink_process, ev, data)
        {
                static struct etimer timer;
                static uint8_t leds_state = 0;
                PROCESS_BEGIN();
        
                while (1)
                {
                        // we set the timer from here every time
                        etimer_set(&timer, CLOCK_CONF_SECOND / 4);
                        // and wait until the vent we receive is the one we're waiting for
                        PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
        
                        // update the LEDs
                        leds_off(0xFF);
                        leds_on(leds_state);
                        leds_state += 1;
                }
                PROCESS_END();
        }
        /*---------------------------------------------------------------------------*/

1、线程启动流程

a) 声明线程PROCESS(name, strname),name线程名字,strname对线程名字的描述。

b) AUTOSTART_PROCESSES(&hello_world_process, &blink_process)开启线程。

c) PROCESS_THREAD(hello_world_process, ev, data)线程宏定义。

第一个参数lc是英文全称是local continuation(本地延续?,这个不好翻译),它可以说就是protothread的控制参数,因为protothread的精华在C的switch控制语句,这个lc就是switch里面的参数;

第二个参数就是timer给这个进程传递的事件了,其实就是一个unsigned char类型的参数,具体的参数值在process .h中定义;

第三个参数也是给进程传递的一个参数(举个例子,假如要退出一个进程的话,那么必须把这个进程所对应的timer也要从timer队列timerlist清除掉,那么进程在死掉之前就要给etimer_process传递这个参数,参数的值就是进程本身,这样才能是etimer_process在timer队列里找到相应的etimer进而清除)。

上一篇:安卓程序解决网络传输乱码问题

下一篇:网络超时检测的三种方法

热点文章推荐
华清学员就业榜单
高薪学员经验分享
热点新闻推荐
前台专线:010-82525158 企业培训洽谈专线:010-82525379 院校合作洽谈专线:010-82525379 Copyright © 2004-2022 北京华清远见科技集团有限公司 版权所有 ,京ICP备16055225号-5京公海网安备11010802025203号

回到顶部