Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9408224
  • 博文数量: 1748
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 20070
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1748)

文章存档

2024年(24)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: 其他平台

2015-07-09 15:50:27

老外写的,直接哪里用

点击(此处)折叠或打开

  1. /*
  2.  * SimpleGPIO.cpp
  3.  *
  4.  * Modifications by Derek Molloy, School of Electronic Engineering, DCU
  5.  * www.derekmolloy.ie
  6.  * Almost entirely based on Software by RidgeRun:
  7.  *
  8.  * Copyright (c) 2011, RidgeRun
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  * notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  * notice, this list of conditions and the following disclaimer in the
  17.  * documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  * must display the following acknowledgement:
  20.  * This product includes software developed by the RidgeRun.
  21.  * 4. Neither the name of the RidgeRun nor the
  22.  * names of its contributors may be used to endorse or promote products
  23.  * derived from this software without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
  26.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.  * DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
  29.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  32.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.  */

  36. #include "SimpleGPIO.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <poll.h>

  44. /****************************************************************
  45.  * gpio_export
  46.  ****************************************************************/
  47. int gpio_export(unsigned int gpio)
  48. {
  49.     int fd, len;
  50.     char buf[MAX_BUF];

  51.     fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
  52.     if (fd < 0) {
  53.         perror("gpio/export");
  54.         return fd;
  55.     }

  56.     len = snprintf(buf, sizeof(buf), "%d", gpio);
  57.     write(fd, buf, len);
  58.     close(fd);

  59.     return 0;
  60. }

  61. /****************************************************************
  62.  * gpio_unexport
  63.  ****************************************************************/
  64. int gpio_unexport(unsigned int gpio)
  65. {
  66.     int fd, len;
  67.     char buf[MAX_BUF];

  68.     fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
  69.     if (fd < 0) {
  70.         perror("gpio/export");
  71.         return fd;
  72.     }

  73.     len = snprintf(buf, sizeof(buf), "%d", gpio);
  74.     write(fd, buf, len);
  75.     close(fd);
  76.     return 0;
  77. }

  78. /****************************************************************
  79.  * gpio_set_dir
  80.  ****************************************************************/
  81. int gpio_set_dir(unsigned int gpio, PIN_DIRECTION out_flag)
  82. {
  83.     int fd;
  84.     char buf[MAX_BUF];

  85.     snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);

  86.     fd = open(buf, O_WRONLY);
  87.     if (fd < 0) {
  88.         perror("gpio/direction");
  89.         return fd;
  90.     }

  91.     if (out_flag == OUTPUT_PIN)
  92.         write(fd, "out", 4);
  93.     else
  94.         write(fd, "in", 3);

  95.     close(fd);
  96.     return 0;
  97. }

  98. /****************************************************************
  99.  * gpio_set_value
  100.  ****************************************************************/
  101. int gpio_set_value(unsigned int gpio, PIN_VALUE value)
  102. {
  103.     int fd;
  104.     char buf[MAX_BUF];

  105.     snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);

  106.     fd = open(buf, O_WRONLY);
  107.     if (fd < 0) {
  108.         perror("gpio/set-value");
  109.         return fd;
  110.     }

  111.     if (value==LOW)
  112.         write(fd, "0", 2);
  113.     else
  114.         write(fd, "1", 2);

  115.     close(fd);
  116.     return 0;
  117. }

  118. /****************************************************************
  119.  * gpio_get_value
  120.  ****************************************************************/
  121. int gpio_get_value(unsigned int gpio, unsigned int *value)
  122. {
  123.     int fd;
  124.     char buf[MAX_BUF];
  125.     char ch;

  126.     snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);

  127.     fd = open(buf, O_RDONLY);
  128.     if (fd < 0) {
  129.         perror("gpio/get-value");
  130.         return fd;
  131.     }

  132.     read(fd, &ch, 1);

  133.     if (ch != '0') {
  134.         *value = 1;
  135.     } else {
  136.         *value = 0;
  137.     }

  138.     close(fd);
  139.     return 0;
  140. }


  141. /****************************************************************
  142.  * gpio_set_edge
  143.  ****************************************************************/

  144. int gpio_set_edge(unsigned int gpio, char *edge)
  145. {
  146.     int fd;
  147.     char buf[MAX_BUF];

  148.     snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);

  149.     fd = open(buf, O_WRONLY);
  150.     if (fd < 0) {
  151.         perror("gpio/set-edge");
  152.         return fd;
  153.     }

  154.     write(fd, edge, strlen(edge) + 1);
  155.     close(fd);
  156.     return 0;
  157. }

  158. /****************************************************************
  159.  * gpio_fd_open
  160.  ****************************************************************/

  161. int gpio_fd_open(unsigned int gpio)
  162. {
  163.     int fd;
  164.     char buf[MAX_BUF];

  165.     snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);

  166.     fd = open(buf, O_RDONLY | O_NONBLOCK );
  167.     if (fd < 0) {
  168.         perror("gpio/fd_open");
  169.     }
  170.     return fd;
  171. }

  172. /****************************************************************
  173.  * gpio_fd_close
  174.  ****************************************************************/

  175. int gpio_fd_close(int fd)
  176. {
  177.     return close(fd);
  178. }


