metro.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * metronome.c:
  3. * Simple test for the PiFace interface board.
  4. *
  5. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  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 published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (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 License
  21. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <wiringPi.h>
  28. #include <piFace.h>
  29. #define PIFACE 200
  30. /*
  31. * middleA:
  32. * Play middle A (on the relays - yea!)
  33. *********************************************************************************
  34. */
  35. static void middleA (void)
  36. {
  37. unsigned int next ;
  38. for (;;)
  39. {
  40. next = micros () + 1136 ;
  41. digitalWrite (PIFACE + 0, 0) ;
  42. digitalWrite (PIFACE + 1, 0) ;
  43. while (micros () < next)
  44. delayMicroseconds (1) ;
  45. next = micros () + 1137 ;
  46. digitalWrite (PIFACE + 0, 1) ;
  47. digitalWrite (PIFACE + 1, 1) ;
  48. while (micros () < next)
  49. delayMicroseconds (1) ;
  50. }
  51. }
  52. int main (int argc, char *argv [])
  53. {
  54. int bpm, msPerBeat, state = 0 ;
  55. unsigned int end ;
  56. printf ("Raspberry Pi PiFace Metronome\n") ;
  57. printf ("=============================\n") ;
  58. piHiPri (50) ;
  59. wiringPiSetupSys () ; // Needed for timing functions
  60. piFaceSetup (PIFACE) ;
  61. if (argc != 2)
  62. {
  63. printf ("Usage: %s <beates per minute>\n", argv [0]) ;
  64. exit (1) ;
  65. }
  66. if (strcmp (argv [1], "a") == 0)
  67. middleA () ;
  68. bpm = atoi (argv [1]) ;
  69. if ((bpm < 40) || (bpm > 208))
  70. {
  71. printf ("%s range is 40 through 208 beats per minute\n", argv [0]) ;
  72. exit (1) ;
  73. }
  74. msPerBeat = 60000 / bpm ;
  75. // Main loop:
  76. // Put some random LED pairs up for a few seconds, then blank ...
  77. for (;;)
  78. {
  79. end = millis () + msPerBeat ;
  80. digitalWrite (PIFACE + 0, state) ;
  81. digitalWrite (PIFACE + 1, state) ;
  82. while (millis () < end)
  83. delayMicroseconds (500) ;
  84. state ^= 1 ;
  85. }
  86. return 0 ;
  87. }