-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmain.cpp
executable file
·210 lines (172 loc) · 4.76 KB
/
main.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
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
#include <library.h>
#include "../../os_host/source/framework/Console.h"
using namespace BIOS;
#include "simcom.h"
// https://dweet.io/follow/la104simcom900
// https://dweet.io/dweet/for/la104simcom900?counter=10123
// https://dweet.io/get/latest/dweet/for/la104simcom900
int gReset = 0;
class CMyHttpReceiver : public CHttpResponse
{
bool mFirstLine{true};
public:
virtual void OnHttpCode(int code) override
{
if (code != 200)
{
CONSOLE::Color(RGB565(ff00ff));
CONSOLE::Print("HTTP Error: %d\n", code);
}
}
virtual void OnBody(char* body, int length) override
{
if (!mFirstLine)
return;
mFirstLine = false;
if (length > 31)
strcpy(body+31-3, "...");
CONSOLE::Color(RGB565(b0b0b0));
CONSOLE::Print("Resp: ");
CONSOLE::Color(RGB565(ffffff));
CONSOLE::Print(body);
CONSOLE::Print("\n");
}
virtual void OnFinished() override
{
mFirstLine = true;
}
};
class CMyHttpRequest : public CHttpRequest
{
char mArguments[RequestArgumentsLength];
public:
CMyHttpRequest(const char* host, const char* path)
{
mProtocol = "TCP";
mHost = host;
mPath = path;
mPort = 80;
strcpy(mArguments, "");
}
virtual CAtStream& Request(CAtStream& s)
{
CAtStreamCounter counter;
GetArguments(counter);
s << "POST " << mPath << " HTTP/1.0\r\n"
<< "Host: " << mHost << "\r\n"
<< "User-Agent: sim800L on LA104 by valky.eu\r\n"
<< "content-type: application/x-www-form-urlencoded\r\n"
<< "content-length: " << counter.Count() << "\r\n"
<< "\r\n";
GetArguments(s);
s << "\r\n";
return s;
}
void SetArguments(char *args)
{
_ASSERT(strlen(args) < sizeof(mArguments)-1);
strcpy(mArguments, args);
}
virtual void GetArguments(CAtStream& s)
{
s << mArguments;
}
};
class CClientApp
{
static CClientApp* pInstance;
CGprs mGprs;
CHttpRequestPost mRequest{"dweet.io", "/dweet/for/la104simcom"};
CMyHttpReceiver mResponse;
bool shouldProcess{false};
int mCounter{0};
public:
void Init()
{
BIOS::GPIO::PinMode(BIOS::GPIO::P3, BIOS::GPIO::Output);
// io pins before special io
BIOS::GPIO::PinMode(BIOS::GPIO::P1, BIOS::GPIO::Uart);
BIOS::GPIO::PinMode(BIOS::GPIO::P2, BIOS::GPIO::Uart);
GPIO::UART::Setup(115200, GPIO::UART::EConfig::length8);
//gprs.SetApn("o2internet"); //O2
mGprs.SetApn("internet"); // 4ka
mGprs.SetReceiver(&mResponse);
mGprs.AttachPrint([](char c){ GPIO::UART::Write(c); });
mGprs.AttachPower([](bool value){ gReset++;
BIOS::GPIO::DigitalWrite(BIOS::GPIO::P3, 1-value); });
}
void comm_yield()
{
while (GPIO::UART::Available())
{
char c = GPIO::UART::Read();
mGprs << c;
if (c == '\n')
shouldProcess = true;
}
}
void gprs_yield()
{
static long last = 0;
long now = millis();
if (shouldProcess || now > last + 500)
{
shouldProcess = false;
last = now;
mGprs();
}
}
void Do()
{
comm_yield();
gprs_yield();
if (mGprs.isReady())
{
mResponse.Reset();
char request[128];
sprintf(request, "time=%d&reset=%d&counter=%d", BIOS::SYS::GetTick(), gReset, mCounter++);
mRequest.SetArguments(request);
mGprs.request(mRequest);
}
}
CClientApp()
{
pInstance = this;
}
};
CClientApp* CClientApp::pInstance = nullptr;
CClientApp app;
#ifdef _ARM
__attribute__((__section__(".entry")))
#endif
int _main(void)
{
CRect rcClient(0, 0, LCD::Width, LCD::Height);
LCD::Bar(rcClient, RGB565(0000b0));
CRect rc1(rcClient);
rc1.bottom = 14;
GUI::Background(rc1, RGB565(4040b0), RGB565(404040));
BIOS::LCD::Print(8, rc1.top, RGB565(ffffff), RGBTRANS, "SIMCOM web client & dweet.io");
CRect rc2(rcClient);
rc2.top = rc2.bottom-14;
GUI::Background(rc2, RGB565(404040), RGB565(202020));
BIOS::LCD::Print(8, rc2.top, RGB565(808080), RGBTRANS, "https://dweet.io/follow/la104simcom");
app.Init();
BIOS::KEY::EKey key;
while ((key = KEY::GetKey()) != KEY::EKey::Escape)
{
app.Do();
}
return 0;
}
void _HandleAssertion(const char* file, int line, const char* cond)
{
CONSOLE::Color(RGB565(ffff00));
CONSOLE::Print("Assertion failed in ");
CONSOLE::Print(file);
CONSOLE::Print(" [%d]: %s\n", line, cond);
#ifdef __APPLE__
//kill(getpid(), SIGSTOP);
#endif
while (1);
}