Skip to content

Commit

Permalink
update some example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitshaoxiang committed May 14, 2021
1 parent 27ad1a5 commit b776e2e
Show file tree
Hide file tree
Showing 8 changed files with 1,257 additions and 0 deletions.
198 changes: 198 additions & 0 deletions examples/Unit/LoRaWAN470/LoRaWAN470.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#include "M5Stack.h"
#include "freertos/queue.h"

#include "TFTTerminal.h"
TFT_eSprite TerminalBuff = TFT_eSprite(&M5.Lcd);
TFTTerminal terminal(&TerminalBuff);

String waitRevice()
{
String recvStr;
do
{
recvStr = Serial2.readStringUntil('\n');
} while (recvStr.length() == 0);
Serial.println(recvStr);
terminal.println(recvStr);
return recvStr;
}

void sendATCMD(String cmdStr)
{
Serial2.print(cmdStr);
delay(100);
}

int sendATCMDAndRevice(String cmdStr)
{
Serial2.print(cmdStr);
delay(100);
waitRevice();
String recvStr = waitRevice();
if (recvStr.indexOf("OK") != -1)
{
return 0;
}
else
{
return -1;
}
}

void setup()
{
M5.begin();
Serial2.begin(115200, SERIAL_8N1, 16, 17);
Serial2.flush();
delay(100);
M5.Lcd.fillRect(0, 0, 320, 240, TFT_BLACK);
M5.Lcd.fillRect(0, 0, 320, 40, TFT_WHITE);
M5.Lcd.setTextColor(TFT_BLACK);
M5.Lcd.setTextDatum(TC_DATUM);
M5.Lcd.drawString("Unit-LoraWan(470) TEST", 160, 10, 4);
M5.Lcd.setTextDatum(TL_DATUM);
M5.Lcd.setTextColor(TFT_WHITE);
TerminalBuff.createSprite(240,200);
terminal.setGeometry(20,55,300,200);
terminal.setFontsize(1);


sendATCMD("AT?\r\n");
delay(100);
Serial2.flush();
sendATCMDAndRevice("AT+ILOGLVL=0\r\n");
sendATCMDAndRevice("AT+CSAVE\r\n");
sendATCMDAndRevice("AT+IREBOOT=0\r\n");
terminal.println("LoraWan Rebooting");
delay(2000);
terminal.println("LoraWan config");
//Set Join Mode OTAA
sendATCMDAndRevice("AT+CJOINMODE=0\r\n");
sendATCMDAndRevice("AT+CDEVEUI=0037CAE1FC3542B9\r\n");
sendATCMDAndRevice("AT+CAPPEUI=70B3D57ED003E04E\r\n");
sendATCMDAndRevice("AT+CAPPKEY=67FA4ED1075A20573BCDD7594C458698\r\n");
sendATCMDAndRevice("AT+CULDLMODE=2\r\n");
//Set ClassC mode
sendATCMDAndRevice("AT+CCLASS=2\r\n");
sendATCMDAndRevice("AT+CWORKMODE=2\r\n");

sendATCMDAndRevice("AT+CRXP=0,0,505300000\r\n");

// TX Freq
// 486.3
// 486.5
// 486.7
// 486.9
// 487.1
// 487.3
// 487.5
// 487.7
//MARK 0000 0100 0000 0000 | 0x0400

sendATCMDAndRevice("AT+CFREQBANDMASK=0400\r");

// RX Freq
//506.7 (RX1)
//506.9 (RX1)
//507.1 (RX1)
//507.3 (RX1)
//507.5 (RX1)
//507.7 (RX1)
//507.9 (RX1)
//508.1 (RX1)
//505.3 (RX2)| 505300000

sendATCMDAndRevice("AT+CJOIN=1,0,10,8\r\n");
}

enum systemstate
{
kIdel = 0,
kJoined,
kSending,
kWaitSend,
kEnd,
};
int system_fsm = kIdel;

int loraWanSendNUM = -1;
int loraWanSendCNT = -1;

