查看: 1903|回复: 0

【搬运】使用M5Stack做一个移动的串口监视器

[复制链接]

该用户从未签到

发表于 2019-11-8 10:41 | 显示全部楼层 |阅读模式
FTDI_TERMINAL.jpeg 项目地址:https://github.com/tomorrow56/M5Stack_USB_Host_FTDI_Serial_Monitor
大部分设备在调试时都使用串口进行数据查看,TFT_eSPI的示例中也有一个关于串口打印的示例,调用这个示例可以直接将RX、TX到M5Core的黑色base,但是对于一些非裸板设备或者端口有线的设备,往往在连接时有些麻烦。为此,使用USB Module可以方便的接入USB来查看数据。作者修改了库,增加FTDI的支持,代码如下[mw_shl_code=arduino,true]#include <M5Stack.h>
#include "M5StackUpdater.h"

#include "cdcftdimod.h"
#include <usbhub.h>

#include "pgmstrings.h"

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

class FTDIMODAsync : public FTDIMODAsyncOper
{
public:
    uint8_t OnInit(FTDIMOD *pftdi);
};

uint8_t FTDIMODAsync::OnInit(FTDIMOD *pftdi)
{
    uint8_t rcode = 0;

    rcode = pftdi->SetBaudRate(115200);

    if (rcode)
    {
        ErrorMessage<uint8_t>(PSTR("SetBaudRate"), rcode);
        return rcode;
    }
    rcode = pftdi->SetFlowControl(FTDI_SIO_DISABLE_FLOW_CTRL);

    if (rcode)
        ErrorMessage<uint8_t>(PSTR("SetFlowControl"), rcode);

    return rcode;
}

USB              Usb;
//USBHub         Hub(&Usb);
FTDIMODAsync        FtdiAsync;
FTDIMOD             Ftdi(&Usb, &FtdiAsync);

// The scrolling area must be a integral multiple of TEXT_HEIGHT
//#define TEXT_HEIGHT 16 // Height of text to be printed and scrolled
#define TEXT_HEIGHT 10 // Height of text to be printed and scrolled
#define TOP_FIXED_AREA 14 // Number of lines in top fixed area (lines counted from top of screen)
#define BOT_FIXED_AREA 0 // Number of lines in bottom fixed area (lines counted from bottom of screen)
#define YMAX 240 // Bottom of screen area

#define LOGO_HEIGHT 16 // Height of text to be printed and scrolled

// The initial y coordinate of the top of the scrolling area
uint16_t yStart = 0;
// yArea must be a integral multiple of TEXT_HEIGHT
uint16_t yArea = YMAX-TOP_FIXED_AREA-BOT_FIXED_AREA;
// The initial y coordinate of the top of the bottom text line
// uint16_t yDraw = YMAX - BOT_FIXED_AREA - TEXT_HEIGHT;
uint16_t yDraw = 0;

// Keep track of the drawing x coordinate
uint16_t xPos = 0;

// For the byte we read from the serial port
byte data = 0;

// A few test variables used during debugging
boolean change_colour = 1;
boolean selected = 1;

// We have to blank the top line each time the display is scrolled, but this takes up to 13 milliseconds
// for a full width line, meanwhile the serial buffer may be filling... and overflowing
// We can speed up scrolling of short text lines by just blanking the character we drew
int blank[19]; // We keep all the strings pixel lengths to optimise the speed of the top line blanking

// Use M5Stack Logo
extern const unsigned char gImage_logoM5[];

boolean InitFlag = false;

void setup()
{
  // Setup the TFT display
  M5.begin();

  if(digitalRead(BUTTON_A_PIN) == 0) {
    Serial.println("Will Load menu binary");
    updateFromFS(SD);
    ESP.restart();
  }

  // Use M5Stack Logo
  M5.Lcd.pushImage(0, 0, 320, 240, (uint16_t *)gImage_logoM5);

//  Serial.begin( 115200 );
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  Serial.println("Start");

/*
  drawString(const char *string, int poX, int poY, int font),
  drawCentreString(const char *string, int dX, int poY, int font), // Deprecated, use setTextDatum() and drawString()
  drawRightString(const char *string, int dX, int poY, int font),  // Deprecated, use setTextDatum() and drawString()
*/
  M5.Lcd.setTextColor(TFT_RED);
  M5.Lcd.drawCentreString("Start", 320/2, 0, 2);

  if (Usb.Init() == -1){
    Serial.println("OSC did not start.");
  }

//  M5.Lcd.fillScreen(TFT_BLACK);
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLUE);
//  M5.Lcd.fillRect(0,0,320,TEXT_HEIGHT, TFT_BLUE);
  M5.Lcd.fillRect(0,0,320,LOGO_HEIGHT, TFT_BLUE);
  M5.Lcd.drawCentreString(" FTDI Terminal ", 320/2, 0, 2);

//  delay(1000);
//  M5.Lcd.fillScreen(TFT_BLACK);

  // Change colour for scrolling zone text
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);

  // Setup scroll area
  // setupScrollArea(TOP_FIXED_AREA, BOT_FIXED_AREA);
  setupScrollArea(0, 0);

  // Zero the array
  for (byte i = 0; i<18; i++){
    blank=0;
  }
}

