Taiko-Nijiiro-Analog-IOBoard/sanro/sanro.ino

118 lines
3.1 KiB
Arduino
Raw Normal View History

2016-03-24 13:39:50 +01:00
/***************************************************************
* *
* Taiko Sanro - Arduino *
* Support Arduino models with ATmega32u4 microprocessors *
* *
2016-03-27 18:29:10 +02:00
* Shiky Chang Chris *
* zhangxunpx@gmail.com wisaly@gmail.com *
2016-03-24 13:39:50 +01:00
* *
***************************************************************/
#define MODE_JIRO 1
#define MODE_DEBUG 0
2016-03-24 13:39:50 +01:00
#define CHANNELS 4
#define SAMPLE_CACHE_LENGTH 12
2016-03-24 13:39:50 +01:00
#define POWER_CACHE_LENGTH 3
#define LIGHT_THRES 5000
#define HEAVY_THRES 20000
2016-03-24 13:39:50 +01:00
2016-03-27 18:29:10 +02:00
#include <limits.h>
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
#if MODE_JIRO
#define HEAVY_THRES LONG_MAX
#endif
2016-03-24 13:39:50 +01:00
unsigned long int lastTime;
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 18:29:10 +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;
}
lastTime = 0;
2016-03-24 13:39:50 +01:00
}
void loop() {
2016-03-27 14:36:32 +02:00
2016-03-24 13:39:50 +01:00
for (short int i = 0; i < CHANNELS; i++) {
2016-03-27 14:36:32 +02:00
channelSample[i] = analogRead (pins [i]);
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;
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 16:20:13 +02:00
if (powerCache [i].get (1) == 0) {
triggered [i] = false;
}
if (!triggered [i]) {
for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++) {
if (powerCache [i].get (j - 1) >= powerCache [i].get (j)) {
break;
} else if (powerCache [i].get (1) >= HEAVY_THRES) {
triggered [i] = true;
Keyboard.print (heavyKeys [i]);
} else if (powerCache [i].get (1) >= LIGHT_THRES) {
triggered [i] = true;
Keyboard.print (lightKeys [i]);
}
2016-03-24 13:39:50 +01:00
}
}
2016-03-24 14:46:45 +01:00
#if MODE_DEBUGjj
2016-03-27 14:36:32 +02:00
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
// Damn Arduino plotter, the Y axis scale changes too frequently! Use these values to "lock" the plotter's scale.
Serial.print (50000);
2016-03-27 18:29:10 +02:00
Serial.print ("\t");
Serial.print (0);
Serial.print ("\t");
2016-03-27 14:36:32 +02:00
Serial.println ("");
#endif
// Force the sample frequency to be less than 1000Hz
unsigned int frameTime = micros () - lastTime;
lastTime = micros ();
if (frameTime < 1000) {
delayMicroseconds (1000 - frameTime);
}
2016-03-27 18:29:10 +02:00
}