wiringpi2 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/ext/wiringpi/WiringPi/examples/blink.c +50 -0
  2. data/ext/wiringpi/WiringPi/examples/delayTest.c +107 -0
  3. data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
  4. data/ext/wiringpi/WiringPi/examples/gertboard.c +94 -0
  5. data/ext/wiringpi/WiringPi/examples/header.h +23 -0
  6. data/ext/wiringpi/WiringPi/examples/isr-osc.c +118 -0
  7. data/ext/wiringpi/WiringPi/examples/isr.c +99 -0
  8. data/ext/wiringpi/WiringPi/examples/lcd.c +129 -0
  9. data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
  10. data/ext/wiringpi/WiringPi/examples/okLed.c +83 -0
  11. data/ext/wiringpi/WiringPi/examples/piface.c +74 -0
  12. data/ext/wiringpi/WiringPi/examples/pwm.c +93 -0
  13. data/ext/wiringpi/WiringPi/examples/serialRead.c +48 -0
  14. data/ext/wiringpi/WiringPi/examples/serialTest.c +75 -0
  15. data/ext/wiringpi/WiringPi/examples/servo.c +57 -0
  16. data/ext/wiringpi/WiringPi/examples/speed.c +123 -0
  17. data/ext/wiringpi/WiringPi/examples/test1.c +111 -0
  18. data/ext/wiringpi/WiringPi/examples/test2.c +58 -0
  19. data/ext/wiringpi/WiringPi/examples/tone.c +59 -0
  20. data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
  21. data/ext/wiringpi/WiringPi/gpio/gpio.c +1371 -0
  22. data/ext/wiringpi/WiringPi/wiringPi/ds1302.c +239 -0
  23. data/ext/wiringpi/WiringPi/wiringPi/ds1302.h +44 -0
  24. data/ext/wiringpi/WiringPi/wiringPi/gertboard.c +166 -0
  25. data/ext/wiringpi/WiringPi/wiringPi/gertboard.h +40 -0
  26. data/ext/wiringpi/WiringPi/wiringPi/lcd.c +380 -0
  27. data/ext/wiringpi/WiringPi/wiringPi/lcd.h +46 -0
  28. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +155 -0
  29. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
  30. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
  31. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
  32. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +195 -0
  33. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
  34. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +236 -0
  35. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
  36. data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
  37. data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
  38. data/ext/wiringpi/WiringPi/wiringPi/piFace.c +179 -0
  39. data/ext/wiringpi/WiringPi/wiringPi/piFace.h +32 -0
  40. data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +50 -0
  41. data/ext/wiringpi/WiringPi/wiringPi/piNes.c +113 -0
  42. data/ext/wiringpi/WiringPi/wiringPi/piNes.h +45 -0
  43. data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
  44. data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +130 -0
  45. data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +34 -0
  46. data/ext/wiringpi/WiringPi/wiringPi/softServo.c +211 -0
  47. data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
  48. data/ext/wiringpi/WiringPi/wiringPi/softTone.c +121 -0
  49. data/ext/wiringpi/WiringPi/wiringPi/softTone.h +38 -0
  50. data/ext/wiringpi/WiringPi/wiringPi/sr595.c +108 -0
  51. data/ext/wiringpi/WiringPi/wiringPi/sr595.h +33 -0
  52. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +1664 -0
  53. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +168 -0
  54. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +227 -0
  55. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
  56. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +119 -0
  57. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +35 -0
  58. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +213 -0
  59. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
  60. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
  61. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
  62. data/ext/wiringpi/extconf.rb +8 -0
  63. data/ext/wiringpi/wiringpi_wrap.c +4378 -0
  64. data/lib/wiringpi/event.rb +21 -0
  65. data/lib/wiringpi/gpio.rb +131 -0
  66. data/lib/wiringpi/i2c.rb +26 -0
  67. data/lib/wiringpi/mcp23x17.rb +31 -0
  68. data/lib/wiringpi/serial.rb +49 -0
  69. data/lib/wiringpi/spi.rb +15 -0
  70. data/lib/wiringpi2.rb +42 -0
  71. metadata +122 -0
