diff --git a/examples/Advanced/Storage/EEPROM/EEPROM.ino b/examples/Advanced/Storage/EEPROM/EEPROM.ino index 2c1f41a4..e27bf463 100644 --- a/examples/Advanced/Storage/EEPROM/EEPROM.ino +++ b/examples/Advanced/Storage/EEPROM/EEPROM.ino @@ -23,22 +23,25 @@ int addr = 0; //EEPROM Start number of an ADDRESS. EEPROM地址起始编号 void setup() { M5.begin(); //Init M5Core2. 初始化 M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 if (!EEPROM.begin(SIZE)){ //Request storage of SIZE size(success return 1). 申请SIZE大小的存储(成功返回1) M5.Lcd.println("\nFailed to initialise EEPROM!"); //串口输出格式化字符串. Serial output format string delay(1000000); } - M5.Lcd.println("\nRead data from Flash. Values are:"); + M5.Lcd.println("\nRead data from EEPROM. Values are:"); for (int i = 0; i < SIZE; i++){ M5.Lcd.printf("%d ",EEPROM.read(i)); //Reads data from 0 to SIZE in EEPROM. 读取EEPROM中从0到SIZE中的数据 } + M5.Lcd.println("\n\nPress BtnA to Write EEPROM"); } void loop() { M5.update(); //Check button down state. 检测按键按下状态 + M5.lcd.setTextSize(1); //Set the text size to 1. 设置文字大小为1 if(M5.BtnA.isPressed()){ //if the button.A is Pressed. 如果按键A按下 M5.lcd.clear(); - M5.lcd.setCursor(0,20); - M5.Lcd.printf("\n%d Bytes datas written on Flash.\nValues are:\n",SIZE); + M5.lcd.setCursor(0,0); + M5.Lcd.printf("\n%d Bytes datas written on EEPROM.\nValues are:\n",SIZE); for(int i=0;i -#include -#include "ADS1100.h" +#include "M5_ADS1100.h" ADS1100 ads; void setup(void) { - M5.begin(true, false, false); - Serial.begin(115200); - M5.Lcd.fillScreen(BLACK); - M5.Lcd.setTextColor(ORANGE); + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 - // The address can be changed making the option of connecting multiple devices - ads.getAddr_ADS1100(ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) + // The address can be changed making the option of connecting multiple devices + // 地址可以改变,以连接多个设备 + ads.getAddr_ADS1100(ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) - // The ADC gain (PGA), Device operating mode, Data rate - // can be changed via the following functions + //The ADC gain (PGA). ADC增益(PGA) + ads.setGain(GAIN_ONE); // 1x gain(default) + // ads.setGain(GAIN_TWO); // 2x gain + // ads.setGain(GAIN_FOUR); // 4x gain + // ads.setGain(GAIN_EIGHT); // 8x gain - ads.setGain(GAIN_ONE); // 1x gain(default) - // ads.setGain(GAIN_TWO); // 2x gain - // ads.setGain(GAIN_FOUR); // 4x gain - // ads.setGain(GAIN_EIGHT); // 8x gain + //Device operating mode. 设备工作模式 + ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) + // ads.setMode(MODE_SINGLE); // Single-conversion mode - ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) - // ads.setMode(MODE_SINGLE); // Single-conversion mode + //Data rate. 数据速率 + ads.setRate(RATE_8); // 8SPS (default) + // ads.setRate(RATE_16); // 16SPS + // ads.setRate(RATE_32); // 32SPS + // ads.setRate(RATE_128); // 128SPS - ads.setRate(RATE_8); // 8SPS (default) - // ads.setRate(RATE_16); // 16SPS - // ads.setRate(RATE_32); // 32SPS - // ads.setRate(RATE_128); // 128SPS + ads.setOSMode(OSMODE_SINGLE); // Set to start a single-conversion. 设置开始一次转换 - ads.setOSMode(OSMODE_SINGLE); // Set to start a single-conversion - - ads.begin(); + ads.begin(); //Sets up the Hardware. 设置硬件 } void loop(void) { - byte error; - int8_t address; - - address = ads.ads_i2cAddress; - // The i2c_scanner uses the return value of - // the Write.endTransmisstion to see if - // a device did acknowledge to the address. - Wire.beginTransmission(address); - error = Wire.endTransmission(); - if (error == 0) - { - int16_t result; + byte error; + int8_t address; - Serial.println("Getting Differential Reading from ADS1100"); - Serial.println(" "); - result = ads.Measure_Differential(); - Serial.print("Digital Value of Analog Input between Channel 0 and 1: "); - Serial.println(result); - M5.Lcd.fillScreen(BLACK); - char data[20] = { 0 }; - sprintf(data, "%d", result); - M5.Lcd.drawCentreString(data, 160, 100, 4); - Serial.println(" "); - Serial.println(" *************************** "); - Serial.println(" "); - } - else - { - Serial.println("ADS1100 Disconnected!"); - Serial.println(" "); - Serial.println(" ************ "); - Serial.println(" "); - M5.Lcd.setTextFont(4); - M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK); - M5.Lcd.drawString("No Found ADC sensor.",20, 100, 4); - } - - delay(1000); -} + address = ads.ads_i2cAddress; + Wire.beginTransmission(address); + error = Wire.endTransmission(); + if (error == 0) //If the device is connected. 如果连接上设备 + { + int16_t result; + result = ads.Measure_Differential(); + M5.Lcd.fillScreen(BLACK); + char data[20] = { 0 }; + sprintf(data, "%d", result); + M5.Lcd.drawCentreString(data, 160, 100, 4); + } + else + { + M5.Lcd.drawString("No Found ADC sensor.",20, 100, 2); + } + delay(1000); +} \ No newline at end of file diff --git a/examples/Unit/ADC_ADS1100/ADS1100.cpp b/examples/Unit/ADC_ADS1100/ADS1100.cpp deleted file mode 100644 index 4df8a0a2..00000000 --- a/examples/Unit/ADC_ADS1100/ADS1100.cpp +++ /dev/null @@ -1,213 +0,0 @@ -/**************************************************************************/ -/* - Distributed with a free-will license. - Use it any way you want, profit or free, provided it fits in the licenses of its associated works. - ADS1100 - This code is designed to work with the ADS1100_I2CADC I2C Mini Module available from ControlEverything.com. - https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS1100_I2CADC#tabs-0-product_tabset-2 -*/ -/**************************************************************************/ - -#if ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -#include - -#include "ADS1100.h" - -/**************************************************************************/ -/* - Abstract away platform differences in Arduino wire library -*/ -/**************************************************************************/ -static uint8_t i2cread(void) -{ - #if ARDUINO >= 100 - return Wire.read(); - #else - return Wire.receive(); - #endif -} - -/**************************************************************************/ -/* - Abstract away platform differences in Arduino wire library -*/ -/**************************************************************************/ -static void i2cwrite(uint8_t x) -{ - #if ARDUINO >= 100 - Wire.write((uint8_t)x); - #else - Wire.send(x); - #endif -} - -/**************************************************************************/ -/* - Writes 8-bits to the destination register -*/ -/**************************************************************************/ -static void writeRegister(uint8_t i2cAddress, uint8_t value) -{ - Wire.beginTransmission(i2cAddress); - i2cwrite((uint8_t)value); - Wire.endTransmission(); -} - -/**************************************************************************/ -/* - Reads 16-bits from the destination register -*/ -/**************************************************************************/ -static uint16_t readRegister(uint8_t i2cAddress) -{ - Wire.beginTransmission(i2cAddress); - Wire.endTransmission(); - Wire.requestFrom(i2cAddress, (uint8_t)2); - return (int16_t)((i2cread() << 8) | i2cread()); -} - -/**************************************************************************/ -/* - Instantiates a new ADS1100 class with appropriate properties -*/ -/**************************************************************************/ -void ADS1100::getAddr_ADS1100(uint8_t i2cAddress) -{ - ads_i2cAddress = i2cAddress; - ads_conversionDelay = ADS1100_CONVERSIONDELAY; -} - -/**************************************************************************/ -/* - Sets up the Hardware -*/ -/**************************************************************************/ -void ADS1100::begin() -{ - Wire.begin(); -} - -/**************************************************************************/ -/* - Sets the Operational status/single-shot conversion start - This determines the operational status of the device -*/ -/**************************************************************************/ -void ADS1100::setOSMode(adsOSMode_t osmode) -{ - ads_osmode = osmode; -} - -/**************************************************************************/ -/* - Gets the Operational status/single-shot conversion start -*/ -/**************************************************************************/ -adsOSMode_t ADS1100::getOSMode() -{ - return ads_osmode; -} - -/**************************************************************************/ -/* - Sets the Device operating mode - This controls the current operational mode of the ADS1100 -*/ -/**************************************************************************/ -void ADS1100::setMode(adsMode_t mode) -{ - ads_mode = mode; -} - -/**************************************************************************/ -/* - Gets the Device operating mode -*/ -/**************************************************************************/ -adsMode_t ADS1100::getMode() -{ - return ads_mode; -} - -/**************************************************************************/ -/* - Sets the Date Rate - This controls the data rate setting -*/ -/**************************************************************************/ -void ADS1100::setRate(adsRate_t rate) -{ - ads_rate = rate; -} - -/**************************************************************************/ -/* - Gets the Date Rate -*/ -/**************************************************************************/ -adsRate_t ADS1100::getRate() -{ - return ads_rate; -} - -/**************************************************************************/ -/* - Sets the gain and input voltage range - This configures the programmable gain amplifier -*/ -/**************************************************************************/ -void ADS1100::setGain(adsGain_t gain) -{ - ads_gain = gain; -} - -/**************************************************************************/ -/* - Gets a gain and input voltage range -*/ -/**************************************************************************/ -adsGain_t ADS1100::getGain() -{ - return ads_gain; -} - -/**************************************************************************/ -/* - Reads the conversion results, measuring the voltage - difference between the P and N input - Generates a signed value since the difference can be either - positive or negative -*/ -/**************************************************************************/ -int16_t ADS1100::Measure_Differential() -{ - // Start with default values - uint16_t config; - - // Set Operational status/single-shot conversion start - config |= ads_osmode; - - // Set Device operating mode - config |= ads_mode; - - // Set Data rate - config |= ads_rate; - - // Set PGA/voltage range - config |= ads_gain; - - // Write config register to the ADC - writeRegister(ads_i2cAddress, config); - - // Wait for the conversion to complete - delay(ads_conversionDelay); - - // Read the conversion results - uint16_t raw_adc = readRegister(ads_i2cAddress); - return (int16_t)raw_adc; -} diff --git a/examples/Unit/ADC_ADS1100/ADS1100.h b/examples/Unit/ADC_ADS1100/ADS1100.h deleted file mode 100644 index 8bf3bc87..00000000 --- a/examples/Unit/ADC_ADS1100/ADS1100.h +++ /dev/null @@ -1,113 +0,0 @@ -/**************************************************************************/ -/* - Distributed with a free-will license. - Use it any way you want, profit or free, provided it fits in the licenses of its associated works. - ADS1100 - This code is designed to work with the ADS1100_I2CADC I2C Mini Module available from ControlEverything.com. - https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS1100_I2CADC#tabs-0-product_tabset-2 -*/ -/**************************************************************************/ - -#if ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -#include - -/************************************************************************** - I2C ADDRESS/BITS -**************************************************************************/ - #define ADS1100_DEFAULT_ADDRESS (0x48) // 1001 000 (ADDR = GND) - -/************************************************************************** - CONVERSION DELAY (in mS) -**************************************************************************/ - #define ADS1100_CONVERSIONDELAY (100) - -/************************************************************************** - CONFIG REGISTER -**************************************************************************/ - #define ADS1100_REG_CONFIG_OS_MASK (0x80) // Conversion - #define ADS1100_REG_CONFIG_OS_NOEFFECT (0x00) // Write: Bit = 0 No effect - #define ADS1100_REG_CONFIG_OS_SINGLE (0x80) // Write: Bit = 1 Begin a conversion (default) - #define ADS1100_REG_CONFIG_OS_BUSY (0x00) // Read: Bit = 0 Device is not performing a conversion - #define ADS1100_REG_CONFIG_OS_NOTBUSY (0x80) // Read: Bit = 1 Device is busy performing a conversion - - #define ADS1100_REG_CONFIG_MODE_MASK (0x10) // Device operating mode - #define ADS1100_REG_CONFIG_MODE_CONTIN (0x00) // Continuous conversion mode (default) - #define ADS1100_REG_CONFIG_MODE_SINGLE (0x10) // Single-conversion mode - - #define ADS1100_REG_CONFIG_DR_MASK (0x0C) // Data rate - #define ADS1100_REG_CONFIG_DR_128SPS (0x00) // 128 samples per second - #define ADS1100_REG_CONFIG_DR_32SPS (0x04) // 32 samples per second - #define ADS1100_REG_CONFIG_DR_16SPS (0x08) // 16 samples per second - #define ADS1100_REG_CONFIG_DR_8SPS (0x0C) // 8 samples per second (default) - - #define ADS1100_REG_CONFIG_PGA_MASK (0x03) // Programmable gain amplifier configuration - #define ADS1100_REG_CONFIG_PGA_1 (0x00) // Gain 1 (default) - #define ADS1100_REG_CONFIG_PGA_2 (0x01) // Gain 2 - #define ADS1100_REG_CONFIG_PGA_4 (0x02) // Gain 4 - #define ADS1100_REG_CONFIG_PGA_8 (0x03) // Gain 8 - -/**************************************************************************/ - - -typedef enum -{ - OSMODE_SINGLE = ADS1100_REG_CONFIG_OS_SINGLE, - OSMODE_BUSY = ADS1100_REG_CONFIG_OS_BUSY, - OSMODE_NOTBUSY = ADS1100_REG_CONFIG_OS_NOTBUSY - -} adsOSMode_t; - -typedef enum -{ - MODE_CONTIN = ADS1100_REG_CONFIG_MODE_CONTIN, - MODE_SINGLE = ADS1100_REG_CONFIG_MODE_SINGLE -} adsMode_t; - -typedef enum -{ - RATE_1288 = ADS1100_REG_CONFIG_DR_128SPS, - RATE_32 = ADS1100_REG_CONFIG_DR_32SPS, - RATE_16 = ADS1100_REG_CONFIG_DR_16SPS, - RATE_8 = ADS1100_REG_CONFIG_DR_8SPS -} adsRate_t; - -typedef enum -{ - GAIN_ONE = ADS1100_REG_CONFIG_PGA_1, - GAIN_TWO = ADS1100_REG_CONFIG_PGA_2, - GAIN_FOUR = ADS1100_REG_CONFIG_PGA_4, - GAIN_EIGHT = ADS1100_REG_CONFIG_PGA_8 -} adsGain_t; - - -class ADS1100 -{ - protected: - // Instance-specific properties - uint8_t ads_conversionDelay; - adsOSMode_t ads_osmode; - adsMode_t ads_mode; - adsRate_t ads_rate; - adsGain_t ads_gain; - - public: - uint8_t ads_i2cAddress; - void getAddr_ADS1100(uint8_t i2cAddress); - void begin(void); - int16_t Measure_Differential(); - void setOSMode(adsOSMode_t osmode); - adsOSMode_t getOSMode(void); - void setMode(adsMode_t mode); - adsMode_t getMode(void); - void setRate(adsRate_t rate); - adsRate_t getRate(void); - void setGain(adsGain_t gain); - adsGain_t getGain(void); - - private: -}; \ No newline at end of file diff --git a/examples/Unit/A_Meter_Unit/ammeter/ammeter.cpp b/examples/Unit/A_Meter_Unit/ammeter/ammeter.cpp deleted file mode 100644 index cfd01f88..00000000 --- a/examples/Unit/A_Meter_Unit/ammeter/ammeter.cpp +++ /dev/null @@ -1,286 +0,0 @@ -#include "ammeter.h" -#include "Wire.h" - -void Ammeter::i2cBegin() { - // Wire.begin(); -} - -bool Ammeter::i2cReadBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff, uint16_t len) { - Wire.beginTransmission(addr); - Wire.write(reg_addr); - uint8_t i = 0; - if (Wire.endTransmission(false) == 0 && Wire.requestFrom(addr, (uint8_t)len)) { - while (Wire.available()) { - buff[i++] = Wire.read(); - } - return true; - } - - return false; -} - -bool Ammeter::i2cWriteBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff, uint16_t len) { - bool function_result = false; - - Wire.beginTransmission(addr); - Wire.write(reg_addr); - for(int i = 0; i < len; i++) { - Wire.write(*(buff+i)); - } - function_result = (Wire.endTransmission() == 0); - return function_result; -} - -bool Ammeter::i2cReadU16(uint8_t addr, uint8_t reg_addr, uint16_t* value) { - uint8_t read_buf[2] = {0x00, 0x00}; - bool result = i2cReadBytes(addr, reg_addr, read_buf, 2); - *value = (read_buf[0] << 8) | read_buf[1]; - return result; -} - -bool Ammeter::i2cWriteU16(uint8_t addr, uint8_t reg_addr, uint16_t value) { - uint8_t write_buf[2]; - write_buf[0] = value >> 8; - write_buf[1] = value & 0xff; - return i2cWriteBytes(addr, reg_addr, write_buf, 2); -} - -float Ammeter::getResolution(ammeterGain_t gain) { - switch (gain) { - case PAG_6144: - return ADS1115_MV_6144 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_4096: - return ADS1115_MV_4096 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_2048: - return ADS1115_MV_2048 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_1024: - return ADS1115_MV_1024 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_512: - return ADS1115_MV_512 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_256: - return ADS1115_MV_256 / AMMETER_PRESSURE_COEFFICIENT; - default: - return ADS1115_MV_256 / AMMETER_PRESSURE_COEFFICIENT; - }; -} - -uint8_t Ammeter::getPGAEEEPROMAddr(ammeterGain_t gain) { - switch (gain) { - case PAG_6144: - return AMMETER_PAG_6144_CAL_ADDR; - case PAG_4096: - return AMMETER_PAG_4096_CAL_ADDR; - case PAG_2048: - return AMMETER_PAG_2048_CAL_ADDR; - case PAG_1024: - return AMMETER_PAG_1024_CAL_ADDR; - case PAG_512: - return AMMETER_PAG_512_CAL_ADDR; - case PAG_256: - return AMMETER_PAG_256_CAL_ADDR; - default: - return 0x00; - }; -} - -uint16_t Ammeter::getCoverTime(ammeterRate_t rate) { - switch (rate) { - case RATE_8: - return 1000 / 8; - case RATE_16: - return 1000 / 16; - case RATE_32: - return 1000 / 32; - case RATE_64: - return 1000 / 64; - case RATE_128: - return 1000 / 128; - case RATE_250: - return 1000 / 250; - case RATE_475: - return 1000 / 475; - case RATE_860: - return 1000 / 860; - default: - return 1000 / 128; - }; -} - -Ammeter::Ammeter(uint8_t ads1115_addr, uint8_t eeprom_addr) { - _ads1115_addr = ads1115_addr; - _eeprom_addr = eeprom_addr; - _gain = PAG_2048; - _mode = SINGLESHOT; - _rate = RATE_128; - calibration_factor = 1; - adc_raw = 0; - resolution = getResolution(_gain); - cover_time = getCoverTime(_rate); -} - -void Ammeter::setGain(ammeterGain_t gain) { - uint16_t reg_value = 0; - bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, ®_value); - - if (result == false) { - return; - } - - reg_value &= ~(0b0111 << 9); - reg_value |= gain << 9; - - result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value); - - if (result) { - _gain = gain; - resolution = getResolution(gain); - int16_t hope = 1; - int16_t actual = 1; - if (readCalibrationFromEEPROM(gain, &hope, &actual)) { - calibration_factor = fabs((double)hope / actual); - } - } -} - -void Ammeter::setRate(ammeterRate_t rate) { - uint16_t reg_value = 0; - bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, ®_value); - if (result == false) { - return; - } - - reg_value &= ~(0b0111 << 5); - reg_value |= rate << 5; - - result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value); - - if (result) { - _rate = rate; - cover_time = getCoverTime(_rate); - } - - return; -} - -void Ammeter::setMode(ammeterMode_t mode) { - uint16_t reg_value = 0; - bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, ®_value); - if (result == false) { - return; - } - - reg_value &= ~(0b0001 << 8); - reg_value |= mode << 8; - - result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value); - if (result) { - _mode = mode; - } - - return; -} - -bool Ammeter::isInConversion() { - uint16_t value = 0x00; - i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, &value); - - return (value & (1 << 15)) ? false : true; -} - -void Ammeter::startSingleConversion() { - uint16_t reg_value = 0; - bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, ®_value); - - if (result == false) { - return; - } - - reg_value &= ~(0b0001 << 15); - reg_value |= 0x01 << 15; - - i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value); -} - -float Ammeter::getCurrent(bool calibration) { - if (calibration) { - return resolution * calibration_factor * getConversion() * AMMETER_MEASURING_DIR; - } else { - return resolution * getConversion() * AMMETER_MEASURING_DIR; - } -} - -int16_t Ammeter::getAdcRaw() { - uint16_t value = 0x00; - i2cReadU16(_ads1115_addr, ADS1115_RA_CONVERSION, &value); - adc_raw = value; - return value; -} - -int16_t Ammeter::getConversion(uint16_t timeout) { - if (_mode == SINGLESHOT) { - startSingleConversion(); - delay(cover_time); - uint64_t time = millis() + timeout; - while (time > millis() && isInConversion()); - } - - return getAdcRaw(); -} - -bool Ammeter::EEPORMWrite(uint8_t address, uint8_t* buff, uint8_t len) { - return i2cWriteBytes(_eeprom_addr, address, buff, len); -} - -bool Ammeter::EEPORMRead(uint8_t address, uint8_t* buff, uint8_t len) { - return i2cReadBytes(_eeprom_addr, address, buff, len); -} - -bool Ammeter::saveCalibration2EEPROM(ammeterGain_t gain, int16_t hope, int16_t actual) { - if (hope == 0 || actual == 0) { - return false; - } - - uint8_t buff[8]; - memset(buff, 0, 8); - buff[0] = gain; - buff[1] = hope >> 8; - buff[2] = hope & 0xFF; - - buff[3] = actual >> 8; - buff[4] = actual & 0xFF; - - for (uint8_t i = 0; i < 5; i++) { - buff[5] ^= buff[i]; - } - - uint8_t addr = getPGAEEEPROMAddr(gain); - return EEPORMWrite(addr, buff, 8); -} - -bool Ammeter::readCalibrationFromEEPROM(ammeterGain_t gain, int16_t* hope, int16_t* actual) { - uint8_t addr = getPGAEEEPROMAddr(gain); - uint8_t buff[8]; - memset(buff, 0, 8); - - *hope = 1; - *actual = 1; - - bool result = EEPORMRead(addr, buff, 8); - - if (result == false) { - return false; - } - - uint8_t xor_result = 0x00; - for (uint8_t i = 0; i < 5; i++) { - xor_result ^= buff[i]; - } - - if (xor_result != buff[5]) { - return false; - } - - *hope = (buff[1] << 8) | buff[2]; - *actual = (buff[3] << 8) | buff[4]; - return true; -} diff --git a/examples/Unit/A_Meter_Unit/ammeter/ammeter.h b/examples/Unit/A_Meter_Unit/ammeter/ammeter.h deleted file mode 100644 index 892244da..00000000 --- a/examples/Unit/A_Meter_Unit/ammeter/ammeter.h +++ /dev/null @@ -1,126 +0,0 @@ -#pragma once - -#include "Arduino.h" - -#define ADS115_ADDR 0x48 -#define EEPROM_ADDR 0x51 - -#define ADS1115_RA_CONVERSION 0x00 -#define ADS1115_RA_CONFIG 0x01 - -#define ADS1115_PGA_6144 0x00 -#define ADS1115_PGA_4096 0x01 -#define ADS1115_PGA_2048 0x02 // default -#define ADS1115_PGA_1024 0x03 -#define ADS1115_PGA_512 0x04 -#define ADS1115_PGA_256 0x05 - -#define ADS1115_MV_6144 0.187500F -#define ADS1115_MV_4096 0.125000F -#define ADS1115_MV_2048 0.062500F // default -#define ADS1115_MV_1024 0.031250F -#define ADS1115_MV_512 0.015625F -#define ADS1115_MV_256 0.007813F - -#define ADS1115_RATE_8 0x00 -#define ADS1115_RATE_16 0x01 -#define ADS1115_RATE_32 0x02 -#define ADS1115_RATE_64 0x03 -#define ADS1115_RATE_128 0x04 // default -#define ADS1115_RATE_250 0x05 -#define ADS1115_RATE_475 0x06 -#define ADS1115_RATE_860 0x07 - -#define ADS1115_MUX_P0N1 0x00 // ammeter only support - -#define ADS1115_COMP_MODE_HYSTERESIS 0x00 // default -#define ADS1115_COMP_MODE_WINDOW 0x01 - -#define ADS1115_MODE_CONTINUOUS 0x00 -#define ADS1115_MODE_SINGLESHOT 0x01 // default - -#define AMMETER_PRESSURE_COEFFICIENT 0.05 - -#define AMMETER_PAG_6144_CAL_ADDR 208 -#define AMMETER_PAG_4096_CAL_ADDR 216 -#define AMMETER_PAG_2048_CAL_ADDR 224 -#define AMMETER_PAG_1024_CAL_ADDR 232 -#define AMMETER_PAG_512_CAL_ADDR 240 -#define AMMETER_PAG_256_CAL_ADDR 248 - -#define AMMETER_MEASURING_DIR -1 - -typedef enum { - PAG_6144 = ADS1115_PGA_6144, - PAG_4096 = ADS1115_PGA_4096, - PAG_2048 = ADS1115_PGA_2048, // default - PAG_1024 = ADS1115_PGA_1024, - PAG_512 = ADS1115_PGA_512, - PAG_256 = ADS1115_PGA_256, -} ammeterGain_t; - -typedef enum { - RATE_8 = ADS1115_RATE_8, - RATE_16 = ADS1115_RATE_16, - RATE_32 = ADS1115_RATE_32, - RATE_64 = ADS1115_RATE_64, - RATE_128 = ADS1115_RATE_128, // default - RATE_250 = ADS1115_RATE_250, - RATE_475 = ADS1115_RATE_475, - RATE_860 = ADS1115_RATE_860, -} ammeterRate_t; - -typedef enum { - SINGLESHOT = ADS1115_MODE_SINGLESHOT, - CONTINUOUS = ADS1115_MODE_CONTINUOUS, -} ammeterMode_t; - -class Ammeter { -private: - void i2cBegin(); - bool i2cReadBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff, uint16_t len); - bool i2cWriteBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff, uint16_t len); - bool i2cReadU16(uint8_t addr, uint8_t reg_addr, uint16_t* value); - bool i2cWriteU16(uint8_t addr, uint8_t reg_addr, uint16_t value); - - float getResolution(ammeterGain_t gain); - uint16_t getCoverTime(ammeterRate_t rate); - uint8_t getPGAEEEPROMAddr(ammeterGain_t gain); - - uint8_t _ads1115_addr; - uint8_t _eeprom_addr; - - float pga_256_calibration_5v_value = 10187; - float pga_2048_calibration_25v_value = 6368; - -public: - ammeterGain_t _gain; - ammeterRate_t _rate; - ammeterMode_t _mode; - - float resolution; - uint16_t cover_time; - int16_t adc_raw; - float calibration_factor; - - -public: - Ammeter(uint8_t ads1115_addr=ADS115_ADDR, uint8_t eeprom_addr=EEPROM_ADDR); - - void setGain(ammeterGain_t gain); - void setRate(ammeterRate_t rate); - void setMode(ammeterMode_t mode); - - float getCurrent(bool calibration = true); - int16_t getConversion(uint16_t timeout = 125); - int16_t getAdcRaw(); - - bool isInConversion(); - void startSingleConversion(); - - bool EEPORMWrite(uint8_t address, uint8_t* buff, uint8_t len); - bool EEPORMRead(uint8_t address, uint8_t* buff, uint8_t len); - - bool saveCalibration2EEPROM(ammeterGain_t gain, int16_t hope, int16_t actual); - bool readCalibrationFromEEPROM(ammeterGain_t gain, int16_t* hope, int16_t* actual); -}; diff --git a/examples/Unit/A_Meter_Unit/ammeter/ammeter.ino b/examples/Unit/A_Meter_Unit/ammeter/ammeter.ino index 464d0e2e..26cb2238 100644 --- a/examples/Unit/A_Meter_Unit/ammeter/ammeter.ino +++ b/examples/Unit/A_Meter_Unit/ammeter/ammeter.ino @@ -1,6 +1,23 @@ -#include -#include "ammeter.h" -#include +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe: Ameter_ADS1115. 电流计 +* date:2021/8/19 +******************************************************************************* + Please connect to Port A,Measure current and display. + 请连接端口A,测量电流并显示到屏幕上 + Pay attention: EEPROM (0x51) has built-in calibration parameters when leaving the factory. + Please do not write to the EEPROM, otherwise the calibration data will be overwritten and the measurement results will be inaccurate. + 注意: EEPROM (0x51)在出厂时具有内置的校准参数。请不要写入EEPROM,否则校准数据会被覆盖,测量结果会不准确。 +*/ + +#include "M5Core2.h" +#include "M5_ADS1115.h" Ammeter ammeter; @@ -15,39 +32,25 @@ int16_t hope = 0.0; ammeterGain_t now_gain = PAG_512; void setup(void) { - M5.begin(); + M5.begin(); //Init M5Core2. 初始化M5Core2 Wire.begin(); - ammeter.setMode(SINGLESHOT); - ammeter.setRate(RATE_8); - ammeter.setGain(PAG_512); - hope = page512_volt / ammeter.resolution; - // | PAG | Max Input Voltage(V) | - // | PAG_6144 | 128 | - // | PAG_4096 | 64 | - // | PAG_2048 | 32 | - // | PAG_512 | 16 | - // | PAG_256 | 8 | - - M5.Lcd.fillScreen(BLACK); - M5.Lcd.setTextFont(4); - - M5.Lcd.setCursor(52, 210); - M5.Lcd.printf("2A"); - - - M5.Lcd.setCursor(230, 210); - M5.Lcd.printf("SAVE"); - - // bool result1 = ammeter.saveCalibration2EEPROM(PAG_256, 1024, 1024); - // delay(10); + ammeter.setMode(SINGLESHOT); /* | PAG | Max Input Voltage(V) | */ + ammeter.setRate(RATE_8); /* | PAG_6144 | 128 | */ + ammeter.setGain(PAG_512); /* | PAG_4096 | 64 | */ + hope = page512_volt / ammeter.resolution; /* | PAG_2048 | 32 | */ + /* | PAG_512 | 16 | */ + /* | PAG_256 | 8 | */ + M5.Lcd.setTextFont(4); //Set font to 4 point font. 设置字体为4号字体 + M5.Lcd.setCursor(52, 210); //Set the cursor at (52,210). 将光标设置在(52, 210) + M5.Lcd.printf("2A SAVE"); } void loop(void) { - M5.update(); + M5.update(); //Check the status of the key. 检测按键的状态 if (M5.BtnA.wasPressed()) { - ammeter.setMode(SINGLESHOT); - ammeter.setRate(RATE_8); + ammeter.setMode(SINGLESHOT); //Set the mode. 设置模式 + ammeter.setRate(RATE_8); //Set the rate. 设置速率 ammeter.setGain(PAG_512); now_gain = PAG_512; hope = page512_volt / ammeter.resolution; @@ -59,8 +62,7 @@ void loop(void) { if (M5.BtnC.wasPressed()) { bool success = ammeter.saveCalibration2EEPROM(now_gain, hope, adc_raw); - M5.Lcd.setCursor(230, 210); - + M5.Lcd.setCursor(224, 210); if (success) { M5.Lcd.setTextColor(GREEN, BLACK); } else { @@ -78,7 +80,6 @@ void loop(void) { } float current = ammeter.getCurrent(); - volt_raw_list[raw_now_ptr] = ammeter.adc_raw; raw_now_ptr = (raw_now_ptr == 9) ? 0 : (raw_now_ptr + 1); @@ -123,4 +124,4 @@ void loop(void) { } M5.Lcd.printf("RAW ADC: %d \r\n", adc_raw); -} +} \ No newline at end of file diff --git a/examples/Unit/BPS_Unit/Arduino/BPS_Unit/BPS_Unit.ino b/examples/Unit/BPS_Unit/Arduino/BPS_Unit/BPS_Unit.ino deleted file mode 100644 index 922d7356..00000000 --- a/examples/Unit/BPS_Unit/Arduino/BPS_Unit/BPS_Unit.ino +++ /dev/null @@ -1,64 +0,0 @@ -#include "bmp280.h" -#include - -#define P0 1013.25 - -BMP280 bmp; - -void display_result(double t, double p, double a) { -// M5.Lcd. - M5.Lcd.drawString("Temperature: ", 20, 40, 4 ); - M5.Lcd.drawFloat(t, 2, 180, 40, 4); - M5.Lcd.drawString("C", 280, 40, 4); - - M5.Lcd.drawString("Pressure: ", 20, 80, 4); - M5.Lcd.drawFloat(p, 2, 180, 80, 4); - M5.Lcd.drawString("Pa", 280, 80, 4); - - M5.Lcd.drawString("Altitude: ", 20, 120, 4); - M5.Lcd.drawFloat(a, 2, 180, 120, 4); - M5.Lcd.drawString("M", 280, 120, 4); -} - -void setup() -{ - M5.begin(); - if(!bmp.begin()){ - Serial.println("BMP init failed!"); - while(1); - } - else Serial.println("BMP init success!"); - - bmp.setOversampling(4); - M5.Lcd.drawString("BPS Example", 90, 0, 4); - -} -void loop() -{ - double T,P; - char result = bmp.startMeasurment(); - - if(result!=0){ - delay(result); - result = bmp.getTemperatureAndPressure(T,P); - - if(result!=0) - { - double A = bmp.altitude(P,P0); - - Serial.print("T = \t");Serial.print(T,2); Serial.print(" degC\t"); - Serial.print("P = \t");Serial.print(P,2); Serial.print(" mBar\t"); - Serial.print("A = \t");Serial.print(A,2); Serial.println(" m"); - display_result(T, P, A); - } - else { - Serial.println("Error."); - } - } - else { - Serial.println("Error."); - } - - delay(2000); - M5.Lcd.fillRect(160, 40, 100, 80, BLACK); -} diff --git a/examples/Unit/BPS_Unit/Arduino/BPS_Unit/bmp280.cpp b/examples/Unit/BPS_Unit/Arduino/BPS_Unit/bmp280.cpp deleted file mode 100644 index 66664059..00000000 --- a/examples/Unit/BPS_Unit/Arduino/BPS_Unit/bmp280.cpp +++ /dev/null @@ -1,437 +0,0 @@ -/* - BMP280.cpp - - Bosch BMP280 pressure sensor library for the Arduino microcontroller. - This library uses I2C connection. - Uses floating-point equations from BMP280 datasheet. - modified by mhafuzul islam - version 1.01 16/9/2014 initial version - - Our example code uses the "pizza-eating" license. You can do anything - you like with this code. No really, anything. If you find it useful, - buy me italian pizza someday. -*/ - -#include "BMP280.h" -#include -#include -#include - - -BMP280::BMP280() -{ - //do nothing -} -/* -* Initialize library and coefficient for measurements -*/ -char BMP280::begin(int sdaPin, int sclPin) -{ - Wire.begin(sdaPin,sclPin); - return (readCalibration()); -} - -char BMP280::begin() -{ - - // Start up the Arduino's "wire" (I2C) library: - Wire.begin(); - return (readCalibration()); -} - -// The BMP280 includes factory calibration data stored on the device. -// Each device has different numbers, these must be retrieved and -// used in the calculations when taking measurements. - -// Retrieve calibration data from device: -char BMP280::readCalibration() { - - if ( - readUInt(0x88, dig_T1) && - readInt(0x8A, dig_T2) && - readInt(0x8C, dig_T3) && - readUInt(0x8E, dig_P1) && - readInt(0x90, dig_P2) && - readInt(0x92, dig_P3) && - readInt(0x94, dig_P4) && - readInt(0x96, dig_P5) && - readInt(0x98, dig_P6) && - readInt(0x9A, dig_P7) && - readInt(0x9C, dig_P8) && - readInt(0x9E, dig_P9)){ -#ifdef _debugSerial - Serial.print("dig_T1="); Serial.println(dig_T1,2); - Serial.print("dig_T2="); Serial.println(dig_T2,2); - Serial.print("dig_T3="); Serial.println(dig_T3,2); - Serial.print("dig_P1="); Serial.println(dig_P1,2); - Serial.print("dig_P2="); Serial.println(dig_P2,2); - Serial.print("dig_P3="); Serial.println(dig_P3,2); - Serial.print("dig_P4="); Serial.println(dig_P4,2); - Serial.print("dig_P5="); Serial.println(dig_P5,2); - Serial.print("dig_P6="); Serial.println(dig_P6,2); - Serial.print("dig_P7="); Serial.println(dig_P7,2); - Serial.print("dig_P8="); Serial.println(dig_P8,2); - Serial.print("dig_P9="); Serial.println(dig_P9,2); -#endif -#ifdef _debugTestData - dig_T1 = 27504.0; - dig_T2 = 26435.0; - dig_T3 = -1000.0; - dig_P1 = 36477.0; - dig_P2 = -10685.0; - dig_P3 = 3024.0; - dig_P4 = 2855.0; - dig_P5 = 140.0; - dig_P6 = -7.0; - dig_P7 = 15500.0; - dig_P8 = -14600.0; - dig_P9 = 6000.0; - Serial.print("dig_T1="); Serial.println(dig_T1,2); - Serial.print("dig_T2="); Serial.println(dig_T2,2); - Serial.print("dig_T3="); Serial.println(dig_T3,2); - Serial.print("dig_P1="); Serial.println(dig_P1,2); - Serial.print("dig_P2="); Serial.println(dig_P2,2); - Serial.print("dig_P3="); Serial.println(dig_P3,2); - Serial.print("dig_P4="); Serial.println(dig_P4,2); - Serial.print("dig_P5="); Serial.println(dig_P5,2); - Serial.print("dig_P6="); Serial.println(dig_P6,2); - Serial.print("dig_P7="); Serial.println(dig_P7,2); - Serial.print("dig_P8="); Serial.println(dig_P8,2); - Serial.print("dig_P9="); Serial.println(dig_P9,2); -#endif - return (1); - } - else - return (0); -} - -/* -** Read a signed integer (two bytes) from device -** @param : address = register to start reading (plus subsequent register) -** @param : value = external variable to store data (function modifies value) -*/ -char BMP280::readInt(char address, double &value) - -{ - unsigned char data[2]; //char is 4bit,1byte - - data[0] = address; - if (readBytes(data,2)) - { - value = (double)(int16_t)(((unsigned int)data[1]<<8)|(unsigned int)data[0]); // - return(1); - } - value = 0; - return(0); -} -/* -** Read an unsigned integer (two bytes) from device -** @param : address = register to start reading (plus subsequent register) -** @param : value = external variable to store data (function modifies value) -*/ - -char BMP280::readUInt(char address, double &value) -{ - unsigned char data[2]; //4bit - data[0] = address; - if (readBytes(data,2)) - { - value = (double)(unsigned int)(((unsigned int)data[1]<<8)|(unsigned int)data[0]); - return(1); - } - value = 0; - return(0); -} -/* -** Read an array of bytes from device -** @param : value = external array to hold data. Put starting register in values[0]. -** @param : length = number of bytes to read -*/ - -char BMP280::readBytes(unsigned char *values, char length) -{ - char x; - - Wire.beginTransmission(BMP280_ADDR); - Wire.write(values[0]); - error = Wire.endTransmission(); - if (error == 0) - { - Wire.requestFrom(BMP280_ADDR,length); - while(Wire.available() != length) ; // wait until bytes are ready - for(x=0;x100 || T <-100)return 0; - - return (1); -} -/* -** Pressure calculation from uncalibrated pressure value. -** @param : P = stores the pressure value. -** @param : uP = uncalibrated pressure value. -*/ -char BMP280::calcPressure(double &P,double uP) -{ - //char result; - double var1 , var2 ; - - var1 = (t_fine/2.0) - 64000.0; -#ifdef _debugSerial - Serial.print("var1 = ");Serial.println(var1,2); -#endif - var2 = var1 * (var1 * dig_P6/32768.0); //not overflow -#ifdef _debugSerial - Serial.print("var2 = ");Serial.println(var2,2); -#endif - var2 = var2 + (var1 * dig_P5 * 2.0); //overflow -#ifdef _debugSerial - Serial.print("var2 = ");Serial.println(var2,2); -#endif - - var2 = (var2/4.0)+((dig_P4)*65536.0); -#ifdef _debugSerial - Serial.print("var2 = ");Serial.println(var2,2); -#endif - - var1 = (dig_P3 * var1 * var1/524288.0 + dig_P2 * var1) / 524288.0; -#ifdef _debugSerial - Serial.print("var1 = ");Serial.println(var1,2); -#endif - var1 = (1.0 + var1/32768.0) * dig_P1; -#ifdef _debugSerial - Serial.print("var1 = ");Serial.println(var1,2); -#endif - - P = 1048576.0- uP; -#ifdef _debugSerial - Serial.print("p = ");Serial.println(p,2); -#endif - - P = (P-(var2/4096.0))*6250.0/var1 ; //overflow -#ifdef _debugSerial - Serial.print("p = ");Serial.println(p,2); -#endif - - var1 = dig_P9*P*P/2147483648.0; //overflow -#ifdef _debugSerial - Serial.print("var1 = ");Serial.println(var1,2); -#endif - - var2 = P*dig_P8/32768.0; -#ifdef _debugSerial - Serial.print("var2 = ");Serial.println(var2,2); -#endif - P = P + (var1+var2+dig_P7)/16.0; -#ifdef _debugSerial - Serial.print("p = ");Serial.println(p,2); -#endif - - P = P/100.0 ; - - if(P>1200.0 || P < 800.0)return (0); - return (1); -} - - - - -double BMP280::sealevel(double P, double A) -// Given a pressure P (mb) taken at a specific altitude (meters), -// return the equivalent pressure (mb) at sea level. -// This produces pressure readings that can be used for weather measurements. -{ - return(P/pow(1-(A/44330.0),5.255)); -} - - -double BMP280::altitude(double P, double P0) -// Given a pressure measurement P (mb) and the pressure at a baseline P0 (mb), -// return altitude (meters) above baseline. -{ - return(44330.0*(1-pow(P/P0,1/5.255))); -} - - -char BMP280::getError(void) - // If any library command fails, you can retrieve an extended - // error code using this command. Errors are from the wire library: - // 0 = Success - // 1 = Data too long to fit in transmit buffer - // 2 = Received NACK on transmit of address - // 3 = Received NACK on transmit of data - // 4 = Other error -{ - return(error); -} diff --git a/examples/Unit/BPS_Unit/Arduino/BPS_Unit/bmp280.h b/examples/Unit/BPS_Unit/Arduino/BPS_Unit/bmp280.h deleted file mode 100644 index 2e894e5f..00000000 --- a/examples/Unit/BPS_Unit/Arduino/BPS_Unit/bmp280.h +++ /dev/null @@ -1,132 +0,0 @@ -/* - BMP280.h - Bosch BMP280 pressure sensor library for the Arduino microcontroller. - This library uses I2C connection. - Uses floating-point equations from BMP280 datasheet. - modified by mhafuzul islam - version 1.01 16/9/2014 initial version - - Our example code uses the "pizza-eating" license. You can do anything - you like with this code. No really, anything. If you find it useful, - buy me italian pizza someday. -*/ - -#ifndef BMP280_h -#define BMP280_h - -#if defined(ARDUINO) && ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -// #define _debugSerial -// #define _debugTestData - -class BMP280 -{ - public: - BMP280(); // base type - - char begin(); - char begin(int sdaPin, int sclPin); - // call pressure.begin() to initialize BMP280 before use - // returns 1 if success, 0 if failure (i2C connection problem.) - - short getOversampling(void); - char setOversampling(short oss); - - char startMeasurment(void); - // command BMP280 to start a pressure measurement - // oversampling: 0 - 3 for oversampling value - // returns (number of ms to wait) for success, 0 for fail - - char calcTemperature(double &T, double &uT); - // calculation the true temperature from the given uncalibrated Temperature - - char calcPressure(double &P, double uP); - //calculation for measuring pressure. - - double sealevel(double P, double A); - // convert absolute pressure to sea-level pressure - // P: absolute pressure (mbar) - // A: current altitude (meters) - // returns sealevel pressure in mbar - - double altitude(double P, double P0); - // convert absolute pressure to altitude (given baseline pressure; sea-level, runway, etc.) - // P: absolute pressure (mbar) - // P0: fixed baseline pressure (mbar) - // returns signed altitude in meters - - char getError(void); - // If any library command fails, you can retrieve an extended - // error code using this command. Errors are from the wire library: - // 0 = Success - // 1 = Data too long to fit in transmit buffer - // 2 = Received NACK on transmit of address - // 3 = Received NACK on transmit of data - // 4 = Other error - - char getTemperatureAndPressure(double& T,double& P); - - private: - - char readCalibration(); - // Retrieve calibration data from device: - // The BMP280 includes factory calibration data stored on the device. - // Each device has different numbers, these must be retrieved and - // used in the calculations when taking measurements. - - char readInt(char address, double &value); - // read an signed int (16 bits) from a BMP280 register - // address: BMP280 register address - // value: external signed int for returned value (16 bits) - // returns 1 for success, 0 for fail, with result in value - - char readUInt(char address, double &value); - // read an unsigned int (16 bits) from a BMP280 register - // address: BMP280 register address - // value: external unsigned int for returned value (16 bits) - // returns 1 for success, 0 for fail, with result in value - - char readBytes(unsigned char *values, char length); - // read a number of bytes from a BMP280 register - // values: array of char with register address in first location [0] - // length: number of bytes to read back - // returns 1 for success, 0 for fail, with read bytes in values[] array - - char writeBytes(unsigned char *values, char length); - // write a number of bytes to a BMP280 register (and consecutive subsequent registers) - // values: array of char with register address in first location [0] - // length: number of bytes to write - // returns 1 for success, 0 for fail - - char getUnPT(double &uP, double &uT); - //get uncalibrated UP and UT value. - - - //int dig_T2 , dig_T3 , dig_T4 , dig_P2 , dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9; - //unsigned int dig_P1 , dig_T1 ; - double dig_T1, dig_T2 , dig_T3 , dig_T4 , dig_P1, dig_P2 , dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9; - short oversampling, oversampling_t; - double t_fine; - char error; -}; - -#define BMP280_ADDR 0x76 // 7-bit address - -#define BMP280_REG_CONTROL 0xF4 -#define BMP280_REG_RESULT_PRESSURE 0xF7 // 0xF7(msb) , 0xF8(lsb) , 0xF9(xlsb) : stores the pressure data. -#define BMP280_REG_RESULT_TEMPRERATURE 0xFA // 0xFA(msb) , 0xFB(lsb) , 0xFC(xlsb) : stores the temperature data. - -#define BMP280_COMMAND_TEMPERATURE 0x2E -#define BMP280_COMMAND_PRESSURE0 0x25 -#define BMP280_COMMAND_PRESSURE1 0x29 -#define BMP280_COMMAND_PRESSURE2 0x2D -#define BMP280_COMMAND_PRESSURE3 0x31 -#define BMP280_COMMAND_PRESSURE4 0x5D -#define BMP280_COMMAND_OVERSAMPLING_MAX 0xF5 - - -#endif diff --git a/examples/Unit/DAC_MCP4725/DAC_MCP4725.ino b/examples/Unit/DAC_MCP4725/DAC_MCP4725.ino index ddd25519..bec1aa9d 100644 --- a/examples/Unit/DAC_MCP4725/DAC_MCP4725.ino +++ b/examples/Unit/DAC_MCP4725/DAC_MCP4725.ino @@ -1,36 +1,44 @@ /* - Description: Use DAC Unit DAC to output 0 ~ 3.3V voltage with an accuracy of 12 bits. - Please install library before compiling: - Adafruit MCP4725: https://github.com/adafruit/Adafruit_MCP4725 +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe:DAC_MCP4725. 数模转换 +* date:2021/8/16 +******************************************************************************* + Please connect to Port A(22、21),Use DAC Unit to output 0 ~ 3.3V voltage with an accuracy of 12 bits. + 请连接端口A(22、21),使用DAC Unit 输出12位精度的0 ~ 3.3V电压。 */ -#include -#include #include -#define DAC_ADDR +#include + +#define DAC_ADDR 0x60 // For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC) + // For MCP4725A0 the address is 0x60 or 0x61 + // For MCP4725A2 the address is 0x64 or 0x65 + Adafruit_MCP4725 dac; void setup(void) { - M5.begin(true, false, false); - Serial.begin(115200); - Serial.println("Hello!"); - M5.Lcd.setTextFont(4); - M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK); - M5.Lcd.drawString("DAC MCP4725 demo.",25, 100, 4); - // For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC) - // For MCP4725A0 the address is 0x60 or 0x61 - // For MCP4725A2 the address is 0x64 or 0x65 - dac.begin(0x60); - - Serial.println("Generating a triangle wave"); - dac.setVoltage(2048, false); + M5.begin(true, false, false); //Init M5Core2. 初始化 M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.print(" DAC MCP4725 demo."); + dac.begin(0x60); //Setups the hardware address and checks the DAC was found. 设置硬件地址并检查是否找到DAC + dac.setVoltage(2048, false); } void loop(void) { - // 12bit value , false mean not write EEPROM - dac.setVoltage(1024, false); - delay(2000); - dac.setVoltage(2048, false); - delay(2000); -} + M5.Lcd.setCursor(100,60); + M5.Lcd.print("1.2V"); + dac.setVoltage(1024, false); //Set the voltage to 1.2V and retain the current voltage output after power off or reset. 设置电压为1.2v,关闭断电或复位后保留当前电压输出 + delay(1000); + M5.Lcd.fillRect(100,60,120,40,BLACK); //Draw a black rectangle at (100,60) with a width of 120 and a height of 40. 在(100,60)处绘制一个宽为120,高为40的黑色矩形 + M5.Lcd.print("2.4V"); + dac.setVoltage(2048, false); //2.4v + delay(1000); + M5.Lcd.fillRect(100,60,120,40,BLACK); +} \ No newline at end of file diff --git a/examples/Unit/ENVIII_SHT30_QMP6988/ENVIII_SHT30_QMP6988.ino b/examples/Unit/ENVIII_SHT30_QMP6988/ENVIII_SHT30_QMP6988.ino new file mode 100644 index 00000000..027f4ffe --- /dev/null +++ b/examples/Unit/ENVIII_SHT30_QMP6988/ENVIII_SHT30_QMP6988.ino @@ -0,0 +1,47 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe:ENVIII_SHT30_QMP6988. 环境传感器 +* date:2021/8/17 +******************************************************************************* + Please connect to Port A(22、21),Read temperature, humidity and atmospheric pressure and display them on the display screen + 请连接端口A(22、21),读取温度、湿度和大气压强并在显示屏上显示 +*/ +#include +#include "Adafruit_Sensor.h" +#include +#include "UNIT_ENV.h" + +SHT3X sht30; +QMP6988 qmp6988; + +float tmp = 0.0; +float hum = 0.0; +float pressure = 0.0; + +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + Wire.begin(); //Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 + qmp6988.init(); + M5.lcd.println(F("ENV Unit III test")); +} + +void loop() { + pressure = qmp6988.calcPressure(); + if(sht30.get()==0){ //Obtain the data of shT30. 获取sht30的数据 + tmp = sht30.cTemp; //Store the temperature obtained from shT30. 将sht30获取到的温度存储 + hum = sht30.humidity; //Store the humidity obtained from the SHT30. 将sht30获取到的湿度存储 + }else{ + tmp=0,hum=0; + } + M5.lcd.fillRect(0,20,100,60,BLACK); //Fill the screen with black (to clear the screen). 将屏幕填充黑色(用来清屏) + M5.lcd.setCursor(0,20); + M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", tmp, hum, pressure); + delay(2000); +} diff --git a/examples/Unit/ENV_DHT12_BMP280/ENV_DHT12_BMP280.ino b/examples/Unit/ENV_DHT12_BMP280/ENV_DHT12_BMP280.ino new file mode 100644 index 00000000..32c400d7 --- /dev/null +++ b/examples/Unit/ENV_DHT12_BMP280/ENV_DHT12_BMP280.ino @@ -0,0 +1,43 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe:ENVII_DH12_BMP280. 环境传感器 +* date:2021/8/17 +******************************************************************************* + Please connect to Port A(22、21),Read temperature, humidity and atmospheric pressure and display them on the display screen + 请连接端口A(22、21),读取温度、湿度和大气压强并在显示屏上显示 +*/ + +#include +#include "Adafruit_Sensor.h" +#include +#include "UNIT_ENV.h" + +DHT12 dht12; +Adafruit_BMP280 bme; + +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + Wire.begin(); //Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 + M5.Lcd.println(F("ENV Unit(DHT12 and BMP280) test")); +} + +void loop() { + while (!bme.begin(0x76)){ + M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); + } + float tmp = dht12.readTemperature(); //Store the temperature obtained from dht12. 将dht12获取到的温度存储 + float hum = dht12.readHumidity(); //Store the humidity obtained from the dht12. 将dht12获取到的湿度存储 + float pressure = bme.readPressure(); //Stores the pressure gained by BMP. 存储bmp获取到的压强 + M5.lcd.fillRect(0,20,100,60,BLACK); //Fill the screen with black (to clear the screen). 将屏幕填充黑色(用来清屏) + M5.lcd.setCursor(0,20); + M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", tmp, hum, pressure); + + delay(2000); +} diff --git a/examples/Unit/FADER/FADER.ino b/examples/Unit/FADER/FADER.ino new file mode 100644 index 00000000..df0c924f --- /dev/null +++ b/examples/Unit/FADER/FADER.ino @@ -0,0 +1,55 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/cn/unit/fader +* 获取更多资料请访问:https://docs.m5stack.com/en/unit/fader +* +* describe:UNIT FADER. 滑动电位器/推子 +* date:2021/8/20 +******************************************************************************* + Connect UNIT FADER to port B and push the FADER slider to adjust the input value and light brightness + 将UNIT FADER连接到B端口, 推动FADER滑杆即可实现调整输入数值大小与灯光亮度 +*/ + +#include "M5Core2.h" +#include "FastLED.h" + +// How many leds in your strip? +#define NUM_LEDS 14 +#define INPUT_PINS 36 + +#define DATA_PIN 26 + +// Define the array of leds +CRGB leds[NUM_LEDS]; + +uint8_t beginHue = 0; +uint8_t deltaHue = 30; +uint8_t brightness = 100; +uint16_t rawADC = 0; + +void setup() +{ + M5.begin(); + M5.Lcd.setTextDatum(MC_DATUM); + M5.Lcd.drawString("FADER UNIT TEST", 160, 60, 4); + FastLED.addLeds(leds, NUM_LEDS); + delay(1000); + pinMode(36, INPUT); + fill_rainbow(leds, NUM_LEDS, beginHue, deltaHue); +} + + +void loop() +{ + rawADC = analogRead(INPUT_PINS); //Read ADC value 读取ADC数值 + brightness = map(rawADC, 0, 4095, 0, 255); //The mapping ADC value is the brightness value range 映射ADC值为亮度值范围 + FastLED.setBrightness(brightness); //Adjust the brightness of the FADER LED 调整FADER LED灯亮度 + FastLED.show(); + Serial.printf("%d\r\n", rawADC); + M5.Lcd.fillRect(0, 120, 320, 100, BLACK); + M5.Lcd.drawString("value: "+String(rawADC), 160, 160, 4); + delay(100); +} \ No newline at end of file diff --git a/examples/Unit/FAN/FAN.ino b/examples/Unit/FAN/FAN.ino new file mode 100644 index 00000000..e12a0b8d --- /dev/null +++ b/examples/Unit/FAN/FAN.ino @@ -0,0 +1,36 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe:Fan. 风扇 +* date:2021/8/16 +******************************************************************************* + Please connect to Port B(26), Adjust the speed of FAN Unit through PWM. + 请连接端口B(26),通过PWM调节风扇单元的转速。 +*/ +#include + +#define motor_pin 26 + +int freq = 10000; +int ledChannel = 0; +int resolution = 10; +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.setCursor(80, 10); //Set the cursor at (80,10). 将光标设置在(80,10)处 + M5.Lcd.println("Fan"); + ledcSetup(ledChannel, freq, resolution); //Sets the frequency and number of counts corresponding to the channel. 设置通道对应的频率和计数位数 + ledcAttachPin(motor_pin, ledChannel); //Binds the specified channel to the specified I/O port for output. 将指定通道绑定到指定 IO 口上以实现输出 +} + +void loop() { + ledcWrite(ledChannel, 1024); //Output PWM. 输出PWM + delay(1000); + ledcWrite(ledChannel, 0); + delay(1000); +} diff --git a/examples/Unit/HALL/HALL.ino b/examples/Unit/HALL/HALL.ino new file mode 100644 index 00000000..59effe58 --- /dev/null +++ b/examples/Unit/HALL/HALL.ino @@ -0,0 +1,35 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe: Hall. 霍尔传感器 +* date:2021/8/18 +******************************************************************************* + Please connect to Port B,Displays a string on the screen. + 请连接端口B,在屏幕上显示字符串。 + Low-level signal can be generated when the magnet S pole is close to the front of the sensor + 当磁体S极靠近传感器前端时,会产生低电平信号 + OR the N pole is close to the back, and the internal LED indicator will light up, the screen wiil display 0. + 或N极靠近背面,内部LED指示灯亮起,屏幕显示0。 +*/ + +#include + +#define HALL 36 + +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.print(" HALL Sensor"); + pinMode(HALL, INPUT); //Set the pins to which the Hall sensor is connected to the input mode. 将霍尔传感器所连接的引脚设置为输入模式 +} + +void loop() { + bool status = digitalRead(HALL); + M5.Lcd.setCursor(20, 80); + M5.Lcd.printf("Hall status : %d", status); +} diff --git a/examples/Unit/HEART_MAX30100/MAX30100_RawData/MAX30100_RawData.ino b/examples/Unit/HEART_MAX30100/MAX30100_RawData/MAX30100_RawData.ino index 3232f6ca..1851913f 100644 --- a/examples/Unit/HEART_MAX30100/MAX30100_RawData/MAX30100_RawData.ino +++ b/examples/Unit/HEART_MAX30100/MAX30100_RawData/MAX30100_RawData.ino @@ -1,71 +1,58 @@ /* - Description: This example shows how the HEART Unit obtains the original value of the heart rate detection and displays it on the screen. The user can also use the Arduino IDE Serial Plotter to view the line graph output. - Please install library before compiling: - Arduino-MAX30100: https://github.com/oxullo/Arduino-MAX30100 +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe:Heart Rate. 心率 +* date:2021/8/16 +******************************************************************************* + Please connect to Port A, HEART Unit obtains the original value of the heart rate detection and displays it on the screen. The user can also use the Arduino IDE Serial Plotter to view the line graph output. + 请连接端口A,HEART Unit获取心率检测的原始值并显示在屏幕上。用户还可以使用Arduino IDE Serial Plotter查看线图输出。 */ -#include -#include "MAX30100.h" + #include -// Sampling is tightly related to the dynamic range of the ADC. -// refer to the datasheet for further info -#define SAMPLING_RATE MAX30100_SAMPRATE_100HZ +#include "MAX30100.h" -// The LEDs currents must be set to a level that avoids clipping and maximises the -// dynamic range +#define SAMPLING_RATE MAX30100_SAMPRATE_100HZ #define IR_LED_CURRENT MAX30100_LED_CURR_24MA #define RED_LED_CURRENT MAX30100_LED_CURR_27_1MA - -// The pulse width of the LEDs driving determines the resolution of -// the ADC (which is a Sigma-Delta). -// set HIGHRES_MODE to true only when setting PULSE_WIDTH to MAX30100_SPC_PW_1600US_16BITS #define PULSE_WIDTH MAX30100_SPC_PW_1600US_16BITS #define HIGHRES_MODE true - -// Instantiate a MAX30100 sensor class -MAX30100 sensor; +MAX30100 sensor; // Instantiate a MAX30100 sensor class. 实例化一个MAX30100传感器类 void setup() -{ M5.begin(); - Serial.print("Initializing MAX30100.."); - - // Initialize the sensor - // Failures are generally due to an improper I2C wiring, missing power supply - // or wrong target chip - while (!sensor.begin()) { - Serial.println("Sensor not found"); - M5.Lcd.setTextFont(4); - M5.Lcd.setCursor(50, 100, 4); - M5.Lcd.println("Sensor not found"); - delay(1000); - } - - Serial.println("SUCCESS"); - - - // Set up the wanted parameters - sensor.setMode(MAX30100_MODE_SPO2_HR); - sensor.setLedsCurrent(IR_LED_CURRENT, RED_LED_CURRENT); - sensor.setLedsPulseWidth(PULSE_WIDTH); - sensor.setSamplingRate(SAMPLING_RATE); - sensor.setHighresModeEnabled(HIGHRES_MODE); +{ + M5.begin(); //Init M5Core2. 初始化M5Core2 + Serial.print("Initializing MAX30100.."); + + while (!sensor.begin()) { // Initialize the sensor. 初始化传感器 + M5.Lcd.setTextFont(4); + M5.Lcd.setCursor(50, 100, 4); + M5.Lcd.println("Sensor not found"); + delay(1000); + } + M5.Lcd.fillScreen(BLACK); + // Set up the wanted parameters. 设置所需的参数 + sensor.setMode(MAX30100_MODE_SPO2_HR); + sensor.setLedsCurrent(IR_LED_CURRENT, RED_LED_CURRENT); + sensor.setLedsPulseWidth(PULSE_WIDTH); + sensor.setSamplingRate(SAMPLING_RATE); + sensor.setHighresModeEnabled(HIGHRES_MODE); } void loop() { - uint16_t ir, red; - - sensor.update(); - - while (sensor.getRawValues(&ir, &red)) { - M5.Lcd.setTextFont(4); - M5.Lcd.setCursor(100, 100, 4); - M5.Lcd.printf("IR:%d ",ir); - M5.Lcd.setCursor(100, 130, 4); - M5.Lcd.printf("RED:%d ",red); - - Serial.print(ir); - Serial.print('\t'); - Serial.println(red); - } + uint16_t ir, red; + sensor.update(); //更新传感器读取到的数据 + while (sensor.getRawValues(&ir, &red)) { //如果获取到数据 + M5.Lcd.setTextFont(4); + M5.Lcd.setCursor(100, 100, 4); + M5.Lcd.printf("IR:%d ",ir); + M5.Lcd.setCursor(100, 130, 4); + M5.Lcd.printf("RED:%d ",red); + } } diff --git a/examples/Unit/LASER/LASER.ino b/examples/Unit/LASER/LASER.ino new file mode 100644 index 00000000..89c01a6d --- /dev/null +++ b/examples/Unit/LASER/LASER.ino @@ -0,0 +1,86 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core sample source code +* 配套 M5Core 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/gray +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray +* +* describe: Hall. 霍尔传感器 +* date:2021/8/18 +******************************************************************************* + Please connect to Port B,LASER Unit wireless UART application: burn the program to two M5Cores + And connect LASER.TX and LASER.RX + Point LASER.TX to LASER.RX and press the button on the panel to send characters to the receiver of LASER.RX. + 请连接端口B,在屏幕上显示字符串。LASER Unit无线UART应用:刻录程序到两个M5Core并分别连接 LASER RX 和 LASER TX + 然后按下LASER TX面板上的按钮,将字符发送到LASER.RX接收器。 +*/ + +#include + +char ch; +#define RX +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + + // Serial2.begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert) + Serial2.begin(9600, SERIAL_8N1, 16, 17); + pinMode(5, OUTPUT); + digitalWrite(5, 1); + + M5.Lcd.setTextSize(4); + M5.Lcd.setTextColor(GREEN); + M5.Lcd.setCursor(60, 50); +#ifdef RX + M5.Lcd.print("LASER RX"); +#elif defined TX + M5.Lcd.print("LASER TX"); +#else + M5.Lcd.setCursor(30, 50); + M5.Lcd.print("LASER TX/RX"); + M5.Lcd.setCursor(50, 200); + M5.Lcd.print('A'); + M5.Lcd.setCursor(150, 200); + M5.Lcd.print('B'); + M5.Lcd.setCursor(240, 200); + M5.Lcd.print('C'); +#endif + M5.Lcd.setCursor(0, 100); +} + +void loop() { + +#ifdef RX +M5.update(); + if(Serial2.available()) { + char ch = Serial2.read(); + M5.Lcd.print(ch); + } + + if (M5.BtnA.wasReleased()) { + M5.Lcd.clear(); + M5.Lcd.setCursor(0, 0); + } +#elif defined TX + Serial2.write('A'); + delay(50); +#else + if (M5.BtnA.wasReleased()) { + ch = 'A'; + Serial2.write(ch); + } else if (M5.BtnB.wasReleased()) { + ch = 'B'; + Serial2.write(ch); + } else if (M5.BtnC.wasReleased()) { + ch = 'C'; + Serial2.write(ch); + } + M5.update(); + if(Serial2.available()) { + char ch = Serial2.read(); + M5.Lcd.print(ch); + } +#endif + + +} diff --git a/examples/Unit/LCD_ST7789V2/LCD_ST7789V2.ino b/examples/Unit/LCD_ST7789V2/LCD_ST7789V2.ino new file mode 100644 index 00000000..ffc381d5 --- /dev/null +++ b/examples/Unit/LCD_ST7789V2/LCD_ST7789V2.ino @@ -0,0 +1,56 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core sample source code +* 配套 M5Core 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/gray +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray +* +* describe: LCD. 显示屏 +* date:2021/8/18 +******************************************************************************* + Please connect to Port A,Displays a string on the screen. + 请连接端口A,在屏幕上显示字符串。 +*/ +#include +#include + +M5UnitLCD display; + +M5Canvas canvas(&display); + +static constexpr char text[] = "Hello world ! こんにちは世界! this is long long string sample. 寿限無、寿限無、五劫の擦り切れ、海砂利水魚の、水行末・雲来末・風来末、喰う寝る処に住む処、藪ら柑子の藪柑子、パイポ・パイポ・パイポのシューリンガン、シューリンガンのグーリンダイ、グーリンダイのポンポコピーのポンポコナの、長久命の長助"; +static constexpr size_t textlen = sizeof(text) / sizeof(text[0]); +int textpos = 0; +int scrollstep = 2; + +void setup(void) +{ + M5.begin(); + display.init(); //Initialize the display. 初始化显示屏 + display.setRotation(3); //Rotating display. 旋转显示屏 + canvas.setColorDepth(1); // Set the color depth. 设置色深 + canvas.setFont(&fonts::lgfxJapanMinchoP_32); //Set the font. 设置字体 + canvas.setTextSize(2); //Set the font size. 设置字号 + canvas.createSprite(display.width() + 64, 72); //Create a canvas with a wide display width of +64 and a height of 72. 创建一块宽显示屏宽度+64,高72的画布 +} + +void loop(void) +{ + int32_t cursor_x = canvas.getCursorX() - scrollstep; + if (cursor_x <= 0) + { + textpos = 0; + cursor_x = display.width(); + } + + canvas.setCursor(cursor_x, 0); //Set the cursor position. 设置光标的位置 + canvas.scroll(-scrollstep, 0); //Set the rolling. 设置滚动 + while (textpos < textlen && cursor_x <= display.width()) + { + canvas.print(text[textpos++]); + cursor_x = canvas.getCursorX(); + } + display.waitDisplay(); + canvas.pushSprite(&display, 0, (display.height() - canvas.height()) >> 1); //Displays the contents of the canvas. 显示画布上的内容 +} \ No newline at end of file diff --git a/examples/Unit/LIGHT/LIGHT.ino b/examples/Unit/LIGHT/LIGHT.ino new file mode 100644 index 00000000..649474a5 --- /dev/null +++ b/examples/Unit/LIGHT/LIGHT.ino @@ -0,0 +1,36 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe: Light. 环境光传感器 +* date:2021/8/18 +******************************************************************************* + Please connect to Port B,Use the Light Unit screen to display the current ambient lighting value + 请连接端口 B ,使用Light Unit 屏幕显示当前环境光照值。 +*/ + +#include + +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.printf("UNIT_LIGHT EXAMPLE\n\n"); + M5.Lcd.println("Analog:"); + M5.Lcd.println("Digital:"); + pinMode(26, INPUT); //Set pin 26 as input mode. 设置引脚26为输入模式 +} + +void loop() { + static uint16_t digitalRead_value = 0, analogRead_value = 0; + analogRead_value = analogRead(36); //Store the analog quantity read from pin 36. 将36号引脚读取到的模拟量存储 + digitalRead_value = digitalRead(26); //Store the number read from pin 26. 将26号引脚读取到的数字量存储 + M5.Lcd.setCursor(90, 30); + M5.Lcd.printf("%d\n", analogRead_value); + M5.Lcd.setCursor(90, 50); + M5.Lcd.printf("%d\n", digitalRead_value); + delay(10); +} diff --git a/examples/Unit/OP90.180_ITR9606/OP90.180_ITR9606.ino b/examples/Unit/OP90.180_ITR9606/OP90.180_ITR9606.ino new file mode 100644 index 00000000..3b2c4d11 --- /dev/null +++ b/examples/Unit/OP90.180_ITR9606/OP90.180_ITR9606.ino @@ -0,0 +1,29 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe:OP 180/90. 非接触式光电限位开关 +* date:2021/8/16 +******************************************************************************* + Please connect to Port B(36),Detect the current OP 90/180 Unit Photoelectric switch status. + 请连接端口B(36),检测当前OP 90/180单元光电开关状态。 +*/ + +#include + +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.setCursor(80, 10); //Set the cursor at (80,10). 将光标设置在(80,10)处 + M5.Lcd.println("90/180 OPTICAL"); + pinMode(36,INPUT_PULLUP); //Set pin 36 to input pull-up mode. 设置36号引脚为输入上拉模式 +} + +void loop() { + M5.Lcd.setCursor(80, 120); + M5.Lcd.printf("IR Receive: %d",digitalRead(36)); //Output the value of pin 36. 输出36号引脚的值 +} diff --git a/examples/Unit/RELAY/RELAY.ino b/examples/Unit/RELAY/RELAY.ino new file mode 100644 index 00000000..f2e0b5ca --- /dev/null +++ b/examples/Unit/RELAY/RELAY.ino @@ -0,0 +1,37 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe:Relay. 继电器 +* date:2021/8/16 +******************************************************************************* + Please connect to Port B(26),Use RELAY to switch on and off the circuit. + 请连接端口B(26),使用继电器开关电路。 +*/ + +#include + +void setup() { + M5.begin(); //Init M5Core2. 初始化 M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.setCursor(50, 0); + M5.Lcd.println(("Relay Example")); + dacWrite(25, 0); //disable the speak noise + pinMode(26, OUTPUT); +} + +void loop(void) { + M5.Lcd.setCursor(100, 40); + M5.Lcd.print("ON"); + digitalWrite(26, HIGH); + delay(1000); + M5.Lcd.fillRect(100,40,60,50,BLACK); + M5.Lcd.print("OFF"); + digitalWrite(26, LOW); + delay(1000); + M5.Lcd.fillRect(100,40,60,50,BLACK); +} diff --git a/examples/Unit/RFID_RC522/RFID_RC522.ino b/examples/Unit/RFID_RC522/RFID_RC522.ino index a961cb87..4e79b35c 100644 --- a/examples/Unit/RFID_RC522/RFID_RC522.ino +++ b/examples/Unit/RFID_RC522/RFID_RC522.ino @@ -1,70 +1,43 @@ /* - Description: Use the RFID Unit to read the Fudan card ID and display the ID on the screen. +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe: RFID. +* date:2021/8/19 +******************************************************************************* + Please connect to Port A(22、21),Use the RFID Unit to read the Fudan card ID and display the ID on the screen. + 请连接端口A(22、21),使用RFID Unit 读取ID卡并在屏幕上显示。 */ -#include -#include "MFRC522_I2C.h" -#include +#include +#include "MFRC522_I2C.h" -// 0x28 is i2c address on SDA. Check your address with i2cscanner if not match. -MFRC522 mfrc522(0x28); // Create MFRC522 instance. +MFRC522 mfrc522(0x28); // Create MFRC522 instance. 创建MFRC522实例 void setup() { - M5.begin(); - - M5.Lcd.fillScreen( BLACK ); - M5.Lcd.setCursor(0, 0); - M5.Lcd.setTextColor(YELLOW); - M5.Lcd.setTextSize(2); + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.println("MFRC522 Test"); + Wire.begin(); //Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 - M5.Lcd.fillScreen( BLACK ); - M5.Lcd.setCursor(0, 0); - M5.Lcd.println("M5StackFire MFRC522"); - - Wire.begin(); // Initialize I2C - - mfrc522.PCD_Init(); // Init MFRC522 - ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details - Serial.println(F("Scan PICC to see UID, type, and data blocks...")); - M5.Lcd.println("Scan PICC to see UID, type, and data blocks..."); + mfrc522.PCD_Init(); // Init MFRC522. 初始化 MFRC522 + M5.Lcd.println("Please put the card\n\nUID:"); } void loop() { - // Look for new cards, and select one if present - if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) { - delay(50); + M5.Lcd.setCursor(40,47); + if (!mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial()) { //如果没有读取到新的卡片 + delay(200); return; } - - // Now a card is selected. The UID and SAK is in mfrc522.uid. - - // Dump UID - Serial.print(F("Card UID:")); - M5.Lcd.println(" "); + M5.Lcd.fillRect(42,47,320,20,BLACK); for (byte i = 0; i < mfrc522.uid.size; i++) { - Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); - Serial.print(mfrc522.uid.uidByte[i], HEX); M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); M5.Lcd.print(mfrc522.uid.uidByte[i], HEX); - } - Serial.println(); - -} - -void ShowReaderDetails() { - // Get the MFRC522 software version - byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); - Serial.print(F("MFRC522 Software Version: 0x")); - Serial.print(v, HEX); - if (v == 0x91) - Serial.print(F(" = v1.0")); - else if (v == 0x92) - Serial.print(F(" = v2.0")); - else - Serial.print(F(" (unknown)")); - Serial.println(""); - // When 0x00 or 0xFF is returned, communication probably failed - if ((v == 0x00) || (v == 0xFF)) { - Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); } -} + M5.Lcd.println(""); +} \ No newline at end of file diff --git a/examples/Unit/RTC_BM8563/RTC_BM8563.ino b/examples/Unit/RTC_BM8563/RTC_BM8563.ino new file mode 100644 index 00000000..1ffbe681 --- /dev/null +++ b/examples/Unit/RTC_BM8563/RTC_BM8563.ino @@ -0,0 +1,54 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe: RTC. 实时时钟 +* date:2021/8/18 +******************************************************************************* + Please connect to Port A,The time is displayed on the screen. + 请连接端口A,屏幕上显示时间。 +*/ + +#include "M5Core2.h" +#include "M5_BM8563.h" + +BM8563 RTC; + +rtc_time_type RTCtime; +rtc_date_type RTCdate; + +char str_buffer[64]; + +void setup() +{ + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.print(" RTC"); + RTC.begin(); //Example Initialize the RTC clock. 初始化RTC时钟 + RTCtime.Hours = 14; //Set the RTC clock time. 设置RTC时钟时间 + RTCtime.Minutes = 40; + RTCtime.Seconds = 5; + + RTCdate.WeekDay = 4; //Set the RTC clock date. 设置RTC时钟日期 + RTCdate.Month = 7; + RTCdate.Date = 15; + RTCdate.Year = 2021; + + RTC.setTime(&RTCtime); //Example Synchronize the set time to the RTC. 将设置的时间同步至RTC + RTC.setDate(&RTCdate); //Synchronize the set date to the RTC. 将设置的日期同步至RTC +} + +void loop() +{ + RTC.getTime(&RTCtime); //To get the time. 获取时间 + RTC.getDate(&RTCdate); //Get the date. 获取日期 + M5.Lcd.setCursor(0,20); + M5.Lcd.printf("RTC Time Now is \n%02d:%02d:%02d\n",RTCtime.Hours, RTCtime.Minutes, RTCtime.Seconds); + M5.Lcd.printf("RTC Date Now is \n%02d:%02d:%02d WeekDay:%02d\n",RTCdate.Year, RTCdate.Month, RTCdate.Date, RTCdate.WeekDay); + delay(1000); + M5.Lcd.fillRect(0,20,320,140,BLACK); +} diff --git a/examples/Unit/TOF/TOF_VL53L0X/TOF_VL53L0X.ino b/examples/Unit/TOF/TOF_VL53L0X/TOF_VL53L0X.ino index 8b47b90b..5df53afb 100644 --- a/examples/Unit/TOF/TOF_VL53L0X/TOF_VL53L0X.ino +++ b/examples/Unit/TOF/TOF_VL53L0X/TOF_VL53L0X.ino @@ -1,8 +1,19 @@ -//the original code by Ted Meyers -//posted here: https://groups.google.com/d/msg/diyrovers/lc7NUZYuJOg/ICPrYNJGBgAJ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe:ToF. 激光测距 +* date:2021/8/16 +******************************************************************************* + Please connect to Port A(22、21),Use ToF Unit to detect distance and display distance data on the screen in real time. + 请连接端口A(22、21),使用ToF Unit检测距离,并在屏幕上实时显示距离数据。 +*/ #include -#include #define VL53L0X_REG_IDENTIFICATION_MODEL_ID 0xc0 #define VL53L0X_REG_IDENTIFICATION_REVISION_ID 0xc2 @@ -11,76 +22,10 @@ #define VL53L0X_REG_SYSRANGE_START 0x00 #define VL53L0X_REG_RESULT_INTERRUPT_STATUS 0x13 #define VL53L0X_REG_RESULT_RANGE_STATUS 0x14 -#define address 0x29 +#define address 0x29 //I2C address byte gbuf[16]; -void setup() { - // put your setup code here, to run once: - //---osmar - M5.begin(); - M5.Lcd.fillScreen(BLACK); - M5.Lcd.setCursor(10, 10); - M5.Lcd.setTextColor(WHITE); - M5.Lcd.setTextSize(10); - - Wire.begin(); // join i2c bus (address optional for master) - Serial.println("VLX53LOX test started."); - //---osmar -} - -void loop() { - Serial.println("----- START TEST ----"); - test(); - Serial.println("----- END TEST ----"); - Serial.println(""); - delay(1000); -} - -void test() { -// byte val1 = read_byte_data_at(VL53L0X_REG_IDENTIFICATION_REVISION_ID); -// Serial.print("Revision ID: "); Serial.println(val1); -// -// val1 = read_byte_data_at(VL53L0X_REG_IDENTIFICATION_MODEL_ID); -// Serial.print("Device ID: "); Serial.println(val1); -// -// val1 = read_byte_data_at(VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD); -// Serial.print("PRE_RANGE_CONFIG_VCSEL_PERIOD="); Serial.println(val1); -// Serial.print(" decode: "); Serial.println(VL53L0X_decode_vcsel_period(val1)); -// -// val1 = read_byte_data_at(VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD); -// Serial.print("FINAL_RANGE_CONFIG_VCSEL_PERIOD="); Serial.println(val1); -// Serial.print(" decode: "); Serial.println(VL53L0X_decode_vcsel_period(val1)); - - write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01); - -// byte val = 0; -// int cnt = 0; -// while (cnt < 100) { // 1 second waiting time max -// delay(10); -// val = read_byte_data_at(VL53L0X_REG_RESULT_RANGE_STATUS); -// if (val & 0x01) break; -// cnt++; -// } -// if (val & 0x01) Serial.println("ready"); else Serial.println("not ready"); - - read_block_data_at(0x14, 12); -// uint16_t acnt = makeuint16(gbuf[7], gbuf[6]); -// uint16_t scnt = makeuint16(gbuf[9], gbuf[8]); - uint16_t dist = makeuint16(gbuf[11], gbuf[10]); - byte DeviceRangeStatusInternal = ((gbuf[0] & 0x78) >> 3); - -// Serial.print("ambient count: "); Serial.println(acnt); -// Serial.print("signal count: "); Serial.println(scnt); - Serial.print("distance "); Serial.println(dist); -// Serial.print("status: "); Serial.println(DeviceRangeStatusInternal); - - M5.Lcd.setCursor(0, 0); - M5.Lcd.fillScreen(BLACK); - M5.Lcd.print( dist); - -} - uint16_t bswap(byte b[]) { // Big Endian unsigned short to little endian unsigned short uint16_t val = ((b[0] << 8) & b[1]); @@ -139,7 +84,7 @@ uint16_t read_word_data_at(byte reg) { while (Wire.available() < 2) delay(1); gbuf[0] = Wire.read(); gbuf[1] = Wire.read(); - return bswap(gbuf); + return bswap(gbuf); } void read_block_data_at(byte reg, int sz) { @@ -159,3 +104,43 @@ uint16_t VL53L0X_decode_vcsel_period(short vcsel_period_reg) { uint16_t vcsel_period_pclks = (vcsel_period_reg + 1) << 1; return vcsel_period_pclks; } + +void setup() { + // put your setup code here, to run once: + Wire.begin(); // join i2c bus (address optional for master) + Serial.begin(115200); // start serial for output + Serial.println("VLX53LOX test started."); + + //---osmar + M5.begin(); + M5.Lcd.setCursor(50, 0, 4); + M5.Lcd.println(("VLX53LOX Example")); + //---osmar +} + +void loop() { + write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01); + + byte val = 0; + int cnt = 0; + while (cnt < 100) { // 1 second waiting time max + delay(10); + val = read_byte_data_at(VL53L0X_REG_RESULT_RANGE_STATUS); + if (val & 0x01) break; + cnt++; + } + if (val & 0x01) Serial.println("ready"); else Serial.println("not ready"); + + read_block_data_at(0x14, 12); + uint16_t acnt = makeuint16(gbuf[7], gbuf[6]); + uint16_t scnt = makeuint16(gbuf[9], gbuf[8]); + uint16_t dist = makeuint16(gbuf[11], gbuf[10]); + byte DeviceRangeStatusInternal = ((gbuf[0] & 0x78) >> 3); + M5.Lcd.fillRect(0, 35, 319, 239, BLACK); + M5.Lcd.setCursor(0, 35, 4); + M5.Lcd.print("ambient count: "); M5.Lcd.println(acnt); + M5.Lcd.print("signal count: "); M5.Lcd.println(scnt); + M5.Lcd.print("distance: "); M5.Lcd.println(dist); + M5.Lcd.print("status: "); M5.Lcd.println(DeviceRangeStatusInternal); + delay(1000); +} \ No newline at end of file diff --git a/examples/Unit/ULTRA_SONIC/ULTRA_SONIC.ino b/examples/Unit/ULTRA_SONIC/ULTRA_SONIC.ino new file mode 100644 index 00000000..8a038fb8 --- /dev/null +++ b/examples/Unit/ULTRA_SONIC/ULTRA_SONIC.ino @@ -0,0 +1,47 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe: Ultrasonic. 超声波测距传感器 +* date:2021/8/19 +******************************************************************************* + Please connect to Port A,Display the distance measured by ultrasonic + 请连接端口A,显示超声波测量的距离 +*/ + +#include + +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + Wire.begin(32, 33); + M5.Lcd.setCursor(105, 0, 4); //Set the cursor at (105,0) and set the font to a 4 point font. 将光标设置在(105,0)处,且设置字体为4号字体 + M5.Lcd.print("Ultrasonic\nDistance:"); +} + +float readEUS() +{ + uint32_t data; + Wire.beginTransmission(0x57); //Transfer data to 0x57. 将数据传输到0x57 + Wire.write(0x01); + Wire.endTransmission(); //Stop data transmission with the Ultrasonic Unit. 停止与Ultrasonic Unit的数据传输 + delay(120); + Wire.requestFrom(0x57,3); //Request 3 bytes from Ultrasonic Unit. 向Ultrasonic Unit请求3个字节。 + data = Wire.read();data <<= 8; + data |= Wire.read();data <<= 8; + data |= Wire.read(); + return float(data) / 1000; +} + +void loop() { + static float newvalue = 0; + newvalue = readEUS(); + if(( newvalue < 1500 )&&( newvalue > 20 )){ + M5.Lcd.setCursor(105,27); + M5.Lcd.printf("%.2fmm",newvalue); + } + delay(100); +} diff --git a/examples/Unit/VIBRATOR/VIBRATOR.ino b/examples/Unit/VIBRATOR/VIBRATOR.ino new file mode 100644 index 00000000..6622fb3a --- /dev/null +++ b/examples/Unit/VIBRATOR/VIBRATOR.ino @@ -0,0 +1,36 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe: Vibrator. 震动电机 +* date:2021/8/19 +******************************************************************************* + Please connect to Port B,Adjust the speed of VIBRATOR Unit through PWM. + 请连接端口B,通过PWM调节Vibrator Unit的速度。 +*/ + +#include + +#define motor_pin 26 +int freq = 10000; +int ledChannel = 0; +int resolution = 10; +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + M5.Lcd.setCursor(110, 10); //Set the cursor at (110,10). 将光标设置在(110,10)处 + M5.Lcd.println("Vibrator"); + ledcSetup(ledChannel, freq, resolution); //Sets the frequency and number of counts corresponding to the channel. 设置通道对应的频率和计数位数 + ledcAttachPin(motor_pin, ledChannel); //Binds the specified channel to the specified I/O port for output. 将指定通道绑定到指定 IO 口上以实现输出 +} + +void loop() { + ledcWrite(ledChannel, 512); //Output PWM. 输出PWM + delay(1000); + ledcWrite(ledChannel, 0); + delay(1000); +} diff --git a/examples/Unit/WEIGHT_HX711/WEIGHT_HX711.ino b/examples/Unit/WEIGHT_HX711/WEIGHT_HX711.ino new file mode 100644 index 00000000..7fa9de07 --- /dev/null +++ b/examples/Unit/WEIGHT_HX711/WEIGHT_HX711.ino @@ -0,0 +1,53 @@ +/* +******************************************************************************* +* Copyright (c) 2021 by M5Stack +* Equipped with M5Core2 sample source code +* 配套 M5Core2 示例源代码 +* Visit the website for more information:https://docs.m5stack.com/en/core/core2 +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2 +* +* describe: Weight. +* date:2021/8/20 +******************************************************************************* + Please connect to Port B,Use WEIGHT Unit to read the analog value returned by the pressure sensor, + convert it into intuitive weight data and send it to M5Core,Press ButtonA to calibrate + 请连接端口B,使用WEIGHT Unit读取压力传感器返回的模拟值,将其转换为直观的重量数据,发送到M5Core,按下按钮A进行校准 +*/ + +#include +#include "HX711.h" + +// HX711 related pin Settings. HX711 相关引脚设置 +#define LOADCELL_DOUT_PIN 36 +#define LOADCELL_SCK_PIN 26 + +HX711 scale; +const long LOADCELL_OFFSET = 50682624; +const long LOADCELL_DIVIDER = 5895655; + +void setup() { + M5.begin(); //Init M5Core2. 初始化M5Core2 + M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2 + + scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); // Initialize library with data output pin, clock input pin and gain factor. 初始化库的数据输出引脚,时钟输入引脚和增益因子。 + scale.set_scale(LOADCELL_DIVIDER); //set the SCALE value this value is used to convert the raw data to measure units. 设置SCALE值,该值用于将原始数据转换为度量单位 + scale.set_offset(LOADCELL_OFFSET); //Set the tare weight. 设置皮重 + scale.set_scale(61.2f); // this value is obtained by calibrating the scale with known weights; see the README for details. 这个值是通过校正已知权重的刻度而得到的 + scale.tare(); // reset the scale to 0. 将比例重置为0 + M5.Lcd.print(" Weight Unit\nConnect the Weight Unit to PortB"); + M5.Lcd.setCursor(20,210,1); + M5.Lcd.print("Calibration"); +} + +void loop() { + M5.update(); + if (M5.BtnA.wasPressed()) { //If button A is pressed. 如果按键A按下 + scale.set_offset(LOADCELL_OFFSET + scale.read()); + scale.set_scale(61.2f); + scale.tare(); + } + int weight = scale.get_units(5); + M5.Lcd.fillRect(75, 80, 320, 80, BLACK); + M5.Lcd.setCursor(75,80); + M5.Lcd.printf("Weight:%1d g", weight); +} \ No newline at end of file diff --git a/library.json b/library.json index b56939cd..1a8c1dc2 100644 --- a/library.json +++ b/library.json @@ -10,7 +10,7 @@ "type": "git", "url": "https://github.com/m5stack/M5Core2.git" }, - "version": "0.0.5", + "version": "0.0.6", "framework": "arduino", "platforms": "espressif32" } diff --git a/library.properties b/library.properties index cdd9a73d..c2000299 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=M5Core2 -version=0.0.5 +version=0.0.6 author=M5Stack maintainer=Hades sentence=Library for M5Stack Core2 development kit @@ -8,4 +8,4 @@ category=Device Control url=https://github.com/m5stack/M5Core2.git architectures=esp32 includes=M5Core2.h -depends=UNIT_ENV,UNIT_4RELAY,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel \ No newline at end of file +depends=M5GFX,UNIT_ENV,UNIT_4RELAY,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,Adafruit MCP4725,MAX30100lib,MFRC522_I2C,M5_BM8563,M5_ADS1100,M5_ADS1115,HX711 Arduino Library,PCA9554 \ No newline at end of file