Monday, 9 September 2019

ARDUINO: ESP32 Bluetooth Serial Pass-through And Debugger - M5StickC

This is a simple Arduino project showing how to use the M5StickC device as a bluetooth/serial port pass through with the LCD screen as a built in debugger.

The device shows up on your phone as a bluetooth device called "M5StickC", and once you've connected from your device you can use an app like Serial Bluetooth Terminal communicate with your device.

Messages coming from USB serial port are printed as white text and messages coming from bluetooth are printed as blue text.



#include "bluetoothserial.h"
#include "m5stickc.h"

// M5StickC screen parameters
#define TEXT_HEIGHT 16                    // 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 BOTTOM_FIXED_AREA 0       // Number of lines in bottom fixed area (lines counted from bottom of screen)
#define YMAX 80                                  // Bottom of screen area
#define XMAX 160                                // Bottom of screen area

uint16_t  yStart = 0;                               // The initial y coordinate of the top of the scrolling area
uint16_t  yArea = YMAX-TOP_FIXED_AREA-BOTTOM_FIXED_AREA; // yArea must be a integral multiple of TEXT_HEIGHT
uint16_t  yDraw = 0;                              // The initial y coordinate of the top of the bottom text line
uint16_t  xPos = 0;                                // Keep track of the drawing x coordinate
byte      data = 0;
int       blank[19];                                    // We keep all the strings pixel lengths to optimise the speed of the top line blanking


BluetoothSerial SerialBT;


void setup() {
  // Setup the TFT display
  M5.begin();
  M5.Lcd.setRotation(3);                               // Must be setRotation(0) for this sketch to work correctly
  M5.Lcd.fillScreen(TFT_BLACK);

  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLUE);
  M5.Lcd.fillRect(0, 0, XMAX, TEXT_HEIGHT, TFT_BLUE);
  M5.Lcd.drawCentreString("Serial Term - 115200", XMAX/2, 0, 2);
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);    // Change colour for scrolling zone text

  setupScrollArea(TOP_FIXED_AREA, BOTTOM_FIXED_AREA); // Setup scroll area
  for (byte i = 0; i<18; i++) blank[i]=0;            // Zero the array

  Serial.begin(115200);                                 // Initialise the serial ports
  SerialBT.begin("M5StickC");                       // Bluetooth device name
  Serial.println("\n# Bluetooth started #\n");  // Let the user know via usb serial port the device is ready
}

void loop() {
  // Check the Serial buffer for a new message
  while(Serial.available()) {
    data = Serial.read();                                 // Read the character from the serial port
    SerialBT.write(data);                                 // Write the message to the Bluetooth serial port
    M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK); 
    writeToScreen(data);                                // Also write to the M5StickC screen
  }

  // Check the bluetooth buffer for a new message
  while(SerialBT.available()) {
    data = SerialBT.read();                             // Read the character from the bluetooth port
    Serial.write(data);                                     // Write the message to the serial port
    M5.Lcd.setTextColor(TFT_BLUE, TFT_BLACK); 
    writeToScreen(data);                                // Also write to the M5StickC screen
  }
  
  delay(20);
}

void writeToScreen(byte data) {
  // If it is a CR or we are near end of line then scroll one line
  if (data == '\r' || xPos > XMAX) {
    xPos = 0;
    yDraw = scroll_line();                                // It can take 13ms to scroll and blank 16 pixel lines
  }
  
  if (data > 31 && data < 128) {
    xPos += M5.Lcd.drawChar(data,xPos,yDraw,2);
  }
}

/**
 * Scroll the display one text line
 */
int scroll_line() {
  // Store the old yStart, this is where we draw the next line
  int yTemp = yStart;
  
  // Use the record of line lengths to optimise the rectangle size we need to erase the top line
  M5.Lcd.fillRect(0,yStart,XMAX,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)
    yStart = 0;

  // Now we can scroll the display
  scrollAddress(yStart);

  return  yTemp;
}

/**
 * Setup the vertical scrolling start address pointer
 */
void scrollAddress(uint16_t vsp) {
  M5.Lcd.writecommand(ST7735_VSCRDEF);  // Vertical scrolling pointer
  M5.Lcd.writedata(vsp>>8);
  M5.Lcd.writedata(vsp);
}

/**
 * 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(ST7735_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);
}