You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

120 line
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 frewqs [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, frewq, halfPeriod ;
  44. pin = newPin ;
  45. newPin = -1 ;
  46. piHiPri (50) ;
  47. for (;;)
  48. {
  49. frewq = frewqs [pin] ;
  50. if (frewq != 0)
  51. {
  52. halfPeriod = 500000 / frewq ;
  53. digitalWrite (pin, HIGH) ;
  54. delayMicroseconds (halfPeriod) ;
  55. digitalWrite (pin, LOW) ;
  56. delayMicroseconds (halfPeriod) ;
  57. }
  58. }
  59. return NULL ;
  60. }
  61. /*
  62. * softToneWrite:
  63. * Write a frequency value to the given pin
  64. *********************************************************************************
  65. */
  66. void softToneWrite (int pin, int frewq)
  67. {
  68. pin &= 63 ;
  69. /**/ if (frewq < 0)
  70. frewq = 0 ;
  71. else if (frewq > 5000) // Max 5KHz
  72. frewq = 5000 ;
  73. frewqs [pin] = frewq ;
  74. }
  75. /*
  76. * softToneCreate:
  77. * Create a new tone thread.
  78. *********************************************************************************
  79. */
  80. int softToneCreate (int pin)
  81. {
  82. int res ;
  83. pinMode (pin, OUTPUT) ;
  84. digitalWrite (pin, LOW) ;
  85. frewqs [pin] = 0 ;
  86. newPin = pin ;
  87. res = piThreadCreate (softToneThread) ;
  88. while (newPin != -1)
  89. delay (1) ;
  90. return res ;
  91. }