Deokgyu Yang | 2c7e86b | 2020-04-28 10:56:11 +0900 | [diff] [blame] | 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 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include <wiringPi.h> |
| 30 | #include <piFace.h> |
| 31 | |
| 32 | #define PIFACE 200 |
| 33 | |
| 34 | /* |
| 35 | * middleA: |
| 36 | * Play middle A (on the relays - yea!) |
| 37 | ********************************************************************************* |
| 38 | */ |
| 39 | |
| 40 | static void middleA (void) |
| 41 | { |
| 42 | unsigned int next ; |
| 43 | |
| 44 | for (;;) |
| 45 | { |
| 46 | next = micros () + 1136 ; |
| 47 | digitalWrite (PIFACE + 0, 0) ; |
| 48 | digitalWrite (PIFACE + 1, 0) ; |
| 49 | while (micros () < next) |
| 50 | delayMicroseconds (1) ; |
| 51 | |
| 52 | next = micros () + 1137 ; |
| 53 | digitalWrite (PIFACE + 0, 1) ; |
| 54 | digitalWrite (PIFACE + 1, 1) ; |
| 55 | while (micros () < next) |
| 56 | delayMicroseconds (1) ; |
| 57 | |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | int main (int argc, char *argv []) |
| 63 | { |
| 64 | int bpm, msPerBeat, state = 0 ; |
| 65 | unsigned int end ; |
| 66 | |
| 67 | printf ("Raspberry Pi PiFace Metronome\n") ; |
| 68 | printf ("=============================\n") ; |
| 69 | |
| 70 | piHiPri (50) ; |
| 71 | |
| 72 | wiringPiSetupSys () ; // Needed for timing functions |
| 73 | piFaceSetup (PIFACE) ; |
| 74 | |
| 75 | if (argc != 2) |
| 76 | { |
| 77 | printf ("Usage: %s <beates per minute>\n", argv [0]) ; |
| 78 | exit (1) ; |
| 79 | } |
| 80 | |
| 81 | if (strcmp (argv [1], "a") == 0) |
| 82 | middleA () ; |
| 83 | |
| 84 | bpm = atoi (argv [1]) ; |
| 85 | |
| 86 | if ((bpm < 40) || (bpm > 208)) |
| 87 | { |
| 88 | printf ("%s range is 40 through 208 beats per minute\n", argv [0]) ; |
| 89 | exit (1) ; |
| 90 | } |
| 91 | |
| 92 | msPerBeat = 60000 / bpm ; |
| 93 | |
| 94 | // Main loop: |
| 95 | // Put some random LED pairs up for a few seconds, then blank ... |
| 96 | |
| 97 | for (;;) |
| 98 | { |
| 99 | end = millis () + msPerBeat ; |
| 100 | |
| 101 | digitalWrite (PIFACE + 0, state) ; |
| 102 | digitalWrite (PIFACE + 1, state) ; |
| 103 | |
| 104 | while (millis () < end) |
| 105 | delayMicroseconds (500) ; |
| 106 | |
| 107 | state ^= 1 ; |
| 108 | } |
| 109 | |
| 110 | return 0 ; |
| 111 | } |