Merge pull request #2 from wisaly/master

refactor cache code
This commit is contained in:
ShikyC 2016-03-24 22:23:37 +08:00
commit 245af0eeb9
2 changed files with 57 additions and 20 deletions

49
sanro/cache.h Normal file
View File

@ -0,0 +1,49 @@
/***************************************************************
* *
* Taiko Sanro - Arduino *
* Cache data structure *
* *
* Chris *
* wisaly@gmail.com *
* *
***************************************************************/
#ifndef CACHE_H
#define CACHE_H
template<class T, int L>
class Cache
{
public:
Cache();
void put(T value);
T get(int offset = 0) const;
private:
T data_[L];
int current_ = 0;
};
template<class T, int L>
Cache<T,L>::Cache()
{
for (int i = 0; i < L; i++){
data_[i] = 0;
}
}
template<class T, int L>
void Cache<T,L>::put(T value)
{
data_[current_] = value;
current_ = (current_ + 1) % L;
}
template<class T, int L>
T Cache<T,L>::get(int offset) const
{
int index = (current_ + offset) % L;
return data_[index];
}
#endif // CACHE_H

View File

@ -22,15 +22,14 @@
#define HEAVY_THRES 5000000
#include <Keyboard.h>
#include "cache.h"
int channelSample [CHANNELS];
int lastChannelSample [CHANNELS];
int sampleCache [CHANNELS][SAMPLE_CACHE_LENGTH];
short int sampleCacheIndex [CHANNELS];
Cache<int,SAMPLE_CACHE_LENGTH> sampleCache [CHANNELS];
long int power [CHANNELS];
long int powerCache [CHANNELS][POWER_CACHE_LENGTH];
short int powerCacheIndex [CHANNELS];
Cache<long int,POWER_CACHE_LENGTH> powerCache [CHANNELS];
bool triggered [CHANNELS];
@ -39,14 +38,6 @@ void setup() {
Keyboard.begin ();
analogReference (DEFAULT);
for (short int i = 0; i < CHANNELS; i++) {
for (short int j = 0; j < SAMPLE_CACHE_LENGTH; j++) {
sampleCache [i][j] = 0;
}
sampleCacheIndex [i] = SAMPLE_CACHE_LENGTH - 1;
for (short int j = 0; j < POWER_CACHE_LENGTH; j++) {
powerCache [i][j] = 0;
}
powerCacheIndex [i] = POWER_CACHE_LENGTH - 1;
power [i] = 0;
lastChannelSample [i] = 0;
triggered [i] = false;
@ -63,28 +54,25 @@ void loop() {
for (short int i = 0; i < CHANNELS; i++) {
sampleCacheIndex [i] = (sampleCacheIndex [i] + 1) % SAMPLE_CACHE_LENGTH;
sampleCache [i][sampleCacheIndex [i]] = channelSample [i] - lastChannelSample [i];
sampleCache [i].put(channelSample [i] - lastChannelSample [i]);
long int tempInt;
tempInt = sampleCache [i][(sampleCacheIndex [i] + 1) % SAMPLE_CACHE_LENGTH];
tempInt = sampleCache [i].get(1);
power [i] -= tempInt * tempInt;
tempInt = sampleCache [i][sampleCacheIndex [i]];
tempInt = sampleCache [i].get();
power [i] += tempInt * tempInt;
if (power [i] < LIGHT_THRES) {
power [i] = 0;
}
powerCacheIndex [i] = (powerCacheIndex [i] + 1 ) % POWER_CACHE_LENGTH;
powerCache [i][powerCacheIndex [i]] = power [i];
powerCache [i].put(power [i]);
lastChannelSample [i] = channelSample [i];
for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++){
if (!triggered) {
if (powerCache [i][(powerCacheIndex [i] + j + 1) % POWER_CACHE_LENGTH] > powerCache [i][(powerCacheIndex [i] + j) % POWER_CACHE_LENGTH] || 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