Skip to content

Commit b776e2e

Browse files
committed
update some example
1 parent 27ad1a5 commit b776e2e

File tree

8 files changed

+1257
-0
lines changed

8 files changed

+1257
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#include "M5Stack.h"
2+
#include "freertos/queue.h"
3+
4+
#include "TFTTerminal.h"
5+
TFT_eSprite TerminalBuff = TFT_eSprite(&M5.Lcd);
6+
TFTTerminal terminal(&TerminalBuff);
7+
8+
String waitRevice()
9+
{
10+
String recvStr;
11+
do
12+
{
13+
recvStr = Serial2.readStringUntil('\n');
14+
} while (recvStr.length() == 0);
15+
Serial.println(recvStr);
16+
terminal.println(recvStr);
17+
return recvStr;
18+
}
19+
20+
void sendATCMD(String cmdStr)
21+
{
22+
Serial2.print(cmdStr);
23+
delay(100);
24+
}
25+
26+
int sendATCMDAndRevice(String cmdStr)
27+
{
28+
Serial2.print(cmdStr);
29+
delay(100);
30+
waitRevice();
31+
String recvStr = waitRevice();
32+
if (recvStr.indexOf("OK") != -1)
33+
{
34+
return 0;
35+
}
36+
else
37+
{
38+
return -1;
39+
}
40+
}
41+
42+
void setup()
43+
{
44+
M5.begin();
45+
Serial2.begin(115200, SERIAL_8N1, 16, 17);
46+
Serial2.flush();
47+
delay(100);
48+
M5.Lcd.fillRect(0, 0, 320, 240, TFT_BLACK);
49+
M5.Lcd.fillRect(0, 0, 320, 40, TFT_WHITE);
50+
M5.Lcd.setTextColor(TFT_BLACK);
51+
M5.Lcd.setTextDatum(TC_DATUM);
52+
M5.Lcd.drawString("Unit-LoraWan(470) TEST", 160, 10, 4);
53+
M5.Lcd.setTextDatum(TL_DATUM);
54+
M5.Lcd.setTextColor(TFT_WHITE);
55+
TerminalBuff.createSprite(240,200);
56+
terminal.setGeometry(20,55,300,200);
57+
terminal.setFontsize(1);
58+
59+
60+
sendATCMD("AT?\r\n");
61+
delay(100);
62+
Serial2.flush();
63+
sendATCMDAndRevice("AT+ILOGLVL=0\r\n");
64+
sendATCMDAndRevice("AT+CSAVE\r\n");
65+
sendATCMDAndRevice("AT+IREBOOT=0\r\n");
66+
terminal.println("LoraWan Rebooting");
67+
delay(2000);
68+
terminal.println("LoraWan config");
69+
//Set Join Mode OTAA
70+
sendATCMDAndRevice("AT+CJOINMODE=0\r\n");
71+
sendATCMDAndRevice("AT+CDEVEUI=0037CAE1FC3542B9\r\n");
72+
sendATCMDAndRevice("AT+CAPPEUI=70B3D57ED003E04E\r\n");
73+
sendATCMDAndRevice("AT+CAPPKEY=67FA4ED1075A20573BCDD7594C458698\r\n");
74+
sendATCMDAndRevice("AT+CULDLMODE=2\r\n");
75+
//Set ClassC mode
76+
sendATCMDAndRevice("AT+CCLASS=2\r\n");
77+
sendATCMDAndRevice("AT+CWORKMODE=2\r\n");
78+
79+
sendATCMDAndRevice("AT+CRXP=0,0,505300000\r\n");
80+
81+
// TX Freq
82+
// 486.3
83+
// 486.5
84+
// 486.7
85+
// 486.9
86+
// 487.1
87+
// 487.3
88+
// 487.5
89+
// 487.7
90+
//MARK 0000 0100 0000 0000 | 0x0400
91+
92+
sendATCMDAndRevice("AT+CFREQBANDMASK=0400\r");
93+
94+
// RX Freq
95+
//506.7 (RX1)
96+
//506.9 (RX1)
97+
//507.1 (RX1)
98+
//507.3 (RX1)
99+
//507.5 (RX1)
100+
//507.7 (RX1)
101+
//507.9 (RX1)
102+
//508.1 (RX1)
103+
//505.3 (RX2)| 505300000
104+
105+
sendATCMDAndRevice("AT+CJOIN=1,0,10,8\r\n");
106+
}
107+
108+
enum systemstate
109+
{
110+
kIdel = 0,
111+
kJoined,
112+
kSending,
113+
kWaitSend,
114+
kEnd,
115+
};
116+
int system_fsm = kIdel;
117+
118+
int loraWanSendNUM = -1;
119+
int loraWanSendCNT = -1;
120+
121+
void loop()
122+
{
123+
String recvStr = waitRevice();
124+
if (recvStr.indexOf("+CJOIN:") != -1)
125+
{
126+
if (recvStr.indexOf("OK") != -1)
127+
{
128+
Serial.println("[ INFO ] JOIN IN SUCCESSFUL");
129+
terminal.println("LoraWan JOIN");
130+
system_fsm = kJoined;
131+
}
132+
else
133+
{
134+
Serial.println("[ INFO ] JOIN IN FAIL");
135+
terminal.println("LoraWan JOIN FAIL");
136+
system_fsm = kIdel;
137+
}
138+
}
139+
else if (recvStr.indexOf("OK+RECV") != -1)
140+
{
141+
if (system_fsm == kJoined)
142+
{
143+
system_fsm = kSending;
144+
}
145+
else if (system_fsm == kWaitSend)
146+
{
147+
system_fsm = kSending;
148+
char strbuff[128];
149+
if(( loraWanSendCNT < 5 )&&( loraWanSendNUM == 8 ))
150+
{
151+
sprintf(strbuff,"TSET OK CNT: %d",loraWanSendCNT);
152+
terminal.println(strbuff);
153+
}
154+
else
155+
{
156+
sprintf(strbuff,"FAILD NUM:%d CNT:%d",loraWanSendNUM,loraWanSendCNT);
157+
terminal.println(strbuff);
158+
}
159+
}
160+
}
161+
else if(recvStr.indexOf("OK+SEND") != -1)
162+
{
163+
String snednum = recvStr.substring(8);
164+
//Serial.printf(" [ INFO ] SEND NUM %s \r\n",snednum.c_str());
165+
loraWanSendNUM = snednum.toInt();
166+
}
167+
else if(recvStr.indexOf("OK+SENT") != -1)
168+
{
169+
String snedcnt = recvStr.substring(8);
170+
//Serial.printf(" [ INFO ] SEND CNT %s \r\n",snedcnt.c_str());
171+
loraWanSendCNT = snedcnt.toInt();
172+
}
173+
174+
if (system_fsm == kSending)
175+
{
176+
terminal.println("LoraWan Sending");
177+
sendATCMD("AT+DTRX=1,8,8,4655434b20535443\r\n");
178+
system_fsm = kWaitSend;
179+
}
180+
// if (M5.BtnA.wasPressed())
181+
// {
182+
// sendATCMDAndRevice("AT+CLINKCHECK=1\r\n");
183+
// delay(100);
184+
// }
185+
// if (M5.BtnB.wasPressed())
186+
// {
187+
// sendATCMDAndRevice("AT+DTRX=1,15,8,4655434b20535443\r\n");
188+
// delay(100);
189+
// }
190+
// if (M5.BtnC.wasPressed())
191+
// {
192+
// sendATCMDAndRevice("AT+DRX?\r\n");
193+
// delay(100);
194+
// }
195+
delay(10);
196+
M5.update();
197+
}
198+
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include "TFTTerminal.h"
2+
3+
TFTTerminal::TFTTerminal(TFT_eSprite *dis_buff_ptr)
4+
{
5+
_dis_buff_ptr = dis_buff_ptr;
6+
memset(discharbuff, 0, 55 * 60);
7+
}
8+
9+
TFTTerminal::~TFTTerminal()
10+
{
11+
12+
}
13+
14+
15+
void TFTTerminal::setcolor( uint16_t color, uint16_t bk_color )
16+
{
17+
_color = color;
18+
_bkcolor = bk_color;
19+
}
20+
21+
void TFTTerminal::setGeometry(uint16_t x, uint16_t y, uint16_t w, uint16_t h )
22+
{
23+
_win_x_pos = x;
24+
_win_y_pos = y;
25+
_win_w = w;
26+
_win_h = h;
27+
28+
_line_x_limit = _win_w / _font_x_size;
29+
_line_y_limit = _win_h / _font_y_size;
30+
}
31+
32+
void TFTTerminal::setFontsize(uint8_t size)
33+
{
34+
size = ( size == 0 ) ? 1 : size;
35+
_font_x_size = 5 * size ;
36+
_font_y_size = 6 * size ;
37+
_fontSize = size;
38+
39+
_line_x_limit = _win_w / _font_x_size;
40+
_line_y_limit = _win_h / _font_y_size;
41+
}
42+
43+
size_t TFTTerminal::write(uint8_t chardata)
44+
{
45+
46+
bool flush_page_flag = false;
47+
uint8_t dis_y_pos = 0;
48+
49+
if ((chardata == '\r') || (chardata == '\n'))
50+
{
51+
xpos = 0;
52+
ypos++;
53+
ypos = ypos % 60;
54+
memset(discharbuff[ypos % 60], 0, 55);
55+
return 1;
56+
}
57+
else if(xpos >= _line_x_limit)
58+
{
59+
xpos = 0;
60+
ypos++;
61+
ypos = ypos % 60;
62+
memset(discharbuff[ypos % 60], 0, 55);
63+
}
64+
65+
discharbuff[ypos][xpos] = chardata;
66+
xpos++;
67+
68+
if ((dispos <= ypos) && ((ypos - dispos) > _line_y_limit))
69+
{
70+
dispos = ypos - _line_y_limit;
71+
flush_page_flag = true;
72+
}
73+
else if ((dispos <= ypos) && ((ypos - dispos) <= _line_y_limit))
74+
{
75+
dis_y_pos = ypos - dispos;
76+
flush_page_flag = false;
77+
}
78+
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
79+
{
80+
dispos = 60 - ( _line_y_limit - ypos );
81+
flush_page_flag = true;
82+
}
83+
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
84+
{
85+
dis_y_pos = 60 - ( dispos - ypos );
86+
flush_page_flag = false;
87+
}
88+
89+
dispos = dispos % 60;
90+
91+
_dis_buff_ptr->setTextColor(_color);
92+
_dis_buff_ptr->setTextSize(_fontSize);
93+
94+
if( flush_page_flag )
95+
{
96+
_dis_buff_ptr->fillSprite(_bkcolor);
97+
98+
for (size_t i = 0; i < _line_y_limit; i++)
99+
{
100+
_dis_buff_ptr->drawString((char *)discharbuff[(dispos + i) % 60], 0, i * _font_y_size);
101+
}
102+
}
103+
else
104+
{
105+
_dis_buff_ptr->drawChar(chardata, ( xpos - 1 ) * _font_x_size, dis_y_pos * _font_y_size );
106+
}
107+
_dis_buff_ptr->pushSprite(_win_x_pos, _win_y_pos);
108+
109+
return 1;
110+
111+
}
112+
113+
size_t TFTTerminal::write(const uint8_t *buffer, size_t size)
114+
{
115+
116+
while ((size != 0) && (*buffer != '\0'))
117+
{
118+
if ((*buffer == '\r') || (*buffer == '\n'))
119+
{
120+
xpos = 0;
121+
ypos++;
122+
ypos = ypos % 60;
123+
memset(discharbuff[ypos % 60], 0, 55);
124+
buffer++;
125+
size--;
126+
continue;
127+
}
128+
else if(xpos >= _line_x_limit)
129+
{
130+
xpos = 0;
131+
ypos++;
132+
ypos = ypos % 60;
133+
memset(discharbuff[ypos % 60], 0, 55);
134+
}
135+
discharbuff[ypos][xpos] = *buffer;
136+
xpos++;
137+
buffer++;
138+
size--;
139+
}
140+
141+
if ((dispos <= ypos) && ((ypos - dispos) > _line_y_limit))
142+
{
143+
dispos = ypos - _line_y_limit;
144+
}
145+
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
146+
{
147+
dispos = 60- ( _line_y_limit - ypos );
148+
}
149+
150+
dispos = dispos % 60;
151+
152+
_dis_buff_ptr->setTextColor(_color);
153+
_dis_buff_ptr->setTextSize(_fontSize);
154+
_dis_buff_ptr->fillSprite(_bkcolor);
155+
//_dis_buff_ptr->fillRect(_win_x_pos, _win_y_pos, _win_w, _win_h, _bkcolor);
156+
for (size_t i = 0; i < _line_y_limit; i++)
157+
{
158+
_dis_buff_ptr->drawString((char *)discharbuff[(dispos + i) % 60], 0, i * _font_y_size);
159+
}
160+
_dis_buff_ptr->pushSprite(_win_x_pos, _win_y_pos);
161+
return 1;
162+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef _TFTTERMINAL_H_
2+
#define _TFTTERMINAL_H_
3+
4+
#include <M5Stack.h>
5+
#include <Print.h>
6+
7+
class TFTTerminal : public Print
8+
{
9+
private:
10+
TFT_eSprite *disptr;
11+
char discharbuff[60][55];
12+
uint32_t xpos = 0,ypos = 0, dispos = 0;
13+
TFT_eSprite* _dis_buff_ptr = NULL;
14+
uint16_t _bkcolor = TFT_BLACK;
15+
uint16_t _color = TFT_GREEN;
16+
17+
uint16_t _win_x_pos = 0,_win_y_pos = 0,_win_w = 320,_win_h = 240;
18+
uint16_t _font_x_size = 6,_font_y_size = 8;
19+
uint8_t _fontSize = 0;
20+
uint16_t _line_x_limit = 53,_line_y_limit = 30;
21+
22+
public:
23+
TFTTerminal(TFT_eSprite *dis_buff_ptr);
24+
~TFTTerminal();
25+
26+
void setcolor( uint16_t color, uint16_t bk_color );
27+
void setGeometry(uint16_t x, uint16_t y, uint16_t w, uint16_t h );
28+
void setFontsize(uint8_t size);
29+
30+
size_t write(uint8_t) ;
31+
size_t write(const uint8_t *buffer, size_t size);
32+
};
33+
34+
35+
#endif

0 commit comments

Comments
 (0)