/* 
   参考にしたURL:
   https://protosupplies.com/product/xy-lpwm-pwm-signal-generator-module/
    
Uses hardware serial to talk to the host computer and software serial for 
communication with the PWM module for compatibility with any MCU
 
Connections
  MCU 5V to module VIN+
  MCU GND to module VIN-
  MCU D11 to module RXD using a logic level shifter or voltage divider
  MCU D10 to module TXD

  When a command is entered in the Serial Monitor on the computer, the MCU will
  relay it to the PWM module and echo it to the Serial Monitor window.  
  
  Note that frequency and duty cycle are upper case i.e. 'F100' or 'D050'
  The query on the other hand is lower or upper case, 'read' or 'READ' depend on the module. 
  
  Ensure that Serial Monitor Window is set for 9600 and 'No line ending'
  
  Any characters returned from the module will be displayed in the Serial Monitor Window.

  Uses Softserial.h library.  Can use hardware serial port if MCU supports it
*/ 
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(10, 11); // RX | TX pins.  Can be reassigned if needed
const long BAUDRATE = 9600;    // Baud rate of the XY-LPWM3 module
char c = ' ';                  // Character being reciefed/transmitted 
String input="";
String recvd="";
//===============================================================================
//  Initialization
//===============================================================================
void setup() 
{
    SoftSerial.begin(BAUDRATE);  // Init soft serial object
    Serial.begin(9600);          // Init hardware serial
    Serial.println("Set Serial Monitor Window for 'No line ending' and '9600 baud'");
    Serial.println("Enter 'Fxxx', 'Fx.xx', 'Fxx.x' or 'Fx.x.x' to set frequency");
    Serial.println("Enter 'D1:xxx','D2:xxx','D3:xxx' to set duty cycle");
    Serial.println("Enter 'READ' to get current settings, depend on module");
    Serial.println("XY-LPWM3_check.ino");
}
//===============================================================================
//  Main
//=============================================================================== 
void loop()
{
    input="";
    // Read characters from the Serial Monitor
    while(Serial.available())
    {
        c = Serial.read();
        input+=c;
    }
    
    if(input!="")
    {
        Serial.println(input);// Echo characters typed to serial monitor window
        SoftSerial.print(input);// Send characters to the XY-LPWM3 module
    }
    delay(500);
  
    recvd="";
    // Watch for any characters returned from module
    while(SoftSerial.available())
    {
        c = SoftSerial.read();// character returned from module
        // Send character returned from module to the Serial Monitor
        //if (c=='F' || c=='D') Serial.print(' ');// Add space between commands
        Serial.print(c);
        recvd+=c;
    }
    
    if(recvd!="")// characters returned from module
    {
      Serial.print(" :length()=");
      Serial.print(recvd.length());
      Serial.println(" characters");
      for(int i=0; i<recvd.length(); i++)
      {
        c=recvd.charAt(i);
        if(c<20)
        {
          Serial.print(" ");
          Serial.print(c,HEX);       
        }
        else
        {
          Serial.print(c,HEX);       
        }
        Serial.print(",");
      }
      Serial.println(" :HEX");
      
      for(int i=0; i<recvd.length();i++)
      {
        if(recvd.charAt(i)<0x20)
        {
          Serial.print("  ");
        }
        else
        {
          Serial.print(" ");
          Serial.print(recvd.charAt(i)); 
        }
        Serial.print(",");
      }
      Serial.println(" :char");
      Serial.println();
    }
    delay(500);
}