勃爆,不做二五仔

This commit is contained in:
ShikyC 2017-11-21 23:25:19 -05:00
parent 3aa41ca472
commit 0cd6127249

View File

@ -12,40 +12,41 @@
// ceramic sensors (the same sensors used in electirc drum kit). // ceramic sensors (the same sensors used in electirc drum kit).
// No longer need microphones. // No longer need microphones.
#define MODE_DEBUG 0
#define CHANNELS 2 #define CHANNELS 2
// Caches for the soundwave and power
#define SAMPLE_CACHE_LENGTH 12 #define SAMPLE_CACHE_LENGTH 12
#define POWER_CACHE_LENGTH 3 #define POWER_CACHE_LENGTH 3
// Light and heacy hit thresholds #define LIGHT_THRES 8000
#define LIGHT_THRES 5000
#define HEAVY_THRES 20000 #define HEAVY_THRES 20000
// Forced sampling frequency
#define FORCED_FREQ 1000 #define FORCED_FREQ 1000
#include "cache.h" #include "cache.h"
unsigned long int lastTime; unsigned long int lastTime;
int channelSample [CHANNELS]; float channelSample [CHANNELS];
int lastChannelSample [CHANNELS]; float lastChannelSample [CHANNELS];
Cache <int, SAMPLE_CACHE_LENGTH> sampleCache [CHANNELS]; Cache <float, SAMPLE_CACHE_LENGTH> sampleCache [CHANNELS];
long int power [CHANNELS]; float power [CHANNELS];
Cache <long int, POWER_CACHE_LENGTH> powerCache [CHANNELS]; Cache <float, POWER_CACHE_LENGTH> powerCache [CHANNELS];
bool triggered [CHANNELS]; bool triggered [CHANNELS];
int pins[] = {A0, A1}; // Don, Kat int input_pins[] = {A0, A1}; // Don, Kat
int output_pins[] = {A2, A3, A4, A5}; // Left Don, Left Kat, Right Don, Right Kat
void setup() { void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
Serial.begin (9600); Serial.begin (9600);
Keyboard.begin (); analogReference (INTERNAL);
analogReference (DEFAULT);
for (short int i = 0; i < CHANNELS; i++) { for (short int i = 0; i < CHANNELS; i++) {
power [i] = 0; power [i] = 0;
lastChannelSample [i] = 0; lastChannelSample [i] = 0;
@ -58,22 +59,22 @@ void loop() {
for (short int i = 0; i < CHANNELS; i++) { for (short int i = 0; i < CHANNELS; i++) {
channelSample[i] = analogRead (pins [i]); channelSample[i] = analogRead(input_pins[i]);
sampleCache [i].put (channelSample [i] - lastChannelSample [i]); sampleCache[i].put(channelSample [i]);
long int tempInt; long int tempInt;
tempInt = sampleCache[i].get(1); tempInt = sampleCache[i].get(1);
power[i] -= tempInt * tempInt; power[i] -= tempInt * tempInt;
tempInt = sampleCache[i].get(); tempInt = sampleCache[i].get();
power[i] += tempInt * tempInt; power[i] += tempInt * tempInt;
if (power [i] < LIGHT_THRES) {
power [i] = 0;
}
powerCache[i].put(power[i]); powerCache[i].put(power[i]);
lastChannelSample[i] = channelSample[i]; lastChannelSample[i] = channelSample[i];
if (powerCache [i].get (1) == 0) { if (powerCache[i].get(1) < LIGHT_THRES) {
triggered[i] = false; triggered[i] = false;
digitalWrite(output_pins[i], HIGH);
digitalWrite(output_pins[i] + 2, HIGH);
digitalWrite(LED_BUILTIN, LOW);
} }
if (!triggered [i]) { if (!triggered [i]) {
@ -82,38 +83,21 @@ void loop() {
break; break;
} else if (powerCache [i].get (1) >= HEAVY_THRES) { } else if (powerCache [i].get (1) >= HEAVY_THRES) {
triggered [i] = true; triggered [i] = true;
Keyboard.print (heavyKeys [i]); digitalWrite(output_pins[i], LOW);
digitalWrite(output_pins[i] + 2, LOW);
digitalWrite(LED_BUILTIN, HIGH);
} else if (powerCache [i].get (1) >= LIGHT_THRES) { } else if (powerCache [i].get (1) >= LIGHT_THRES) {
triggered [i] = true; triggered [i] = true;
Keyboard.print (lightKeys [i]); digitalWrite(output_pins[i], LOW);
digitalWrite(LED_BUILTIN, HIGH);
} }
} }
} }
#if MODE_DEBUG
Serial.print (power [i]);
Serial.print ("\t");
#endif
// End of each channel
}
#if MODE_DEBUG
Serial.print (50000);
Serial.print ("\t");
Serial.print (0);
Serial.print ("\t");
Serial.println ("");
#endif
// Force the sample frequency to be less than 1000Hz
unsigned int frameTime = micros () - lastTime; unsigned int frameTime = micros () - lastTime;
lastTime = micros (); lastTime = micros ();
if (frameTime < FORCED_FREQ) { if (frameTime < FORCED_FREQ) {
delayMicroseconds (FORCED_FREQ - frameTime); delayMicroseconds (FORCED_FREQ - frameTime);
} else { }
// Performance bottleneck;
Serial.print ("Exception: forced frequency is too high for the microprocessor to catch up.");
} }
} }