Intel Edison Board IoT mraa API 사용 mraa_led_blink 프로젝트 #include "mraa.h" #include <stdio.h> #include <unistd.h> int main() { mraa_platform_t platform = mraa_get_platform_type(); char *platform_name = mraa_get_platform_name(); mraa_gpio_context d_pin = NULL; switch (platform) { case MRAA_INTEL_GALILEO_GEN1: d_pin = mraa_gpio_init_raw(3); break; case MRAA_INTEL_GALILEO_GEN2: d_pin = mraa_gpio_init(13); break ; case MRAA_INTEL_EDISON_FAB_C: d_pin = mraa_gpio_init(13); break; default: fprintf(stderr, "Unsupported platform, exiting"); return MRAA_ERROR_INVALID_PLATFORM; } if (d_pin == NULL) { fprintf(stderr, "MRAA couldn't initialize GPIO, exiting"); return MRAA_ERROR_UNSPECIFIED; } // set the pin as output if (mraa_gpio_dir(d_pin, MRAA_GPIO_OUT) != MRAA_SUCCESS) { fprintf(stderr, "Can't set digital pin as output, exiting"); return MRAA_ERROR_UNSPECIFIED; }; printf("platform name : %s\n",platform_name); // loop forever toggling the on board LED every second for (;;) { mraa_gpio_write(d_pin, 0); sleep(1); mraa_gpio_write(d_pin, 1); sleep(1); } return MRAA_SUCCESS; } mraa_led_fade 프로젝트 프로젝트를 생성한다. #include <stdio.h> #include <stdlib.h> #include "mraa.h" #define LED 9 int main() { // init mraa_pwm_context led = mraa_pwm_init(LED); mraa_pwm_enable(led,1); // LED enable float inc = 0.0196; float sum = 0.0; while(1) { sum += inc; mraa_pwm_write(led,sum); usleep(30000); // 30000 microsecond if(sum >= 1.0 || sum <= 0) { inc = (-1)*inc; } } return 0; } mraa_led_btn 프로젝트 프로젝트를 생성한다. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include "mraa.h" #define BTN 9 #define LED 13 int main() { int val, last_val = 0; mraa_gpio_context btn = NULL, led = NULL; btn = mraa_gpio_init(BTN); led = mraa_gpio_init(LED); if(btn == NULL || led == NULL) { perror("fail to init btn, led"); return MRAA_ERROR_UNSPECIFIED; } if(mraa_gpio_dir(led,MRAA_GPIO_OUT) != 0) { perror("fail to set led : out"); return 1; } if(mraa_gpio_dir(btn,MRAA_GPIO_IN) != 0) { perror("fail to set btn : in"); return 1; } if(mraa_gpio_mode(btn,MRAA_GPIO_PULLUP) != 0) { perror("fail to set btn : pullup"); return 1; } for(;;) { val = mraa_gpio_read(btn); if(val != last_val) { printf(" read : %d\n",val); if(val == 1) { mraa_gpio_write(led,1); }else { mraa_gpio_write(led,0); } last_val = val; } } }
'사물인터넷' 카테고리의 다른 글
Intel Edison board Eclipse Java LED 제어하기 (0) | 2016.03.08 |
---|---|
Intel Edison board 웹에서 LED 제어하기 (0) | 2016.03.08 |
Intel Edison Board IoT Eclipse (0) | 2016.03.08 |
Intel Edison Board GPIO Control (0) | 2016.03.08 |
Intel Edison board 시작 (2) | 2016.03.08 |