Notice
Recent Posts
Recent Comments
Today
Total
05-15 08:05
Archives
관리 메뉴

Jeongchul Kim

Intel Edison Board IoT mraa API 사용 본문

사물인터넷

Intel Edison Board IoT mraa API 사용

김 정출 2016. 3. 8. 11:28


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;

}

}

}




Comments