forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWidgetSD.h
73 lines (65 loc) · 1.94 KB
/
WidgetSD.h
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
/**
* @file WidgetSD.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Mar 2015
* @brief
*
*/
#ifndef WidgetSD_h
#define WidgetSD_h
#include <Blynk/BlynkApi.h>
class WidgetSD
{
public:
WidgetSD() {}
void setVPin(int vPin) {}
void onWrite(BlynkReq& request, const BlynkParam& param);
private:
};
#include <SD.h>
void WidgetSD::onWrite(BlynkReq& request, const BlynkParam& param)
{
const char* cmd = param[0].asStr();
if (!strcmp(cmd, "ls")) {
if (File dir = SD.open(param[1].asStr())) {
while (File entry = dir.openNextFile()) {
char mem[32] = "";
BlynkParam result(mem, 0, sizeof(mem));
result.add(entry.name());
if (entry.isDirectory()) {
result.add("/");
} else {
result.add(entry.size());
}
Blynk.virtualWrite(request.pin, result);
entry.close();
}
dir.close();
}
} else if (!strcmp(cmd, "get")) { // dc dc dc dc d[l|e]
if (File f = SD.open(param[1].asStr())) {
if (!f.isDirectory()) {
char mem[32] = "dc";
const int maxlen = sizeof(mem)-3;
int len;
do {
len = f.read(mem+3, maxlen);
if (len < 0) {
mem[1] = 'e'; // Error!
len = 0;
} else if (len < maxlen) {
mem[1] = 'l'; // Last chunk
}
Blynk.virtualWriteBinary(request.pin, mem, len + 3);
} while (len == maxlen);
} else {
}
f.close();
}
} else {
//BLYNK_LOG2(BLYNK_F("Invalid SD command: "), cmd);
}
}
#endif