refactor code

This commit is contained in:
Ma Qiming 2016-03-27 22:20:13 +08:00
parent b2600886aa
commit 5d95133c45

View File

@ -21,6 +21,11 @@
#include <Keyboard.h> #include <Keyboard.h>
#include "cache.h" #include "cache.h"
#include "limits.h"
#if MODE_JIRO
#define HEAVY_THRES LONG_MAX
#endif
int channelSample [CHANNELS]; int channelSample [CHANNELS];
int lastChannelSample [CHANNELS]; int lastChannelSample [CHANNELS];
@ -31,9 +36,9 @@ Cache <long int, POWER_CACHE_LENGTH> powerCache [CHANNELS];
bool triggered [CHANNELS]; bool triggered [CHANNELS];
#if MODE_DEBUG int pins[] = { A0, A1, A2, A3 }; // L don, R don, L kat, R kat
long int lastTime; char lightKeys[] = { 'g', 'h', 'f', 'j' };
#endif char heavyKeys[] = { 't', 'y', 'r', 'u' };
void setup() { void setup() {
Serial.begin (9600); Serial.begin (9600);
@ -44,26 +49,28 @@ void setup() {
lastChannelSample [i] = 0; lastChannelSample [i] = 0;
triggered [i] = false; triggered [i] = false;
} }
#if MODE_DEBUG
lastTime = 0;
#endif
} }
void loop() { void loop() {
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 #if MODE_DEBUG
Serial.print (micros () - lastTime); long int sampleLastTime = 0;
lastTime = micros (); long int calcLastTime = 0;
Serial.print ("\t");
#endif #endif
for (short int i = 0; i < CHANNELS; i++) { for (short int i = 0; i < CHANNELS; i++) {
#if MODE_DEBUG
long int lastTime = micros();
#endif
channelSample[i] = analogRead (pins[i]);
#if MODE_DEBUG
sampleLastTime += micros () - lastTime;
lastTime = micros();
#endif
sampleCache [i].put (channelSample [i] - lastChannelSample [i]); sampleCache [i].put (channelSample [i] - lastChannelSample [i]);
long int tempInt; long int tempInt;
@ -79,48 +86,30 @@ void loop() {
powerCache [i].put (power [i]); powerCache [i].put (power [i]);
lastChannelSample [i] = channelSample [i]; lastChannelSample [i] = channelSample [i];
for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++){ if (powerCache [i].get (1) == 0) {
if (powerCache [i].get (j) == 0) {
triggered [i] = false; triggered [i] = false;
} }
if (!triggered [i]) { bool isDescending = true;
if (powerCache [i].get (j + 1) >= powerCache [i].get (j) || j != POWER_CACHE_LENGTH - 2) { for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++){
if (!triggered [i] && (powerCache [i].get (j - 1) > powerCache [i].get (j))) {
isDescending = false;
break; break;
} else {
#if MODE_JIRO
if (power [i] >= LIGHT_THRES) {
triggered [i] = true;
switch (i) {
case 0: Keyboard.print ('g'); break;
case 1: Keyboard.print ('h'); break;
case 2: Keyboard.print ('f'); break;
case 3: Keyboard.print ('j'); break;
} }
} }
#else if (isDescending) {
if (power [i] >= HEAVY_THRES) { if (power [i] >= HEAVY_THRES) {
triggered [i] = true; triggered [i] = true;
switch (i) { Keyboard.print (heavyKeys [i]);
case 0: Keyboard.print ('t'); break;
case 1: Keyboard.print ('y'); break;
case 2: Keyboard.print ('r'); break;
case 3: Keyboard.print ('u'); break;
}
} else if (power [i] >= LIGHT_THRES) { } else if (power [i] >= LIGHT_THRES) {
triggered [i] = true; triggered [i] = true;
switch (i) { Keyboard.print (lightKeys [i]);
case 0: Keyboard.print ('g'); break;
case 1: Keyboard.print ('h'); break;
case 2: Keyboard.print ('f'); break;
case 3: Keyboard.print ('j'); break;
}
} }
#if MODE_DEBUG
calcLastTime += micros () - lastTime;
#endif #endif
} }
}
}
#if MODE_DEBUG #if MODE_DEBUG
Serial.print (power [i]); Serial.print (power [i]);
@ -131,8 +120,9 @@ void loop() {
} }
#if MODE_DEBUG #if MODE_DEBUG
Serial.print (micros () - lastTime); Serial.print(sampleLastTime);
lastTime = micros (); Serial.print('\t');
Serial.print(calcLastTime);
Serial.println (""); Serial.println ("");
#endif #endif