2016-03-24 13:39:50 +01:00
|
|
|
/***************************************************************
|
|
|
|
* *
|
|
|
|
* Taiko Sanro - Arduino *
|
|
|
|
* Support Arduino models with ATmega32u4 microprocessors *
|
|
|
|
* *
|
2016-03-27 14:36:32 +02:00
|
|
|
* Shiky Chang *
|
2016-03-24 13:39:50 +01:00
|
|
|
* zhangxunpx@gmail.com *
|
|
|
|
* *
|
|
|
|
***************************************************************/
|
|
|
|
|
|
|
|
#define MODE_JIRO 1
|
2016-03-27 14:36:32 +02:00
|
|
|
#define MODE_DEBUG 1
|
2016-03-24 13:39:50 +01:00
|
|
|
|
|
|
|
#define CHANNELS 4
|
|
|
|
|
2016-03-27 14:36:32 +02:00
|
|
|
#define SAMPLE_CACHE_LENGTH 50
|
2016-03-24 13:39:50 +01:00
|
|
|
#define POWER_CACHE_LENGTH 3
|
|
|
|
|
2016-03-27 14:36:32 +02:00
|
|
|
#define LIGHT_THRES 0
|
|
|
|
#define HEAVY_THRES 70000
|
2016-03-24 13:39:50 +01:00
|
|
|
|
|
|
|
#include <Keyboard.h>
|
2016-03-24 15:17:49 +01:00
|
|
|
#include "cache.h"
|
2016-03-27 16:20:13 +02:00
|
|
|
#include "limits.h"
|
|
|
|
|
|
|
|
#if MODE_JIRO
|
|
|
|
#define HEAVY_THRES LONG_MAX
|
|
|
|
#endif
|
2016-03-24 13:39:50 +01:00
|
|
|
|
|
|
|
int channelSample [CHANNELS];
|
2016-03-24 14:46:45 +01:00
|
|
|
int lastChannelSample [CHANNELS];
|
2016-03-27 14:36:32 +02:00
|
|
|
Cache <int, SAMPLE_CACHE_LENGTH> sampleCache [CHANNELS];
|
2016-03-24 13:39:50 +01:00
|
|
|
|
|
|
|
long int power [CHANNELS];
|
2016-03-27 14:36:32 +02:00
|
|
|
Cache <long int, POWER_CACHE_LENGTH> powerCache [CHANNELS];
|
2016-03-24 13:39:50 +01:00
|
|
|
|
|
|
|
bool triggered [CHANNELS];
|
|
|
|
|
2016-03-27 16:20:13 +02:00
|
|
|
int pins[] = { A0, A1, A2, A3 }; // L don, R don, L kat, R kat
|
|
|
|
char lightKeys[] = { 'g', 'h', 'f', 'j' };
|
|
|
|
char heavyKeys[] = { 't', 'y', 'r', 'u' };
|
2016-03-27 14:36:32 +02:00
|
|
|
|
2016-03-24 13:39:50 +01:00
|
|
|
void setup() {
|
|
|
|
Serial.begin (9600);
|
|
|
|
Keyboard.begin ();
|
|
|
|
analogReference (DEFAULT);
|
|
|
|
for (short int i = 0; i < CHANNELS; i++) {
|
|
|
|
power [i] = 0;
|
2016-03-24 14:46:45 +01:00
|
|
|
lastChannelSample [i] = 0;
|
2016-03-24 13:39:50 +01:00
|
|
|
triggered [i] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
2016-03-27 14:36:32 +02:00
|
|
|
#if MODE_DEBUG
|
2016-03-27 16:20:13 +02:00
|
|
|
long int sampleLastTime = 0;
|
|
|
|
long int calcLastTime = 0;
|
2016-03-27 14:36:32 +02:00
|
|
|
#endif
|
|
|
|
|
2016-03-24 13:39:50 +01:00
|
|
|
for (short int i = 0; i < CHANNELS; i++) {
|
2016-03-27 16:20:13 +02:00
|
|
|
|
|
|
|
#if MODE_DEBUG
|
|
|
|
long int lastTime = micros();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
channelSample[i] = analogRead (pins[i]);
|
|
|
|
|
|
|
|
#if MODE_DEBUG
|
|
|
|
sampleLastTime += micros () - lastTime;
|
|
|
|
lastTime = micros();
|
|
|
|
#endif
|
2016-03-27 14:36:32 +02:00
|
|
|
|
|
|
|
sampleCache [i].put (channelSample [i] - lastChannelSample [i]);
|
2016-03-24 13:39:50 +01:00
|
|
|
|
|
|
|
long int tempInt;
|
2016-03-27 14:36:32 +02:00
|
|
|
tempInt = sampleCache [i].get (1);
|
2016-03-24 13:39:50 +01:00
|
|
|
power [i] -= tempInt * tempInt;
|
2016-03-27 14:36:32 +02:00
|
|
|
tempInt = sampleCache [i].get ();
|
2016-03-24 13:39:50 +01:00
|
|
|
power [i] += tempInt * tempInt;
|
2016-03-27 14:36:32 +02:00
|
|
|
|
2016-03-24 13:39:50 +01:00
|
|
|
if (power [i] < LIGHT_THRES) {
|
|
|
|
power [i] = 0;
|
|
|
|
}
|
|
|
|
|
2016-03-27 14:36:32 +02:00
|
|
|
powerCache [i].put (power [i]);
|
2016-03-24 14:46:45 +01:00
|
|
|
lastChannelSample [i] = channelSample [i];
|
2016-03-27 14:36:32 +02:00
|
|
|
|
2016-03-27 16:20:13 +02:00
|
|
|
if (powerCache [i].get (1) == 0) {
|
|
|
|
triggered [i] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDescending = true;
|
2016-03-24 13:39:50 +01:00
|
|
|
for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++){
|
2016-03-27 16:20:13 +02:00
|
|
|
if (!triggered [i] && (powerCache [i].get (j - 1) > powerCache [i].get (j))) {
|
|
|
|
isDescending = false;
|
|
|
|
break;
|
2016-03-27 14:36:32 +02:00
|
|
|
}
|
2016-03-27 16:20:13 +02:00
|
|
|
}
|
|
|
|
if (isDescending) {
|
|
|
|
if (power [i] >= HEAVY_THRES) {
|
|
|
|
triggered [i] = true;
|
|
|
|
Keyboard.print (heavyKeys [i]);
|
|
|
|
} else if (power [i] >= LIGHT_THRES) {
|
|
|
|
triggered [i] = true;
|
|
|
|
Keyboard.print (lightKeys [i]);
|
2016-03-24 13:39:50 +01:00
|
|
|
}
|
2016-03-27 16:20:13 +02:00
|
|
|
|
|
|
|
#if MODE_DEBUG
|
|
|
|
calcLastTime += micros () - lastTime;
|
|
|
|
#endif
|
2016-03-24 13:39:50 +01:00
|
|
|
}
|
2016-03-24 14:46:45 +01:00
|
|
|
|
2016-03-27 14:36:32 +02:00
|
|
|
#if MODE_DEBUG
|
|
|
|
Serial.print (power [i]);
|
|
|
|
Serial.print ("\t");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// End of each channel
|
2016-03-24 13:39:50 +01:00
|
|
|
}
|
|
|
|
|
2016-03-27 14:36:32 +02:00
|
|
|
#if MODE_DEBUG
|
2016-03-27 16:20:13 +02:00
|
|
|
Serial.print(sampleLastTime);
|
|
|
|
Serial.print('\t');
|
|
|
|
Serial.print(calcLastTime);
|
2016-03-27 14:36:32 +02:00
|
|
|
Serial.println ("");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|