Merge remote-tracking branch 'refs/remotes/ShikyC/master'

This commit is contained in:
Ma Qiming 2016-03-27 20:37:22 +08:00
commit 94dbf66e0e
2 changed files with 65 additions and 45 deletions

View File

@ -11,39 +11,35 @@
#ifndef CACHE_H #ifndef CACHE_H
#define CACHE_H #define CACHE_H
template<class T, int L> template <class T, int L>
class Cache class Cache {
{
public: public:
Cache(); Cache ();
void put(T value); void put (T value);
T get(int offset = 0) const; T get (int offset = 0) const;
private: private:
T data_[L]; T data_ [L];
int current_ = 0; int current_ = 0;
}; };
template<class T, int L> template <class T, int L>
Cache<T,L>::Cache() Cache <T, L>::Cache () {
{ for (int i = 0; i < L; i++) {
for (int i = 0; i < L; i++){ data_ [i] = 0;
data_[i] = 0;
} }
} }
template<class T, int L> template <class T, int L>
void Cache<T,L>::put(T value) void Cache <T, L>::put (T value) {
{ data_ [current_] = value;
data_[current_] = value;
current_ = (current_ + 1) % L; current_ = (current_ + 1) % L;
} }
template<class T, int L> template <class T, int L>
T Cache<T,L>::get(int offset) const T Cache <T, L>::get (int offset) const {
{
int index = (current_ + offset) % L; int index = (current_ + offset) % L;
return data_[index]; return data_ [index];
} }
#endif // CACHE_H #endif // CACHE_H

View File

@ -3,36 +3,38 @@
* Taiko Sanro - Arduino * * Taiko Sanro - Arduino *
* Support Arduino models with ATmega32u4 microprocessors * * Support Arduino models with ATmega32u4 microprocessors *
* * * *
* Copyright © 2016 Shiky Chang * * Shiky Chang *
* zhangxunpx@gmail.com * * zhangxunpx@gmail.com *
* * * *
***************************************************************/ ***************************************************************/
#define MODE_JIRO 1 #define MODE_JIRO 1
#define MODE_DEBUG 1
#define CHANNELS 4 #define CHANNELS 4
// Input delay ~ (SAMPLE_CACHE_LENGTH + POWER_CACHE_LENGTH) / sample frequency #define SAMPLE_CACHE_LENGTH 50
// _CACHE_LENGTH must be less than 256
#define SAMPLE_CACHE_LENGTH 150
#define POWER_CACHE_LENGTH 3 #define POWER_CACHE_LENGTH 3
// _THRES must be less than 2^32 = 4294967296 = 4.29e9 #define LIGHT_THRES 0
#define LIGHT_THRES 2500000 #define HEAVY_THRES 70000
#define HEAVY_THRES 5000000
#include <Keyboard.h> #include <Keyboard.h>
#include "cache.h" #include "cache.h"
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];
#if MODE_DEBUG
long int lastTime;
#endif
void setup() { void setup() {
Serial.begin (9600); Serial.begin (9600);
Keyboard.begin (); Keyboard.begin ();
@ -42,37 +44,49 @@ void setup() {
lastChannelSample [i] = 0; lastChannelSample [i] = 0;
triggered [i] = false; triggered [i] = false;
} }
#if MODE_DEBUG
lastTime = 0;
#endif
} }
void loop() { void loop() {
// Analog input: 0~5V -> 0~1023
channelSample[0] = analogRead (A0); // L don channelSample[0] = analogRead (A0); // L don
channelSample[1] = analogRead (A1); // R don channelSample[1] = analogRead (A1); // R don
channelSample[2] = analogRead (A2); // L kat channelSample[2] = analogRead (A2); // L kat
channelSample[3] = analogRead (A3); // R kat channelSample[3] = analogRead (A3); // R kat
#if MODE_DEBUG
Serial.print (micros () - lastTime);
lastTime = micros ();
Serial.print ("\t");
#endif
for (short int i = 0; i < CHANNELS; i++) { for (short int i = 0; i < CHANNELS; 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];
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) == 0) {
triggered [i] = false;
}
if (!triggered) { if (!triggered) {
if (powerCache [i].get(j + 1) > powerCache [i].get(j) || j != POWER_CACHE_LENGTH - 2) { if (powerCache [i].get (j + 1) >= powerCache [i].get (j) || j != POWER_CACHE_LENGTH - 2) {
break; break;
} else { } else {
#if MODE_JIRO #if MODE_JIRO
@ -108,9 +122,19 @@ void loop() {
} }
} }
// This is the end of each channel #if MODE_DEBUG
Serial.print (power [i]);
Serial.print ("\t");
#endif
// End of each channel
} }
delay (1); #if MODE_DEBUG
Serial.print (micros () - lastTime);
lastTime = micros ();
Serial.println ("");
#endif
} }