forked from goran-mahovlic/StressPress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StressPress.ino
164 lines (133 loc) · 3.94 KB
/
StressPress.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
164
/*
* StressPress by Radiona
*
* You need to press anti stress ball to get more and more funny info
* Switch is placed in anti stress ball
*
* by: {"Irena Krčelić","Marko Jovanović","Goran Mahovlić"}
*/
#include <WaveHC.h>
#include <WaveUtil.h>
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the volumes root directory
FatReader file; // This object represent the WAV file
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
// time to play each tone in milliseconds
#define PLAY_TIME 2000
#define antiStressPin A0
/*
* Define macro to put error messages in flash memory
*/
#define error(msg) error_P(PSTR(msg))
boolean StressStatus;
uint8_t currentIndex = 0;
unsigned long oldMillis = 0;
//////////////////////////////////// SETUP
void setup() {
Serial.begin(9600);
pinMode(antiStressPin,INPUT);
if (!card.init()) error("card.init");
// enable optimized read - some cards may timeout
card.partialBlockRead(true);
if (!vol.init(card)) error("vol.init");
if (!root.openRoot(vol)) error("openRoot");
PgmPrintln("Index files");
indexFiles();
oldMillis = millis();
}
//////////////////////////////////// LOOP
void loop() {
StressStatus = digitalRead(antiStressPin);
if (millis() - oldMillis > 30000){
playIndex(0);
currentIndex = 0;
oldMillis = millis();
}
if (!StressStatus){
while(!StressStatus) {//Added to avoid hacking long hold)
StressStatus = digitalRead(antiStressPin);
}
if (currentIndex<11){
currentIndex++;
Serial.println(currentIndex);
}
else{
currentIndex = 0;}
playIndex(currentIndex);
oldMillis = millis();
}
}
/////////////////////////////////// HELPERS
/*
* print error message and halt
*/
void error_P(const char *str) {
PgmPrint("Error: ");
SerialPrint_P(str);
sdErrorCheck();
while(1);
}
/*
* print error message and halt if SD I/O error, great for debugging!
*/
void sdErrorCheck(void) {
if (!card.errorCode()) return;
PgmPrint("\r\nSD I/O error: ");
Serial.print(card.errorCode(), HEX);
PgmPrint(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
// Number of files.
#define FILE_COUNT 12
// Files are 'touch tone phone' DTMF tones, P = #, S = *
// Most phones don't have A, B, C, and D tones.
// file names are of the form DTMFx.WAV where x is one of
// the letters from fileLetter[]
char fileLetter[] = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B'};
// index of DTMF files in the root directory
uint16_t fileIndex[FILE_COUNT];
/*
* Find files and save file index. A file's index is is the
* index of it's directory entry in it's directory file.
*/
void indexFiles(void) {
char name[10];
// copy flash string to RAM
strcpy_P(name, PSTR("DTMFx.WAV"));
for (uint8_t i = 0; i < FILE_COUNT; i++) {
// Make file name
name[4] = fileLetter[i];
Serial.println(name);
// Open file by name
if (!file.open(root, name)) error("open by name");
// Save file's index (byte offset of directory entry divided by entry size)
// Current position is just after entry so subtract one.
fileIndex[i] = root.readPosition()/32 - 1;
}
PgmPrintln("Done");
}
/*
* Play file by index and print latency in ms
*/
void playIndex(uint8_t index) {
// start time
uint32_t t = millis();
// open by index
if (!file.open(root, fileIndex[index])) {
error("open by index");
}
// create and play Wave
if (!wave.create(file)) error("wave.create");
wave.play();
// print time to open file and start play
Serial.println(millis() - t);
// stop after PLAY_TIME ms
while((millis() - t) < PLAY_TIME);
wave.stop();
// check for play errors
sdErrorCheck();
PgmPrintln("Done");
}