3.5.4 打印消息到控制台

你可以使用printf将消息打印到控制台,从而知道应用程序是如何运行的。这是调试代码的很有用的手段,因为你可以打印变量的值、知道什么时候代码将阻塞等等。

我们尝试使用函数unsigned char leds_get(void);打印LED的状态。

获取LED的状态,并将其打印到屏幕。修改test-leds.c如下:

  1. #include "contiki.h"
  2. #include "dev/leds.h"
  3. #include <stdio.h>
  4. char hello[] = "hello from the mote!";
  5. /*-------------------------------------------------*/
  6. PROCESS(test_leds_process, "Test LEDs");
  7. AUTOSTART_PROCESSES(&test_leds_process);
  8. /*-------------------------------------------------*/
  9. PROCESS_THREAD(test_leds_process, ev, data)
  10. {
  11. PROCESS_BEGIN();
  12. leds_on(LEDS_RED);
  13. printf("%s\n", hello);
  14. printf("The LED %u is %u\n", LEDS_RED, leds_get());
  15. PROCESS_END();
  16. }

如果有一个LED是点亮状态,你将得到LED的数量。这是在每个平台的platform-conf.h或者board.h中定义的。

练习:如果你打开多个LED将会怎样?你将得到多少数目?对于Z1和RE-Mote,他们的数目是相同的吗?