m5stack / M5StickC-Plus

M5StickCPlus Arduino Library
MIT License
372 stars 89 forks source link

{Update} Can I suggest an update the to I2C tester.ino #51

Open MDAR opened 1 year ago

MDAR commented 1 year ago

On my journey of finding out what is wrong with the 8Servo units that I have, I have needed to get my head around I2C ports and how to scan them.

As the StickC and StickC-Plus have two ports, I modified your I2C_tester.ino code so that I can toggle between the ports, after a restart

This is the script I used for the M5StickC

Pressing Button A will set it to the TOP / HAT I2C port and restart

Pressing Button B will set it to the Bottom / Grove port and restart

It's not pretty code, but it does work.

I just wish I could get the 8Servo (or in fact the Servo Hat) to work.

/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
*                  Equipped with M5StickC sample source code
*                          配套  M5StickC 示例源代码
* Visit for more information: https://docs.m5stack.com/en/core/m5stickc
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/m5stickc
*
* Describe:  I2C Scanner.  I2C探测
* Date: 2021/7/27
*******************************************************************************
*/
/*
This program scans the addresses 1-127 continuosly and shows the devices found
on the TFT. 该程序连续扫描地址 1-127 并显示在外部(内部)I2C发现的设备。
*/
#include <M5StickC.h>
#include <Preferences.h>

Preferences preferences;

int I2C_SDA = 0;
int I2C_SCL = 26;

bool releaseA = false;
bool releaseB = false;
bool portName = 1;

String portNameAlias = "Top Port";

int textColor = YELLOW;

void btn_task(void *arg) { // set this VOID as a task, to run outside of LOOP
    while (1) {

        if (M5.BtnA.wasPressed() ) {
            releaseA = !releaseA;
              Serial.println("Chaneg Define");
//        Wire.end();
//            delay (300);
//        Wire.begin(0, 26); // Detect external I2C, Top pins if this sentence is  added,
 I2C_SDA = 0;
 I2C_SCL = 26;
        Serial.println("TOP I2C port activated");
            M5.Lcd.setCursor(0, 0);
            M5.Lcd.println("   RESTARTING                          ");
        portName = 1;
  // Store the counter to the Preferences
  preferences.putUInt("I2C_SDA", I2C_SDA );
  preferences.putUInt("I2C_SCL", I2C_SCL );
  preferences.putBool("portName", portName);

  // Close the Preferences
  preferences.end();
  delay(300);
  ESP.restart();

        }
             if (M5.BtnB.wasPressed() ) {
            releaseB = !releaseB;
              Serial.println("Chaneg Define");
 //       Wire.end();
 //           delay (300);
 //       Wire.begin(33, 32); // Detect external I2C, Top pins if this sentence is  added,
 I2C_SDA = 32;
 I2C_SCL = 33;
        Serial.println("Bottom Grove I2C port activated");
                    M5.Lcd.setCursor(0, 0);
                    M5.Lcd.println("      RESTARTING                               ");
        portName = 0;
  // Store the counter to the Preferences
  preferences.putUInt("I2C_SDA", I2C_SDA );
  preferences.putUInt("I2C_SCL", I2C_SCL );
  preferences.putBool("portName", portName);

  // Close the Preferences
  preferences.end();
  delay(300);
  ESP.restart();
        }
        M5.update();
        vTaskDelay(20);
    }

}

void setup() {
    M5.begin();    // Init M5StickC(Init of external I2C is also included ).
                   // 初始化M5StickC(初始化外部I2C也包含在内)
    Serial.begin(9600);
    Serial.println("  ");
        Serial.println(" Hello World ");
        Serial.println("  ");

     preferences.begin("I2C", false);
     I2C_SDA = preferences.getUInt("I2C_SDA", 0); // 0 is incase the prefer does not exist
     I2C_SCL = preferences.getUInt("I2C_SCL", 26); // 26 is incase the prefer does not exist
     Wire.begin(I2C_SDA, I2C_SCL);
     portName = preferences.getBool("portName", 1); // 1 is incase the prefer does not exist, changed later to Top or BASE
if (portName){
  portNameAlias = "Top Port";
  }
else {
  portNameAlias = "Base Port";
}

Serial.println( " I2C Pins in use - SDA = "+String(I2C_SDA)+" , SCL = "+String(I2C_SCL) );

  //   Wire.begin();  // Init wire and join the I2C network. // joins Grove port by default
  //   Wire.begin(0, 26); // Detect external I2C, Top pins if this sentence is  added,
  // Wire.begin(33,32);  //Detect external I2C, Grove port if this sentence is  added,

    M5.Lcd.setTextColor(
        YELLOW);  // Set the font color to yellow.  设置字体颜色为黄色
    M5.Lcd.setRotation(3);                  // Rotate the screen.  旋转屏幕
  //  M5.Lcd.println("M5StickC I2C Tester");  // Print a string on the screen.
                                            // 在屏幕上打印字符串
    Serial.println("M5StickC I2C Tester - "+portNameAlias);
   // Serial.println("M5StickC I2C Tester - Bottom  Port ");

    delay(3000);
    M5.Lcd.fillScreen(
        BLACK);  // Make the screen full of black (equivalent to clear() to
                 // clear the screen).  使屏幕充满黑色(等效clear()清屏)

     xTaskCreate(btn_task,   /* Task function. */
                "btn_task", /* String with name of task. */
                8096,       /* Stack size in bytes. */
                NULL,       /* Parameter passed as input of the task */
                1,          /* Priority of the task. */
                NULL);      /* Task handle. */
}

void loop() {
scanI2C();
delay(200);
}

void scanI2C() {
 //   Wire.begin(I2C_SDA, I2C_SCL);

    int address;
    int error;
    M5.Lcd.setCursor(0, 0);
    M5.Lcd.println("scan "+portNameAlias+" [HEX]");
   // M5.Lcd.println("scan Base port [HEX]");
    Serial.println("   'END' "+portNameAlias+"   ");
    Serial.println("     New Scan >>   ");

    for (address = 1; address < 127; address++) {
        Wire.beginTransmission(
            address);  // Data transmission to the specified device address
                       // starts.   开始向指定的设备地址进行传输数据
        error = Wire.endTransmission(); /*Stop data transmission with the slave.
                   停止与从机的数据传输 0: success.  成功 1: The amount of data
                   exceeds the transmission buffer capacity limit.
                   数据量超过传送缓存容纳限制 return value:              2:
                   Received NACK when sending address.  传送地址时收到 NACK 3:
                   Received NACK when transmitting data.  传送数据时收到 NACK
                                              4: Other errors.  其它错误 */
        if (error == 0) {
            M5.Lcd.print(address, HEX); // show the Decimal address as a HEX value
            M5.Lcd.print(" ");
            Serial.print("Found @ 0x");
            Serial.print(address,HEX); // show the Decimal address as a HEX value
            Serial.print("  & ");
        } else
            M5.Lcd.print(".");
           // Serial.println(" ----- ");

        delay(10);
    }

    if (textColor == YELLOW) {
        textColor = CYAN;
    } else
        textColor = YELLOW;
    M5.Lcd.setTextColor(textColor,
                        BLACK);  // Set the foreground color of the text to
                                 // textColor and the background color to BLACK.
                                 // 设置文字的前景色为textColor背景色为BLACK
}