Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

softTone.c 2.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <stdio.h>
  29. #include <pthread.h>
  30. #include "wiringPi.h"
  31. #include "softTone.h"
  32. #define MAX_PINS 64
  33. #define PULSE_TIME 100
  34. static int freqs [MAX_PINS] ;
  35. static int newPin = -1 ;
  36. /*
  37. * softToneThread:
  38. * Thread to do the actual PWM output
  39. *********************************************************************************
  40. */
  41. static PI_THREAD (softToneThread)
  42. {
  43. int pin, freq, halfPeriod ;
  44. pin = newPin ;
  45. newPin = -1 ;
  46. piHiPri (50) ;
  47. for (;;)
  48. {
  49. freq = freqs [pin] ;
  50. if (freq == 0)
  51. delay (1) ;
  52. else
  53. {
  54. halfPeriod = 500000 / freq ;
  55. digitalWrite (pin, HIGH) ;
  56. delayMicroseconds (halfPeriod) ;
  57. digitalWrite (pin, LOW) ;
  58. delayMicroseconds (halfPeriod) ;
  59. }
  60. }
  61. return NULL ;
  62. }
  63. /*
  64. * softToneWrite:
  65. * Write a frequency value to the given pin
  66. *********************************************************************************
  67. */
  68. void softToneWrite (int pin, int freq)
  69. {
  70. pin &= 63 ;
  71. /**/ if (freq < 0)
  72. freq = 0 ;
  73. else if (freq > 5000) // Max 5KHz
  74. freq = 5000 ;
  75. freqs [pin] = freq ;
  76. }
  77. /*
  78. * softToneCreate:
  79. * Create a new tone thread.
  80. *********************************************************************************
  81. */
  82. int softToneCreate (int pin)
  83. {
  84. int res ;
  85. pinMode (pin, OUTPUT) ;
  86. digitalWrite (pin, LOW) ;
  87. freqs [pin] = 0 ;
  88. newPin = pin ;
  89. res = piThreadCreate (softToneThread) ;
  90. while (newPin != -1)
  91. delay (1) ;
  92. return res ;
  93. }