Added files via upload

This commit is contained in:
ShikyC 2016-03-27 20:36:32 +08:00
parent 245af0eeb9
commit 15ce24de82
2 changed files with 65 additions and 45 deletions

View File

@ -11,39 +11,35 @@
#ifndef CACHE_H
#define CACHE_H
template<class T, int L>
class Cache
{
template <class T, int L>
class Cache {
public:
Cache();
void put(T value);
T get(int offset = 0) const;
Cache ();
void put (T value);
T get (int offset = 0) const;
private:
T data_[L];
T data_ [L];
int current_ = 0;
};
template<class T, int L>
Cache<T,L>::Cache()
{
for (int i = 0; i < L; i++){
data_[i] = 0;
template <class T, int L>
Cache <T, L>::Cache () {
for (int i = 0; i < L; i++) {
data_ [i] = 0;
}
}
template<class T, int L>
void Cache<T,L>::put(T value)
{
data_[current_] = value;
template <class T, int L>
void Cache <T, L>::put (T value) {
data_ [current_] = value;
current_ = (current_ + 1) % L;
}
template<class T, int L>
T Cache<T,L>::get(int offset) const
{
template <class T, int L>
T Cache <T, L>::get (int offset) const {
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 *
* Support Arduino models with ATmega32u4 microprocessors *
* *
* Copyright © 2016 Shiky Chang *
* Shiky Chang *
* zhangxunpx@gmail.com *
* *
***************************************************************/
#define MODE_JIRO 1
#define MODE_DEBUG 1
#define CHANNELS 4
// Input delay ~ (SAMPLE_CACHE_LENGTH + POWER_CACHE_LENGTH) / sample frequency
// _CACHE_LENGTH must be less than 256
#define SAMPLE_CACHE_LENGTH 150
#define SAMPLE_CACHE_LENGTH 50
#define POWER_CACHE_LENGTH 3
// _THRES must be less than 2^32 = 4294967296 = 4.29e9
#define LIGHT_THRES 2500000
#define HEAVY_THRES 5000000
#define LIGHT_THRES 0
#define HEAVY_THRES 70000
#include <Keyboard.h>
#include "cache.h"
int channelSample [CHANNELS];
int lastChannelSample [CHANNELS];
Cache<int,SAMPLE_CACHE_LENGTH> sampleCache [CHANNELS];
Cache <int, SAMPLE_CACHE_LENGTH> sampleCache [CHANNELS];
long int power [CHANNELS];
Cache<long int,POWER_CACHE_LENGTH> powerCache [CHANNELS];
Cache <long int, POWER_CACHE_LENGTH> powerCache [CHANNELS];
bool triggered [CHANNELS];
#if MODE_DEBUG
long int lastTime;
#endif
void setup() {
Serial.begin (9600);
Keyboard.begin ();
@ -42,37 +44,49 @@ void setup() {
lastChannelSample [i] = 0;
triggered [i] = false;
}
#if MODE_DEBUG
lastTime = 0;
#endif
}
void loop() {
// Analog input: 0~5V -> 0~1023
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
Serial.print (micros () - lastTime);
lastTime = micros ();
Serial.print ("\t");
#endif
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;
tempInt = sampleCache [i].get(1);
tempInt = sampleCache [i].get (1);
power [i] -= tempInt * tempInt;
tempInt = sampleCache [i].get();
tempInt = sampleCache [i].get ();
power [i] += tempInt * tempInt;
if (power [i] < LIGHT_THRES) {
power [i] = 0;
}
powerCache [i].put(power [i]);
powerCache [i].put (power [i]);
lastChannelSample [i] = channelSample [i];
for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++){
if (powerCache [i].get (j) == 0) {
triggered [i] = false;
}
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;
} else {
#if MODE_JIRO
@ -108,9 +122,19 @@ void loop() {
}
}
// This is the end of each channel
}
delay (1);
}
#if MODE_DEBUG
Serial.print (power [i]);
Serial.print ("\t");
#endif
// End of each channel
}
#if MODE_DEBUG
Serial.print (micros () - lastTime);
lastTime = micros ();
Serial.println ("");
#endif
}