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

@ -12,8 +12,7 @@
#define CACHE_H
template <class T, int L>
class Cache
{
class Cache {
public:
Cache ();
void put (T value);
@ -25,23 +24,20 @@ private:
};
template <class T, int L>
Cache<T,L>::Cache()
{
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)
{
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
{
T Cache <T, L>::get (int offset) const {
int index = (current_ + offset) % L;
return data_ [index];
}

View File

@ -3,23 +3,21 @@
* 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"
@ -33,6 +31,10 @@ 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,16 +44,24 @@ 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]);
@ -67,12 +77,16 @@ void loop() {
}
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
#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
}