wiringpi 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/wiringpi/WiringPi/devLib/ds1302.c +240 -0
- data/ext/wiringpi/WiringPi/devLib/ds1302.h +44 -0
- data/ext/wiringpi/WiringPi/devLib/font.h +2577 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.c +164 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.c +495 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.h +52 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.c +673 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.h +39 -0
- data/ext/wiringpi/WiringPi/devLib/maxdetect.c +165 -0
- data/ext/wiringpi/{serial.h → WiringPi/devLib/maxdetect.h} +14 -15
- data/ext/wiringpi/WiringPi/devLib/piFace.c +112 -0
- data/ext/wiringpi/WiringPi/devLib/piFace.h +32 -0
- data/ext/wiringpi/WiringPi/devLib/piFaceOld.c +178 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.c +118 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.c +113 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.h +45 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/7segments.c +221 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/buttons.c +83 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/gertboard.c +96 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/record.c +60 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/temperature.c +78 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/voltmeter.c +73 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/vumeter.c +152 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/blink.c +59 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/buttons.c +103 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/ladder.c +337 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/metro.c +111 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/motor.c +120 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/reaction.c +194 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow0.c +51 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow1.c +258 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piglow.c +176 -0
- data/ext/wiringpi/WiringPi/examples/blink.c +48 -0
- data/ext/wiringpi/WiringPi/examples/blink12.c +111 -0
- data/ext/wiringpi/WiringPi/examples/blink12drcs.c +125 -0
- data/ext/wiringpi/WiringPi/examples/blink6drcs.c +115 -0
- data/ext/wiringpi/WiringPi/examples/blink8.c +57 -0
- data/ext/wiringpi/WiringPi/examples/clock.c +201 -0
- data/ext/wiringpi/WiringPi/examples/delayTest.c +107 -0
- data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
- data/ext/wiringpi/WiringPi/examples/header.h +23 -0
- data/ext/wiringpi/WiringPi/examples/isr-osc.c +118 -0
- data/ext/wiringpi/WiringPi/examples/isr.c +110 -0
- data/ext/wiringpi/WiringPi/examples/lcd-adafruit.c +347 -0
- data/ext/wiringpi/WiringPi/examples/lcd.c +286 -0
- data/ext/wiringpi/WiringPi/examples/lowPower.c +68 -0
- data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
- data/ext/wiringpi/WiringPi/examples/okLed.c +82 -0
- data/ext/wiringpi/WiringPi/examples/pwm.c +58 -0
- data/ext/wiringpi/WiringPi/examples/q2w/binary.c +79 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink-io.c +61 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink.c +50 -0
- data/ext/wiringpi/WiringPi/examples/q2w/bright.c +59 -0
- data/ext/wiringpi/WiringPi/examples/q2w/button.c +63 -0
- data/ext/wiringpi/WiringPi/examples/q2w/volts.c +62 -0
- data/ext/wiringpi/WiringPi/examples/rht03.c +69 -0
- data/ext/wiringpi/WiringPi/examples/serialRead.c +48 -0
- data/ext/wiringpi/WiringPi/examples/serialTest.c +75 -0
- data/ext/wiringpi/WiringPi/examples/servo.c +57 -0
- data/ext/wiringpi/WiringPi/examples/softPwm.c +89 -0
- data/ext/wiringpi/WiringPi/examples/softTone.c +54 -0
- data/ext/wiringpi/WiringPi/examples/speed.c +95 -0
- data/ext/wiringpi/WiringPi/examples/spiSpeed.c +118 -0
- data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
- data/ext/wiringpi/WiringPi/gpio/gpio.c +1351 -0
- data/ext/wiringpi/WiringPi/gpio/pins.c +33 -0
- data/ext/wiringpi/WiringPi/gpio/readall.c +334 -0
- data/ext/wiringpi/WiringPi/gpio/version.h +1 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +201 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.c +81 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.c +84 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +149 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +164 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016reg.h +48 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +189 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +236 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +110 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +43 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +126 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +90 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +51 -0
- data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +75 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +166 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.c +211 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.c +150 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.h +39 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.c +109 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +2007 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +224 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +232 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +136 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +36 -0
- data/ext/wiringpi/{serial.c → WiringPi/wiringPi/wiringSerial.c} +38 -29
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
- data/ext/wiringpi/{wiringShift.c → WiringPi/wiringPi/wiringShift.c} +3 -4
- data/ext/wiringpi/{wiringShift.h → WiringPi/wiringPi/wiringShift.h} +5 -5
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.c +731 -0
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.h +26 -0
- data/ext/wiringpi/extconf.rb +7 -1
- data/ext/wiringpi/wiringpi_wrap.c +1741 -218
- data/lib/wiringpi.rb +24 -222
- data/lib/wiringpi/event.rb +21 -0
- data/lib/wiringpi/gpio.rb +139 -0
- data/lib/wiringpi/i2c.rb +26 -0
- data/lib/wiringpi/mcp23x17.rb +31 -0
- data/lib/wiringpi/serial.rb +49 -0
- data/lib/wiringpi/spi.rb +15 -0
- metadata +146 -22
- data/ext/wiringpi/wiringPi.c +0 -729
- data/ext/wiringpi/wiringPi.h +0 -69
@@ -0,0 +1,59 @@
|
|
1
|
+
/*
|
2
|
+
* blink.c:
|
3
|
+
* Simple "blink" test for the PiFace interface board.
|
4
|
+
*
|
5
|
+
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
|
6
|
+
***********************************************************************
|
7
|
+
* This file is part of wiringPi:
|
8
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
9
|
+
*
|
10
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
11
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
13
|
+
* (at your option) any later version.
|
14
|
+
*
|
15
|
+
* wiringPi is distributed in the hope that it will be useful,
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
* GNU Lesser General Public License for more details.
|
19
|
+
*
|
20
|
+
* You should have received a copy of the GNU Lesser General Public License
|
21
|
+
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
***********************************************************************
|
23
|
+
*/
|
24
|
+
|
25
|
+
#include <stdio.h>
|
26
|
+
|
27
|
+
#include <wiringPi.h>
|
28
|
+
#include <piFace.h>
|
29
|
+
|
30
|
+
// Use 200 as the pin-base for the PiFace board, and pick a pin
|
31
|
+
// for the LED that's not connected to a relay
|
32
|
+
|
33
|
+
#define PIFACE 200
|
34
|
+
#define LED (PIFACE+2)
|
35
|
+
|
36
|
+
int main (int argc, char *argv [])
|
37
|
+
{
|
38
|
+
printf ("Raspberry Pi PiFace Blink\n") ;
|
39
|
+
printf ("=========================\n") ;
|
40
|
+
|
41
|
+
// Always initialise wiringPi. Use wiringPiSys() if you don't need
|
42
|
+
// (or want) to run as root
|
43
|
+
|
44
|
+
wiringPiSetupSys () ;
|
45
|
+
|
46
|
+
// Setup the PiFace board
|
47
|
+
|
48
|
+
piFaceSetup (PIFACE) ;
|
49
|
+
|
50
|
+
for (;;)
|
51
|
+
{
|
52
|
+
digitalWrite (LED, HIGH) ; // On
|
53
|
+
delay (500) ; // mS
|
54
|
+
digitalWrite (LED, LOW) ; // Off
|
55
|
+
delay (500) ;
|
56
|
+
}
|
57
|
+
|
58
|
+
return 0 ;
|
59
|
+
}
|
@@ -0,0 +1,103 @@
|
|
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
|
+
}
|
@@ -0,0 +1,337 @@
|
|
1
|
+
/*
|
2
|
+
* ladder.c:
|
3
|
+
*
|
4
|
+
* Gordon Henderson, June 2012
|
5
|
+
***********************************************************************
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <stdlib.h>
|
10
|
+
#include <unistd.h>
|
11
|
+
#include <math.h>
|
12
|
+
|
13
|
+
#include <wiringPi.h>
|
14
|
+
#include <piFace.h>
|
15
|
+
|
16
|
+
#ifndef TRUE
|
17
|
+
# define TRUE (1==1)
|
18
|
+
# define FALSE (1==2)
|
19
|
+
#endif
|
20
|
+
|
21
|
+
#undef DEBUG
|
22
|
+
|
23
|
+
#define NUM_LEDS 8
|
24
|
+
|
25
|
+
|
26
|
+
// Map the LEDs to the hardware pins
|
27
|
+
// using PiFace pin numbers here
|
28
|
+
|
29
|
+
#define PIFACE 200
|
30
|
+
|
31
|
+
const int ledMap [NUM_LEDS] =
|
32
|
+
{
|
33
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8
|
34
|
+
200, 201, 202, 203, 204, 205, 206, 207
|
35
|
+
} ;
|
36
|
+
|
37
|
+
|
38
|
+
// Some constants for our circuit simulation
|
39
|
+
|
40
|
+
const double vBatt = 9.0 ; // Volts (ie. a PP3)
|
41
|
+
const double capacitor = 0.001 ; // 1000uF
|
42
|
+
const double rCharge = 2200.0 ; // ohms
|
43
|
+
const double rDischarge = 68000.0 ; // ohms
|
44
|
+
const double timeInc = 0.01 ; // Seconds
|
45
|
+
|
46
|
+
double vCharge, vCap, vCapLast ;
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
/*
|
51
|
+
* setup:
|
52
|
+
* Program the GPIO correctly and initialise the lamps
|
53
|
+
***********************************************************************
|
54
|
+
*/
|
55
|
+
|
56
|
+
void setup (void)
|
57
|
+
{
|
58
|
+
int i ;
|
59
|
+
|
60
|
+
wiringPiSetupSys () ;
|
61
|
+
|
62
|
+
if (piFaceSetup (200) == -1)
|
63
|
+
exit (1) ;
|
64
|
+
|
65
|
+
// Enable internal pull-ups
|
66
|
+
|
67
|
+
for (i = 0 ; i < 8 ; ++i)
|
68
|
+
pullUpDnControl (PIFACE + i, PUD_UP) ;
|
69
|
+
|
70
|
+
// Calculate the actual charging voltage - standard calculation of
|
71
|
+
// vCharge = r2 / (r1 + r2) * vBatt
|
72
|
+
//
|
73
|
+
//
|
74
|
+
// -----+--- vBatt
|
75
|
+
// |
|
76
|
+
// R1
|
77
|
+
// |
|
78
|
+
// +---+---- vCharge
|
79
|
+
// | |
|
80
|
+
// R2 C
|
81
|
+
// | |
|
82
|
+
// -----+---+-----
|
83
|
+
|
84
|
+
vCharge = rDischarge / (rCharge + rDischarge) * vBatt ;
|
85
|
+
|
86
|
+
// Start with no charge
|
87
|
+
|
88
|
+
vCap = vCapLast = 0.0 ;
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
/*
|
93
|
+
* introLeds
|
94
|
+
* Put a little pattern on the LEDs to start with
|
95
|
+
*********************************************************************************
|
96
|
+
*/
|
97
|
+
|
98
|
+
void introLeds (void)
|
99
|
+
{
|
100
|
+
int i, j ;
|
101
|
+
|
102
|
+
|
103
|
+
printf ("Pi Ladder\n") ;
|
104
|
+
printf ("=========\n\n") ;
|
105
|
+
printf (" vBatt: %6.2f volts\n", vBatt) ;
|
106
|
+
printf (" rCharge: %6.0f ohms\n", rCharge) ;
|
107
|
+
printf (" rDischarge: %6.0f ohms\n", rDischarge) ;
|
108
|
+
printf (" vCharge: %6.2f volts\n", vCharge) ;
|
109
|
+
printf (" capacitor: %6.0f uF\n", capacitor * 1000.0) ;
|
110
|
+
|
111
|
+
// Flash 3 times:
|
112
|
+
|
113
|
+
for (j = 0 ; j < 3 ; ++j)
|
114
|
+
{
|
115
|
+
for (i = 0 ; i < NUM_LEDS ; ++i)
|
116
|
+
digitalWrite (ledMap [i], 1) ;
|
117
|
+
delay (500) ;
|
118
|
+
for (i = 0 ; i < NUM_LEDS ; ++i)
|
119
|
+
digitalWrite (ledMap [i], 0) ;
|
120
|
+
delay (100) ;
|
121
|
+
}
|
122
|
+
|
123
|
+
// All On
|
124
|
+
|
125
|
+
for (i = 0 ; i < NUM_LEDS ; ++i)
|
126
|
+
digitalWrite (ledMap [i], 1) ;
|
127
|
+
delay (500) ;
|
128
|
+
|
129
|
+
// Countdown...
|
130
|
+
|
131
|
+
for (i = NUM_LEDS - 1 ; i >= 0 ; --i)
|
132
|
+
{
|
133
|
+
digitalWrite (ledMap [i], 0) ;
|
134
|
+
delay (100) ;
|
135
|
+
}
|
136
|
+
delay (500) ;
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
/*
|
141
|
+
* winningLeds
|
142
|
+
* Put a little pattern on the LEDs to start with
|
143
|
+
*********************************************************************************
|
144
|
+
*/
|
145
|
+
|
146
|
+
void winningLeds (void)
|
147
|
+
{
|
148
|
+
int i, j ;
|
149
|
+
|
150
|
+
// Flash 3 times:
|
151
|
+
|
152
|
+
for (j = 0 ; j < 3 ; ++j)
|
153
|
+
{
|
154
|
+
for (i = 0 ; i < NUM_LEDS ; ++i)
|
155
|
+
digitalWrite (ledMap [i], 1) ;
|
156
|
+
delay (500) ;
|
157
|
+
for (i = 0 ; i < NUM_LEDS ; ++i)
|
158
|
+
digitalWrite (ledMap [i], 0) ;
|
159
|
+
delay (100) ;
|
160
|
+
}
|
161
|
+
|
162
|
+
// All On
|
163
|
+
|
164
|
+
for (i = 0 ; i < NUM_LEDS ; ++i)
|
165
|
+
digitalWrite (ledMap [i], 1) ;
|
166
|
+
delay (500) ;
|
167
|
+
|
168
|
+
// Countup...
|
169
|
+
|
170
|
+
for (i = 0 ; i < NUM_LEDS ; ++i)
|
171
|
+
{
|
172
|
+
digitalWrite (ledMap [i], 0) ;
|
173
|
+
delay (100) ;
|
174
|
+
}
|
175
|
+
delay (500) ;
|
176
|
+
}
|
177
|
+
|
178
|
+
|
179
|
+
/*
|
180
|
+
* chargeCapacitor: dischargeCapacitor:
|
181
|
+
* Add or remove charge to the capacitor.
|
182
|
+
* Standard capacitor formulae.
|
183
|
+
*********************************************************************************
|
184
|
+
*/
|
185
|
+
|
186
|
+
void chargeCapacitor (void)
|
187
|
+
{
|
188
|
+
vCap = (vCapLast - vCharge) *
|
189
|
+
exp (- timeInc / (rCharge * capacitor)) + vCharge ;
|
190
|
+
|
191
|
+
#ifdef DEBUG
|
192
|
+
printf ("+vCap: %7.4f\n", vCap) ;
|
193
|
+
#endif
|
194
|
+
|
195
|
+
vCapLast = vCap ;
|
196
|
+
}
|
197
|
+
|
198
|
+
void dischargeCapacitor (void)
|
199
|
+
{
|
200
|
+
vCap = vCapLast *
|
201
|
+
exp (- timeInc / (rDischarge * capacitor)) ;
|
202
|
+
|
203
|
+
#ifdef DEBUG
|
204
|
+
printf ("-vCap: %7.4f\n", vCap) ;
|
205
|
+
#endif
|
206
|
+
|
207
|
+
vCapLast = vCap ;
|
208
|
+
}
|
209
|
+
|
210
|
+
|
211
|
+
/*
|
212
|
+
* ledBargraph:
|
213
|
+
* Output the supplied number as a bargraph on the LEDs
|
214
|
+
*********************************************************************************
|
215
|
+
*/
|
216
|
+
|
217
|
+
void ledBargraph (double value, int topLedOn)
|
218
|
+
{
|
219
|
+
int topLed = (int)floor (value / vCharge * (double)NUM_LEDS) + 1 ;
|
220
|
+
int i ;
|
221
|
+
|
222
|
+
if (topLed > NUM_LEDS)
|
223
|
+
topLed = NUM_LEDS ;
|
224
|
+
|
225
|
+
if (!topLedOn)
|
226
|
+
--topLed ;
|
227
|
+
|
228
|
+
for (i = 0 ; i < topLed ; ++i)
|
229
|
+
digitalWrite (ledMap [i], 1) ;
|
230
|
+
|
231
|
+
for (i = topLed ; i < NUM_LEDS ; ++i)
|
232
|
+
digitalWrite (ledMap [i], 0) ;
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
/*
|
237
|
+
* ledOnAction:
|
238
|
+
* Make sure the leading LED is on and check the button
|
239
|
+
*********************************************************************************
|
240
|
+
*/
|
241
|
+
|
242
|
+
void ledOnAction (void)
|
243
|
+
{
|
244
|
+
if (digitalRead (PIFACE) == LOW)
|
245
|
+
{
|
246
|
+
chargeCapacitor () ;
|
247
|
+
ledBargraph (vCap, TRUE) ;
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
/*
|
253
|
+
* ledOffAction:
|
254
|
+
* Make sure the leading LED is off and check the button
|
255
|
+
*********************************************************************************
|
256
|
+
*/
|
257
|
+
|
258
|
+
void ledOffAction (void)
|
259
|
+
{
|
260
|
+
dischargeCapacitor () ;
|
261
|
+
|
262
|
+
// Are we still pushing the button?
|
263
|
+
|
264
|
+
if (digitalRead (PIFACE) == LOW)
|
265
|
+
{
|
266
|
+
vCap = vCapLast = 0.0 ;
|
267
|
+
ledBargraph (vCap, FALSE) ;
|
268
|
+
|
269
|
+
// Wait until we release the button
|
270
|
+
|
271
|
+
while (digitalRead (PIFACE) == LOW)
|
272
|
+
delay (10) ;
|
273
|
+
}
|
274
|
+
}
|
275
|
+
|
276
|
+
|
277
|
+
/*
|
278
|
+
***********************************************************************
|
279
|
+
* The main program
|
280
|
+
***********************************************************************
|
281
|
+
*/
|
282
|
+
|
283
|
+
int main (void)
|
284
|
+
{
|
285
|
+
unsigned int then, ledOnTime, ledOffTime ;
|
286
|
+
unsigned int ourDelay = (int)(1000.0 * timeInc) ;
|
287
|
+
|
288
|
+
setup () ;
|
289
|
+
introLeds () ;
|
290
|
+
|
291
|
+
// Setup the LED times - TODO reduce the ON time as the game progresses
|
292
|
+
|
293
|
+
ledOnTime = 1000 ;
|
294
|
+
ledOffTime = 1000 ;
|
295
|
+
|
296
|
+
// This is our Gate/Squarewave loop
|
297
|
+
|
298
|
+
for (;;)
|
299
|
+
{
|
300
|
+
|
301
|
+
// LED ON:
|
302
|
+
|
303
|
+
(void)ledBargraph (vCap, TRUE) ;
|
304
|
+
then = millis () + ledOnTime ;
|
305
|
+
while (millis () < then)
|
306
|
+
{
|
307
|
+
ledOnAction () ;
|
308
|
+
delay (ourDelay) ;
|
309
|
+
}
|
310
|
+
|
311
|
+
// Have we won yet?
|
312
|
+
// We need vCap to be in the top NUM_LEDS of the vCharge
|
313
|
+
|
314
|
+
if (vCap > ((double)(NUM_LEDS - 1) / (double)NUM_LEDS * vCharge)) // Woo hoo!
|
315
|
+
{
|
316
|
+
winningLeds () ;
|
317
|
+
while (digitalRead (PIFACE) == HIGH)
|
318
|
+
delay (10) ;
|
319
|
+
while (digitalRead (PIFACE) == LOW)
|
320
|
+
delay (10) ;
|
321
|
+
vCap = vCapLast = 0.0 ;
|
322
|
+
}
|
323
|
+
|
324
|
+
// LED OFF:
|
325
|
+
|
326
|
+
(void)ledBargraph (vCap, FALSE) ;
|
327
|
+
then = millis () + ledOffTime ;
|
328
|
+
while (millis () < then)
|
329
|
+
{
|
330
|
+
ledOffAction () ;
|
331
|
+
delay (ourDelay) ;
|
332
|
+
}
|
333
|
+
|
334
|
+
}
|
335
|
+
|
336
|
+
return 0 ;
|
337
|
+
}
|