-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.ino
226 lines (181 loc) · 5.31 KB
/
device.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "ATM0130.h"
Device::Device() {
this->last_millis = 0;
this->ssid = "";
this->pass = "";
readSettings();
this->isTimeConfigured = false;
this->isWiFiConnected = false;
this->tryWiFiConnect = true;
this->button_state = 0b00000000;
this->button_pressed = 0b00000000;
this->button_pressing = 0b00000000;
this->button_release = 0b00000000;
}
void Device::setButtonState(uint8_t states) {
this->button_pressed = ~this->button_state & states;
this->button_pressing = this->button_pressing & states;
this->button_pressing = this->button_pressing | this->button_state;
this->button_release = this->button_state & ~states;
this->button_state = states; // 今回の状態を保存
}
bool Device::getButtonState(uint8_t button, uint8_t state) {
if (state == BUTTON_NOTPRESS) return (bool)( ((~this->button_state) >> button) & 0b1 );
else if (state == BUTTON_PRESSED ) return (bool)( (this->button_pressed >> button) & 0b1 );
else if (state == BUTTON_PRESSING) return (bool)( (this->button_pressing >> button) & 0b1 );
else if (state == BUTTON_RELEASE ) return (bool)( (this->button_release >> button) & 0b1 );
return false;
}
void handleRootGet() {
String html = "";
html += "<h1>WiFi Settings</h1>";
html += "<form method='post'>";
html += " <input type='text' name='ssid' placeholder='ssid'><br>";
html += " <input type='text' name='pass' placeholder='pass'><br>";
html += " <input type='submit'><br>";
html += "</form>";
server.send(200, "text/html", html);
}
void handleRootPost() {
String getssid = server.arg("ssid");
String getpass = server.arg("pass");
SPIFFS.begin();
File f = SPIFFS.open("/wifi_setting.txt", "w");
f.println(getssid);
f.println(getpass);
f.close();
SPIFFS.end();
Serial.println("getSSID: " + getssid);
Serial.println("getPASS: " + getpass);
myATM0130.clearScreen(BLACK16);
myATM0130.setColor(GREEN16);
myATM0130.putStr(0,00,"setting changed\0");
myATM0130.updateScreen();
String html = "";
html += "<h1>WiFi Settings</h1>";
html += getssid + "<br>";
html += getpass + "<br>";
server.send(200, "text/html", html);
}
void Device::serverBegin(){
byte mac[6];
if(this->tryWiFiConnect) WiFiEnd();
WiFi.macAddress(mac);
serverssid = "";
for (int i = 0; i < 6; i++) {
serverssid += String(mac[i], HEX);
}
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(serverssid.c_str(), serverpass.c_str());
server.on("/", HTTP_GET, handleRootGet);
server.on("/", HTTP_POST, handleRootPost);
server.begin();
this->isServerStarted = true;
}
void Device::serverWaitAccess(){
server.handleClient();
}
void Device::serverEnd(){
server.close();
this->isServerStarted = false;
readSettings();
}
String Device::getServerssid(){
return this->serverssid;
}
String Device::getServerpass(){
return this->serverpass;
}
void Device::WiFiBegin() {
WiFi.begin(this->ssid, this->pass);
this->tryWiFiConnect = true;
}
void Device::WiFiConnectCheck(){
Serial.println("SSID: " + this->ssid);
Serial.println("PASS: " + this->pass);
if (WiFi.status() == WL_CONNECTED){
this->isWiFiConnected = true;
setTime();
this->tryWiFiConnect = false;
}
else{
this->isWiFiConnected = false;
}
}
void Device::WiFiEnd(){
if(this->isWiFiConnected){
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
this->isWiFiConnected = false;
this->tryWiFiConnect = false;
}
}
void Device::setTime(){
if (this->isWiFiConnected){
configTime(JST, 0, "ntp.nict.jp", "time.google.com", "ntp.jst.mfeed.ad.jp");
}
}
void Device::getTime(){
time_t t;
t = time(NULL);
tm = localtime(&t);
Serial.printf("%04d/%02d/%02d %02d:%02d:%02d\n",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
if(tm->tm_year >= 122) this->isTimeConfigured = true;
}
uint8_t Device::getHour(){
return tm->tm_hour;
}
uint8_t Device::getMinute(){
return tm->tm_min;
}
uint8_t Device::getSecond(){
return tm->tm_sec;
}
bool Device::readSettings(){
SPIFFS.begin();
File f = SPIFFS.open("/wifi_setting.txt","r");
if(f == NULL) return false;
ssid = f.readStringUntil('\n');
pass = f.readStringUntil('\n');
f.close();
SPIFFS.end();
ssid.trim();
pass.trim();
WiFiBegin();
return true;
}
bool Device::save(uint8_t life, uint8_t stomach, int8_t favorability, int8_t hiddenFavorability){
SPIFFS.begin();
File f = SPIFFS.open("/character_data","w");
if(f == NULL) return false;
f.println(life);
f.println(stomach);
f.println(favorability);
f.println(hiddenFavorability);
f.close();
SPIFFS.end();
}
void Device::sleep(){
digitalWrite(LED_PIN,LOW);
}
void Device::timeLog(){
String timelog = String(tm->tm_year+1900) +"/"+ String(tm->tm_mon+1) + "/" + String(tm->tm_mday) + " " + String(tm->tm_hour) + ":" + String(tm->tm_min) + ":" + String(tm->tm_sec);
Serial.println(timelog);
SPIFFS.begin();
File f = SPIFFS.open("/time_log.txt", "w");
f.println(timelog);
f.close();
SPIFFS.end();
}
void Device::readTimeLog(){
String timelog;
SPIFFS.begin();
File f = SPIFFS.open("/time_log.txt","r");
if(f == NULL) return;
timelog = f.readStringUntil('\n');
f.close();
SPIFFS.end();
Serial.println("Last Timelog: " + timelog);
}