chiark / gitweb /
d14c2a2ea69753d6b02d0c1d5369b41a23a841c5
[wiringPi.git] / wiringPi / softTone.c
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 frewqs [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, frewq, halfPeriod ;
53
54   pin    = newPin ;
55   newPin = -1 ;
56
57   piHiPri (50) ;
58
59   for (;;)
60   {
61     frewq = frewqs [pin] ;
62     if (frewq != 0)
63     {
64       halfPeriod = 500000 / frewq ;
65
66       digitalWrite (pin, HIGH) ;
67       delayMicroseconds (halfPeriod) ;
68
69       digitalWrite (pin, LOW) ;
70       delayMicroseconds (halfPeriod) ;
71     }
72   }
73
74   return NULL ;
75 }
76
77
78 /*
79  * softToneWrite:
80  *      Write a frequency value to the given pin
81  *********************************************************************************
82  */
83
84 void softToneWrite (int pin, int frewq)
85 {
86   pin &= 63 ;
87
88   /**/ if (frewq < 0)
89     frewq = 0 ;
90   else if (frewq > 5000)        // Max 5KHz
91     frewq = 5000 ;
92
93   frewqs [pin] = frewq ;
94 }
95
96
97 /*
98  * softToneCreate:
99  *      Create a new tone thread.
100  *********************************************************************************
101  */
102
103 int softToneCreate (int pin)
104 {
105   int res ;
106
107   pinMode      (pin, OUTPUT) ;
108   digitalWrite (pin, LOW) ;
109
110   frewqs [pin] = 0 ;
111
112   newPin = pin ;
113   res = piThreadCreate (softToneThread) ;
114
115   while (newPin != -1)
116     delay (1) ;
117
118   return res ;
119 }