点击(此处)折叠或打开

  1. /*
  2.  * SimpleGPIO.h
  3.  *
  4.  * Copyright Derek Molloy, School of Electronic Engineering, Dublin City University
  5.  * www.derekmolloy.ie
  6.  *
  7.  * Based on Software by RidgeRun
  8.  * Copyright (c) 2011, RidgeRun
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  * notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  * notice, this list of conditions and the following disclaimer in the
  17.  * documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  * must display the following acknowledgement:
  20.  * This product includes software developed by the RidgeRun.
  21.  * 4. Neither the name of the RidgeRun nor the
  22.  * names of its contributors may be used to endorse or promote products
  23.  * derived from this software without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
  26.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.  * DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
  29.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  32.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.  */

  36. #ifndef SIMPLEGPIO_H_
  37. #define SIMPLEGPIO_H_

  38.  /****************************************************************
  39.  * Constants
  40.  ****************************************************************/

  41. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  42. #define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
  43. #define MAX_BUF 64

  44. enum PIN_DIRECTION{
  45.     INPUT_PIN=0,
  46.     OUTPUT_PIN=1
  47. };

  48. enum PIN_VALUE{
  49.     LOW=0,
  50.     HIGH=1
  51. };

  52. /****************************************************************
  53.  * gpio_export
  54.  ****************************************************************/
  55. int gpio_export(unsigned int gpio);
  56. int gpio_unexport(unsigned int gpio);
  57. int gpio_set_dir(unsigned int gpio, PIN_DIRECTION out_flag);
  58. int gpio_set_value(unsigned int gpio, PIN_VALUE value);
  59. int gpio_get_value(unsigned int gpio, unsigned int *value);
  60. int gpio_set_edge(unsigned int gpio, char *edge);
  61. int gpio_fd_open(unsigned int gpio);
  62. int gpio_fd_close(int fd);

  63. #endif /* SIMPLEGPIO_H_ */


点击(此处)折叠或打开

  1. /*
  2.  * TestApplication.cpp
  3.  *
  4.  * Copyright Derek Molloy, School of Electronic Engineering, Dublin City University
  5.  * www.derekmolloy.ie
  6.  *
  7.  * YouTube Channel: http://www.youtube.com/derekmolloydcu/
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without modification,
  10.  * are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  * notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  * notice, this list of conditions and the following disclaimer in the
  15.  * documentation and/or other materials provided with the distribution.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  19.  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL I
  20.  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  22.  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  */

  27. #include <iostream>
  28. #include <string>
  29. #include <unistd.h>
  30. #include "SimpleGPIO.h"
  31. using namespace std;

  32. unsigned int LEDGPIO = 60; // GPIO1_28 = (1x32) + 28 = 60
  33. unsigned int ButtonGPIO = 15; // GPIO0_15 = (0x32) + 15 = 15

  34. int main(int argc, char *argv[]){

  35.     cout << "Testing the GPIO Pins" << endl;

  36.         gpio_export(LEDGPIO); // The LED
  37.     gpio_export(ButtonGPIO); // The push button switch
  38.         gpio_set_dir(LEDGPIO, OUTPUT_PIN); // The LED is an output
  39.     gpio_set_dir(ButtonGPIO, INPUT_PIN); // The push button input
  40.     
  41.     // Flash the LED 5 times
  42.     for(int i=0; i<5; i++){
  43.         cout << "Setting the LED on" << endl;
  44.                 gpio_set_value(LEDGPIO, HIGH);
  45.         usleep(200000); // on for 200ms
  46.         cout << "Setting the LED off" << endl;
  47.                 gpio_set_value(LEDGPIO, LOW);
  48.         usleep(200000); // off for 200ms
  49.     }

  50.     // Wait for the push button to be pressed
  51.     cout << "Please press the button!" << endl;

  52.     unsigned int value = LOW;
  53.     do {
  54.         gpio_get_value(ButtonGPIO, &value);
  55.         cout << ".";
  56.         usleep(1000); // sleep for one millisecond
  57.     } while (value!=HIGH);
  58.     cout << endl << "Button was just pressed!" << endl;

  59.     cout << "Finished Testing the GPIO Pins" << endl;
  60.     gpio_unexport(LEDGPIO); // unexport the LED
  61.     gpio_unexport(ButtonGPIO); // unexport the push button
  62.     return 0;
  63. }

阅读(2608) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~