diff --git a/sanro/cache.h b/sanro/cache.h index 891de9a..c246c8f 100644 --- a/sanro/cache.h +++ b/sanro/cache.h @@ -11,39 +11,35 @@ #ifndef CACHE_H #define CACHE_H -template -class Cache -{ +template +class Cache { public: - Cache(); - void put(T value); - T get(int offset = 0) const; + Cache (); + void put (T value); + T get (int offset = 0) const; private: - T data_[L]; + T data_ [L]; int current_ = 0; }; -template -Cache::Cache() -{ - for (int i = 0; i < L; i++){ - data_[i] = 0; +template +Cache ::Cache () { + for (int i = 0; i < L; i++) { + data_ [i] = 0; } } -template -void Cache::put(T value) -{ - data_[current_] = value; +template +void Cache ::put (T value) { + data_ [current_] = value; current_ = (current_ + 1) % L; } -template -T Cache::get(int offset) const -{ +template +T Cache ::get (int offset) const { int index = (current_ + offset) % L; - return data_[index]; + return data_ [index]; } -#endif // CACHE_H +#endif // CACHE_H diff --git a/sanro/sanro.ino b/sanro/sanro.ino index b7a9697..9d564b0 100644 --- a/sanro/sanro.ino +++ b/sanro/sanro.ino @@ -3,36 +3,38 @@ * Taiko Sanro - Arduino * * Support Arduino models with ATmega32u4 microprocessors * * * - * Copyright © 2016 Shiky Chang * + * Shiky Chang * * zhangxunpx@gmail.com * * * ***************************************************************/ #define MODE_JIRO 1 +#define MODE_DEBUG 1 #define CHANNELS 4 -// Input delay ~ (SAMPLE_CACHE_LENGTH + POWER_CACHE_LENGTH) / sample frequency -// _CACHE_LENGTH must be less than 256 -#define SAMPLE_CACHE_LENGTH 150 +#define SAMPLE_CACHE_LENGTH 50 #define POWER_CACHE_LENGTH 3 -// _THRES must be less than 2^32 = 4294967296 = 4.29e9 -#define LIGHT_THRES 2500000 -#define HEAVY_THRES 5000000 +#define LIGHT_THRES 0 +#define HEAVY_THRES 70000 #include #include "cache.h" int channelSample [CHANNELS]; int lastChannelSample [CHANNELS]; -Cache sampleCache [CHANNELS]; +Cache sampleCache [CHANNELS]; long int power [CHANNELS]; -Cache powerCache [CHANNELS]; +Cache powerCache [CHANNELS]; bool triggered [CHANNELS]; +#if MODE_DEBUG +long int lastTime; +#endif + void setup() { Serial.begin (9600); Keyboard.begin (); @@ -42,37 +44,49 @@ void setup() { lastChannelSample [i] = 0; triggered [i] = false; } +#if MODE_DEBUG +lastTime = 0; +#endif } void loop() { -// Analog input: 0~5V -> 0~1023 channelSample[0] = analogRead (A0); // L don channelSample[1] = analogRead (A1); // R don channelSample[2] = analogRead (A2); // L kat channelSample[3] = analogRead (A3); // R kat +#if MODE_DEBUG + Serial.print (micros () - lastTime); + lastTime = micros (); + Serial.print ("\t"); +#endif + for (short int i = 0; i < CHANNELS; i++) { - - sampleCache [i].put(channelSample [i] - lastChannelSample [i]); + + sampleCache [i].put (channelSample [i] - lastChannelSample [i]); long int tempInt; - tempInt = sampleCache [i].get(1); + tempInt = sampleCache [i].get (1); power [i] -= tempInt * tempInt; - tempInt = sampleCache [i].get(); + tempInt = sampleCache [i].get (); 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]; - + for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++){ + + if (powerCache [i].get (j) == 0) { + triggered [i] = false; + } + if (!triggered) { - if (powerCache [i].get(j + 1) > powerCache [i].get(j) || j != POWER_CACHE_LENGTH - 2) { + if (powerCache [i].get (j + 1) >= powerCache [i].get (j) || j != POWER_CACHE_LENGTH - 2) { break; } else { #if MODE_JIRO @@ -108,9 +122,19 @@ void loop() { } } -// This is the end of each channel - } - - delay (1); -} +#if MODE_DEBUG + Serial.print (power [i]); + Serial.print ("\t"); +#endif +// End of each channel + } + +#if MODE_DEBUG + Serial.print (micros () - lastTime); + lastTime = micros (); + Serial.println (""); +#endif + +} +