@@ -0,0 +1,130 @@
1
+ /*
2
+ * softPwm.c:
3
+ * Provide 2 channels of software driven PWM.
4
+ * Copyright (c) 2012 Gordon Henderson
5
+ ***********************************************************************
6
+ * This file is part of wiringPi:
7
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
8
+ *
9
+ * wiringPi is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as
11
+ * published by the Free Software Foundation, either version 3 of the
12
+ * License, or (at your option) any later version.
13
+ *
14
+ * wiringPi is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with wiringPi.
21
+ * If not, see <http://www.gnu.org/licenses/>.
22
+ ***********************************************************************
23
+ */
24
+
25
+ #include <stdio.h>
26
+ #include <pthread.h>
27
+
28
+ #include "wiringPi.h"
29
+ #include "softPwm.h"
30
+
31
+ #define MAX_PINS 64
32
+
33
+ // The PWM Frequency is derived from the "pulse time" below. Essentially,
34
+ // the frequency is a function of the range and this pulse time.
35
+ // The total period will be range * pulse time in uS, so a pulse time
36
+ // of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS
37
+ // which is a frequency of 100Hz.
38
+ //
39
+ // It's possible to get a higher frequency by lowering the pulse time,
40
+ // however CPU uage will skyrocket as wiringPi uses a hard-loop to time
41
+ // periods under 100uS - this is because the Linux timer calls are just
42
+ // accurate at all, and have an overhead.
43
+ //
44
+ // Another way to increase the frequency is to reduce the range - however
45
+ // that reduces the overall output accuracy...
46
+
47
+ #define PULSE_TIME 100
48
+
49
+ static int marks [MAX_PINS] ;
50
+ static int range [MAX_PINS] ;
51
+
52
+ int newPin = -1 ;
53
+
54
+
55
+ /*
56
+ * softPwmThread:
57
+ * Thread to do the actual PWM output
58
+ *********************************************************************************
59
+ */
60
+
61
+ static PI_THREAD (softPwmThread)
62
+ {
63
+ int pin, mark, space ;
64
+
65
+ pin = newPin ;
66
+ newPin = -1 ;
67
+
68
+ piHiPri (50) ;
69
+
70
+ for (;;)
71
+ {
72
+ mark = marks [pin] ;
73
+ space = range [pin] - mark ;
74
+
75
+ if (mark != 0)
76
+ digitalWrite (pin, HIGH) ;
77
+ delayMicroseconds (mark * 100) ;
78
+
79
+ if (space != 0)
80
+ digitalWrite (pin, LOW) ;
81
+ delayMicroseconds (space * 100) ;
82
+ }
83
+
84
+ return NULL ;
85
+ }
86
+
87
+
88
+ /*
89
+ * softPwmWrite:
90
+ * Write a PWM value to the given pin
91
+ *********************************************************************************
92
+ */
93
+
94
+ void softPwmWrite (int pin, int value)
95
+ {
96
+ pin &= 63 ;
97
+
98
+ /**/ if (value < 0)
99
+ value = 0 ;
100
+ else if (value > range [pin])
101
+ value = range [pin] ;
102
+
103
+ marks [pin] = value ;
104
+ }
105
+
106
+
107
+ /*
108
+ * softPwmCreate:
109
+ * Create a new PWM thread.
110
+ *********************************************************************************
111
+ */
112
+
113
+ int softPwmCreate (int pin, int initialValue, int pwmRange)
114
+ {
115
+ int res ;
116
+
117
+ pinMode (pin, OUTPUT) ;
118
+ digitalWrite (pin, LOW) ;
119
+
120
+ marks [pin] = initialValue ;
121
+ range [pin] = pwmRange ;
122
+
123
+ newPin = pin ;
124
+ res = piThreadCreate (softPwmThread) ;
125
+
126
+ while (newPin != -1)
127
+ delay (1) ;
128
+
129
+ return res ;
130
+ }
@@ -0,0 +1,34 @@
1
+ /*
2
+ * softPwm.h:
3
+ * Provide 2 channels of software driven PWM.
4
+ * Copyright (c) 2012 Gordon Henderson
5
+ ***********************************************************************
6
+ * This file is part of wiringPi:
7
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
8
+ *
9
+ * wiringPi is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as
11
+ * published by the Free Software Foundation, either version 3 of the
12
+ * License, or (at your option) any later version.
13
+ *
14
+ * wiringPi is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with wiringPi.
21
+ * If not, see <http://www.gnu.org/licenses/>.
22
+ ***********************************************************************
23
+ */
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ extern int softPwmCreate (int pin, int value, int range) ;
30
+ extern void softPwmWrite (int pin, int value) ;
31
+
32
+ #ifdef __cplusplus
33
+ }
34
+ #endif
@@ -0,0 +1,211 @@
1
+ /*
2
+ * softServo.c:
3
+ * Provide N channels of software driven PWM suitable for RC
4
+ * servo motors.
5
+ * Copyright (c) 2012 Gordon Henderson
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
12
+ * published by the Free Software Foundation, either version 3 of the
13
+ * License, or (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
21
+ * License along with wiringPi.
22
+ * If not, see <http://www.gnu.org/licenses/>.
23
+ ***********************************************************************
24
+ */
25
+
26
+ //#include <stdio.h>
27
+ #include <string.h>
28
+ #include <time.h>
29
+ #include <sys/time.h>
30
+ #include <pthread.h>
31
+
32
+ #include "wiringPi.h"
33
+ #include "softServo.h"
34
+
35
+ // RC Servo motors are a bit of an oddity - designed in the days when
36
+ // radio control was experimental and people were tryin to make
37
+ // things as simple as possible as it was all very expensive...
38
+ //
39
+ // So... To drive an RC Servo motor, you need to send it a modified PWM
40
+ // signal - it needs anything from 1ms to 2ms - with 1ms meaning
41
+ // to move the server fully left, and 2ms meaning to move it fully
42
+ // right. Then you need a long gap before sending the next pulse.
43
+ // The reason for this is that you send a multiplexed stream of these
44
+ // pulses up the radio signal into the reciever which de-multiplexes
45
+ // them into the signals for each individual servo. Typically there
46
+ // might be 8 channels, so you need at least 8 "slots" of 2mS pulses
47
+ // meaning the entire frame must fit into a 16mS slot - which would
48
+ // then be repeated...
49
+ //
50
+ // In practice we have a total slot width of about 20mS - so we're sending 50
51
+ // updates per second to each servo.
52
+ //
53
+ // In this code, we don't need to be too fussy about the gap as we're not doing
54
+ // the multipexing, but it does need to be at least 10mS, and preferably 16
55
+ // from what I've been able to determine.
56
+
57
+ // WARNING:
58
+ // This code is really experimental. It was written in response to some people
59
+ // asking for a servo driver, however while it works, there is too much
60
+ // jitter to successfully drive a small servo - I have tried it with a micro
61
+ // servo and it worked, but the servo ran hot due to the jitter in the signal
62
+ // being sent to it.
63
+ //
64
+ // If you want servo control for the Pi, then use the servoblaster kernel
65
+ // module.
66
+
67
+ #define MAX_SERVOS 8
68
+
69
+ static int pinMap [MAX_SERVOS] ; // Keep track of our pins
70
+ static int pulseWidth [MAX_SERVOS] ; // microseconds
71
+
72
+
73
+ /*
74
+ * softServoThread:
75
+ * Thread to do the actual Servo PWM output
76
+ *********************************************************************************
77
+ */
78
+
79
+ static PI_THREAD (softServoThread)
80
+ {
81
+ register int i, j, k, m, tmp ;
82
+ int lastDelay, pin, servo ;
83
+
84
+ int myDelays [MAX_SERVOS] ;
85
+ int myPins [MAX_SERVOS] ;
86
+
87
+ struct timeval tNow, tStart, tPeriod, tGap, tTotal ;
88
+ struct timespec tNs ;
89
+
90
+ tTotal.tv_sec = 0 ;
91
+ tTotal.tv_usec = 8000 ;
92
+
93
+ piHiPri (50) ;
94
+
95
+ for (;;)
96
+ {
97
+ gettimeofday (&tStart, NULL) ;
98
+
99
+ memcpy (myDelays, pulseWidth, sizeof (myDelays)) ;
100
+ memcpy (myPins, pinMap, sizeof (myPins)) ;
101
+
102
+ // Sort the delays (& pins), shortest first
103
+
104
+ for (m = MAX_SERVOS / 2 ; m > 0 ; m /= 2 )
105
+ for (j = m ; j < MAX_SERVOS ; ++j)
106
+ for (i = j - m ; i >= 0 ; i -= m)
107
+ {
108
+ k = i + m ;
109
+ if (myDelays [k] >= myDelays [i])
110
+ break ;
111
+ else // Swap
112
+ {
113
+ tmp = myDelays [i] ; myDelays [i] = myDelays [k] ; myDelays [k] = tmp ;
114
+ tmp = myPins [i] ; myPins [i] = myPins [k] ; myPins [k] = tmp ;
115
+ }
116
+ }
117
+
118
+ // All on
119
+
120
+ lastDelay = 0 ;
121
+ for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
122
+ {
123
+ if ((pin = myPins [servo]) == -1)
124
+ continue ;
125
+
126
+ digitalWrite (pin, HIGH) ;
127
+ myDelays [servo] = myDelays [servo] - lastDelay ;
128
+ lastDelay += myDelays [servo] ;
129
+ }
130
+
131
+ // Now loop, turning them all off as required
132
+
133
+ for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
134
+ {
135
+ if ((pin = myPins [servo]) == -1)
136
+ continue ;
137
+
138
+ delayMicroseconds (myDelays [servo]) ;
139
+ digitalWrite (pin, LOW) ;
140
+ }
141
+
142
+ // Wait until the end of an 8mS time-slot
143
+
144
+ gettimeofday (&tNow, NULL) ;
145
+ timersub (&tNow, &tStart, &tPeriod) ;
146
+ timersub (&tTotal, &tPeriod, &tGap) ;
147
+ tNs.tv_sec = tGap.tv_sec ;
148
+ tNs.tv_nsec = tGap.tv_usec * 1000 ;
149
+ nanosleep (&tNs, NULL) ;
150
+ }
151
+
152
+ return NULL ;
153
+ }
154
+
155
+
156
+ /*
157
+ * softServoWrite:
158
+ * Write a Servo value to the given pin
159
+ *********************************************************************************
160
+ */
161
+
162
+ void softServoWrite (int servoPin, int value)
163
+ {
164
+ int servo ;
165
+
166
+ servoPin &= 63 ;
167
+
168
+ /**/ if (value < -250)
169
+ value = -250 ;
170
+ else if (value > 1250)
171
+ value = 1250 ;
172
+
173
+ for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
174
+ if (pinMap [servo] == servoPin)
175
+ pulseWidth [servo] = value + 1000 ; // uS
176
+ }
177
+
178
+
179
+ /*
180
+ * softServoSetup:
181
+ * Setup the software servo system
182
+ *********************************************************************************
183
+ */
184
+
185
+ int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7)
186
+ {
187
+ int servo ;
188
+
189
+ if (p0 != -1) { pinMode (p0, OUTPUT) ; digitalWrite (p0, LOW) ; }
190
+ if (p1 != -1) { pinMode (p1, OUTPUT) ; digitalWrite (p1, LOW) ; }
191
+ if (p2 != -1) { pinMode (p2, OUTPUT) ; digitalWrite (p2, LOW) ; }
192
+ if (p3 != -1) { pinMode (p3, OUTPUT) ; digitalWrite (p3, LOW) ; }
193
+ if (p4 != -1) { pinMode (p4, OUTPUT) ; digitalWrite (p4, LOW) ; }
194
+ if (p5 != -1) { pinMode (p5, OUTPUT) ; digitalWrite (p5, LOW) ; }
195
+ if (p6 != -1) { pinMode (p6, OUTPUT) ; digitalWrite (p6, LOW) ; }
196
+ if (p7 != -1) { pinMode (p7, OUTPUT) ; digitalWrite (p7, LOW) ; }
197
+
198
+ pinMap [0] = p0 ;
199
+ pinMap [1] = p1 ;
200
+ pinMap [2] = p2 ;
201
+ pinMap [3] = p3 ;
202
+ pinMap [4] = p4 ;
203
+ pinMap [5] = p5 ;
204
+ pinMap [6] = p6 ;
205
+ pinMap [7] = p7 ;
206
+
207
+ for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
208
+ pulseWidth [servo] = 1500 ; // Mid point
209
+
210
+ return piThreadCreate (softServoThread) ;
211
+ }
@@ -0,0 +1,35 @@
1
+ /*
2
+ * softServo.h:
3
+ * Provide N channels of software driven PWM suitable for RC
4
+ * servo motors.
5
+ * Copyright (c) 2012 Gordon Henderson
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
12
+ * published by the Free Software Foundation, either version 3 of the
13
+ * License, or (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
21
+ * License along with wiringPi.
22
+ * If not, see <http://www.gnu.org/licenses/>.
23
+ ***********************************************************************
24
+ */
25
+
26
+ #ifdef __cplusplus
27
+ extern "C" {
28
+ #endif
29
+
30
+ extern void softServoWrite (int pin, int value) ;
31
+ extern int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7) ;
32
+
33
+ #ifdef __cplusplus
34
+ }
35
+ #endif
@@ -0,0 +1,121 @@
1
+ /*
2
+ * softTone.c:
3
+ * For that authentic retro sound...
4
+ * Er... A little experiment to produce tones out of a Pi using
5
+ * one (or 2) GPIO pins and a piezeo "speaker" element.
6
+ * (Or a high impedance speaker, but don'y blame me if you blow-up
7
+ * the GPIO pins!)
8
+ * Copyright (c) 2012 Gordon Henderson
9
+ ***********************************************************************
10
+ * This file is part of wiringPi:
11
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
12
+ *
13
+ * wiringPi is free software: you can redistribute it and/or modify
14
+ * it under the terms of the GNU Lesser General Public License as
15
+ * published by the Free Software Foundation, either version 3 of the
16
+ * License, or (at your option) any later version.
17
+ *
18
+ * wiringPi is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU Lesser General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU Lesser General Public
24
+ * License along with wiringPi.
25
+ * If not, see <http://www.gnu.org/licenses/>.
26
+ ***********************************************************************
27
+ */
28
+
29
+ #include <stdio.h>
30
+ #include <pthread.h>
31
+
32
+ #include "wiringPi.h"
33
+ #include "softTone.h"
34
+
35
+ #define MAX_PINS 64
36
+
37
+ #define PULSE_TIME 100
38
+
39
+ static int freqs [MAX_PINS] ;
40
+
41
+ static int newPin = -1 ;
42
+
43
+
44
+ /*
45
+ * softToneThread:
46
+ * Thread to do the actual PWM output
47
+ *********************************************************************************
48
+ */
49
+
50
+ static PI_THREAD (softToneThread)
51
+ {
52
+ int pin, freq, halfPeriod ;
53
+
54
+ pin = newPin ;
55
+ newPin = -1 ;
56
+
57
+ piHiPri (50) ;
58
+
59
+ for (;;)
60
+ {
61
+ freq = freqs [pin] ;
62
+ if (freq == 0)
63
+ delay (1) ;
64
+ else
65
+ {
66
+ halfPeriod = 500000 / freq ;
67
+
68
+ digitalWrite (pin, HIGH) ;
69
+ delayMicroseconds (halfPeriod) ;
70
+
71
+ digitalWrite (pin, LOW) ;
72
+ delayMicroseconds (halfPeriod) ;
73
+ }
74
+ }
75
+
76
+ return NULL ;
77
+ }
78
+
79
+
80
+ /*
81
+ * softToneWrite:
82
+ * Write a frequency value to the given pin
83
+ *********************************************************************************
84
+ */
85
+
86
+ void softToneWrite (int pin, int freq)
87
+ {
88
+ pin &= 63 ;
89
+
90
+ /**/ if (freq < 0)
91
+ freq = 0 ;
92
+ else if (freq > 5000) // Max 5KHz
93
+ freq = 5000 ;
94
+
95
+ freqs [pin] = freq ;
96
+ }
97
+
98
+
99
+ /*
100
+ * softToneCreate:
101
+ * Create a new tone thread.
102
+ *********************************************************************************
103
+ */
104
+
105
+ int softToneCreate (int pin)
106
+ {
107
+ int res ;
108
+
109
+ pinMode (pin, OUTPUT) ;
110
+ digitalWrite (pin, LOW) ;
111
+
112
+ freqs [pin] = 0 ;
113
+
114
+ newPin = pin ;
115
+ res = piThreadCreate (softToneThread) ;
116
+
117
+ while (newPin != -1)
118
+ delay (1) ;
119
+
120
+ return res ;
121
+ }