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

@ -12,8 +12,7 @@
#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);
@ -25,23 +24,20 @@ private:
}; };
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];
} }

View File

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