Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / examples / PiFace / buttons.c @ 32:cadb9025f1e0

History | View | Annotate | Download (2.75 KB)

1
/*
2
 * buttons.c:
3
 *        Simple test for the PiFace interface board.
4
 *
5
 *        Read the buttons and output the same to the LEDs
6
 *
7
 * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
8
 ***********************************************************************
9
 * This file is part of wiringPi:
10
 *        https://projects.drogon.net/raspberry-pi/wiringpi/
11
 *
12
 *    wiringPi is free software: you can redistribute it and/or modify
13
 *    it under the terms of the GNU Lesser General Public License as published by
14
 *    the Free Software Foundation, either version 3 of the License, or
15
 *    (at your option) any later version.
16
 *
17
 *    wiringPi is distributed in the hope that it will be useful,
18
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 *    GNU Lesser General Public License for more details.
21
 *
22
 *    You should have received a copy of the GNU Lesser General Public License
23
 *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
24
 ***********************************************************************
25
 */
26

    
27
#include <stdio.h>
28

    
29
#include <wiringPi.h>
30
#include <piFace.h>
31

    
32
int outputs [4] = { 0,0,0,0 } ;
33

    
34
// Use 200 as the pin-base for the PiFace board
35

    
36
#define        PIFACE_BASE        200
37

    
38

    
39
/*
40
 * scanButton:
41
 *        Read the guiven button - if it's pressed, then flip the state
42
 *        of the correspoinding output pin
43
 *********************************************************************************
44
 */
45

    
46
void scanButton (int button)
47
{
48
  if (digitalRead (PIFACE_BASE + button) == LOW)
49
  {
50
    outputs [button] ^= 1 ;
51
    digitalWrite (PIFACE_BASE + button, outputs [button]) ;
52
    printf ("Button %d pushed - output now: %s\n",
53
                button, (outputs [button] == 0) ? "Off" : "On") ;
54
  }
55

    
56
  while (digitalRead (PIFACE_BASE + button) == LOW)
57
    delay (1) ;
58
}
59

    
60

    
61
/*
62
 * start here
63
 *********************************************************************************
64
 */
65

    
66
int main (void)
67
{
68
  int pin, button ;
69

    
70
  printf ("Raspberry Pi wiringPi + PiFace test program\n") ;
71
  printf ("===========================================\n") ;
72
  printf ("\n") ;
73
  printf (
74
"This program reads the buttons and uses them to toggle the first 4\n"
75
"outputs. Push a button once to turn an output on, and push it again to\n"
76
"turn it off again.\n\n") ;
77

    
78
// Always initialise wiringPi. Use wiringPiSys() if you don't need
79
//        (or want) to run as root
80

    
81
  wiringPiSetupSys () ;
82

    
83
  piFaceSetup (PIFACE_BASE) ;
84

    
85
// Enable internal pull-ups & start with all off
86

    
87
  for (pin = 0 ; pin < 8 ; ++pin)
88
  {
89
    pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ;
90
    digitalWrite    (PIFACE_BASE + pin, 0) ;
91
  }
92

    
93
// Loop, scanning the buttons
94

    
95
  for (;;)
96
  {
97
    for (button = 0 ; button < 4 ; ++button)
98
      scanButton (button) ;
99
    delay (5) ;
100
  }
101

    
102
  return 0 ;
103
}