-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeatleBite.ino
163 lines (125 loc) · 4.45 KB
/
BeatleBite.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <ProTrinketKeyboard.h>
constexpr int sensorPin = A1;
constexpr int threshold = 80;
constexpr int samples = 250;
constexpr int sample_period =10000 / samples;
//init adc to bypass analogRead (overkill?)
void initChannel(){
uint8_t pin = sensorPin;
constexpr uint8_t analog_reference = DEFAULT;
#if defined(analogPinToChannel)
#if defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
#endif
pin = analogPinToChannel(pin);
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
#elif defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
if (pin >= 24) pin -= 24; // allow for channel or pin numbers
#else
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
#endif
#if defined(ADCSRB) && defined(MUX5)
// the MUX5 bit of ADCSRB selects whether we're reading from channels
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
#endif
DIDR0 = 0x01; // turn off the digital input for adc0
ADMUX = (analog_reference << 6) | (pin & 0x07);
ADCSRA = 0;
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
ADCSRA |= (1 << ADEN); //enable ADC
}
//get a reading. oversample to increase resolution, and discard last bit to reduce noise
int getData(){
constexpr int bit_over = 3;
constexpr int oversample_len = pow(4, bit_over);
int oversample[oversample_len];
ADCSRA |= (1 << ADSC); // start the conversion
while(ADCSRA & (1 << ADSC)); //wait and discard first lecture
for(int i=0; i<oversample_len; i++){
ADCSRA |= (1 << ADSC);
while(ADCSRA & (1 << ADSC));
byte m = ADCL; // fetch adc data
byte j = ADCH;
oversample[i] = (j << 8) | m; // form into an int
}
int acc=0;
for(int i=0; i<oversample_len; i++){
acc+=oversample[i];
}
return acc >> (bit_over+1);
}
void setup(){
initChannel();
delay(500);
// start USB stuff
TrinketKeyboard.begin();
pinMode(13, OUTPUT);
}
uint16_t samples_buffer[samples];
void loop(){
for(int i =0; i<samples; i++)
samples_buffer[i]=0;
long led_last_time=millis();
int lstatus=LOW;
//wait for first reading above Threshold
while(true){
TrinketKeyboard.poll();
//slow blink
if(millis()-led_last_time >= 1000){
led_last_time=millis();
lstatus= (lstatus==LOW)? HIGH : LOW;
digitalWrite(13, lstatus);
}
uint16_t value=getData();
if(value>=threshold){
samples_buffer[0]=value;
break;
}
}
int index=1;
long first_time=millis();
//fill samples buffer
long last_time=millis();
while(index<samples){
TrinketKeyboard.poll();
//medium blink
if(millis()-led_last_time >= 100){
led_last_time=millis();
lstatus= (lstatus==LOW)? HIGH : LOW;
digitalWrite(13, lstatus);
}
//space evenly analog reads
if(millis() - last_time < sample_period)
continue;
last_time=millis();
int value= getData();
samples_buffer[index]=value;
++index;
}
long running_time=millis()-first_time;
//output the buffer as keypresses
for(int i = 0; i<samples; ++i){
//fast blink
if(millis()-led_last_time >= 10){
led_last_time=millis();
lstatus= (lstatus==LOW)? HIGH : LOW;
digitalWrite(13, lstatus);
}
TrinketKeyboard.println(samples_buffer[i]);
}
//write total acquisition time
TrinketKeyboard.pressKey(0, KEYCODE_ARROW_UP);
TrinketKeyboard.pressKey(0,0);
TrinketKeyboard.pressKey(0, KEYCODE_ARROW_RIGHT);
TrinketKeyboard.pressKey(0,0);
TrinketKeyboard.print(running_time);
TrinketKeyboard.pressKey(0, KEYCODE_ARROW_LEFT);
TrinketKeyboard.pressKey(0,0);
TrinketKeyboard.pressKey(0, KEYCODE_ARROW_DOWN);
TrinketKeyboard.pressKey(0,0);
delay(2000);
}