-
Notifications
You must be signed in to change notification settings - Fork 1
/
LN_DIGITAL.c
executable file
·105 lines (85 loc) · 2.89 KB
/
LN_DIGITAL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* buttons.c:
* Simple test for the PiFace interface board.
*
* Read the buttons and output the same to the LEDs
*
* Copyright (c) 2012-2013 Gordon Henderson. <[email protected]>
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <piFace.h>
int outputs [4] = { 0,0,0,0 } ;
// Use 200 as the pin-base for the LN_DIGITAL_BASE board
#define LN_DIGITAL_BASE 200
/*
* scanButton:
* Read the guiven button - if it's pressed, then flip the state
* of the correspoinding output pin
*********************************************************************************
*/
void scanButton (int button)
{
if (digitalRead (LN_DIGITAL_BASE + button) == LOW)
{
outputs [button] ^= 1 ;
int even = button*2;
int odd = button*2 + 1;
digitalWrite (LN_DIGITAL_BASE + even, outputs [button]) ;
digitalWrite (LN_DIGITAL_BASE + odd, outputs [button]) ;
printf ("Button %d pushed - output now: %s\n",
button, (outputs [button] == 0) ? "Off" : "On") ;
}
while (digitalRead (LN_DIGITAL_BASE + button) == LOW)
delay (1) ;
}
/*
* start here
*********************************************************************************
*/
int main (void)
{
int pin, button ;
printf ("Banana Pro wiringPi + LNDigital test program\n") ;
printf ("===========================================\n") ;
printf ("\n") ;
printf (
"This program reads the buttons and uses them to toggle the first 4\n"
"outputs. Push a button once to turn an output on, and push it again to\n"
"turn it off again.\n\n") ;
// Always initialise wiringPi. Use wiringPiSys() if you don't need
// (or want) to run as root
wiringPiSetupSys () ;
piFaceSetup (LN_DIGITAL_BASE) ;
// Enable internal pull-ups & start with all off
for (pin = 0 ; pin < 8 ; ++pin)
{
pullUpDnControl (LN_DIGITAL_BASE + pin, PUD_UP) ;
digitalWrite (LN_DIGITAL_BASE + pin, 0) ;
}
// Loop, scanning the buttons
for (;;)
{
for (button = 0 ; button < 4 ; ++button)
scanButton (button) ;
delay (5) ;
}
return 0 ;
}