Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * RPI GPIO reader/writer example
  3.  * @author Gyengus(Tm)
  4.  * @link http://gyengus.hu
  5.  * Build:  gcc -o bin/rpi_gpi_set rpi_gpio_set.c -l bcm2835
  6.  * Usage: sudo rpi_gpio_set PIN 0|1|t|?
  7.  */
  8.  
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <bcm2835.h>
  12.  
  13. int main(int argc, char *argv[]) {
  14.     uint8_t PIN;
  15.  
  16.     if (strcmp(argv[1], "11") == 0) {
  17.     PIN = RPI_GPIO_P1_11;
  18.     } else if (strcmp(argv[1], "15") == 0) {
  19.     PIN = RPI_GPIO_P1_15;
  20.     } else if (strcmp(argv[1], "16") == 0) {
  21.     PIN = RPI_GPIO_P1_16;
  22.     } else if (strcmp(argv[1], "18") == 0) {
  23.     PIN = RPI_GPIO_P1_18; // GPIO24
  24.     } else if (strcmp(argv[1], "22") == 0) {
  25.     PIN = RPI_GPIO_P1_22; // GPIO 25
  26.     }
  27.     // If you call this, it will not actually access the GPIO
  28.     // Use for testing
  29.     //bcm2835_set_debug(1);
  30.  
  31.     if (!bcm2835_init()) {
  32.     printf("Hiba!\n");
  33.     return 1;
  34.     }
  35.  
  36.     // Set the pin to be an output
  37.     bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
  38.  
  39.     if (argv[2][0] == '0') {
  40.         // To low
  41.     bcm2835_gpio_write(PIN, LOW);
  42.     } else if (argv[2][0] == '1') {
  43.     // To high
  44.     bcm2835_gpio_write(PIN, HIGH);
  45.     } else if (argv[2][0] == 't') {
  46.     // Toggle PIN
  47.     if (bcm2835_gpio_lev(PIN)) {
  48.         bcm2835_gpio_write(PIN, LOW);
  49.         } else {
  50.         bcm2835_gpio_write(PIN, HIGH);
  51.     }
  52.     } else if (argv[2][0] == '?') {
  53.     // Retrieve PIN status
  54.     printf("%d", bcm2835_gpio_lev(PIN));
  55.     }
  56.  
  57.     bcm2835_close();
  58.     return 0;
  59. } // main