25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

122 lines
2.6 KiB

  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. }