硬件连接:Grove底板连接上,Grove的botton模块查到D2上。
软件:使用Edison Eclipse开发环境
首先,看下Botton模块的原理图:
可以看出,按键的逻辑是:为按下时为低电平,按下为高电平。在查看原理图可以看到上图D1引脚对应Edison的D2引脚。
下面看下程序,其中gpio2为按键输入,gpio12为底板ds2的LED。我们使用
mraa_gpio_isr给gpio2设置了一个上升沿触发的中断,并给它出入中断处理函数。
实验现象是:每按下一次按键,DS2的LED状态反转一次。
- #include "mraa.h"
- #include
- #include
- void key_handle(void * parg);
- mraa_gpio_context d_pin = NULL;
- mraa_gpio_context b_pin = NULL; //for button
- char buf[30] = "Hell Edison!n";
- int main() {
- /* Setup your example here, code that should run once
- */
- mraa_platform_t platform = mraa_get_platform_type();
- 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);
- b_pin = mraa_gpio_init(2);
- 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;
- }
- if (b_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;
- };
- if (mraa_gpio_dir(b_pin, MRAA_GPIO_IN) != MRAA_SUCCESS) {
- fprintf(stderr, "Can't set digital pin as output, exiting");
- return MRAA_ERROR_UNSPECIFIED;
- };
- if (mraa_gpio_isr(b_pin, MRAA_GPIO_EDGE_RISING, key_handle, (void *)&buf ) != MRAA_SUCCESS) {
- fprintf(stderr, "Can't set digital pin as output, exiting");
- return MRAA_ERROR_UNSPECIFIED;
- };
- /* Code in this loop will run repeatedly
- */
- for (;;) {
- sleep(1);
- }
- return MRAA_SUCCESS;
- }
- void key_handle(void * parg)
- {
- static unsigned char i = 0;
- i = ~i;
- fprintf(stdout, (char *)parg);
- if (i) {
- mraa_gpio_write(d_pin, 1);
- }
- else {
- mraa_gpio_write(d_pin, 0);
- }
- }