Added files via upload

This commit is contained in:
ShikyC 2016-03-24 21:46:45 +08:00
parent 61e729ff1d
commit 4be4b54a3a

View File

@ -18,12 +18,13 @@
#define POWER_CACHE_LENGTH 3
// _THRES must be less than 2^32 = 4294967296 = 4.29e9
#define LIGHT_THRES 3000000
#define HEAVY_THRES 10000000
#define LIGHT_THRES 2500000
#define HEAVY_THRES 5000000
#include <Keyboard.h>
int channelSample [CHANNELS];
int lastChannelSample [CHANNELS];
int sampleCache [CHANNELS][SAMPLE_CACHE_LENGTH];
short int sampleCacheIndex [CHANNELS];
@ -47,6 +48,7 @@ void setup() {
}
powerCacheIndex [i] = POWER_CACHE_LENGTH - 1;
power [i] = 0;
lastChannelSample [i] = 0;
triggered [i] = false;
}
}
@ -59,40 +61,16 @@ void loop() {
channelSample[2] = analogRead (A2); // L kat
channelSample[3] = analogRead (A3); // R kat
// Cache Operation
//
// Init status: [ 0 ] [ 1 ] [ 2 ] ... [n-3] [n-2] [n-1]
// |
// Pointer
//---------------------------------------------------------------
// Loop 0: [NEW] [ 1 ] [ 2 ] ... [n-3] [NEW] [n-1]
// | |
// Pointer Newest Diff
//---------------------------------------------------------------
// Loop 1: [ 0 ] [NEW] [ 2 ] ... [n-3] [n-2] [NEW]
// | | |
// Last sample Pointer Newest Diff
//---------------------------------------------------------------
//
// ...
// Pointer + 1: oldest differentiated sample
// Pointer : newest sample (original value from the analog port) <----
// Pointer - 1: last sample (original value from the analog port)
// Pointer - 2: newest differentiated sample
// Pointer - 3: last differentiated sample
// ...
for (short int i = 0; i < CHANNELS; i++) {
sampleCacheIndex [i] = (sampleCacheIndex [i] + 1) % SAMPLE_CACHE_LENGTH;
sampleCache [i][sampleCacheIndex [i]] = channelSample [i];
sampleCache [i][(sampleCacheIndex [i] - 2) % SAMPLE_CACHE_LENGTH] = channelSample [i] - sampleCache [i][(sampleCacheIndex [i] - 1) % SAMPLE_CACHE_LENGTH];
sampleCache [i][sampleCacheIndex [i]] = channelSample [i] - lastChannelSample [i];
long int tempInt;
tempInt = sampleCache [i][(sampleCacheIndex [i] + 1) % SAMPLE_CACHE_LENGTH];
power [i] -= tempInt * tempInt;
tempInt = sampleCache [i][(sampleCacheIndex [i] - 2) % SAMPLE_CACHE_LENGTH];
tempInt = sampleCache [i][sampleCacheIndex [i]];
power [i] += tempInt * tempInt;
if (power [i] < LIGHT_THRES) {
@ -102,9 +80,11 @@ void loop() {
powerCacheIndex [i] = (powerCacheIndex [i] + 1 ) % POWER_CACHE_LENGTH;
powerCache [i][powerCacheIndex [i]] = 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) % POWER_CACHE_LENGTH] > powerCache [i][(powerCacheIndex [i] + j - 1) % POWER_CACHE_LENGTH] || j != POWER_CACHE_LENGTH - 2) {
if (powerCache [i][(powerCacheIndex [i] + j + 1) % POWER_CACHE_LENGTH] > powerCache [i][(powerCacheIndex [i] + j) % POWER_CACHE_LENGTH] || j != POWER_CACHE_LENGTH - 2) {
break;
} else {
#if MODE_JIRO