void loop()
{
    Usb.Task();

    if( Usb.getUsbTaskState() == USB_STATE_RUNNING ){
      uint8_t  rcode;
/*      char strbuf[] = "DEADBEEF";
      //char strbuf[] = "The quick brown fox jumps over the lazy dog";
      //char strbuf[] = "This string contains 61 character to demonstrate FTDI buffers"; //add one symbol to it to see some garbage
//      Serial.print(".");
      rcode = Ftdi.SndData(strlen(strbuf), (uint8_t*)strbuf);
      if (rcode){
        ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
      }
      delay(50);
*/
      uint8_t  buf[64];
      for (uint8_t i=0; i<64; i++){
        buf = 0;
     }

      uint16_t rcvd = 64;

      rcode = Ftdi.RcvData(&rcvd, buf);

      if (rcode && rcode != hrNAK){
        ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
      }

      if( rcvd > 2 ) { //more than 2 bytes received
        for(uint16_t i = 2; i < rcvd; i++ ) {

        // Countermeasures against the loss of display of the first data
        if(InitFlag == false){
          xPos = 0;
          yDraw = scroll_line();
          scrollAddress(0);
          InitFlag = true;
        }

          Serial.print((char)buf);
//          data = (char)buf;
          data = buf;
          if (data > 31 && data < 128) {
            xPos += M5.Lcd.drawChar(data, xPos, yDraw, 1);
            // blank[(18+(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT)%19]=xPos; // Keep a record of line lengths
          }
          if (data == '\t') {
            xPos += M5.Lcd.drawChar('  ', xPos, yDraw, 1);
          }
          if (data == '\r' || xPos>311) {
            xPos = 0;
            yDraw = scroll_line(); // It can take 13ms to scroll and blank 16 pixel lines
          }
          //change_colour = 1; // Line to indicate buffer is being emptied
        }
      }
    }
}

// ##############################################################################################
// Call this function to scroll the display one text line
// ##############################################################################################
int scroll_line() {
  int yTemp = yStart; // Store the old yStart, this is where we draw the next line
  // Use the record of line lengths to optimise the rectangle size we need to erase the top line
  // M5.Lcd.fillRect(0,yStart,blank[(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT],TEXT_HEIGHT, TFT_BLACK);
  M5.Lcd.fillRect(0,yStart,320,TEXT_HEIGHT, TFT_BLACK);

  // Change the top of the scroll area
  yStart+=TEXT_HEIGHT;
  // The value must wrap around as the screen memory is a circular buffer
  // if (yStart >= YMAX - BOT_FIXED_AREA) yStart = TOP_FIXED_AREA + (yStart - YMAX + BOT_FIXED_AREA);
  if (yStart >= YMAX){
    yStart = 0;
  }
  // Now we can scroll the display
  scrollAddress(yStart);
  return  yTemp;
}

// ##############################################################################################
// Setup a portion of the screen for vertical scrolling
// ##############################################################################################
// We are using a hardware feature of the display, so we can only scroll in portrait orientation
void setupScrollArea(uint16_t tfa, uint16_t bfa) {
  M5.Lcd.writecommand(ILI9341_VSCRDEF); // Vertical scroll definition
  M5.Lcd.writedata(tfa >> 8);           // Top Fixed Area line count
  M5.Lcd.writedata(tfa);
  M5.Lcd.writedata((YMAX-tfa-bfa)>>8);  // Vertical Scrolling Area line count
  M5.Lcd.writedata(YMAX-tfa-bfa);
  M5.Lcd.writedata(bfa >> 8);           // Bottom Fixed Area line count
  M5.Lcd.writedata(bfa);
}

// ##############################################################################################
// Setup the vertical scrolling start address pointer
// ##############################################################################################
void scrollAddress(uint16_t vsp) {
  M5.Lcd.writecommand(ILI9341_VSCRSADD); // Vertical scrolling pointer
  M5.Lcd.writedata(vsp>>8);
  M5.Lcd.writedata(vsp);
}[/mw_shl_code]
相关文件下载 M5Stack_USB_Host_FTDI_Serial_Monitor-master.zip (261.56 KB, 下载次数: 16)




您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐

求助VSCode+PlatformIO Arduino怎么把文件包含到工程中
求助VSCode+PlatformIO Ar
请教一下论坛大佬 PlatformIO对文件进行编译过程中报错“undefined reference to xxx
一款基于Arduino平台的零代码就可驱动的串口彩屏分享
一款基于Arduino平台的零
多年前在论坛上看到一个帖子: 引起了我的共鸣:能不能有一种彩屏,可以像Arduino I
自制麦轮小车robomasterV4详细教程
自制麦轮小车robomasterV4
一 介绍 这个帖子中,我将介绍用arduino制作一台PID调制的麦轮小车底盘部分的方法 资
MS39233 无刷直流马达—完美替代 ST SPIN233
MS39233 无刷直流马达—完
MS39233 是一款低压三个半桥驱动器。完美替代ST SPIN233。它可应用于低电压及电池供电
用arduino nano 连接esp8266,代码写不进esp8266里面,有偿求解决
用arduino nano 连接esp82
哪位大佬能帮我解决下,发二十元红包请喝杯奶茶。+V:lcmazjq 接线图和错误信息在附件
Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
快速回复 返回顶部 返回列表