void loop()
{
String recvStr = waitRevice();
if (recvStr.indexOf("+CJOIN:") != -1)
{
if (recvStr.indexOf("OK") != -1)
{
Serial.println("[ INFO ] JOIN IN SUCCESSFUL");
terminal.println("LoraWan JOIN");
system_fsm = kJoined;
}
else
{
Serial.println("[ INFO ] JOIN IN FAIL");
terminal.println("LoraWan JOIN FAIL");
system_fsm = kIdel;
}
}
else if (recvStr.indexOf("OK+RECV") != -1)
{
if (system_fsm == kJoined)
{
system_fsm = kSending;
}
else if (system_fsm == kWaitSend)
{
system_fsm = kSending;
char strbuff[128];
if(( loraWanSendCNT < 5 )&&( loraWanSendNUM == 8 ))
{
sprintf(strbuff,"TSET OK CNT: %d",loraWanSendCNT);
terminal.println(strbuff);
}
else
{
sprintf(strbuff,"FAILD NUM:%d CNT:%d",loraWanSendNUM,loraWanSendCNT);
terminal.println(strbuff);
}
}
}
else if(recvStr.indexOf("OK+SEND") != -1)
{
String snednum = recvStr.substring(8);
//Serial.printf(" [ INFO ] SEND NUM %s \r\n",snednum.c_str());
loraWanSendNUM = snednum.toInt();
}
else if(recvStr.indexOf("OK+SENT") != -1)
{
String snedcnt = recvStr.substring(8);
//Serial.printf(" [ INFO ] SEND CNT %s \r\n",snedcnt.c_str());
loraWanSendCNT = snedcnt.toInt();
}

if (system_fsm == kSending)
{
terminal.println("LoraWan Sending");
sendATCMD("AT+DTRX=1,8,8,4655434b20535443\r\n");
system_fsm = kWaitSend;
}
// if (M5.BtnA.wasPressed())
// {
// sendATCMDAndRevice("AT+CLINKCHECK=1\r\n");
// delay(100);
// }
// if (M5.BtnB.wasPressed())
// {
// sendATCMDAndRevice("AT+DTRX=1,15,8,4655434b20535443\r\n");
// delay(100);
// }
// if (M5.BtnC.wasPressed())
// {
// sendATCMDAndRevice("AT+DRX?\r\n");
// delay(100);
// }
delay(10);
M5.update();
}

162 changes: 162 additions & 0 deletions examples/Unit/LoRaWAN470/TFTTerminal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include "TFTTerminal.h"

TFTTerminal::TFTTerminal(TFT_eSprite *dis_buff_ptr)
{
_dis_buff_ptr = dis_buff_ptr;
memset(discharbuff, 0, 55 * 60);
}

TFTTerminal::~TFTTerminal()
{

}


void TFTTerminal::setcolor( uint16_t color, uint16_t bk_color )
{
_color = color;
_bkcolor = bk_color;
}

void TFTTerminal::setGeometry(uint16_t x, uint16_t y, uint16_t w, uint16_t h )
{
_win_x_pos = x;
_win_y_pos = y;
_win_w = w;
_win_h = h;

_line_x_limit = _win_w / _font_x_size;
_line_y_limit = _win_h / _font_y_size;
}

void TFTTerminal::setFontsize(uint8_t size)
{
size = ( size == 0 ) ? 1 : size;
_font_x_size = 5 * size ;
_font_y_size = 6 * size ;
_fontSize = size;

_line_x_limit = _win_w / _font_x_size;
_line_y_limit = _win_h / _font_y_size;
}

