Simon Ford
/
DrumMachine
A drum machine sequenced by text file
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 // drum machine sequenced by text file, sford 00002 // e.g. line contains -o--, where o is a hit 00003 00004 #include "mbed.h" 00005 00006 class Drum { 00007 public: 00008 Drum(PinName p) : x(p) {} 00009 00010 void off() { x = 0; } 00011 void hit() { 00012 x = 1; 00013 t.attach(this, &Drum::off, 0.01); 00014 } 00015 00016 DigitalOut x; 00017 Timeout t; 00018 }; 00019 00020 Drum drums[4] = {p21, p22, p23, p24}; 00021 AnalogIn speed(p20); 00022 BusOut leds(LED1, LED2, LED3, LED4); 00023 00024 LocalFileSystem local("local"); 00025 00026 int main() { 00027 int count = 0; 00028 while(1) { 00029 FILE *fp = fopen("/local/drums.txt", "r"); 00030 if(!fp) { 00031 error("Couldn't open file\n"); 00032 } 00033 char tracks[4]; 00034 while(!feof(fp)) { 00035 fscanf(fp, "%c%c%c%c\n", &tracks[0], &tracks[1], &tracks[2], &tracks[3]); 00036 for(int i=0; i<4; i++) { 00037 if(tracks[i] == 'o') { 00038 drums[i].hit(); 00039 } 00040 } 00041 leds = 1 << (count % 4); 00042 count++; 00043 wait(0.05 + speed); 00044 } 00045 count = 0; 00046 fclose(fp); 00047 } 00048 }
Generated on Tue Jul 12 2022 20:14:43 by 1.7.2