Update sanro.ino

This commit is contained in:
ShikyC 2017-01-16 16:45:42 -05:00 committed by GitHub
parent b33272e00a
commit 2fdde3f4a4

View File

@ -1,117 +1,116 @@
/*************************************************************** /***************************************************************
* * * *
* Taiko Sanro - Arduino * * Taiko Sanro - Arduino *
* Support Arduino models with ATmega32u4 microprocessors * * Support Arduino models with ATmega32u4 microprocessors *
* * * *
* Shiky Chang Chris * * Shiky Chang Chris *
* zhangxunpx@gmail.com wisaly@gmail.com * * zhangxunpx@gmail.com wisaly@gmail.com *
* * * *
***************************************************************/ ***************************************************************/
#define MODE_JIRO 1 #define MODE_JIRO 1
#define MODE_DEBUG 0 #define MODE_DEBUG 0
#define CHANNELS 4 #define CHANNELS 4
#define SAMPLE_CACHE_LENGTH 12 #define SAMPLE_CACHE_LENGTH 12
#define POWER_CACHE_LENGTH 3 #define POWER_CACHE_LENGTH 3
#define LIGHT_THRES 5000 #define LIGHT_THRES 5000
#define HEAVY_THRES 20000 #define HEAVY_THRES 20000
#include <limits.h> #include <limits.h>
#include <Keyboard.h> #include <Keyboard.h>
#include "cache.h" #include "cache.h"
#if MODE_JIRO #if MODE_JIRO
#define HEAVY_THRES LONG_MAX #define HEAVY_THRES LONG_MAX
#endif #endif
unsigned long int lastTime; unsigned long int lastTime;
int channelSample [CHANNELS]; int channelSample [CHANNELS];
int lastChannelSample [CHANNELS]; int lastChannelSample [CHANNELS];
Cache <int, SAMPLE_CACHE_LENGTH> sampleCache [CHANNELS]; Cache <int, SAMPLE_CACHE_LENGTH> sampleCache [CHANNELS];
long int power [CHANNELS]; long int power [CHANNELS];
Cache <long int, POWER_CACHE_LENGTH> powerCache [CHANNELS]; Cache <long int, POWER_CACHE_LENGTH> powerCache [CHANNELS];
bool triggered [CHANNELS]; bool triggered [CHANNELS];
int pins[] = {A0, A1, A2, A3}; // L don, R don, L kat, R kat int pins[] = {A0, A1, A2, A3}; // L don, R don, L kat, R kat
char lightKeys[] = {'g', 'h', 'f', 'j'}; char lightKeys[] = {'g', 'h', 'f', 'j'};
char heavyKeys[] = {'t', 'y', 'r', 'u'}; char heavyKeys[] = {'t', 'y', 'r', 'u'};
void setup() { void setup() {
Serial.begin (9600); Serial.begin (9600);
Keyboard.begin (); Keyboard.begin ();
analogReference (DEFAULT); analogReference (DEFAULT);
for (short int i = 0; i < CHANNELS; i++) { for (short int i = 0; i < CHANNELS; i++) {
power [i] = 0; power [i] = 0;
lastChannelSample [i] = 0; lastChannelSample [i] = 0;
triggered [i] = false; triggered [i] = false;
} }
lastTime = 0; lastTime = 0;
} }
void loop() { void loop() {
for (short int i = 0; i < CHANNELS; i++) { for (short int i = 0; i < CHANNELS; i++) {
channelSample[i] = analogRead (pins [i]); channelSample[i] = analogRead (pins [i]);
sampleCache [i].put (channelSample [i] - lastChannelSample [i]); sampleCache [i].put (channelSample [i] - lastChannelSample [i]);
long int tempInt; long int tempInt;
tempInt = sampleCache [i].get (1); tempInt = sampleCache [i].get (1);
power [i] -= tempInt * tempInt; power [i] -= tempInt * tempInt;
tempInt = sampleCache [i].get (); tempInt = sampleCache [i].get ();
power [i] += tempInt * tempInt; power [i] += tempInt * tempInt;
if (power [i] < LIGHT_THRES) { if (power [i] < LIGHT_THRES) {
power [i] = 0; power [i] = 0;
} }
powerCache [i].put (power [i]); powerCache [i].put (power [i]);
lastChannelSample [i] = channelSample [i]; lastChannelSample [i] = channelSample [i];
if (powerCache [i].get (1) == 0) { if (powerCache [i].get (1) == 0) {
triggered [i] = false; triggered [i] = false;
} }
if (!triggered [i]) { if (!triggered [i]) {
for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++) { for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++) {
if (powerCache [i].get (j - 1) >= powerCache [i].get (j)) { if (powerCache [i].get (j - 1) >= powerCache [i].get (j)) {
break; break;
} else if (powerCache [i].get (1) >= HEAVY_THRES) { } else if (powerCache [i].get (1) >= HEAVY_THRES) {
triggered [i] = true; triggered [i] = true;
Keyboard.print (heavyKeys [i]); Keyboard.print (heavyKeys [i]);
} else if (powerCache [i].get (1) >= LIGHT_THRES) { } else if (powerCache [i].get (1) >= LIGHT_THRES) {
triggered [i] = true; triggered [i] = true;
Keyboard.print (lightKeys [i]); Keyboard.print (lightKeys [i]);
} }
} }
} }
#if MODE_DEBUGjj #if MODE_DEBUG
Serial.print (power [i]); Serial.print (power [i]);
Serial.print ("\t"); Serial.print ("\t");
#endif #endif
// End of each channel // End of each channel
} }
#if MODE_DEBUG #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);
Serial.print (50000); Serial.print ("\t");
Serial.print ("\t"); Serial.print (0);
Serial.print (0); Serial.print ("\t");
Serial.print ("\t");
Serial.println ("");
Serial.println (""); #endif
#endif
// Force the sample frequency to be less than 1000Hz
// Force the sample frequency to be less than 1000Hz unsigned int frameTime = micros () - lastTime;
unsigned int frameTime = micros () - lastTime; lastTime = micros ();
lastTime = micros (); if (frameTime < 1000) {
if (frameTime < 1000) { delayMicroseconds (1000 - frameTime);
delayMicroseconds (1000 - frameTime); }
}
} }