-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainLed.cpp
139 lines (121 loc) · 4.13 KB
/
mainLed.cpp
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
//
// A "Led" server, which allows control over a single NeoPixel (color,
// duration, on/off pattern). The led can be controlled through a web UI or
// through REST calls (and/or, depending on Iotsa compile time options, COAP calls).
// The web interface can be disabled by building iotsa with IOTSA_WITHOUT_WEB.
//
// This is the application that is usually shipped with new iotsa boards.
//
#include "iotsa.h"
#include "iotsaWifi.h"
#include "iotsaLed.h"
// CHANGE: Add application includes and declarations here
#define WITH_OTA // Enable Over The Air updates from ArduinoIDE. Needs at least 1MB flash.
IotsaApplication application("Iotsa LED Server");
IotsaWifiMod wifiMod(application);
#ifdef WITH_OTA
#include "iotsaOta.h"
IotsaOtaMod otaMod(application);
#endif
//
// LED module.
//
#ifndef NEOPIXEL_PIN
#define NEOPIXEL_PIN 15 // pulled-down during boot, can be used for NeoPixel afterwards
#endif
class IotsaLedControlMod : public IotsaLedMod {
public:
using IotsaLedMod::IotsaLedMod;
void serverSetup() override;
#ifdef IOTSA_WITH_WEB
String info() override;
#endif
protected:
#ifdef IOTSA_WITH_API
bool getHandler(const char *path, JsonObject& reply) override;
bool putHandler(const char *path, const JsonVariant& request, JsonObject& reply) override;
#endif
private:
void handler();
};
#ifdef IOTSA_WITH_WEB
void
IotsaLedControlMod::handler() {
// Handles the page that is specific to the Led module, greets the user and
// optionally stores a new name to greet the next time.
bool anyChanged = false;
uint32_t _rgb = 0xffffff;
int _count = 1;
int _onDuration = 0;
int _offDuration = 0;
if( server->hasArg("rgb")) {
_rgb = strtol(server->arg("rgb").c_str(), 0, 16);
anyChanged = true;
}
if( server->hasArg("onDuration")) {
_onDuration = server->arg("onDuration").toInt();
anyChanged = true;
}
if( server->hasArg("offDuration")) {
_offDuration = server->arg("offDuration").toInt();
anyChanged = true;
}
if( server->hasArg("count")) {
_count = server->arg("count").toInt();
anyChanged = true;
}
if (anyChanged) set(_rgb, _onDuration, _offDuration, _count);
String message = "<html><head><title>Led Server</title></head><body><h1>Led Server</h1>";
message += "<form method='get'>";
message += "Color (hex rrggbb): <input type='text' name='rgb'><br>";
message += "On time (ms): <input type='text' name='onDuration'><br>";
message += "Off time (ms): <input type='text' name='offDuration'><br>";
message += "Repeat count: <input type='text' name='count'><br>";
message += "<input type='submit'></form></body></html>";
server->send(200, "text/html", message);
}
String IotsaLedControlMod::info() {
// Return some information about this module, for the main page of the web server.
String rv = "<p>See <a href=\"/led\">/led</a> for flashing the led in a color pattern.</p>";
return rv;
}
#endif // IOTSA_WITH_WEB
#ifdef IOTSA_WITH_API
bool IotsaLedControlMod::getHandler(const char *path, JsonObject& reply) {
reply["rgb"] = rgb;
reply["onDuration"] = onDuration;
reply["offDuration"] = offDuration;
reply["isOn"] = isOn;
reply["count"] = remainingCount;
return true;
}
bool IotsaLedControlMod::putHandler(const char *path, const JsonVariant& request, JsonObject& reply) {
uint32_t _rgb = request["rgb"]|0xffffff;
int _onDuration = request["onDuration"]|0;
int _offDuration = request["offDuration"]|0;
int _count = request["count"]|0;
set(_rgb, _onDuration, _offDuration, _count);
return true;
}
#endif
void IotsaLedControlMod::serverSetup() {
// Setup the web server hooks for this module.
#ifdef IOTSA_WITH_WEB
server->on("/led", std::bind(&IotsaLedControlMod::handler, this));
#endif // IOTSA_WITH_WEB
#ifdef IOTSA_WITH_API
api.setup("/api/led", true, true);
#endif
name = "led";
}
// Instantiate the Led module, and install it in the framework
IotsaLedControlMod ledMod(application, NEOPIXEL_PIN);
// Standard setup() method, hands off most work to the application framework
void setup(void){
application.setup();
application.serverSetup();
}
// Standard loop() routine, hands off most work to the application framework
void loop(void){
application.loop();
}