size_t TFTTerminal::write(uint8_t chardata)
{

bool flush_page_flag = false;
uint8_t dis_y_pos = 0;

if ((chardata == '\r') || (chardata == '\n'))
{
xpos = 0;
ypos++;
ypos = ypos % 60;
memset(discharbuff[ypos % 60], 0, 55);
return 1;
}
else if(xpos >= _line_x_limit)
{
xpos = 0;
ypos++;
ypos = ypos % 60;
memset(discharbuff[ypos % 60], 0, 55);
}

discharbuff[ypos][xpos] = chardata;
xpos++;

if ((dispos <= ypos) && ((ypos - dispos) > _line_y_limit))
{
dispos = ypos - _line_y_limit;
flush_page_flag = true;
}
else if ((dispos <= ypos) && ((ypos - dispos) <= _line_y_limit))
{
dis_y_pos = ypos - dispos;
flush_page_flag = false;
}
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
{
dispos = 60 - ( _line_y_limit - ypos );
flush_page_flag = true;
}
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
{
dis_y_pos = 60 - ( dispos - ypos );
flush_page_flag = false;
}

dispos = dispos % 60;

_dis_buff_ptr->setTextColor(_color);
_dis_buff_ptr->setTextSize(_fontSize);

if( flush_page_flag )
{
_dis_buff_ptr->fillSprite(_bkcolor);

for (size_t i = 0; i < _line_y_limit; i++)
{
_dis_buff_ptr->drawString((char *)discharbuff[(dispos + i) % 60], 0, i * _font_y_size);
}
}
else
{
_dis_buff_ptr->drawChar(chardata, ( xpos - 1 ) * _font_x_size, dis_y_pos * _font_y_size );
}
_dis_buff_ptr->pushSprite(_win_x_pos, _win_y_pos);

return 1;

}

size_t TFTTerminal::write(const uint8_t *buffer, size_t size)
{

while ((size != 0) && (*buffer != '\0'))
{
if ((*buffer == '\r') || (*buffer == '\n'))
{
xpos = 0;
ypos++;
ypos = ypos % 60;
memset(discharbuff[ypos % 60], 0, 55);
buffer++;
size--;
continue;
}
else if(xpos >= _line_x_limit)
{
xpos = 0;
ypos++;
ypos = ypos % 60;
memset(discharbuff[ypos % 60], 0, 55);
}
discharbuff[ypos][xpos] = *buffer;
xpos++;
buffer++;
size--;
}

if ((dispos <= ypos) && ((ypos - dispos) > _line_y_limit))
{
dispos = ypos - _line_y_limit;
}
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
{
dispos = 60- ( _line_y_limit - ypos );
}

dispos = dispos % 60;

_dis_buff_ptr->setTextColor(_color);
_dis_buff_ptr->setTextSize(_fontSize);
_dis_buff_ptr->fillSprite(_bkcolor);
//_dis_buff_ptr->fillRect(_win_x_pos, _win_y_pos, _win_w, _win_h, _bkcolor);
for (size_t i = 0; i < _line_y_limit; i++)
{
_dis_buff_ptr->drawString((char *)discharbuff[(dispos + i) % 60], 0, i * _font_y_size);
}
_dis_buff_ptr->pushSprite(_win_x_pos, _win_y_pos);
return 1;
}
35 changes: 35 additions & 0 deletions examples/Unit/LoRaWAN470/TFTTerminal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _TFTTERMINAL_H_
#define _TFTTERMINAL_H_

#include <M5Stack.h>
#include <Print.h>

class TFTTerminal : public Print
{
private:
TFT_eSprite *disptr;
char discharbuff[60][55];
uint32_t xpos = 0,ypos = 0, dispos = 0;
TFT_eSprite* _dis_buff_ptr = NULL;
uint16_t _bkcolor = TFT_BLACK;
uint16_t _color = TFT_GREEN;

uint16_t _win_x_pos = 0,_win_y_pos = 0,_win_w = 320,_win_h = 240;
uint16_t _font_x_size = 6,_font_y_size = 8;
uint8_t _fontSize = 0;
uint16_t _line_x_limit = 53,_line_y_limit = 30;

public:
TFTTerminal(TFT_eSprite *dis_buff_ptr);
~TFTTerminal();

void setcolor( uint16_t color, uint16_t bk_color );
void setGeometry(uint16_t x, uint16_t y, uint16_t w, uint16_t h );
void setFontsize(uint8_t size);

size_t write(uint8_t) ;
size_t write(const uint8_t *buffer, size_t size);
};


#endif
Loading

0 comments on commit b776e2e

Please sign in to comment.