/
ducky_arduino.ino
155 lines (121 loc) · 2.83 KB
/
ducky_arduino.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
#include "SdFat.h"
#include <LiquidCrystal.h>
#include <string.h>
LiquidCrystal lcd(8, 7, 5, 4, 3, 2);
SdFat SD;
static const int SD_CS_PIN = 10;
static const int BTN_NEXT_PIN = 6;
static const int BTN_PLAY_PIN = 9;
int btnNextState = 0;
int btnPlayState = 0;
static const int DELAY_B_RELEASE = 10; // How many millis before release
static const int DELAY_A_RELEASE = 30; // How many millis after release
File root;
File currentFile;
void setup() {
// Only for debug
Serial.begin(9600);
// SD init
initializeSD();
// buttons init
pinMode(BTN_NEXT_PIN, INPUT);
pinMode(BTN_PLAY_PIN, INPUT);
// lcd init
lcd.begin(16, 2);
// show first file on lcd
root = SD.open("/");
currentFile = getNextFile();
printFileOnLCD(currentFile);
}
void loop() {
int btnPlayCurrentState = digitalRead(BTN_PLAY_PIN);
if (btnPlayCurrentState != btnPlayState) {
// Button "Play" pushed
if (btnPlayCurrentState == HIGH) {
playCurrentFile();
}
btnPlayState = btnPlayCurrentState;
}
int btnNextCurrentState = digitalRead(BTN_NEXT_PIN);
if (btnNextCurrentState != btnNextState) {
// Button "Next" pushed
if (btnNextCurrentState == HIGH) {
currentFile = getNextFile();
printFileOnLCD(currentFile);
}
btnNextState = btnNextCurrentState;
}
}
void initializeSD() {
pinMode(SD_CS_PIN, OUTPUT);
if (!SD.begin()) {
lcd.clear();
lcd.print("Error: SD init");
}
return;
}
File getNextFile() {
File entry = root.openNextFile();
if (!entry) {
root.rewind();
entry = root.openNextFile();
}
while (entry.isDir()) {
entry = root.openNextFile();
} // TODO: what if there's no file? how to stop?
return entry;
}
void printFileOnLCD(File f) {
/**
* Print filename and size of the specified file
* on the initialized lcd screen
*/
char fileName[16];
f.getName(fileName, 16);
lcd.clear();
lcd.print(fileName);
lcd.setCursor(0,1);
lcd.print(f.size());// + " b");
}
int read8Bytes(char* buf) {
/**
* Copy 8 bytes from the current file to the specified buffer.
* Returns the number of bytes successfully read.
*/
int i = 0;
while (i != 8)
{
if (!currentFile.available()) {
return i;
}
buf[i] = currentFile.read();
i++;
}
return i;
}
void releaseKey(char* buf)
{
memset(buf, 0, 8);
Serial.write(buf, 8); // Release key
}
void playCurrentFile() {
/**
* Output the content of the currently selected
* file via the usb port.
*/
char buf[8] = { 0 };
while (read8Bytes(buf)) {
if (buf[0] == 0x07) { // Delay
uint8_t delay_time[7];
memcpy(delay_time, &buf[1], 7 * sizeof(uint8_t));
delay((int)delay_time);
}
else {
Serial.write(buf, 8);
delay(DELAY_B_RELEASE);
releaseKey(buf);
delay(DELAY_A_RELEASE);
}
}
currentFile.seek(0);
}