├── README.md ├── examples ├── FactoryTest_V2.0 │ ├── src │ │ ├── app_relay.h │ │ ├── app_servo.h │ │ ├── app_dc_motor.h │ │ ├── app_rs232.h │ │ ├── app_rs485.h │ │ ├── app_microphone.h │ │ ├── app_rgb.h │ │ ├── app_step_motor.h │ │ ├── app_luminosity.h │ │ ├── app_dac.h │ │ ├── app_joystick.h │ │ ├── app_keyboard.h │ │ ├── app_encoder.h │ │ ├── app_adc.h │ │ ├── app_relay.cpp │ │ ├── app_env.h │ │ ├── app_rfid.h │ │ ├── main.h │ │ ├── app_microphone.cpp │ │ ├── app_luminosity.cpp │ │ ├── app_dac.cpp │ │ ├── app_adc.cpp │ │ ├── app_rs232.cpp │ │ ├── app_rs485.cpp │ │ ├── app_servo.cpp │ │ ├── app_joystick.cpp │ │ ├── app_dc_motor.cpp │ │ ├── app_rgb.cpp │ │ ├── app_rfid.cpp │ │ ├── app_env.cpp │ │ ├── app_test_page.h │ │ ├── app_step_motor.cpp │ │ ├── main.cpp │ │ ├── app_encoder.cpp │ │ └── app_keyboard.cpp │ ├── .gitignore │ ├── .vscode │ │ ├── extensions.json │ │ └── settings.json │ ├── lib │ │ ├── M5Unit-ENV │ │ │ ├── library.properties │ │ │ ├── src │ │ │ │ ├── M5_ENV.h │ │ │ │ ├── SHT3X.h │ │ │ │ ├── DHT12.h │ │ │ │ ├── SHT3X.cpp │ │ │ │ ├── DHT12.cpp │ │ │ │ ├── QMP6988.h │ │ │ │ └── QMP6988.cpp │ │ │ ├── library.json │ │ │ ├── README.md │ │ │ ├── LICENSE │ │ │ └── examples │ │ │ │ ├── Unit_ENVIII_M5Atom │ │ │ │ └── Unit_ENVIII_M5Atom.ino │ │ │ │ ├── Unit_BPS_M5Core2 │ │ │ │ └── Unit_BPS_M5Core2.ino │ │ │ │ ├── Unit_BPS_M5Core │ │ │ │ └── Unit_BPS_M5Core.ino │ │ │ │ ├── Unit_BPS_M5StickC │ │ │ │ └── Unit_BPS_M5StickC.ino │ │ │ │ ├── Unit_BPS_M5StickCPlus │ │ │ │ └── Unit_BPS_M5StickCPlus.ino │ │ │ │ ├── Unit_ENVIII_M5StickC │ │ │ │ └── Unit_ENVIII_M5StickC.ino │ │ │ │ ├── Unit_ENVIII_M5Core2 │ │ │ │ └── Unit_ENVIII_M5Core2.ino │ │ │ │ ├── Unit_ENVIII_M5StickCPlus │ │ │ │ └── Unit_ENVIII_M5StickCPlus.ino │ │ │ │ ├── Unit_ENVIII_M5Core │ │ │ │ └── Unit_ENVIII_M5Core.ino │ │ │ │ ├── Unit_ENV_M5Atom │ │ │ │ └── Unit_ENV_M5Atom.ino │ │ │ │ ├── Unit_ENVII_M5Atom │ │ │ │ └── Unit_ENVII_M5Atom.ino │ │ │ │ ├── Unit_ENV_M5Core2 │ │ │ │ └── Unit_ENV_M5Core2.ino │ │ │ │ ├── Unit_ENV_M5StickC │ │ │ │ └── Unit_ENV_M5StickC.ino │ │ │ │ ├── Unit_ENV_M5StickCPlus │ │ │ │ └── Unit_ENV_M5StickCPlus.ino │ │ │ │ ├── Unit_ENV_M5Core │ │ │ │ └── Unit_ENV_M5Core.ino │ │ │ │ ├── Unit_ENVII_M5StickC │ │ │ │ └── Unit_ENVII_M5StickC.ino │ │ │ │ ├── Unit_ENVII_M5Core2 │ │ │ │ └── Unit_ENVII_M5Core2.ino │ │ │ │ ├── Unit_ENVII_M5StickCPlus │ │ │ │ └── Unit_ENVII_M5StickCPlus.ino │ │ │ │ ├── Unit_ENVII_M5Core │ │ │ │ └── Unit_ENVII_M5Core.ino │ │ │ │ ├── Unit_CO2_M5Core │ │ │ │ └── Unit_CO2_M5Core.ino │ │ │ │ ├── Unit_CO2_M5StickC │ │ │ │ └── Unit_CO2_M5StickC.ino │ │ │ │ ├── Unit_CO2_M5StickCPlus │ │ │ │ └── Unit_CO2_M5StickCPlus.ino │ │ │ │ └── Unit_CO2_M5Atom │ │ │ │ └── Unit_CO2_M5Atom.ino │ │ └── README │ ├── test │ │ └── README │ ├── generate_user_custom.py │ ├── platformio.ini │ └── include │ │ └── README ├── SENSOR │ ├── TEMP_HUM │ │ └── TEMP_HUM.ino │ ├── LUMINOSITY │ │ └── LUMINOSITY.ino │ ├── MICROPHONE │ │ └── MICROPHONE.ino │ ├── SHT30_TEST │ │ └── SHT30_TEST.ino │ ├── AIR_PRESSURE │ │ └── AIR_PRESSURE.ino │ └── AIR_PRESSURE_QMP6988 │ │ └── AIR_PRESSURE_QMP6988.ino ├── servo │ └── servo.ino ├── RS-485 │ └── RS-485.ino ├── RS-232 │ └── RS-232.ino ├── JOYSTICK │ └── JOYSTICK.ino ├── DAC │ └── DAC.ino ├── MATRIX │ └── MATRIX.ino ├── KEYBOARD │ └── KEYBOARD.ino ├── Microphone │ └── Microphone.ino ├── STEPMOTOR │ └── STEPMOTOR.ino ├── DC-Motor │ └── DC-Motor.ino ├── FactoryTest_V1.0 │ ├── 测试说明.txt │ ├── Readme.txt │ └── FactoryTest_V1.0.ino ├── FactoryTest_V1.1 │ ├── 测试说明.txt │ ├── Readme.txt │ └── FactoryTest_V1.1.ino ├── ADC │ └── ADC.ino ├── ENCODER │ └── ENCODER.ino ├── RELAY │ └── RELAY.ino └── RFID │ ├── RFID.ino │ └── MFRC522_I2C.h ├── library.properties ├── .gitignore ├── library.json └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # M5IoTKit 2 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_relay.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void init_relay(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_servo.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void init_servo(void); 6 | void servo(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_dc_motor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void init_dc_motor(void); 6 | void dcmotor(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_rs232.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void init_rs232(void); 6 | int16_t uart232(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_rs485.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void init_rs485(void); 6 | int16_t uart485(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_microphone.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void microphone(uint16_t *analog_data, uint16_t *digital_data); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_rgb.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | #include 5 | 6 | void init_rgb(void); 7 | void matrix(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_step_motor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void init_step_motor(void); 6 | void step_motor(void); 7 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_luminosity.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void luminosity(uint16_t *analog_data, uint16_t *digital_data); 6 | 7 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_dac.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | #include "M5Unified.h" 5 | 6 | #define DAC_ADDR 0x4C 7 | 8 | void init_dac_test(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_joystick.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | void init_joystick(void); 6 | void joystick(uint16_t *x, uint16_t *y, uint8_t *btn); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_keyboard.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | 5 | #include 6 | 7 | void init_keyboard(void); 8 | char keyboard(void); 9 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_encoder.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | #include "AiEsp32RotaryEncoder.h" 5 | 6 | void init_encoder(bool with_button); 7 | void get_encoder(uint32_t *encoder, uint8_t *button, bool with_button); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_adc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "M5Unified.h" 4 | #include "main.h" 5 | #include 6 | 7 | #define ADC_ADDR 0x48 8 | 9 | void init_adc_test(void); 10 | float readChannel(ADS1115_MUX channel); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_relay.cpp: -------------------------------------------------------------------------------- 1 | #include "app_relay.h" 2 | 3 | const int In_0 = 13; 4 | 5 | void init_relay(void) 6 | { 7 | gpio_reset_pin(GPIO_NUM_13); 8 | pinMode(In_0, OUTPUT); 9 | digitalWrite(In_0, HIGH); 10 | } 11 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_env.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | // #include 5 | #include "M5_ENV.h" 6 | 7 | void get_sht30_info(float *temp, float *hum, uint32_t delay_time); 8 | void get_qmp6988_info(float *pres); 9 | void init_qmp6988(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_rfid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "main.h" 4 | #include 5 | #include 6 | #include 7 | 8 | extern uint8_t init_status; 9 | 10 | bool init_rfid(void); 11 | void ShowReaderDetails(byte *uid, byte *length); 12 | // void rfid_dump_info(void); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/main.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "M5Unified.h" 5 | 6 | #define I2C_PORT I2C_NUM_0 7 | #define I2C_SDA 21 8 | #define I2C_SCL 22 9 | 10 | extern uint8_t flag_i2c_test_failed; 11 | 12 | const int Analog = 35; 13 | const int Digtal = 2; 14 | 15 | bool i2c_check(uint8_t addr); -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ], 7 | "unwantedRecommendations": [ 8 | "ms-vscode.cpptools-extension-pack" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=DEMO-BOARD 2 | version=0.0.3 3 | author=M5Stack 4 | maintainer=M5Stack 5 | sentence=Library for DEMO-BOARD 6 | paragraph=See more on http://M5Stack.com 7 | category=Device Control 8 | url=https://github.com/m5stack/DEMO-BOARD 9 | architectures=esp32 10 | includes= 11 | depends=M5Stack,Keypad,M5Unit-ENV,FastLED,Adafruit BMP280 Library,Adafruit Unified Sensor,UNIT_ENV,MFRC522 i2c Library,Adafruit BusIO, -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "DEMO-BOARD", 3 | "description": "An ESP32 Arduino example", 4 | "keywords": "DEMO-BOARD", 5 | "authors": { 6 | "name": "M5Stack", 7 | "url": "http://www.m5stack.com" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/m5stack/DEMO-BOARD.git" 12 | }, 13 | "version": "0.0.3", 14 | "frameworks": "arduino", 15 | "platforms": "espressif32" 16 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/library.properties: -------------------------------------------------------------------------------- 1 | name=M5Unit-ENV 2 | version=0.0.6 3 | author=M5Stack 4 | maintainer=M5Stack 5 | sentence=Library for M5Stack UNIT ENV 6 | paragraph=See more on https://docs.m5stack.com/en/unit/envIII 7 | category=Device Control 8 | url=https://github.com/m5stack/M5Unit-ENV 9 | architectures=esp32 10 | includes=M5_ENV.h 11 | depends=Adafruit BMP280 Library,Adafruit Unified Sensor,Adafruit BusIO -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/src/M5_ENV.h: -------------------------------------------------------------------------------- 1 | /*! 2 | * @brief An environmental sensor From M5Stack 3 | * @copyright Copyright (c) 2022 by M5Stack[https://m5stack.com] 4 | * 5 | * @Links [ENV III](https://docs.m5stack.com/en/unit/envIII) 6 | * @version V0.0.5 7 | * @date 2022-07-020 8 | */ 9 | #ifndef _M5_ENV_H_ 10 | #define _M5_ENV_H_ 11 | 12 | #include "DHT12.h" 13 | #include "QMP6988.h" 14 | #include "SHT3X.h" 15 | 16 | #endif -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "M5Unit-ENV", 3 | "description": "Library for M5Stack UNIT ENV", 4 | "keywords": "M5Stack UNIT ENV", 5 | "authors": { 6 | "name": "M5Stack", 7 | "url": "http://www.m5stack.com" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/m5stack/M5Unit-ENV.git" 12 | }, 13 | "version": "0.0.6", 14 | "frameworks": "arduino", 15 | "platforms": "espressif32" 16 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/src/SHT3X.h: -------------------------------------------------------------------------------- 1 | #ifndef _SHT3X_H_ 2 | #define _SHT3X_H_ 3 | 4 | #if ARDUINO >= 100 5 | #include "Arduino.h" 6 | #else 7 | #include "WProgram.h" 8 | #endif 9 | 10 | #include "Wire.h" 11 | 12 | class SHT3X { 13 | public: 14 | SHT3X(uint8_t address = 0x44); 15 | byte get(void); 16 | float cTemp = 0; 17 | float fTemp = 0; 18 | float humidity = 0; 19 | 20 | private: 21 | uint8_t _address; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_microphone.cpp: -------------------------------------------------------------------------------- 1 | #include "app_microphone.h" 2 | 3 | void microphone(uint16_t *analog_data, uint16_t *digital_data) { 4 | static uint32_t lumi_delay_time = 0; 5 | 6 | if (lumi_delay_time < millis()) { 7 | *analog_data = analogRead(Analog); 8 | *digital_data = digitalRead(Digtal); 9 | 10 | Serial.printf("Analog:%0d Digtal:%0d\n", *analog_data, *digital_data); 11 | lumi_delay_time = millis() + 50; 12 | } 13 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_luminosity.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "app_luminosity.h" 3 | 4 | void luminosity(uint16_t *analog_data, uint16_t *digital_data) { 5 | static uint32_t lumi_delay_time = 0; 6 | 7 | if (lumi_delay_time < millis()) { 8 | *analog_data = analogRead(Analog); 9 | *digital_data = digitalRead(Digtal); 10 | 11 | Serial.printf("Analog:%0d Digtal:%0d\n", *analog_data, *digital_data); 12 | lumi_delay_time = millis() + 50; 13 | } 14 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "array": "cpp", 4 | "*.tcc": "cpp", 5 | "memory": "cpp", 6 | "istream": "cpp", 7 | "functional": "cpp", 8 | "tuple": "cpp", 9 | "utility": "cpp", 10 | "deque": "cpp", 11 | "list": "cpp", 12 | "string": "cpp", 13 | "unordered_map": "cpp", 14 | "unordered_set": "cpp", 15 | "vector": "cpp", 16 | "string_view": "cpp", 17 | "initializer_list": "cpp", 18 | "*.bk": "cpp" 19 | } 20 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/test/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for PlatformIO Test Runner and project tests. 3 | 4 | Unit Testing is a software testing method by which individual units of 5 | source code, sets of one or more MCU program modules together with associated 6 | control data, usage procedures, and operating procedures, are tested to 7 | determine whether they are fit for use. Unit testing finds problems early 8 | in the development cycle. 9 | 10 | More information about PlatformIO Unit Testing: 11 | - https://docs.platformio.org/en/latest/advanced/unit-testing/index.html 12 | -------------------------------------------------------------------------------- /examples/SENSOR/TEMP_HUM/TEMP_HUM.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DHT12.h" 3 | #include //The DHT12 uses I2C comunication. 4 | 5 | DHT12 dht12; //Preset scale CELSIUS and ID 0x5c. 6 | 7 | void setup() { 8 | M5.begin(); 9 | Wire.begin(); 10 | M5.Lcd.setCursor(80, 0, 4); 11 | M5.Lcd.print("TEMPERATURE"); 12 | } 13 | 14 | void loop() { 15 | 16 | float tmp = dht12.readTemperature(); 17 | float hum = dht12.readHumidity(); 18 | M5.Lcd.setCursor(30, 100, 4); 19 | M5.Lcd.printf("Temp: %2.1f Humi: %2.0f%%", tmp, hum); 20 | 21 | delay(100); 22 | } 23 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_dac.cpp: -------------------------------------------------------------------------------- 1 | #include "app_dac.h" 2 | 3 | static void outVoltage(uint8_t ch, uint16_t v) { 4 | uint8_t tx_data[2] = {0}; 5 | 6 | tx_data[0] = ((v >> 2) & 0xff); 7 | tx_data[1] = ((v << 6) & 0xff); 8 | M5.Ex_I2C.writeRegister(DAC_ADDR, 0x10 | (ch << 1), tx_data, 2, 100000U); 9 | } 10 | 11 | void init_dac_test(void) 12 | { 13 | if (i2c_check(DAC_ADDR)) { 14 | outVoltage(0, 256); // 1.25V 15 | outVoltage(1, 512); // 2.50V 16 | outVoltage(2, 768); // 3.75V 17 | outVoltage(3, 1023); // 5.00V 18 | } else { 19 | flag_i2c_test_failed = 1; 20 | } 21 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/README.md: -------------------------------------------------------------------------------- 1 | # M5Unit-ENV 2 | 3 | ## Overview 4 | 5 | ### SKU:U001 & U001-B & U001-C & U090 6 | 7 | Contains M5Stack-**UNIT ENV & BPS** series related case programs. ENV is an environmental sensor with integrated SHT30 and QMP6988 internally to detect temperature, humidity, and atmospheric pressure data. 8 | 9 | ## Related Link 10 | 11 | - [Document & Datasheet](https://docs.m5stack.com/en/unit/envIII) 12 | 13 | ## Required Libraries: 14 | 15 | - [Adafruit_BMP280_Library](https://github.com/adafruit/Adafruit_BMP280_Library) 16 | 17 | ## License 18 | 19 | - [M5Unit-ENV - MIT](LICENSE) -------------------------------------------------------------------------------- /examples/servo/servo.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "driver/ledc.h" 3 | 4 | const int servo_pin = 15; 5 | int freq = 50; 6 | int ledChannel = 0; 7 | int resolution = 8; 8 | 9 | void setup() { 10 | 11 | M5.begin(); 12 | M5.Lcd.setCursor(120, 110, 4); 13 | M5.Lcd.println("SERVO"); 14 | ledcSetup(ledChannel, freq, resolution); 15 | ledcAttachPin(servo_pin, ledChannel); 16 | 17 | } 18 | 19 | void loop() { 20 | 21 | ledcWrite(ledChannel, 6);//0° 22 | delay(1000); 23 | ledcWrite(ledChannel, 18);//90° 24 | delay(1000); 25 | ledcWrite(ledChannel, 30);//180° 26 | delay(1000); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_adc.cpp: -------------------------------------------------------------------------------- 1 | #include "app_adc.h" 2 | 3 | ADS1115_WE adc = ADS1115_WE(ADC_ADDR); 4 | 5 | float readChannel(ADS1115_MUX channel) { 6 | float voltage = 0.0; 7 | adc.setCompareChannels(channel); 8 | voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt 9 | return voltage; 10 | } 11 | 12 | void init_adc_test(void) 13 | { 14 | if(!adc.init()){ 15 | Serial.println("ADS1115 not connected!"); 16 | flag_i2c_test_failed = 1; 17 | } 18 | else { 19 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); 20 | adc.setCompareChannels(ADS1115_COMP_0_GND); 21 | adc.setMeasureMode(ADS1115_CONTINUOUS); 22 | } 23 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/src/DHT12.h: -------------------------------------------------------------------------------- 1 | /* 2 | DHT12.h - Library for DHT12 sensor. 3 | v0.0.1 Beta 4 | Created by Bobadas, July 30,2016. 5 | Released into the public domain. 6 | */ 7 | #ifndef DHT12_h 8 | #define DHT12_h 9 | #include "Arduino.h" 10 | #include "Wire.h" 11 | 12 | #define CELSIUS 1 13 | #define KELVIN 2 14 | #define FAHRENHEIT 3 15 | 16 | class DHT12 { 17 | public: 18 | DHT12(uint8_t scale = 0, uint8_t id = 0); 19 | float readTemperature(uint8_t scale = 0); 20 | float readHumidity(); 21 | 22 | private: 23 | uint8_t read(); 24 | uint8_t datos[5]; 25 | uint8_t _id; 26 | uint8_t _scale; 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_rs232.cpp: -------------------------------------------------------------------------------- 1 | #include "app_rs232.h" 2 | 3 | static int rs232_rx_count = 0; 4 | static int rs232_rx_num = 0; 5 | 6 | void init_rs232(void) 7 | { 8 | rs232_rx_count = 0; 9 | rs232_rx_num = 0; 10 | Serial2.begin(115200, SERIAL_8N1, 16, 17); 11 | } 12 | 13 | int16_t uart232(void) { 14 | 15 | Serial2.write('a'); 16 | if (Serial2.available()) { 17 | int ch = Serial2.read(); 18 | // Serial.write(ch); 19 | if (ch == 'a') { 20 | rs232_rx_num++; 21 | } 22 | } 23 | // rs232_rx_count++; 24 | // if (rs232_rx_count > 6000) rs232_rx_count = 0; 25 | // if (rs232_rx_count == 10) Serial2.write('a'); 26 | 27 | return rs232_rx_num; 28 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_rs485.cpp: -------------------------------------------------------------------------------- 1 | #include "app_rs485.h" 2 | 3 | static int rx_count = 0; 4 | static int rx_num = 0; 5 | 6 | void init_rs485(void) 7 | { 8 | rx_count = 0; 9 | rx_num = 0; 10 | Serial1.begin(115200, SERIAL_8N1, 22, 21); 11 | Serial2.begin(115200, SERIAL_8N1, 16, 17); 12 | } 13 | 14 | int16_t uart485(void) { 15 | Serial2.write('a'); 16 | // rx_count++; 17 | // if (rx_count > 200) rx_count = 0; 18 | // if (rx_count == 10) Serial2.write('a'); 19 | 20 | if (Serial1.available()) { // TX,RX有数据 21 | int ch = Serial1.read(); //读取RS485的数据 22 | // Serial.write(ch); 23 | if (ch == 'a') { 24 | rx_num++; 25 | } 26 | } 27 | 28 | return rx_num; 29 | } -------------------------------------------------------------------------------- /examples/SENSOR/LUMINOSITY/LUMINOSITY.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int Analog = 35; 4 | const int Digtal = 2; 5 | void setup() { 6 | // put your setup code here, to run once: 7 | M5.begin(); 8 | pinMode(Digtal, INPUT_PULLUP); 9 | dacWrite(25, 0); 10 | 11 | M5.Lcd.setCursor(100, 0, 4); 12 | M5.Lcd.print("LUMINOSITY"); 13 | 14 | } 15 | uint16_t a_data; 16 | uint16_t d_data; 17 | 18 | void loop() { 19 | // put your main code here, to run repeatedly: 20 | a_data = analogRead(Analog); 21 | d_data = digitalRead(Digtal); 22 | 23 | Serial.printf("Analog:%0d Digtal:%0d\n", a_data, d_data); 24 | 25 | M5.Lcd.setCursor(30, 120, 4); 26 | M5.Lcd.printf("Analog:%0d Digtal:%0d\n", a_data, d_data); 27 | 28 | delay(200); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /examples/SENSOR/MICROPHONE/MICROPHONE.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int Analog = 35; 4 | const int Digtal = 2; 5 | void setup() { 6 | // put your setup code here, to run once: 7 | M5.begin(); 8 | pinMode(Digtal, INPUT_PULLUP); 9 | dacWrite(25, 0); 10 | 11 | M5.Lcd.setCursor(100, 0, 4); 12 | M5.Lcd.print("MICROPHONE"); 13 | 14 | } 15 | uint16_t a_data; 16 | uint16_t d_data; 17 | 18 | void loop() { 19 | // put your main code here, to run repeatedly: 20 | a_data = analogRead(Analog); 21 | d_data = digitalRead(Digtal); 22 | 23 | Serial.printf("Analog:%0d Digtal:%0d\n", a_data, d_data); 24 | 25 | M5.Lcd.setCursor(30, 120, 4); 26 | M5.Lcd.printf("Analog:%0d Digtal:%0d\n", a_data, d_data); 27 | 28 | delay(200); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /examples/RS-485/RS-485.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() { 4 | // put your setup code here, to run once: 5 | M5.begin(); 6 | 7 | //Serial.begin(9600); 8 | //Serial2.begin(9600, SERIAL_8N1, 16, 17); 9 | Serial2.begin(115200, SERIAL_8N1, 16, 17); 10 | Serial1.begin(115200, SERIAL_8N1, 22, 21); 11 | 12 | M5.Lcd.setCursor(100, 0, 4); 13 | M5.Lcd.print("RS-485"); 14 | 15 | } 16 | int rx_num = 0; 17 | void loop() { 18 | // put your main code here, to run repeatedly: 19 | if(Serial1.available()) { 20 | int ch = Serial1.read(); 21 | if(ch == 'a'){ 22 | rx_num++; 23 | } 24 | M5.Lcd.setCursor(30, 120, 4); 25 | M5.Lcd.printf("rx_num = %d\n", rx_num); 26 | } 27 | delay(1000); 28 | Serial2.write('a'); 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_servo.cpp: -------------------------------------------------------------------------------- 1 | #include "app_servo.h" 2 | 3 | #define SERVO_PIN 13 4 | int servo_count = 0; 5 | int freq = 50; 6 | int ledChannel = 0; 7 | int resolution = 8; 8 | 9 | void init_servo(void) 10 | { 11 | gpio_reset_pin(GPIO_NUM_13); 12 | ledcSetup(ledChannel, freq, resolution); 13 | ledcAttachPin(SERVO_PIN, ledChannel); 14 | servo_count = 0; 15 | } 16 | 17 | void servo(void) 18 | { 19 | servo_count++; 20 | if (servo_count > 3000) servo_count = 0; 21 | if ((servo_count / 1000) == 0) { 22 | ledcWrite(ledChannel, 6); // 0° 23 | } 24 | if ((servo_count / 1000) == 1) { 25 | ledcWrite(ledChannel, 18); // 0° 26 | } 27 | 28 | if ((servo_count / 1000) == 2) { 29 | ledcWrite(ledChannel, 30); // 0° 30 | } 31 | } -------------------------------------------------------------------------------- /examples/RS-232/RS-232.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() { 4 | // put your setup code here, to run once: 5 | M5.begin(); 6 | 7 | Serial.begin(115200); 8 | 9 | // Serial2.begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert) 10 | Serial2.begin(115200, SERIAL_8N1, 16, 17); 11 | 12 | M5.Lcd.setCursor(100, 0, 4); 13 | M5.Lcd.print("RS-232"); 14 | 15 | } 16 | int rx_num = 0; 17 | void loop() { 18 | // put your main code here, to run repeatedly: 19 | 20 | if(Serial2.available()) { 21 | int ch = Serial2.read(); 22 | Serial.write(ch); 23 | if(ch == 'a'){ 24 | rx_num++; 25 | } 26 | M5.Lcd.setCursor(30, 120, 4); 27 | M5.Lcd.printf("rx_num = %d\n", rx_num); 28 | } 29 | 30 | Serial2.write('a'); 31 | delay(1000); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /examples/SENSOR/SHT30_TEST/SHT30_TEST.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Description: Use ENV II Unit to read temperature, humidity 3 | */ 4 | #include 5 | #include 6 | #include "SHT3X.h" 7 | 8 | SHT3X sht30; 9 | float tmp = 0.0; 10 | float hum = 0.0; 11 | 12 | void setup() { 13 | M5.begin(); 14 | M5.Power.begin(); 15 | Wire.begin(); 16 | M5.Lcd.setBrightness(10); 17 | M5.Lcd.setTextSize(3); 18 | M5.Lcd.clear(BLACK); 19 | } 20 | 21 | void loop() { 22 | if(sht30.get()==0){ 23 | tmp = sht30.cTemp; 24 | hum = sht30.humidity; 25 | } 26 | Serial.printf("Temperatura: %2.2f*C Humedad: %0.2f%%\r\n", tmp, hum); 27 | M5.Lcd.setCursor(0, 0); 28 | M5.Lcd.setTextColor(WHITE, BLACK); 29 | 30 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\n", tmp, hum); 31 | 32 | delay(100); 33 | } 34 | -------------------------------------------------------------------------------- /examples/SENSOR/AIR_PRESSURE/AIR_PRESSURE.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Adafruit_Sensor.h" 4 | #include 5 | 6 | /* 7 | note: need add library Adafruit_BMP280 from library manage 8 | */ 9 | 10 | Adafruit_BMP280 bme; 11 | void setup() { 12 | // put your setup code here, to run once: 13 | M5.begin(); 14 | Wire.begin(); 15 | 16 | M5.Lcd.setCursor(70, 0, 4); 17 | M5.Lcd.print("AIR_PRESSURE"); 18 | 19 | if (!bme.begin(0x76)){ 20 | Serial.println("Could not find a valid BMP280 sensor, check wiring!"); 21 | while (1); 22 | } 23 | 24 | } 25 | 26 | void loop() { 27 | // put your main code here, to run repeatedly: 28 | float pressure = bme.readPressure(); 29 | M5.Lcd.setCursor(50, 100, 4); 30 | M5.Lcd.printf("Pressure:%2.0fPa\r\n",pressure); 31 | delay(100); 32 | } 33 | -------------------------------------------------------------------------------- /examples/JOYSTICK/JOYSTICK.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | note:Reading the adc value requires writing the pin 25 of the adc to 0. 5 | dacWrite(25, 0); 6 | */ 7 | 8 | void setup() { 9 | // put your setup code here, to run once: 10 | M5.begin(); 11 | pinMode(2, INPUT_PULLUP); 12 | dacWrite(25, 0); 13 | 14 | M5.Lcd.setCursor(100, 0, 4); 15 | M5.Lcd.print("JOYSTICK"); 16 | } 17 | 18 | uint16_t x_data; 19 | uint16_t y_data; 20 | uint8_t button_data; 21 | void loop() { 22 | // put your main code here, to run repeatedly: 23 | x_data = analogRead(35); 24 | y_data = analogRead(36); 25 | button_data = digitalRead(2); 26 | 27 | Serial.printf("x:%0d y:%0d button:%d\n", x_data, y_data, button_data); 28 | 29 | M5.Lcd.setCursor(30, 120, 4); 30 | M5.Lcd.printf("x:%04d y:%04d button:%d\n", x_data, y_data, button_data); 31 | 32 | delay(200); 33 | } 34 | -------------------------------------------------------------------------------- /examples/DAC/DAC.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DAC_ADDR 0x4C 4 | 5 | void outVoltage(uint8_t ch,uint16_t v){ 6 | 7 | Wire.beginTransmission(DAC_ADDR); 8 | Wire.write(0x10|(ch<<1)); 9 | 10 | Wire.write((v >> 2) & 0xff); 11 | Wire.write((v << 6) & 0xff); 12 | Wire.endTransmission(); 13 | } 14 | 15 | void setup() { 16 | // put your setup code here, to run once: 17 | M5.begin(); 18 | Wire.begin(21, 22); 19 | dacWrite(25, 0); 20 | 21 | M5.Lcd.setCursor(140, 0, 4); 22 | M5.Lcd.print("DAC"); 23 | 24 | outVoltage(0,256); //1.25v 25 | outVoltage(1,512); //2.50v 26 | outVoltage(2,768); //3.75v 27 | outVoltage(3,1023); //5.00v 28 | 29 | M5.Lcd.setCursor(40, 100, 4); 30 | M5.Lcd.println("ch0:1.25V ch1:2.50V"); 31 | M5.Lcd.setCursor(40, 130, 4); 32 | M5.Lcd.println("ch2:3.75V ch3:5.00V"); 33 | } 34 | 35 | void loop() { 36 | delay(200); 37 | } 38 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_joystick.cpp: -------------------------------------------------------------------------------- 1 | #include "app_joystick.h" 2 | 3 | void init_joystick(void) 4 | { 5 | gpio_reset_pin(GPIO_NUM_2); 6 | gpio_reset_pin(GPIO_NUM_13); 7 | gpio_reset_pin(GPIO_NUM_15); 8 | pinMode(2, INPUT_PULLUP); 9 | pinMode(13, INPUT_PULLUP); 10 | pinMode(15, INPUT_PULLUP); 11 | } 12 | 13 | void joystick(uint16_t *x, uint16_t *y, uint8_t *btn) 14 | { 15 | uint16_t x_data = 0; 16 | uint16_t y_data = 0; 17 | uint8_t button_data = 0; 18 | static uint32_t joystick_delay_time = 0; 19 | 20 | if (joystick_delay_time < millis()) { 21 | x_data = analogRead(2); 22 | y_data = analogRead(13); 23 | button_data = digitalRead(15); 24 | Serial.printf("x:%0d y:%0d button:%d\n", x_data, y_data, button_data); 25 | *x = x_data; 26 | *y = y_data; 27 | *btn = button_data; 28 | joystick_delay_time = millis() + 200; 29 | } 30 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/generate_user_custom.py: -------------------------------------------------------------------------------- 1 | Import("env") 2 | import os 3 | 4 | firmware_name = env.GetProjectOption("custom_firmware_name") 5 | firmware_version = env.GetProjectOption('custom_firmware_version') 6 | firmware_suffix = env.GetProjectOption('custom_firmware_suffix') 7 | firmware_dir = env.GetProjectOption('custom_firmware_dir') 8 | 9 | firmware_bin = '%s_%s%s' % (firmware_name, firmware_version, firmware_suffix) 10 | firmware_path = os.path.join(firmware_dir, firmware_bin) 11 | 12 | app_bin = os.path.join('$BUILD_DIR','${PROGNAME}%s' % firmware_suffix) 13 | 14 | env.AddCustomTarget( 15 | name="firmware", 16 | dependencies=['buildfs'], 17 | actions=[ 18 | '"$PYTHONEXE" "$UPLOADER" --chip esp32 merge_bin ' 19 | '-o %s %s $ESP32_APP_OFFSET %s' 20 | % (firmware_path, ' '.join([ 21 | addr + " " + name for addr, name in env['FLASH_EXTRA_IMAGES'] 22 | ]), app_bin) 23 | ], 24 | title="Generate User Custom") 25 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_dc_motor.cpp: -------------------------------------------------------------------------------- 1 | #include "app_dc_motor.h" 2 | 3 | #define MOTOR_P 12 4 | #define MOTOR_N 15 5 | 6 | uint8_t dc_direct_flag = 0; 7 | 8 | void init_dc_motor(void) 9 | { 10 | gpio_reset_pin(GPIO_NUM_12); 11 | gpio_reset_pin(GPIO_NUM_15); 12 | pinMode(MOTOR_P, OUTPUT); 13 | pinMode(MOTOR_N, OUTPUT); 14 | } 15 | 16 | void dcmotor(void) 17 | { 18 | static uint32_t dc_count = 0; 19 | 20 | if (dc_count < millis()) { 21 | dc_count = millis() + 3000; 22 | if (dc_direct_flag) { 23 | dc_direct_flag = 0; 24 | } else { 25 | dc_direct_flag = 1; 26 | } 27 | } 28 | if (dc_direct_flag) { 29 | Serial.println("forwardforward"); 30 | digitalWrite(MOTOR_P, HIGH); 31 | digitalWrite(MOTOR_N, LOW); 32 | } else { 33 | Serial.println("backback"); 34 | digitalWrite(MOTOR_P, LOW); 35 | digitalWrite(MOTOR_N, HIGH); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /examples/MATRIX/MATRIX.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | /*** 6 | * note:You should first install the Fastled library. 7 | * 8 | ***/ 9 | 10 | #define DATA_PIN 15 11 | #define LED_TYPE WS2811 12 | #define COLOR_ORDER GRB 13 | #define NUM_LEDS 64 14 | CRGB leds[NUM_LEDS]; 15 | 16 | 17 | #define BRIGHTNESS 5 18 | 19 | void setup() { 20 | // put your setup code here, to run once: 21 | M5.begin(); 22 | M5.Lcd.setCursor(120, 110, 4); 23 | M5.Lcd.println("MATRIX"); 24 | 25 | FastLED.addLeds(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); 26 | FastLED.setBrightness(BRIGHTNESS); 27 | 28 | } 29 | 30 | void loop() { 31 | // put your main code here, to run repeatedly: 32 | for(int i = 0; i < 64; i++){ 33 | leds[i] = CRGB::White; 34 | FastLED.show(); 35 | } 36 | delay(500); 37 | // Now turn the LED off, then pause 38 | for(int i = 0; i < 64; i++){ 39 | leds[i] = CRGB::Black; 40 | FastLED.show(); 41 | } 42 | delay(500); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env] 12 | platform = espressif32 13 | board = esp32dev 14 | framework = arduino 15 | lib_deps = 16 | m5stack/M5GFX @ ^0.1.9 17 | m5stack/M5Unified @ ^0.1.9 18 | wollewald/ADS1115_WE @ ^1.4.3 19 | computer991/Arduino_MFRC522v2 @ ^2.0.1 20 | fastled/FastLED @ ^3.6.0 21 | igorantolic/Ai Esp32 Rotary Encoder @ ^1.6 22 | chris--a/Keypad @ ^3.1.1 23 | 24 | [env:release] 25 | build_type = release 26 | build_flags = -DCORE_DEBUG_LEVEL=5 -O0 27 | extra_scripts = post:generate_user_custom.py 28 | custom_firmware_version = 0.0.2 29 | custom_firmware_name = K024_Demoboard_JIE_20230913 30 | custom_firmware_suffix = .bin 31 | custom_firmware_dir = ./ -------------------------------------------------------------------------------- /examples/KEYBOARD/KEYBOARD.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | /*** 6 | * note:You should first install the keyboard library. 7 | * https://github.com/Chris--A/Keypad 8 | ***/ 9 | 10 | const byte ROWS = 4; //four rows 11 | const byte COLS = 4; //three columns 12 | 13 | 14 | byte rowPins[ROWS] = {17,16,21,22}; 15 | byte colPins[COLS] = {5, 26, 13, 15}; 16 | 17 | char keys[ROWS][COLS] = { 18 | {'a','b','c','d'}, 19 | {'e','f','g','h'}, 20 | {'i','j','k','l'}, 21 | {'m','n','o','p'} 22 | }; 23 | 24 | Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 25 | 26 | void setup() { 27 | // put your setup code here, to run once: 28 | M5.begin(); 29 | 30 | M5.Lcd.setCursor(100, 0, 4); 31 | M5.Lcd.println("KEYBOARD"); 32 | } 33 | 34 | void loop() { 35 | //put your main code here, to run repeatedly: 36 | char key = keypad.getKey(); 37 | if(key){ 38 | Serial.println(key); 39 | M5.Lcd.fillRect(150, 150, 80, 50, BLACK); 40 | M5.Lcd.setCursor(150, 140, 4); 41 | M5.Lcd.printf("%c",'A'); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /examples/Microphone/Microphone.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() { 4 | M5.begin(); 5 | //disable the speak noise 6 | dacWrite(25, 0); 7 | 8 | M5.Lcd.setTextColor(YELLOW); 9 | M5.Lcd.setTextSize(2); 10 | M5.Lcd.setCursor(65, 50); 11 | M5.Lcd.printf("Microphone\n"); 12 | pinMode(26, INPUT); 13 | M5.Lcd.setCursor(85, 80); 14 | M5.Lcd.print("GPIO36:"); 15 | M5.Lcd.setCursor(85, 100); 16 | M5.Lcd.print("GPIO26:"); 17 | } 18 | 19 | uint16_t analogRead_value = 0; 20 | uint16_t digitalRead_value = 0; 21 | void loop() { 22 | // put your main code here, to run repeatedly: 23 | M5.Lcd.setCursor(175, 80); 24 | M5.Lcd.setTextColor(BLACK); 25 | M5.Lcd.printf("%d\n", analogRead_value); 26 | M5.Lcd.setCursor(175, 100); 27 | M5.Lcd.printf("%d\n", digitalRead_value); 28 | analogRead_value = analogRead(36); 29 | digitalRead_value = digitalRead(26); 30 | M5.Lcd.setTextColor(YELLOW); 31 | M5.Lcd.setCursor(175, 80); 32 | M5.Lcd.printf("%d\n", analogRead_value); 33 | M5.Lcd.setCursor(175, 100); 34 | M5.Lcd.printf("%d\n", digitalRead_value); 35 | delay(10); 36 | } 37 | -------------------------------------------------------------------------------- /examples/STEPMOTOR/STEPMOTOR.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MOTOR_A = 2; 4 | const int MOTOR_B = 5; 5 | const int MOTOR_C = 12; 6 | const int MOTOR_D = 13; 7 | void setup() { 8 | M5.begin(); 9 | // put your setup code here, to run once: 10 | M5.Lcd.setCursor(80,110,4); 11 | M5.Lcd.println("STEPMOTOR"); 12 | pinMode(MOTOR_A, OUTPUT); 13 | pinMode(MOTOR_B, OUTPUT); 14 | pinMode(MOTOR_C, OUTPUT); 15 | pinMode(MOTOR_D, OUTPUT); 16 | } 17 | void loop() { 18 | 19 | digitalWrite(MOTOR_A, HIGH); 20 | digitalWrite(MOTOR_B, HIGH); 21 | digitalWrite(MOTOR_C, LOW); 22 | digitalWrite(MOTOR_D, LOW); 23 | delay(2); 24 | digitalWrite(MOTOR_A, LOW); 25 | digitalWrite(MOTOR_B, HIGH); 26 | digitalWrite(MOTOR_C, HIGH); 27 | digitalWrite(MOTOR_D, LOW); 28 | delay(2); 29 | digitalWrite(MOTOR_A, LOW); 30 | digitalWrite(MOTOR_B, LOW); 31 | digitalWrite(MOTOR_C, HIGH); 32 | digitalWrite(MOTOR_D, HIGH); 33 | delay(2); 34 | digitalWrite(MOTOR_A, HIGH); 35 | digitalWrite(MOTOR_B, LOW); 36 | digitalWrite(MOTOR_C, LOW); 37 | digitalWrite(MOTOR_D, HIGH); 38 | delay(2); 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 M5Stack 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 M5Stack 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/src/SHT3X.cpp: -------------------------------------------------------------------------------- 1 | #include "SHT3X.h" 2 | 3 | /* Motor() 4 | 5 | */ 6 | SHT3X::SHT3X(uint8_t address) { 7 | // Wire.begin(0,26, 100000UL); 8 | _address = address; 9 | } 10 | 11 | byte SHT3X::get() { 12 | unsigned int data[6]; 13 | 14 | // Start I2C Transmission 15 | Wire.beginTransmission(_address); 16 | // Send measurement command 17 | Wire.write(0x2C); 18 | Wire.write(0x06); 19 | // Stop I2C transmission 20 | if (Wire.endTransmission() != 0) return 1; 21 | 22 | delay(200); 23 | 24 | // Request 6 bytes of data 25 | Wire.requestFrom(_address, (uint8_t)6); 26 | 27 | // Read 6 bytes of data 28 | // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc 29 | for (int i = 0; i < 6; i++) { 30 | data[i] = Wire.read(); 31 | }; 32 | 33 | delay(50); 34 | 35 | if (Wire.available() != 0) return 2; 36 | 37 | // Convert the data 38 | cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45; 39 | fTemp = (cTemp * 1.8) + 32; 40 | humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /examples/DC-Motor/DC-Motor.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Name: DC-Motor.ino 3 | Created: 2019/5/17 10:06:15 4 | Author: shaoxiang 5 | */ 6 | 7 | #include 8 | // The setup() function runs once each time the micro-controller starts 9 | void setup() { 10 | // init lcd, serial, but don't init sd card 11 | M5.begin(); 12 | M5.Lcd.setCursor(120,0,4); 13 | M5.Lcd.println("DC-motor"); 14 | M5.Lcd.setCursor(30, 200); 15 | M5.Lcd.println("Forward"); 16 | M5.Lcd.setCursor(140, 200); 17 | M5.Lcd.println("Stop"); 18 | M5.Lcd.setCursor(220, 200); 19 | M5.Lcd.println("Reverse"); 20 | 21 | pinMode(21, OUTPUT); 22 | pinMode(22, OUTPUT); 23 | pinMode(13, INPUT_PULLUP); 24 | pinMode(15, INPUT_PULLUP); 25 | } 26 | 27 | // Add the main program code into the continuous loop() function 28 | void loop() { 29 | M5.update(); 30 | 31 | if (M5.BtnA.wasReleased()) { 32 | digitalWrite(22, LOW); 33 | digitalWrite(21, HIGH); 34 | } else if (M5.BtnB.wasReleased()) { 35 | digitalWrite(21, LOW); 36 | digitalWrite(22, LOW); 37 | } else if (M5.BtnC.wasReleased()) { 38 | digitalWrite(21, LOW); 39 | digitalWrite(22, HIGH); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_rgb.cpp: -------------------------------------------------------------------------------- 1 | #include "app_rgb.h" 2 | 3 | #define DATA_PIN 13 4 | //#define CLK_PIN 4 5 | #define LED_TYPE WS2811 6 | #define COLOR_ORDER GRB 7 | #define NUM_LEDS 64 8 | CRGB leds[NUM_LEDS]; 9 | #define BRIGHTNESS 5 10 | 11 | static int led_count = 0; 12 | 13 | void init_rgb(void) 14 | { 15 | gpio_reset_pin(GPIO_NUM_13); 16 | pinMode(DATA_PIN, OUTPUT); 17 | digitalWrite(DATA_PIN, LOW); 18 | FastLED.addLeds(leds, NUM_LEDS); 19 | FastLED.setBrightness(BRIGHTNESS); 20 | led_count = 0; 21 | } 22 | 23 | void matrix(void) { 24 | led_count++; 25 | if (led_count > 400) led_count = 0; 26 | if ((led_count / 100) == 0) { 27 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Red; 28 | leds[0] = CRGB::Red; 29 | FastLED.show(); 30 | } 31 | 32 | if ((led_count / 100) == 1) { 33 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Green; 34 | leds[0] = CRGB::Green; 35 | FastLED.show(); 36 | } 37 | if ((led_count / 100) == 2) { 38 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Blue; 39 | leds[0] = CRGB::Blue; 40 | FastLED.show(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /examples/FactoryTest_V1.0/测试说明.txt: -------------------------------------------------------------------------------- 1 | 2 | 注意:PIN 指定当前测试项目下,需要连结的引脚 3 | 当检测iic接口时,porta 口需断开 4 | 5 | 1 joystick 6 | 7 | 正常 x范围 0 - 4000 8 | 正常 y范围 0 - 4000 9 | 正常 button 0/1 10 | 11 | 2 DAC * 4 12 | 使用万用表测量通道电压 13 | ch0 1.25v 14 | ch1 2.50v 15 | ch2 3.75v 16 | ch3 5.00v 17 | 18 | 3 ADC * 4 19 | 外接一个3.3v或者5.0v电源线,分别接入通道1到4 20 | 通道1到4会数据会随着变化 21 | 22 | 3.3v - 17000 23 | 5.0v - 26000 24 | 25 | 注意:每一路通道左边为地线,右边为adc输入引脚,切勿短路 26 | 27 | 4 encoder 28 | 29 | 向左或者向右旋转 code数值会相应加减 30 | 按下时 button 会 0/1变化 31 | 32 | 33 | 34 | 5 MATRIX 35 | 灯会 依次 红 - 绿 - 蓝闪烁。 36 | 37 | 6 TEMPERATURE 38 | 正常显示温度和湿度 39 | 40 | 7 air 41 | 正常显示气压值 42 | 43 | 8 luminosity 44 | 打开手机手电筒,照射元器件,可以看到明显数值变化 45 | 46 | 9 microphone 47 | 对着手电筒说话,或者吹起时,可以看到明显数值变化 48 | 49 | 10 keyboard 50 | 按下按键 屏幕对应显示。 51 | 52 | 11 dc-motor 53 | 接线后电机旋转 54 | 55 | 12 relay 56 | 将接线以此插入 ch0 -ch7,通道灯会点亮,并伴随有继电器吸合的声音 57 | 58 | 13 servo 59 | 接线后舵机旋转 60 | 61 | 14 STEPMOTOR 62 | 接线后电机旋转 63 | 64 | 65 | 15 RS232 66 | 接线后,并将串口短接,(第二排,第二 第三针短路),可以看到显示屏 re_num 数据递增 67 | 68 | 16 RS485 69 | 接线后,并将串口短接,(第二排,第二 第三针短路),可以看到显示屏 re_num 数据递增 70 | 71 | 17 RFID 72 | 刷卡后。识别出芯片ID,并打印到界面。 -------------------------------------------------------------------------------- /examples/FactoryTest_V1.1/测试说明.txt: -------------------------------------------------------------------------------- 1 | 2 | 注意:PIN 指定当前测试项目下,需要连结的引脚 3 | 当检测iic接口时,porta 口需断开 4 | 5 | 1 joystick 6 | 7 | 正常 x范围 0 - 4000 8 | 正常 y范围 0 - 4000 9 | 正常 button 0/1 10 | 11 | 2 DAC * 4 12 | 使用万用表测量通道电压 13 | ch0 1.25v 14 | ch1 2.50v 15 | ch2 3.75v 16 | ch3 5.00v 17 | 18 | 3 ADC * 4 19 | 外接一个3.3v或者5.0v电源线,分别接入通道1到4 20 | 通道1到4会数据会随着变化 21 | 22 | 3.3v - 17000 23 | 5.0v - 26000 24 | 25 | 注意:每一路通道左边为地线,右边为adc输入引脚,切勿短路 26 | 27 | 4 encoder 28 | 29 | 向左或者向右旋转 code数值会相应加减 30 | 按下时 button 会 0/1变化 31 | 32 | 33 | 34 | 5 MATRIX 35 | 灯会 依次 红 - 绿 - 蓝闪烁。 36 | 37 | 6 TEMPERATURE 38 | 正常显示温度和湿度 39 | 40 | 7 air 41 | 正常显示气压值 42 | 43 | 8 luminosity 44 | 打开手机手电筒,照射元器件,可以看到明显数值变化 45 | 46 | 9 microphone 47 | 对着手电筒说话,或者吹起时,可以看到明显数值变化 48 | 49 | 10 keyboard 50 | 按下按键 屏幕对应显示。 51 | 52 | 11 dc-motor 53 | 接线后电机旋转 54 | 55 | 12 relay 56 | 将接线以此插入 ch0 -ch7,通道灯会点亮,并伴随有继电器吸合的声音 57 | 58 | 13 servo 59 | 接线后舵机旋转 60 | 61 | 14 STEPMOTOR 62 | 接线后电机旋转 63 | 64 | 65 | 15 RS232 66 | 接线后,并将串口短接,(第二排,第二 第三针短路),可以看到显示屏 re_num 数据递增 67 | 68 | 16 RS485 69 | 接线后,并将串口短接,(第二排,第二 第三针短路),可以看到显示屏 re_num 数据递增 70 | 71 | 17 RFID 72 | 刷卡后。识别出芯片ID,并打印到界面。 -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link into executable file. 4 | 5 | The source code of each library should be placed in a an own separate directory 6 | ("lib/your_library_name/[here are source files]"). 7 | 8 | For example, see a structure of the following two libraries `Foo` and `Bar`: 9 | 10 | |--lib 11 | | | 12 | | |--Bar 13 | | | |--docs 14 | | | |--examples 15 | | | |--src 16 | | | |- Bar.c 17 | | | |- Bar.h 18 | | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html 19 | | | 20 | | |--Foo 21 | | | |- Foo.c 22 | | | |- Foo.h 23 | | | 24 | | |- README --> THIS FILE 25 | | 26 | |- platformio.ini 27 | |--src 28 | |- main.c 29 | 30 | and a contents of `src/main.c`: 31 | ``` 32 | #include 33 | #include 34 | 35 | int main (void) 36 | { 37 | ... 38 | } 39 | 40 | ``` 41 | 42 | PlatformIO Library Dependency Finder will find automatically dependent 43 | libraries scanning project source files. 44 | 45 | More information about PlatformIO Library Dependency Finder 46 | - https://docs.platformio.org/page/librarymanager/ldf.html 47 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_rfid.cpp: -------------------------------------------------------------------------------- 1 | #include "app_rfid.h" 2 | 3 | const uint8_t customAddress = 0x28; 4 | 5 | TwoWire &customI2C = Wire; 6 | 7 | MFRC522DriverI2C driver{customAddress, customI2C}; // Create I2C driver. 8 | 9 | MFRC522 mfrc522{driver}; // Create MFRC522 instance. 10 | 11 | uint8_t init_status = 0; 12 | 13 | bool init_rfid(void) 14 | { 15 | bool res = 0; 16 | 17 | if (i2c_check(0x28)) { 18 | res = mfrc522.PCD_Init(); 19 | delay(5); 20 | // MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial); 21 | } else { 22 | flag_i2c_test_failed = 1; 23 | } 24 | 25 | if (res) init_status = 1; 26 | else init_status = 0; 27 | 28 | return res; 29 | } 30 | 31 | void ShowReaderDetails(byte *uid, byte *length) 32 | { 33 | if ( !mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) { 34 | // return; 35 | } 36 | // Serial.print(F("Card UID:")); 37 | *length = mfrc522.uid.size; 38 | for (byte i = 0; i < mfrc522.uid.size; i++) { 39 | // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 40 | // Serial.print(mfrc522.uid.uidByte[i], HEX); 41 | uid[i] = mfrc522.uid.uidByte[i]; 42 | } 43 | // Serial.println(); 44 | // MFRC522Debug::PICC_DumpToSerial(mfrc522, Serial, &(mfrc522.uid)); 45 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_env.cpp: -------------------------------------------------------------------------------- 1 | #include "app_env.h" 2 | 3 | SHT3X sht30; 4 | QMP6988 qmp6988; 5 | 6 | void get_sht30_info(float *temp, float *hum, uint32_t delay_time) { 7 | static uint32_t get_delay_millis = 0; 8 | 9 | if (get_delay_millis < millis()) { 10 | get_delay_millis = millis() + delay_time; 11 | if(sht30.get()==0){ 12 | Serial.print("Temperature in Celsius : "); 13 | Serial.println(sht30.cTemp); 14 | *temp = sht30.cTemp; 15 | Serial.print("Temperature in Fahrenheit : "); 16 | Serial.println(sht30.fTemp); 17 | Serial.print("Relative Humidity : "); 18 | Serial.println(sht30.humidity); 19 | *hum = sht30.humidity; 20 | Serial.println(); 21 | } 22 | else 23 | { 24 | Serial.println("Error!"); 25 | } 26 | } 27 | } 28 | 29 | void init_qmp6988(void) 30 | { 31 | if (i2c_check(0x70)) 32 | qmp6988.init(0x70, &Wire); 33 | } 34 | 35 | void get_qmp6988_info(float *pres) { 36 | static uint32_t get_qmp_delay_time; 37 | float pressure = 0.0; 38 | 39 | if (get_qmp_delay_time < millis()) { 40 | get_qmp_delay_time = millis() + 50; 41 | pressure = qmp6988.calcPressure(); 42 | 43 | *pres = pressure; 44 | Serial.printf("Pressure:%2.0fPa\r\n", pressure); 45 | } 46 | } -------------------------------------------------------------------------------- /examples/ADC/ADC.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ADC_ADDR 0x48 4 | 5 | uint16_t InVoltage(uint8_t ch){ 6 | 7 | uint8_t data_L = 0; 8 | uint8_t data_H = 0; 9 | uint16_t data_adc = 0; 10 | 11 | Wire.beginTransmission(ADC_ADDR); 12 | Wire.write(0X01); 13 | Wire.write(0XC0 | (ch << 4)); 14 | Wire.write(0X83); 15 | Wire.endTransmission(); 16 | 17 | Wire.beginTransmission(ADC_ADDR); 18 | Wire.write(0x00); 19 | Wire.endTransmission(); 20 | 21 | delay(50); 22 | 23 | Wire.requestFrom(ADC_ADDR, 2); 24 | while(Wire.available()){ 25 | data_H = Wire.read(); 26 | data_L = Wire.read(); 27 | } 28 | 29 | data_adc = (data_H << 8) | data_L; 30 | return data_adc; 31 | } 32 | void setup() { 33 | // put your setup code here, to run once: 34 | M5.begin(); 35 | Wire.begin(); 36 | dacWrite(25, 0); 37 | 38 | M5.Lcd.setCursor(140, 0, 4); 39 | M5.Lcd.print("ADC"); 40 | 41 | } 42 | uint16_t adc_ch0 = 0; 43 | uint16_t adc_ch1 = 0; 44 | uint16_t adc_ch2 = 0; 45 | uint16_t adc_ch3 = 0; 46 | void loop() { 47 | adc_ch0 = InVoltage(0); 48 | adc_ch1 = InVoltage(1); 49 | adc_ch2 = InVoltage(2); 50 | adc_ch3 = InVoltage(3); 51 | Serial.printf("ch0:%d ch1:%d ch2:%d ch3:%d\n", adc_ch0, adc_ch1, adc_ch2,adc_ch3); 52 | 53 | M5.Lcd.setCursor(40, 100, 4); 54 | M5.Lcd.printf("ch0:%05d ch1:%05d\n", adc_ch0, adc_ch1); 55 | M5.Lcd.setCursor(40, 130, 4); 56 | M5.Lcd.printf("ch2:%05d ch3:%05d\n", adc_ch2, adc_ch3); 57 | delay(500); 58 | } 59 | -------------------------------------------------------------------------------- /examples/ENCODER/ENCODER.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int phaseA = 2; 4 | const int phaseB = 13; 5 | const int Button = 15; 6 | 7 | #define GET_CODE() uint8_t(digitalRead(phaseA) << 4 | digitalRead(phaseB)) 8 | 9 | int32_t count = 65536; 10 | int32_t count_last = 65536; 11 | int32_t count_change = 0; 12 | uint8_t code = 0; 13 | uint8_t code_old = 0; 14 | 15 | void encoderEvent() { 16 | code = GET_CODE(); 17 | if(code != code_old) { 18 | if(code == 0x00) { 19 | count_last = count; 20 | if(code_old == 0x10) { 21 | count--; 22 | count_change == -65536 ? count_change : count_change--; 23 | } else { 24 | count_change == 65536 ? count_change : count_change++; 25 | } 26 | } 27 | code_old = code; 28 | } 29 | } 30 | 31 | void setup() { 32 | //put your setup code here, to run once: 33 | M5.begin(); 34 | pinMode(phaseA, INPUT_PULLUP); 35 | pinMode(phaseB, INPUT_PULLUP); 36 | pinMode(Button, INPUT_PULLUP); 37 | dacWrite(25, 0); 38 | 39 | M5.Lcd.setCursor(100, 0, 4); 40 | M5.Lcd.print("ENCODER"); 41 | 42 | code = GET_CODE(); 43 | code_old = code; 44 | 45 | } 46 | 47 | void loop() { 48 | // put your main code here, to run repeatedly: 49 | uint8_t value = digitalRead(Button); 50 | encoderEvent(); 51 | Serial.printf("code = %d,button = %d\r\n ",count_change,value); 52 | 53 | M5.Lcd.setCursor(30, 120, 4); 54 | M5.Lcd.printf("code = %05d,button = %d\r\n ",count_change,value); 55 | delay(1); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_test_page.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "M5Unified.h" 4 | #include "main.h" 5 | #include "app_dac.h" 6 | #include "app_adc.h" 7 | #include "app_rfid.h" 8 | #include "app_env.h" 9 | #include "app_luminosity.h" 10 | #include "app_microphone.h" 11 | #include "app_rs485.h" 12 | #include "app_rs232.h" 13 | #include "app_relay.h" 14 | #include "app_rgb.h" 15 | #include "app_servo.h" 16 | #include "app_joystick.h" 17 | #include "app_encoder.h" 18 | #include "app_dc_motor.h" 19 | #include "app_step_motor.h" 20 | #include "app_keyboard.h" 21 | 22 | extern uint8_t page_status; 23 | 24 | enum {PAGE_DAC = 0, 25 | PAGE_ADC, 26 | PAGE_RFID, 27 | PAGE_TEMP, 28 | PAGE_AIR_PRESSURE, 29 | PAGE_LUMINOSITY, 30 | PAGE_MICROPHONE, 31 | PAGE_RS485, 32 | PAGE_RS232, 33 | PAGE_RELAY, 34 | PAGE_RGB, 35 | PAGE_SERVO, 36 | PAGE_JOYSTICK, 37 | PAGE_ENCODER, 38 | PAGE_DC_MOTOR, 39 | PAGE_STEP_MOTOR, 40 | PAGE_KEYBOARD}; 41 | 42 | void page_dac_test(void); 43 | void page_adc_test(void); 44 | void page_rfid_test(void); 45 | void page_temp_test(void); 46 | void page_pressure_test(void); 47 | void page_luminosity_test(void); 48 | void page_microphone_test(void); 49 | void page_rs485_test(void); 50 | void page_rs232_test(void); 51 | void page_relay_test(void); 52 | void page_rgb_test(void); 53 | void page_servo_test(void); 54 | void page_joystick_test(void); 55 | void page_encoder_test(void); 56 | void page_dcmotor_test(void); 57 | void page_stepmotor_test(void); 58 | void page_keyboard_test(void); -------------------------------------------------------------------------------- /examples/SENSOR/AIR_PRESSURE_QMP6988/AIR_PRESSURE_QMP6988.ino: -------------------------------------------------------------------------------- 1 | /* 2 | note: need add library UNIT_ENV from library manage 3 | */ 4 | #include 5 | 6 | #include "M5_ENV.h" 7 | 8 | SHT3X sht30; 9 | QMP6988 qmp6988; 10 | 11 | float tmp = 0.0, hum = 0.0, pressure = 0.0; 12 | 13 | void setup() { 14 | M5.begin(); // Init M5Stack. 初始化M5Stack 15 | M5.Power.begin(); // Init power 初始化电源模块 16 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 17 | M5.Lcd.drawString("AIR_PRESSURE", 70, 0); 18 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 19 | qmp6988.init(); 20 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 21 | } 22 | 23 | void loop() { 24 | pressure = qmp6988.calcPressure(); 25 | if (sht30.get() == 0) { // Obtain the data of shT30. 获取sht30的数据 26 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 27 | // 将sht30获取到的温度存储 28 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 29 | // 将sht30获取到的湿度存储 30 | } else { 31 | tmp = 0, hum = 0; 32 | } 33 | M5.lcd.fillRect(0, 20, 100, 60, 34 | BLACK); // Fill the screen with black (to clear the 35 | // screen). 将屏幕填充黑色(用来清屏) 36 | M5.lcd.setCursor(0, 20); 37 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 38 | tmp, hum, pressure); 39 | delay(2000); 40 | } 41 | -------------------------------------------------------------------------------- /examples/RELAY/RELAY.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | const int In_0 = 23; 5 | const int In_1 = 19; 6 | const int In_2 = 18; 7 | const int In_3 = 2; 8 | const int In_4 = 5; 9 | const int In_5 = 12; 10 | const int In_6 = 13; 11 | const int In_7 = 15; 12 | 13 | void setup() { 14 | // put your setup code here, to run once: 15 | M5.begin(); 16 | M5.Lcd.setCursor(120, 110, 4); 17 | M5.Lcd.println("RELAY"); 18 | pinMode(In_0,OUTPUT); 19 | pinMode(In_1,OUTPUT); 20 | pinMode(In_2,OUTPUT); 21 | pinMode(In_3,OUTPUT); 22 | pinMode(In_4,OUTPUT); 23 | pinMode(In_5,OUTPUT); 24 | pinMode(In_6,OUTPUT); 25 | pinMode(In_7,OUTPUT); 26 | 27 | 28 | digitalWrite(In_0, LOW); 29 | digitalWrite(In_1, LOW); 30 | digitalWrite(In_2, LOW); 31 | digitalWrite(In_3, LOW); 32 | digitalWrite(In_4, LOW); 33 | digitalWrite(In_5, LOW); 34 | digitalWrite(In_6, LOW); 35 | digitalWrite(In_7, LOW); 36 | 37 | } 38 | 39 | void loop() { 40 | // put your main code here, to run repeatedly: 41 | digitalWrite(In_0, LOW); 42 | digitalWrite(In_1, LOW); 43 | digitalWrite(In_2, LOW); 44 | digitalWrite(In_3, LOW); 45 | digitalWrite(In_4, LOW); 46 | digitalWrite(In_5, LOW); 47 | digitalWrite(In_6, LOW); 48 | digitalWrite(In_7, LOW); 49 | 50 | delay(2000); 51 | 52 | digitalWrite(In_0, HIGH); 53 | digitalWrite(In_1, HIGH); 54 | digitalWrite(In_2, HIGH); 55 | digitalWrite(In_3, HIGH); 56 | digitalWrite(In_4, HIGH); 57 | digitalWrite(In_5, HIGH); 58 | digitalWrite(In_6, HIGH); 59 | digitalWrite(In_7, HIGH); 60 | 61 | delay(2000); 62 | 63 | } 64 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/include/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project header files. 3 | 4 | A header file is a file containing C declarations and macro definitions 5 | to be shared between several project source files. You request the use of a 6 | header file in your project source file (C, C++, etc) located in `src` folder 7 | by including it, with the C preprocessing directive `#include'. 8 | 9 | ```src/main.c 10 | 11 | #include "header.h" 12 | 13 | int main (void) 14 | { 15 | ... 16 | } 17 | ``` 18 | 19 | Including a header file produces the same results as copying the header file 20 | into each source file that needs it. Such copying would be time-consuming 21 | and error-prone. With a header file, the related declarations appear 22 | in only one place. If they need to be changed, they can be changed in one 23 | place, and programs that include the header file will automatically use the 24 | new version when next recompiled. The header file eliminates the labor of 25 | finding and changing all the copies as well as the risk that a failure to 26 | find one copy will result in inconsistencies within a program. 27 | 28 | In C, the usual convention is to give header files names that end with `.h'. 29 | It is most portable to use only letters, digits, dashes, and underscores in 30 | header file names, and at most one dot. 31 | 32 | Read more about using header files in official GCC documentation: 33 | 34 | * Include Syntax 35 | * Include Operation 36 | * Once-Only Headers 37 | * Computed Includes 38 | 39 | https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html 40 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/src/DHT12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | DHT12.cpp - Library for DHT12 sensor. 3 | v0.0.1 Beta 4 | Created by Bobadas, July 30,2016. 5 | Released into the public domain. 6 | */ 7 | #include "DHT12.h" 8 | 9 | DHT12::DHT12(uint8_t scale, uint8_t id) { 10 | if (id == 0 || id > 126) 11 | _id = 0x5c; 12 | else 13 | _id = id; 14 | if (scale == 0 || scale > 3) 15 | _scale = CELSIUS; 16 | else 17 | _scale = scale; 18 | } 19 | 20 | uint8_t DHT12::read() { 21 | Wire.beginTransmission(_id); 22 | Wire.write(0); 23 | if (Wire.endTransmission() != 0) return 1; 24 | Wire.requestFrom(_id, (uint8_t)5); 25 | for (int i = 0; i < 5; i++) { 26 | datos[i] = Wire.read(); 27 | }; 28 | delay(50); 29 | if (Wire.available() != 0) return 2; 30 | if (datos[4] != (datos[0] + datos[1] + datos[2] + datos[3])) return 3; 31 | return 0; 32 | } 33 | 34 | float DHT12::readTemperature(uint8_t scale) { 35 | float resultado = 0; 36 | uint8_t error = read(); 37 | if (error != 0) return (float)error / 100; 38 | if (scale == 0) scale = _scale; 39 | switch (scale) { 40 | case CELSIUS: 41 | resultado = (datos[2] + (float)datos[3] / 10); 42 | break; 43 | case FAHRENHEIT: 44 | resultado = ((datos[2] + (float)datos[3] / 10) * 1.8 + 32); 45 | break; 46 | case KELVIN: 47 | resultado = (datos[2] + (float)datos[3] / 10) + 273.15; 48 | break; 49 | }; 50 | return resultado; 51 | } 52 | 53 | float DHT12::readHumidity() { 54 | float resultado; 55 | uint8_t error = read(); 56 | if (error != 0) return (float)error / 100; 57 | resultado = (datos[0] + (float)datos[1] / 10); 58 | return resultado; 59 | } 60 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVIII_M5Atom/Unit_ENVIII_M5Atom.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with Atom-Lite/Matrix sample source code 5 | * 配套 Atom-Lite/Matrix 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envIII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envIII 8 | * 9 | * Product: ENVIII_SHT30_QMP6988. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port,Read temperature, humidity and atmospheric pressure and 13 | display them on the display Serial 14 | 请连接端口,读取温度、湿度和大气压强并在显示屏上显示 15 | */ 16 | #include 17 | #include "M5_ENV.h" 18 | 19 | SHT3X sht30; 20 | QMP6988 qmp6988; 21 | 22 | float tmp = 0.0; 23 | float hum = 0.0; 24 | float pressure = 0.0; 25 | 26 | void setup() { 27 | M5.begin(); // Init M5Atom. 初始化M5Atom 28 | Wire.begin(26, 32); // Initialize pin 26,32. 初始化26,32引脚 29 | qmp6988.init(); 30 | Serial.println(F("ENVIII Unit(SHT30 and QMP6988) test")); 31 | } 32 | 33 | void loop() { 34 | pressure = qmp6988.calcPressure(); 35 | if (sht30.get() == 0) { // Obtain the data of shT30. 获取sht30的数据 36 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 37 | // 将sht30获取到的温度存储 38 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 39 | // 将sht30获取到的湿度存储 40 | } else { 41 | tmp = 0, hum = 0; 42 | } 43 | Serial.printf( 44 | "Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n---\n", tmp, 45 | hum, pressure); 46 | delay(2000); 47 | } 48 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_step_motor.cpp: -------------------------------------------------------------------------------- 1 | #include "app_step_motor.h" 2 | 3 | const int MOTOR_A = 12; 4 | const int MOTOR_B = 15; 5 | const int MOTOR_C = 2; 6 | const int MOTOR_D = 13; 7 | 8 | uint8_t step_status = 0; 9 | 10 | void init_step_motor(void) 11 | { 12 | gpio_reset_pin(GPIO_NUM_12); 13 | gpio_reset_pin(GPIO_NUM_15); 14 | gpio_reset_pin(GPIO_NUM_2); 15 | gpio_reset_pin(GPIO_NUM_13); 16 | 17 | pinMode(MOTOR_A, OUTPUT); 18 | pinMode(MOTOR_B, OUTPUT); 19 | pinMode(MOTOR_C, OUTPUT); 20 | pinMode(MOTOR_D, OUTPUT); 21 | } 22 | 23 | void step_motor(void) { 24 | static uint32_t step_motor_delay = 0; 25 | 26 | if (step_motor_delay < millis()) { 27 | switch (step_status) 28 | { 29 | case 0: 30 | digitalWrite(MOTOR_A, HIGH); 31 | digitalWrite(MOTOR_B, HIGH); 32 | digitalWrite(MOTOR_C, LOW); 33 | digitalWrite(MOTOR_D, LOW); 34 | break; 35 | case 1: 36 | digitalWrite(MOTOR_A, LOW); 37 | digitalWrite(MOTOR_B, HIGH); 38 | digitalWrite(MOTOR_C, HIGH); 39 | digitalWrite(MOTOR_D, LOW); 40 | break; 41 | case 2: 42 | digitalWrite(MOTOR_A, LOW); 43 | digitalWrite(MOTOR_B, LOW); 44 | digitalWrite(MOTOR_C, HIGH); 45 | digitalWrite(MOTOR_D, HIGH); 46 | break; 47 | case 3: 48 | digitalWrite(MOTOR_A, HIGH); 49 | digitalWrite(MOTOR_B, LOW); 50 | digitalWrite(MOTOR_C, LOW); 51 | digitalWrite(MOTOR_D, HIGH); 52 | break; 53 | 54 | default: 55 | break; 56 | } 57 | step_status++; 58 | if (step_status > 3) step_status = 0; 59 | step_motor_delay = millis() + 2; 60 | } 61 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_BPS_M5Core2/Unit_BPS_M5Core2.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with M5Core2 sample source code 5 | * 配套 M5Core2 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/bps 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/bps 8 | * 9 | * Product: BPS_BMP280. 气压计 10 | * Date: 2022/8/18 11 | ******************************************** *********************************** 12 | Please connect to Port A(22、21),Read atmospheric pressure and temperature and 13 | display them on the display screen 14 | Libraries: 15 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 16 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | Adafruit_BMP280 bme; 23 | 24 | void setup() { 25 | M5.begin(); // Init M5Core2. 初始化 M5Core2 26 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 27 | while (!bme.begin( 28 | 0x76)) { // Init this sensor,True if the init was successful, otherwise 29 | // false. 初始化传感器,如果初始化成功返回1 30 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 31 | } 32 | M5.Lcd.clear(); // Clear the screen. 清屏 33 | } 34 | 35 | float pressure, 36 | Temp; // Store the vuale of pressure and Temperature. 存储压力和温度() 37 | 38 | void loop() { 39 | pressure = bme.readPressure(); 40 | Temp = bme.readTemperature(); 41 | M5.Lcd.setCursor(0, 0); //将光标设置在(0 ,0). Set the cursor to (0,0) 42 | M5.Lcd.setTextSize(3); //设置字体大小为3. Set the font size to 3 43 | M5.Lcd.printf("Pressure:%2.0fPa\nTemperature:%2.0f^C", pressure, Temp); 44 | delay(100); 45 | } 46 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_BPS_M5Core/Unit_BPS_M5Core.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with M5Core sample source code 5 | * 配套 M5Core 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/bps 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/bps 8 | * 9 | * Product: BPS_BMP280. 气压计 10 | * Date: 2022/8/18 11 | ******************************************** *********************************** 12 | Please connect to Port A(22、21),Read atmospheric pressure and temperature and 13 | display them on the display screen 14 | Libraries: 15 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 16 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | Adafruit_BMP280 bme; 23 | 24 | void setup() { 25 | M5.begin(); // Init M5Stack. 初始化M5Stack 26 | M5.Power.begin(); // Init power 初始化电源模块 27 | M5.Lcd.setTextSize(2); //设置字体大小为2. Set the font size to 2 28 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 29 | while (!bme.begin( 30 | 0x76)) { // Init this sensor,True if the init was successful, otherwise 31 | // false. 初始化传感器,如果初始化成功返回1 32 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 33 | } 34 | M5.Lcd.clear(); // Clear the screen. 清屏 35 | } 36 | 37 | float pressure, 38 | Temp; // Store the vuale of pressure and Temperature. 存储压力和温度 39 | 40 | void loop() { 41 | pressure = bme.readPressure(); 42 | Temp = bme.readTemperature(); 43 | M5.Lcd.setCursor(0, 0); //将光标设置在(0 ,0). Set the cursor to (0,0) 44 | M5.Lcd.printf("Pressure:%2.0fPa\nTemperature:%2.0f^C", pressure, Temp); 45 | delay(100); 46 | } 47 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_BPS_M5StickC/Unit_BPS_M5StickC.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickC sample source code 5 | * 配套 M5StickC 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/bps 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/bps 8 | * 9 | * Product: BPS_BMP280. 气压计 10 | * Date: 2022/8/18 11 | ******************************************** *********************************** 12 | Please connect to Port,Read atmospheric pressure and temperature and display 13 | them on the display screen 请连接端口,读取大气压强和温度并在显示屏上显示 14 | Libraries: 15 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 16 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 17 | */ 18 | #include 19 | #include 20 | 21 | Adafruit_BMP280 bme; 22 | 23 | void setup() { 24 | M5.begin(); // Init M5StickC. 初始化 M5StickC 25 | M5.Lcd.setRotation(3); // Rotate the screen. 旋转屏幕 26 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 27 | while (!bme.begin( 28 | 0x76)) { // Init this sensor,True if the init was successful, otherwise 29 | // false. 初始化传感器,如果初始化成功返回1 30 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 31 | } 32 | M5.Lcd.fillScreen(BLACK); // Clear the screen. 清屏 33 | } 34 | 35 | float pressure, 36 | Temp; // Store the vuale of pressure and Temperature. 存储压力和温度() 37 | 38 | void loop() { 39 | pressure = bme.readPressure(); 40 | Temp = bme.readTemperature(); 41 | M5.Lcd.setCursor(0, 0); //将光标设置在(0 ,0). Set the cursor to (0,0) 42 | M5.Lcd.setTextSize(2); //设置字体大小为2. Set the font size to 2 43 | M5.Lcd.printf("Pre:%2.0fPa\nTem:%2.0f^C", pressure, Temp); 44 | delay(100); 45 | } 46 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_BPS_M5StickCPlus/Unit_BPS_M5StickCPlus.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickCPlus sample source code 5 | * 配套 M5StickCPlus 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/bps 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/bps 8 | * 9 | * Product: BPS_BMP280. 气压计 10 | * Date: 2022/8/18 11 | ******************************************** *********************************** 12 | Please connect to Port,Read atmospheric pressure and temperature and display 13 | them on the display screen 请连接端口,读取大气压强和温度并在显示屏上显示 14 | Libraries: 15 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 16 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 17 | */ 18 | #include 19 | #include 20 | 21 | Adafruit_BMP280 bme; 22 | 23 | void setup() { 24 | M5.begin(); // Init M5StickCPlus. 初始化 M5StickCPlus 25 | M5.Lcd.setRotation(3); // Rotate the screen. 旋转屏幕 26 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 27 | while (!bme.begin( 28 | 0x76)) { // Init this sensor,True if the init was successful, otherwise 29 | // false. 初始化传感器,如果初始化成功返回1 30 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 31 | } 32 | M5.Lcd.fillScreen(BLACK); // Clear the screen. 清屏 33 | } 34 | 35 | float pressure, 36 | Temp; // Store the vuale of pressure and Temperature. 存储压力和温度() 37 | 38 | void loop() { 39 | pressure = bme.readPressure(); 40 | Temp = bme.readTemperature(); 41 | M5.Lcd.setCursor(0, 0); //将光标设置在(0 ,0). Set the cursor to (0,0) 42 | M5.Lcd.setTextSize(2); //设置字体大小为2. Set the font size to 2 43 | M5.Lcd.printf("Pre:%2.0fPa\nTem:%2.0f^C", pressure, Temp); 44 | delay(100); 45 | } 46 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVIII_M5StickC/Unit_ENVIII_M5StickC.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickC sample source code 5 | * 配套 M5StickC 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envIII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envIII 8 | * 9 | * Product: ENVIII_SHT30_QMP6988. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port,Read temperature, humidity and atmospheric pressure and 13 | display them on the display screen 14 | 请连接端口,读取温度、湿度和大气压强并在显示屏上显示 15 | */ 16 | #include 17 | #include "M5_ENV.h" 18 | 19 | SHT3X sht30; 20 | QMP6988 qmp6988; 21 | 22 | float tmp = 0.0; 23 | float hum = 0.0; 24 | float pressure = 0.0; 25 | 26 | void setup() { 27 | M5.begin(); // Init M5StickC. 初始化M5StickC 28 | M5.Lcd.setRotation(3); // Rotate the screen. 旋转屏幕 29 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 30 | qmp6988.init(); 31 | M5.lcd.println(F("ENVIII Unit(SHT30 and QMP6988) test")); 32 | } 33 | 34 | void loop() { 35 | pressure = qmp6988.calcPressure(); 36 | if (sht30.get() == 0) { // Obtain the data of shT30. 获取sht30的数据 37 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 38 | // 将sht30获取到的温度存储 39 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 40 | // 将sht30获取到的湿度存储 41 | } else { 42 | tmp = 0, hum = 0; 43 | } 44 | M5.lcd.fillRect(0, 20, 100, 60, 45 | BLACK); // Fill the screen with black (to clear the 46 | // screen). 将屏幕填充黑色(用来清屏) 47 | M5.lcd.setCursor(0, 20); 48 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 49 | tmp, hum, pressure); 50 | delay(2000); 51 | } 52 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVIII_M5Core2/Unit_ENVIII_M5Core2.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5Core2 sample source code 5 | * 配套 M5Core2 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envIII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envIII 8 | * 9 | * Product: ENVIII_SHT30_QMP6988. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port A,Read temperature, humidity and atmospheric pressure 13 | and display them on the display screen 14 | 请连接端口A,读取温度、湿度和大气压强并在显示屏上显示 15 | */ 16 | #include 17 | #include "UNIT_ENV.h" 18 | 19 | SHT3X sht30; 20 | QMP6988 qmp6988; 21 | 22 | float tmp = 0.0; 23 | float hum = 0.0; 24 | float pressure = 0.0; 25 | 26 | void setup() { 27 | M5.begin(); // Init M5Core2. 初始化M5Core2 28 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 29 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 30 | qmp6988.init(); 31 | M5.lcd.println(F("ENVIII Unit(SHT30 and QMP6988) test")); 32 | } 33 | 34 | void loop() { 35 | pressure = qmp6988.calcPressure(); 36 | if (sht30.get() == 0) { // Obtain the data of shT30. 获取sht30的数据 37 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 38 | // 将sht30获取到的温度存储 39 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 40 | // 将sht30获取到的湿度存储 41 | } else { 42 | tmp = 0, hum = 0; 43 | } 44 | M5.lcd.fillRect(0, 20, 100, 60, 45 | BLACK); // Fill the screen with black (to clear the 46 | // screen). 将屏幕填充黑色(用来清屏) 47 | M5.lcd.setCursor(0, 20); 48 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 49 | tmp, hum, pressure); 50 | delay(2000); 51 | } 52 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVIII_M5StickCPlus/Unit_ENVIII_M5StickCPlus.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickCPlus sample source code 5 | * 配套 M5StickCPlus 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envIII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envIII 8 | * 9 | * Product: ENVIII_SHT30_QMP6988. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port,Read temperature, humidity and atmospheric pressure and 13 | display them on the display screen 14 | 请连接端口,读取温度、湿度和大气压强并在显示屏上显示 15 | */ 16 | #include 17 | #include "M5_ENV.h" 18 | 19 | SHT3X sht30; 20 | QMP6988 qmp6988; 21 | 22 | float tmp = 0.0; 23 | float hum = 0.0; 24 | float pressure = 0.0; 25 | 26 | void setup() { 27 | M5.begin(); // Init M5StickCPlus. 初始化M5StickCPlus 28 | M5.Lcd.setRotation(3); // Rotate the screen. 旋转屏幕 29 | Wire.begin(32, 30 | 33); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 31 | qmp6988.init(); 32 | M5.lcd.println(F("ENVIII Unit(SHT30 and QMP6988) test")); 33 | } 34 | 35 | void loop() { 36 | pressure = qmp6988.calcPressure(); 37 | if (sht30.get() == 0) { // Obtain the data of shT30. 获取sht30的数据 38 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 39 | // 将sht30获取到的温度存储 40 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 41 | // 将sht30获取到的湿度存储 42 | } else { 43 | tmp = 0, hum = 0; 44 | } 45 | M5.lcd.fillRect(0, 20, 100, 60, 46 | BLACK); // Fill the screen with black (to clear the 47 | // screen). 将屏幕填充黑色(用来清屏) 48 | M5.lcd.setCursor(0, 20); 49 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 50 | tmp, hum, pressure); 51 | delay(2000); 52 | } 53 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVIII_M5Core/Unit_ENVIII_M5Core.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5Core sample source code 5 | * 配套 M5Core 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envIII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envIII 8 | * 9 | * Product: ENVIII_SHT30_QMP6988. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port A(22、21),Read temperature, humidity and atmospheric 13 | pressure and display them on the display screen 14 | 请连接端口A(22、21),读取温度、湿度和大气压强并在显示屏上显示 15 | */ 16 | #include 17 | #include "M5_ENV.h" 18 | 19 | SHT3X sht30; 20 | QMP6988 qmp6988; 21 | 22 | float tmp = 0.0; 23 | float hum = 0.0; 24 | float pressure = 0.0; 25 | 26 | void setup() { 27 | M5.begin(); // Init M5Stack. 初始化M5Stack 28 | M5.Power.begin(); // Init power 初始化电源模块 29 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 30 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 31 | qmp6988.init(); 32 | M5.lcd.println(F("ENVIII Unit(SHT30 and QMP6988) test")); 33 | } 34 | 35 | void loop() { 36 | pressure = qmp6988.calcPressure(); 37 | if (sht30.get() == 0) { // Obtain the data of shT30. 获取sht30的数据 38 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 39 | // 将sht30获取到的温度存储 40 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 41 | // 将sht30获取到的湿度存储 42 | } else { 43 | tmp = 0, hum = 0; 44 | } 45 | M5.lcd.fillRect(0, 20, 100, 60, 46 | BLACK); // Fill the screen with black (to clear the 47 | // screen). 将屏幕填充黑色(用来清屏) 48 | M5.lcd.setCursor(0, 20); 49 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 50 | tmp, hum, pressure); 51 | delay(2000); 52 | } 53 | -------------------------------------------------------------------------------- /examples/FactoryTest_V1.0/Readme.txt: -------------------------------------------------------------------------------- 1 | Note:PIN is under the current test project,which pin would to be connected 2 | When detecting the IIC interface, the portA port must be disconnected 3 | 4 | 1 joystick 5 | 6 | Normal x range 0 - 4000 7 | Normal x range 0 - 4000 8 | Normal button 0/1 9 | 10 | 2 DAC * 4 11 | 12 | Measure channel voltage with a multimeter 13 | ch0 1.25v 14 | ch1 2.50v 15 | ch2 3.75v 16 | ch3 5.00v 17 | 18 | 3 ADC * 4 19 | 20 | Connect a 3.3v or 5.0v power cable and connect to channels 1 to 4 respectively 21 | The data of channels 1 to 4 will change accordingly 22 | 3.3v - 17000 23 | 5.0v - 26000 24 | 25 | Waring: The left side of each channel is the ground wire, and the right side is the adc input pin. Do not short circuit 26 | 27 | 4 encoder 28 | 29 | Rotating code value left or right will increase or decrease accordingly 30 | Button will change by 0/1 when pressed 31 | 32 | 5 MATRIX 33 | The lights will flash red-green-blue 34 | 35 | 6 TEMPERATURE 36 | Display of temperature and humidity 37 | 38 | 7 air 39 | Display air pressure 40 | 41 | 8 luminosity 42 | Turn on the mobile phone flashlight, irradiate the components, you can see the obvious value change 43 | 44 | 9 microphone 45 | When speaking into a micropythone, you can see obvious numerical changes 46 | 47 | 10 keyboard 48 | Press the key to display the letters on the screen 49 | 50 | 11 dc-motor 51 | Motor rotates after wiring 52 | 53 | 12 relay 54 | Insert the wiring into ch0-ch7, the channel light will be on with relay closing sound 55 | 56 | 57 | 13 servo 58 | Servo rotation after wiring 59 | 60 | 14 STEPMOTOR 61 | Motor rotates after wiring 62 | 63 | 64 | 65 | 15 RS232 66 | After wiring, short the serial port (the second row, the second pin and the third pin are short-circuited), you can see that the display re_num data increases 67 | 68 | 69 | 16 RS485 70 | After wiring, short the serial port (the second row, the second pin and the third pin are short-circuited), you can see that the display re_num data increases 71 | 72 | 17 RFID 73 | After read card. Recognize the chip ID and print it to the screen. 74 | -------------------------------------------------------------------------------- /examples/FactoryTest_V1.1/Readme.txt: -------------------------------------------------------------------------------- 1 | Note:PIN is under the current test project,which pin would to be connected 2 | When detecting the IIC interface, the portA port must be disconnected 3 | 4 | 1 joystick 5 | 6 | Normal x range 0 - 4000 7 | Normal x range 0 - 4000 8 | Normal button 0/1 9 | 10 | 2 DAC * 4 11 | 12 | Measure channel voltage with a multimeter 13 | ch0 1.25v 14 | ch1 2.50v 15 | ch2 3.75v 16 | ch3 5.00v 17 | 18 | 3 ADC * 4 19 | 20 | Connect a 3.3v or 5.0v power cable and connect to channels 1 to 4 respectively 21 | The data of channels 1 to 4 will change accordingly 22 | 3.3v - 17000 23 | 5.0v - 26000 24 | 25 | Waring: The left side of each channel is the ground wire, and the right side is the adc input pin. Do not short circuit 26 | 27 | 4 encoder 28 | 29 | Rotating code value left or right will increase or decrease accordingly 30 | Button will change by 0/1 when pressed 31 | 32 | 5 MATRIX 33 | The lights will flash red-green-blue 34 | 35 | 6 TEMPERATURE 36 | Display of temperature and humidity 37 | 38 | 7 air 39 | Display air pressure 40 | 41 | 8 luminosity 42 | Turn on the mobile phone flashlight, irradiate the components, you can see the obvious value change 43 | 44 | 9 microphone 45 | When speaking into a micropythone, you can see obvious numerical changes 46 | 47 | 10 keyboard 48 | Press the key to display the letters on the screen 49 | 50 | 11 dc-motor 51 | Motor rotates after wiring 52 | 53 | 12 relay 54 | Insert the wiring into ch0-ch7, the channel light will be on with relay closing sound 55 | 56 | 57 | 13 servo 58 | Servo rotation after wiring 59 | 60 | 14 STEPMOTOR 61 | Motor rotates after wiring 62 | 63 | 64 | 65 | 15 RS232 66 | After wiring, short the serial port (the second row, the second pin and the third pin are short-circuited), you can see that the display re_num data increases 67 | 68 | 69 | 16 RS485 70 | After wiring, short the serial port (the second row, the second pin and the third pin are short-circuited), you can see that the display re_num data increases 71 | 72 | 17 RFID 73 | After read card. Recognize the chip ID and print it to the screen. 74 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENV_M5Atom/Unit_ENV_M5Atom.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with Atom-Lite/Matrix sample source code 5 | * 配套 Atom-Lite/Matrix 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/core/atom_lite 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/atom_lite 8 | * 9 | * Describe: ENV_DH12_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port A,Read temperature, humidity and atmospheric pressure 13 | and display them on the display Serial 14 | 请连接端口A,读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include "Adafruit_Sensor.h" 23 | #include 24 | 25 | DHT12 dht12; 26 | Adafruit_BMP280 bme; 27 | 28 | void setup() { 29 | M5.begin(true, false, true); // Init M5Atom. 初始化M5Atom 30 | M5.dis.fillpix(0xFFFFE0); 31 | Wire.begin(26, 32); // Initialize pin 26,32. 初始化26,32引脚 32 | Serial.println(F("ENV Unit(DHT12 and BMP280) test")); 33 | } 34 | 35 | void loop() { 36 | while (!bme.begin(0x76)) { 37 | Serial.println("Could not find a valid BMP280 sensor, check wiring!"); 38 | M5.dis.fillpix(0xff0000); 39 | } 40 | M5.dis.fillpix(0x00ff00); 41 | float tmp = dht12.readTemperature(); // Store the temperature obtained from 42 | // dht12. 将dht12获取到的温度存储 43 | float hum = dht12.readHumidity(); // Store the humidity obtained from the 44 | // dht12. 将dht12获取到的湿度存储 45 | float pressure = bme.readPressure(); // Stores the pressure gained by BMP. 46 | // 存储bmp获取到的压强 47 | Serial.printf( 48 | "Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n------\n", tmp, 49 | hum, pressure); 50 | 51 | delay(2000); 52 | } 53 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVII_M5Atom/Unit_ENVII_M5Atom.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with Atom-Lite/Matrix sample source code 5 | * 配套 Atom-Lite/Matrix 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envII 7 | * 获取更多资料请访问:https://docs.m5stack.com/zh_CN/unit/envII 8 | * 9 | * Product: ENVII_SHT30_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port,Read temperature, humidity and atmospheric pressure and 13 | display them on the display Serial 14 | 请连接端口,读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include "Adafruit_Sensor.h" 23 | #include 24 | 25 | SHT3X sht30; 26 | Adafruit_BMP280 bme; 27 | 28 | float tmp = 0.0; 29 | float hum = 0.0; 30 | float pressure = 0.0; 31 | 32 | void setup() { 33 | M5.begin(true, false, true); // Init M5Atom. 初始化 M5Atom 34 | M5.dis.fillpix(0xFFFFE0); 35 | Wire.begin(26, 32); // Initialize pin 26,32. 初始化26,32引脚 36 | Serial.println(F("ENV Unit(SHT30 and BMP280) test...\n")); 37 | } 38 | 39 | void loop() { 40 | while (!bme.begin(0x76)) { //初始化bme传感器. Init the sensor of bme 41 | Serial.println("Could not find a valid BMP280 sensor, check wiring!"); 42 | M5.dis.fillpix(0xff0000); 43 | } 44 | M5.dis.fillpix(0x00ff00); 45 | pressure = bme.readPressure(); // Stores the pressure gained by BMP. 46 | // 存储bmp获取到的压强 47 | sht30.get(); // Obtain the data of shT30. 获取sht30的数据 48 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 49 | // 将sht30获取到的温度存储 50 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 51 | // 将sht30获取到的湿度存储 52 | Serial.printf("Temp: %2.1f\nHumi: %2.0f%%\nPressure:%2.0fPa\n\n", tmp, hum, 53 | pressure); 54 | delay(2000); 55 | } 56 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "M5Unified.h" 3 | #include 4 | #include "main.h" 5 | #include "app_test_page.h" 6 | 7 | void setup() { 8 | M5.begin(); 9 | 10 | M5.Display.setEpdMode(epd_mode_t::epd_fastest); // fastest but very-low quality. 11 | if (M5.Display.width() < M5.Display.height()) 12 | { /// Landscape mode. 13 | M5.Display.setRotation(M5.Display.getRotation() ^ 1); 14 | } 15 | M5.Display.setFont(&fonts::efontCN_12); 16 | M5.Display.clear(WHITE); 17 | 18 | M5.Log.setLogLevel(m5::log_target_serial, ESP_LOG_DEBUG); 19 | M5.Log.setEnableColor(m5::log_target_serial, true); 20 | M5.Log.setSuffix(m5::log_target_serial, "\n"); 21 | 22 | M5.Ex_I2C.begin(I2C_PORT, I2C_SDA, I2C_SCL); 23 | init_dac_test(); 24 | } 25 | 26 | void loop() { 27 | switch (page_status) 28 | { 29 | case PAGE_DAC: 30 | page_dac_test(); 31 | break; 32 | case PAGE_ADC: 33 | page_adc_test(); 34 | break; 35 | case PAGE_RFID: 36 | page_rfid_test(); 37 | break; 38 | case PAGE_TEMP: 39 | page_temp_test(); 40 | break; 41 | case PAGE_AIR_PRESSURE: 42 | page_pressure_test(); 43 | break; 44 | case PAGE_LUMINOSITY: 45 | page_luminosity_test(); 46 | break; 47 | case PAGE_MICROPHONE: 48 | page_microphone_test(); 49 | break; 50 | case PAGE_RS485: 51 | page_rs485_test(); 52 | break; 53 | case PAGE_RS232: 54 | page_rs232_test(); 55 | break; 56 | case PAGE_RELAY: 57 | page_relay_test(); 58 | break; 59 | case PAGE_RGB: 60 | page_rgb_test(); 61 | break; 62 | case PAGE_SERVO: 63 | page_servo_test(); 64 | break; 65 | case PAGE_JOYSTICK: 66 | page_joystick_test(); 67 | break; 68 | case PAGE_ENCODER: 69 | page_encoder_test(); 70 | break; 71 | case PAGE_DC_MOTOR: 72 | page_dcmotor_test(); 73 | break; 74 | case PAGE_STEP_MOTOR: 75 | page_stepmotor_test(); 76 | break; 77 | case PAGE_KEYBOARD: 78 | page_keyboard_test(); 79 | break; 80 | 81 | default: 82 | break; 83 | } 84 | } 85 | 86 | bool i2c_check(uint8_t addr) 87 | { 88 | m5gfx::i2c::beginTransaction(I2C_PORT, addr, 100000); 89 | if (m5gfx::i2c::endTransaction(I2C_PORT).error() == 0) 90 | { 91 | return true; 92 | } 93 | else { 94 | return false; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENV_M5Core2/Unit_ENV_M5Core2.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5Core2 sample source code 5 | * 配套 M5Core2 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/core/core2 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/core2 8 | * 9 | * Product: ENV_DH12_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port A,Read temperature, humidity and atmospheric pressure 13 | and display them on the display screen 14 | 请连接端口A,读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include "Adafruit_Sensor.h" 23 | #include 24 | 25 | DHT12 dht12; 26 | Adafruit_BMP280 bme; 27 | 28 | void setup() { 29 | M5.begin(true, false, true, // Init M5Core2 and adding the I2C bus. 30 | true); // 初始化 M5Core2 并加入i2c总线 31 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 32 | M5.Lcd.println(F("ENV Unit(DHT12 and BMP280) test")); 33 | } 34 | 35 | void loop() { 36 | while (!bme.begin(0x76)) { 37 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 38 | } 39 | float tmp = dht12.readTemperature(); // Store the temperature obtained from 40 | // dht12. 将dht12获取到的温度存储 41 | float hum = dht12.readHumidity(); // Store the humidity obtained from the 42 | // dht12. 将dht12获取到的湿度存储 43 | float pressure = bme.readPressure(); // Stores the pressure gained by BMP. 44 | // 存储bmp获取到的压强 45 | M5.lcd.fillRect(0, 20, 100, 60, 46 | BLACK); // Fill the screen with black (to clear the 47 | // screen). 将屏幕填充黑色(用来清屏) 48 | M5.lcd.setCursor(0, 20); 49 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 50 | tmp, hum, pressure); 51 | 52 | delay(2000); 53 | } 54 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENV_M5StickC/Unit_ENV_M5StickC.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickC sample source code 5 | * 配套 M5StickC 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/core/m5stickc 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/m5stickc 8 | * 9 | * Product: ENI_DH12_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port,Read temperature, humidity and atmospheric pressure and 13 | display them on the display screen 14 | 请连接端口,读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include 23 | #include "Adafruit_Sensor.h" 24 | 25 | DHT12 dht12; 26 | Adafruit_BMP280 bme; 27 | 28 | void setup() { 29 | M5.begin(); // Init M5StickC. 初始化M5StickC 30 | M5.Lcd.setRotation(3); // Rotate the screen. 旋转屏幕 31 | Wire.begin(32, 32 | 33); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 33 | M5.Lcd.println(F("ENV Unit(DHT12 and BMP280) test")); 34 | } 35 | 36 | void loop() { 37 | while (!bme.begin(0x76)) { 38 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 39 | } 40 | float tmp = dht12.readTemperature(); // Store the temperature obtained from 41 | // dht12. 将dht12获取到的温度存储 42 | float hum = dht12.readHumidity(); // Store the humidity obtained from the 43 | // dht12. 将dht12获取到的湿度存储 44 | float pressure = bme.readPressure(); // Stores the pressure gained by BMP. 45 | // 存储bmp获取到的压强 46 | M5.lcd.fillRect(0, 10, 160, 80, 47 | BLACK); // Fill the screen with black (to clear the 48 | // screen). 将屏幕填充黑色(用来清屏) 49 | M5.lcd.setCursor(0, 10); 50 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 51 | tmp, hum, pressure); 52 | 53 | delay(2000); 54 | } 55 | -------------------------------------------------------------------------------- /examples/RFID/RFID.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MFRC522_I2C.h" 3 | #include 4 | 5 | // 0x28 is i2c address on SDA. Check your address with i2cscanner if not match. 6 | MFRC522 mfrc522(0x28); // Create MFRC522 instance. 7 | 8 | void setup() { 9 | //M5.Lcd.begin(); 10 | //M5.Lcd.fillScreen( BLACK ); 11 | // M5.Lcd.setCursor(0, 0); 12 | // M5.Lcd.setTextColor(YELLOW); 13 | //M5.Lcd.setTextSize(2); 14 | 15 | // M5.Lcd.fillScreen( BLACK ); 16 | //M5.Lcd.setCursor(0, 0); 17 | 18 | //Serial.begin(115200); // Initialize serial communications with the PC 19 | // Initialize I2C 20 | M5.begin(); 21 | Wire.begin(); 22 | M5.Lcd.setCursor(140, 0, 4); 23 | M5.Lcd.println("RFID"); 24 | mfrc522.PCD_Init(); // Init MFRC522 25 | ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details 26 | Serial.println(F("Scan PICC to see UID, type, and data blocks...")); 27 | M5.Lcd.setCursor(0,30,2); 28 | M5.Lcd.println("Scan PICC to see UID, type, and data blocks..."); 29 | } 30 | 31 | void loop() { 32 | // Look for new cards, and select one if present 33 | if (!mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial()) { 34 | delay(50); 35 | return; 36 | } 37 | 38 | // Now a card is selected. The UID and SAK is in mfrc522.uid. 39 | 40 | // Dump UID 41 | Serial.print(F("Card UID:")); 42 | M5.Lcd.println(" "); 43 | for (byte i = 0; i < mfrc522.uid.size; i++) { 44 | Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 45 | Serial.print(mfrc522.uid.uidByte[i], HEX); 46 | M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 47 | M5.Lcd.print(mfrc522.uid.uidByte[i], HEX); 48 | } 49 | Serial.println(); 50 | 51 | } 52 | 53 | void ShowReaderDetails() { 54 | // Get the MFRC522 software version 55 | byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); 56 | Serial.print(F("MFRC522 Software Version: 0x")); 57 | Serial.print(v, HEX); 58 | if (v == 0x91) 59 | Serial.print(F(" = v1.0")); 60 | else if (v == 0x92) 61 | Serial.print(F(" = v2.0")); 62 | else 63 | Serial.print(F(" (unknown)")); 64 | Serial.println(""); 65 | // When 0x00 or 0xFF is returned, communication probably failed 66 | if((v == 0x00) || (v == 0xFF)) { 67 | Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENV_M5StickCPlus/Unit_ENV_M5StickCPlus.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickCPlus sample source code 5 | * 配套 M5StickCPlus 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/core/m5stickc 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/m5stickc 8 | * 9 | * Product: ENI_DH12_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port,Read temperature, humidity and atmospheric pressure and 13 | display them on the display screen 14 | 请连接端口,读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include 23 | #include "Adafruit_Sensor.h" 24 | 25 | DHT12 dht12; 26 | Adafruit_BMP280 bme; 27 | 28 | void setup() { 29 | M5.begin(); // Init M5StickCPlus. 初始化M5StickCPlus 30 | M5.Lcd.setRotation(3); // Rotate the screen. 旋转屏幕 31 | Wire.begin(32, 32 | 33); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线 33 | M5.Lcd.println(F("ENV Unit(DHT12 and BMP280) test")); 34 | } 35 | 36 | void loop() { 37 | while (!bme.begin(0x76)) { 38 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 39 | } 40 | float tmp = dht12.readTemperature(); // Store the temperature obtained from 41 | // dht12. 将dht12获取到的温度存储 42 | float hum = dht12.readHumidity(); // Store the humidity obtained from the 43 | // dht12. 将dht12获取到的湿度存储 44 | float pressure = bme.readPressure(); // Stores the pressure gained by BMP. 45 | // 存储bmp获取到的压强 46 | M5.lcd.fillRect(0, 10, 160, 80, 47 | BLACK); // Fill the screen with black (to clear the 48 | // screen). 将屏幕填充黑色(用来清屏) 49 | M5.lcd.setCursor(0, 10); 50 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 51 | tmp, hum, pressure); 52 | 53 | delay(2000); 54 | } 55 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENV_M5Core/Unit_ENV_M5Core.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with M5Core sample source code 5 | * 配套 M5Core 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/core/gray 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray 8 | * 9 | * Describe: ENV_DH12_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port A(22、21),Read temperature, humidity and atmospheric 13 | pressure and display them on the display screen 14 | 请连接端口A(22、21),读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | #include 20 | #include "M5_ENV.h" 21 | #include 22 | #include "Adafruit_Sensor.h" 23 | 24 | DHT12 dht12; 25 | Adafruit_BMP280 bme; 26 | 27 | void setup() { 28 | M5.begin(true, false, true, // Init M5Stack and adding the I2C bus. 29 | true); // 初始化M5Stack并加入i2c总线 30 | M5.Power.begin(); // Init power 初始化电源模块 31 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 32 | M5.Lcd.println(F("ENV Unit(DHT12 and BMP280) test")); 33 | } 34 | 35 | void loop() { 36 | while (!bme.begin(0x76)) { 37 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 38 | } 39 | float tmp = dht12.readTemperature(); // Store the temperature obtained from 40 | // dht12. 将dht12获取到的温度存储 41 | float hum = dht12.readHumidity(); // Store the humidity obtained from the 42 | // dht12. 将dht12获取到的湿度存储 43 | float pressure = bme.readPressure(); // Stores the pressure gained by BMP. 44 | // 存储bmp获取到的压强 45 | M5.lcd.fillRect(0, 20, 100, 60, 46 | BLACK); // Fill the screen with black (to clear the 47 | // screen). 将屏幕填充黑色(用来清屏) 48 | M5.lcd.setCursor(0, 20); 49 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 50 | tmp, hum, pressure); 51 | 52 | delay(2000); 53 | } 54 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_encoder.cpp: -------------------------------------------------------------------------------- 1 | #include "app_encoder.h" 2 | 3 | #define ROTARY_ENCODER_A_PIN 13 4 | #define ROTARY_ENCODER_B_PIN 2 5 | #define ROTARY_ENCODER_BUTTON_PIN 15 6 | #define ROTARY_ENCODER_NO_PIN -1 7 | 8 | #define ROTARY_ENCODER_VCC_PIN -1 9 | #define ROTARY_ENCODER_STEPS 2 10 | 11 | static uint8_t button_value = 0; 12 | static uint32_t encoder_value = 0; 13 | 14 | AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, 15 | ROTARY_ENCODER_NO_PIN, 16 | ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS); 17 | 18 | static void rotary_onButtonClick() 19 | { 20 | static unsigned long lastTimePressed = 0; 21 | //ignore multiple press in that time milliseconds 22 | if (millis() - lastTimePressed < 500) 23 | { 24 | return; 25 | } 26 | lastTimePressed = millis(); 27 | Serial.print("button pressed "); 28 | Serial.print(millis()); 29 | Serial.println(" milliseconds after restart"); 30 | } 31 | 32 | static void rotary_loop(uint32_t *encoder, uint8_t *button, bool with_button) 33 | { 34 | static uint8_t button_data; 35 | //dont print anything unless value changed 36 | if (rotaryEncoder.encoderChanged()) 37 | { 38 | encoder_value = rotaryEncoder.readEncoder(); 39 | Serial.print("Value: "); 40 | Serial.println(encoder_value); 41 | *encoder = encoder_value; 42 | } 43 | if (with_button) { 44 | button_data = digitalRead(ROTARY_ENCODER_BUTTON_PIN); 45 | *button = button_data; 46 | if (rotaryEncoder.isEncoderButtonClicked()) 47 | { 48 | rotary_onButtonClick(); 49 | } 50 | } 51 | } 52 | 53 | void IRAM_ATTR readEncoderISR() 54 | { 55 | rotaryEncoder.readEncoder_ISR(); 56 | } 57 | 58 | void init_encoder(bool with_button) 59 | { 60 | gpio_reset_pin(GPIO_NUM_2); 61 | gpio_reset_pin(GPIO_NUM_13); 62 | if (with_button) { 63 | gpio_reset_pin(GPIO_NUM_15); 64 | pinMode(ROTARY_ENCODER_BUTTON_PIN, INPUT_PULLUP); 65 | } 66 | rotaryEncoder.begin(); 67 | rotaryEncoder.setup(readEncoderISR); 68 | bool circleValues = false; 69 | rotaryEncoder.setBoundaries(-65536, 65536, circleValues); 70 | rotaryEncoder.setAcceleration(1); 71 | } 72 | 73 | void get_encoder(uint32_t *encoder, uint8_t *button, bool with_button) 74 | { 75 | static uint32_t encoder_delay = 0; 76 | 77 | if (encoder_delay < millis()) { 78 | rotary_loop(encoder, button, with_button); 79 | encoder_delay = millis() + 50; 80 | } 81 | } 82 | 83 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVII_M5StickC/Unit_ENVII_M5StickC.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickC sample source code 5 | * 配套 M5StickC 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envII 8 | * 9 | * Product: ENVII_SHT30_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port,Read temperature, humidity and atmospheric pressure and 13 | display them on the display screen 14 | 请连接端口,读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include 23 | #include "Adafruit_Sensor.h" 24 | 25 | SHT3X sht30; 26 | Adafruit_BMP280 bme; 27 | 28 | float tmp = 0.0; 29 | float hum = 0.0; 30 | float pressure = 0.0; 31 | 32 | void setup() { 33 | M5.begin(); // Init M5StickC. 初始化 M5StickC 34 | M5.Lcd.setRotation(3); // Rotate the screen. 旋转屏幕 35 | M5.Lcd.println(F("ENVII Unit(SHT30 and BMP280) test...\n")); 36 | Wire.begin(32, 33); // Initialize pin 32,33. 初始化32,33引脚 37 | } 38 | 39 | void loop() { 40 | while (!bme.begin(0x76)) { //初始化bme传感器. Init the sensor of bme 41 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 42 | } 43 | pressure = bme.readPressure(); // Stores the pressure gained by BMP. 44 | // 存储bmp获取到的压强 45 | sht30.get(); // Obtain the data of shT30. 获取sht30的数据 46 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 47 | // 将sht30获取到的温度存储 48 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 49 | // 将sht30获取到的湿度存储 50 | M5.Lcd.setCursor(0, 20); 51 | M5.Lcd.fillRect(0, 20, 100, 60, 52 | BLACK); // Fill the screen with black (to clear the 53 | // screen). 将屏幕填充满黑色(用来清屏) 54 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 55 | tmp, hum, pressure); 56 | delay(2000); 57 | } 58 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVII_M5Core2/Unit_ENVII_M5Core2.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5Core2 sample source code 5 | * 配套 M5Core2 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envII 8 | * 9 | * Product: ENVII_SHT30_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port A,Read temperature, humidity and atmospheric pressure 13 | and display them on the display screen 14 | 请连接端口A,读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include "Adafruit_Sensor.h" 23 | #include 24 | 25 | SHT3X sht30; 26 | Adafruit_BMP280 bme; 27 | 28 | float tmp = 0.0; 29 | float hum = 0.0; 30 | float pressure = 0.0; 31 | 32 | void setup() { 33 | M5.begin(true, false, true, // Init M5Core2 and adding the I2C bus. 34 | true); // 初始化 M5Core2 并加入i2c总线 35 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 36 | M5.lcd.println(F("ENVII Unit(SHT30 and BMP280) test...\n")); 37 | } 38 | 39 | void loop() { 40 | while (!bme.begin(0x76)) { //初始化bme传感器. Init the sensor of bme 41 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 42 | } 43 | pressure = bme.readPressure(); // Stores the pressure gained by BMP. 44 | // 存储bmp获取到的压强 45 | sht30.get(); // Obtain the data of shT30. 获取sht30的数据 46 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 47 | // 将sht30获取到的温度存储 48 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 49 | // 将sht30获取到的湿度存储 50 | M5.lcd.setCursor(0, 50); 51 | M5.lcd.fillRect(0, 50, 100, 60, 52 | BLACK); // Fill the screen with black (to clear the 53 | // screen). 将屏幕填充满黑色(用来清屏) 54 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 55 | tmp, hum, pressure); 56 | delay(2000); 57 | } 58 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVII_M5StickCPlus/Unit_ENVII_M5StickCPlus.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickCPlus sample source code 5 | * 配套 M5StickCPlus 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envII 8 | * 9 | * Product: ENVII_SHT30_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port,Read temperature, humidity and atmospheric pressure and 13 | display them on the display screen 14 | 请连接端口,读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include 23 | #include "Adafruit_Sensor.h" 24 | 25 | SHT3X sht30; 26 | Adafruit_BMP280 bme; 27 | 28 | float tmp = 0.0; 29 | float hum = 0.0; 30 | float pressure = 0.0; 31 | 32 | void setup() { 33 | M5.begin(); // Init M5StickCPlus. 初始化 M5StickCPlus 34 | M5.Lcd.setRotation(3); // Rotate the screen. 旋转屏幕 35 | M5.Lcd.println(F("ENVII Unit(SHT30 and BMP280) test...\n")); 36 | Wire.begin(32, 33); // Initialize pin 32,33. 初始化32,33引脚 37 | } 38 | 39 | void loop() { 40 | while (!bme.begin(0x76)) { //初始化bme传感器. Init the sensor of bme 41 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 42 | } 43 | pressure = bme.readPressure(); // Stores the pressure gained by BMP. 44 | // 存储bmp获取到的压强 45 | sht30.get(); // Obtain the data of shT30. 获取sht30的数据 46 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 47 | // 将sht30获取到的温度存储 48 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 49 | // 将sht30获取到的湿度存储 50 | M5.Lcd.setCursor(0, 20); 51 | M5.Lcd.fillRect(0, 20, 100, 60, 52 | BLACK); // Fill the screen with black (to clear the 53 | // screen). 将屏幕填充满黑色(用来清屏) 54 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 55 | tmp, hum, pressure); 56 | delay(2000); 57 | } 58 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_ENVII_M5Core/Unit_ENVII_M5Core.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with M5Core sample source code 5 | * 配套 M5Core 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/envII 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/envII 8 | * 9 | * Describe: ENVII_SHT30_BMP280. 环境传感器 10 | * Date: 2022/7/20 11 | ******************************************************************************* 12 | Please connect to Port A(22、21),Read temperature, humidity and atmospheric 13 | pressure and display them on the display screen 14 | 请连接端口A(22、21),读取温度、湿度和大气压强并在显示屏上显示 15 | Libraries: 16 | - [Adafruit_BMP280](https://github.com/adafruit/Adafruit_BMP280_Library) 17 | - [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor) 18 | */ 19 | 20 | #include 21 | #include "M5_ENV.h" 22 | #include 23 | #include "Adafruit_Sensor.h" 24 | 25 | Adafruit_BMP280 bme; 26 | SHT3X sht30; 27 | 28 | float tmp = 0.0; 29 | float hum = 0.0; 30 | float pressure = 0.0; 31 | 32 | void setup() { 33 | M5.begin(true, false, true, // Init M5Stack and adding the I2C bus. 34 | true); // 初始化M5Stack并加入i2c总线 35 | M5.Power.begin(); // Init power 初始化电源模块 36 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 37 | M5.lcd.println(F("ENVII Unit(SHT30 and BMP280) test...\n")); 38 | } 39 | 40 | void loop() { 41 | while (!bme.begin(0x76)) { //初始化bme传感器. Init the sensor of bme 42 | M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); 43 | } 44 | pressure = bme.readPressure(); // Stores the pressure gained by BMP. 45 | // 存储bmp获取到的压强 46 | sht30.get(); // Obtain the data of shT30. 获取sht30的数据 47 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 48 | // 将sht30获取到的温度存储 49 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 50 | // 将sht30获取到的湿度存储 51 | M5.lcd.fillRect(0, 20, 100, 60, 52 | BLACK); // Fill the screen with black (to clear the 53 | // screen). 将屏幕填充黑色(用来清屏) 54 | M5.lcd.setCursor(0, 20); 55 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 56 | tmp, hum, pressure); 57 | delay(2000); 58 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_CO2_M5Core/Unit_CO2_M5Core.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with M5Stack sample source code 5 | * 配套 M5Stack 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/co2 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/co2 8 | * 9 | * Product: CO2. 二氧化碳 10 | * Date: 2022/9/1 11 | ******************************************** *********************************** 12 | Please connect Unit to the port, Read CO2 content, temperature, humidity and 13 | display them on the display screen 14 | 请Unit连接至端口,读取二氧化碳含量,温度,适度,并显示在屏幕上 15 | Libraries: 16 | - [Sensirion I2C SCD4x](https://github.com/Sensirion/arduino-i2c-scd4x)] 17 | - [Sensirion Core](https://github.com/Sensirion/arduino-core) 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | SensirionI2CScd4x scd4x; 24 | 25 | void setup() { 26 | M5.begin(); 27 | M5.Power.begin(); 28 | M5.Lcd.setTextFont(4); 29 | M5.Lcd.drawString("Unit CO2", 110, 0); 30 | uint16_t error; 31 | char errorMessage[256]; 32 | 33 | scd4x.begin(Wire); 34 | 35 | // stop potentially previously started measurement 36 | error = scd4x.stopPeriodicMeasurement(); 37 | if (error) { 38 | Serial.print("Error trying to execute stopPeriodicMeasurement(): "); 39 | errorToString(error, errorMessage, 256); 40 | Serial.println(errorMessage); 41 | } 42 | 43 | // Start Measurement 44 | error = scd4x.startPeriodicMeasurement(); 45 | if (error) { 46 | Serial.print("Error trying to execute startPeriodicMeasurement(): "); 47 | errorToString(error, errorMessage, 256); 48 | Serial.println(errorMessage); 49 | } 50 | 51 | Serial.println("Waiting for first measurement... (5 sec)"); 52 | } 53 | 54 | void loop() { 55 | uint16_t error; 56 | char errorMessage[256]; 57 | 58 | delay(100); 59 | 60 | // Read Measurement 61 | uint16_t co2 = 0; 62 | float temperature = 0.0f; 63 | float humidity = 0.0f; 64 | bool isDataReady = false; 65 | error = scd4x.getDataReadyFlag(isDataReady); 66 | if (error) { 67 | M5.Lcd.print("Error trying to execute readMeasurement(): "); 68 | errorToString(error, errorMessage, 256); 69 | Serial.println(errorMessage); 70 | return; 71 | } 72 | if (!isDataReady) { 73 | return; 74 | } 75 | error = scd4x.readMeasurement(co2, temperature, humidity); 76 | if (error) { 77 | M5.Lcd.print("Error trying to execute readMeasurement(): "); 78 | errorToString(error, errorMessage, 256); 79 | Serial.println(errorMessage); 80 | } else if (co2 == 0) { 81 | M5.Lcd.println("Invalid sample detected, skipping."); 82 | } else { 83 | M5.Lcd.setCursor(0, 40); 84 | M5.Lcd.printf("Co2:%d\n", co2); 85 | M5.Lcd.printf("Temperature:%f\n", temperature); 86 | M5.Lcd.printf("Humidity:%f\n", humidity); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_CO2_M5StickC/Unit_CO2_M5StickC.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with M5StickC sample source code 5 | * 配套 M5StickC 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/co2 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/co2 8 | * 9 | * Product: CO2. 二氧化碳 10 | * Date: 2022/9/1 11 | ******************************************************************************* 12 | Please connect Unit to the port, Read CO2 content, temperature, humidity and 13 | display them on the display screen 14 | 请Unit连接至端口,读取二氧化碳含量,温度,适度,并显示在屏幕上 15 | Libraries: 16 | - [Sensirion I2C SCD4x](https://github.com/Sensirion/arduino-i2c-scd4x)] 17 | - [Sensirion Core](https://github.com/Sensirion/arduino-core) 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | SensirionI2CScd4x scd4x; 24 | 25 | void setup() { 26 | M5.begin(); 27 | M5.Lcd.setRotation(3); 28 | M5.Lcd.drawString("Unit CO2", 50, 0); 29 | uint16_t error; 30 | char errorMessage[256]; 31 | Wire.begin(32, 33); 32 | scd4x.begin(Wire); 33 | 34 | // stop potentially previously started measurement 35 | error = scd4x.stopPeriodicMeasurement(); 36 | if (error) { 37 | Serial.print("Error trying to execute stopPeriodicMeasurement(): "); 38 | errorToString(error, errorMessage, 256); 39 | Serial.println(errorMessage); 40 | } 41 | 42 | // Start Measurement 43 | error = scd4x.startPeriodicMeasurement(); 44 | if (error) { 45 | Serial.print("Error trying to execute startPeriodicMeasurement(): "); 46 | errorToString(error, errorMessage, 256); 47 | Serial.println(errorMessage); 48 | } 49 | 50 | Serial.println("Waiting for first measurement... (5 sec)"); 51 | } 52 | 53 | void loop() { 54 | uint16_t error; 55 | char errorMessage[256]; 56 | 57 | delay(100); 58 | 59 | // Read Measurement 60 | uint16_t co2 = 0; 61 | float temperature = 0.0f; 62 | float humidity = 0.0f; 63 | bool isDataReady = false; 64 | error = scd4x.getDataReadyFlag(isDataReady); 65 | if (error) { 66 | M5.Lcd.println("Error trying to execute readMeasurement(): "); 67 | errorToString(error, errorMessage, 256); 68 | Serial.println(errorMessage); 69 | return; 70 | } 71 | if (!isDataReady) { 72 | return; 73 | } 74 | error = scd4x.readMeasurement(co2, temperature, humidity); 75 | if (error) { 76 | M5.Lcd.println("Error trying to execute readMeasurement(): "); 77 | errorToString(error, errorMessage, 256); 78 | Serial.println(errorMessage); 79 | } else if (co2 == 0) { 80 | M5.Lcd.println("Invalid sample detected, skipping."); 81 | } else { 82 | M5.Lcd.setCursor(0, 25); 83 | M5.Lcd.printf("Co2:%d\n", co2); 84 | M5.Lcd.printf("Temperature:%f\n", temperature); 85 | M5.Lcd.printf("Humidity:%f\n", humidity); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_CO2_M5StickCPlus/Unit_CO2_M5StickCPlus.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with M5StickCPlus sample source code 5 | * 配套 M5StickCPlus 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/co2 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/co2 8 | * 9 | * Product: CO2. 二氧化碳 10 | * Date: 2022/9/1 11 | ******************************************************************************* 12 | Please connect Unit to the port, Read CO2 content, temperature, humidity and 13 | display them on the display screen 14 | 请Unit连接至端口,读取二氧化碳含量,温度,适度,并显示在屏幕上 15 | Libraries: 16 | - [Sensirion I2C SCD4x](https://github.com/Sensirion/arduino-i2c-scd4x)] 17 | - [Sensirion Core](https://github.com/Sensirion/arduino-core) 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | SensirionI2CScd4x scd4x; 24 | 25 | void setup() { 26 | M5.begin(); 27 | M5.Lcd.setRotation(3); 28 | M5.Lcd.setTextFont(2); 29 | M5.Lcd.drawString("Unit CO2", 50, 0); 30 | uint16_t error; 31 | char errorMessage[256]; 32 | Wire.begin(32, 33); 33 | scd4x.begin(Wire); 34 | 35 | // stop potentially previously started measurement 36 | error = scd4x.stopPeriodicMeasurement(); 37 | if (error) { 38 | Serial.println("Error trying to execute stopPeriodicMeasurement(): "); 39 | errorToString(error, errorMessage, 256); 40 | Serial.println(errorMessage); 41 | } 42 | 43 | // Start Measurement 44 | error = scd4x.startPeriodicMeasurement(); 45 | if (error) { 46 | Serial.println("Error trying to execute startPeriodicMeasurement(): "); 47 | errorToString(error, errorMessage, 256); 48 | Serial.println(errorMessage); 49 | } 50 | 51 | Serial.println("Waiting for first measurement... (5 sec)"); 52 | } 53 | 54 | void loop() { 55 | uint16_t error; 56 | char errorMessage[256]; 57 | 58 | delay(100); 59 | 60 | // Read Measurement 61 | uint16_t co2 = 0; 62 | float temperature = 0.0f; 63 | float humidity = 0.0f; 64 | bool isDataReady = false; 65 | error = scd4x.getDataReadyFlag(isDataReady); 66 | if (error) { 67 | M5.Lcd.println("Error trying to execute readMeasurement(): "); 68 | errorToString(error, errorMessage, 256); 69 | Serial.println(errorMessage); 70 | return; 71 | } 72 | if (!isDataReady) { 73 | return; 74 | } 75 | error = scd4x.readMeasurement(co2, temperature, humidity); 76 | if (error) { 77 | M5.Lcd.println("Error trying to execute readMeasurement(): "); 78 | errorToString(error, errorMessage, 256); 79 | Serial.println(errorMessage); 80 | } else if (co2 == 0) { 81 | M5.Lcd.println("Invalid sample detected, skipping."); 82 | } else { 83 | M5.Lcd.setCursor(0, 25); 84 | M5.Lcd.printf("Co2:%d\n", co2); 85 | M5.Lcd.printf("Temperature:%f\n", temperature); 86 | M5.Lcd.printf("Humidity:%f\n", humidity); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/examples/Unit_CO2_M5Atom/Unit_CO2_M5Atom.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2022 by M5Stack 4 | * Equipped with Atom-Lite/Matrix sample source code 5 | * 配套 Atom-Lite/Matrix 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/co2 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/co2 8 | * 9 | * Product: CO2. 二氧化碳 10 | * Date: 2022/9/1 11 | ******************************************** *********************************** 12 | Please connect Unit to the port, Read CO2 content, temperature, humidity and 13 | display them on the display screen 14 | 请Unit连接至端口,读取二氧化碳含量,温度,适度,并显示在屏幕上 15 | Libraries: 16 | - [Sensirion I2C SCD4x](https://github.com/Sensirion/arduino-i2c-scd4x)] 17 | - [Sensirion Core](https://github.com/Sensirion/arduino-core) 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | SensirionI2CScd4x scd4x; 24 | 25 | void setup() { 26 | M5.begin(true, false, true); 27 | Wire.begin(26, 32); 28 | uint16_t error; 29 | char errorMessage[256]; 30 | M5.dis.fillpix(0x00ffff); 31 | scd4x.begin(Wire); 32 | 33 | // stop potentially previously started measurement 34 | error = scd4x.stopPeriodicMeasurement(); 35 | if (error) { 36 | Serial.print("Error trying to execute stopPeriodicMeasurement(): "); 37 | errorToString(error, errorMessage, 256); 38 | Serial.println(errorMessage); 39 | } 40 | 41 | // Start Measurement 42 | error = scd4x.startPeriodicMeasurement(); 43 | if (error) { 44 | Serial.print("Error trying to execute startPeriodicMeasurement(): "); 45 | errorToString(error, errorMessage, 256); 46 | Serial.println(errorMessage); 47 | } 48 | 49 | Serial.println("Waiting for first measurement... (5 sec)"); 50 | } 51 | 52 | void loop() { 53 | uint16_t error; 54 | char errorMessage[256]; 55 | 56 | delay(100); 57 | 58 | // Read Measurement 59 | uint16_t co2 = 0; 60 | float temperature = 0.0f; 61 | float humidity = 0.0f; 62 | bool isDataReady = false; 63 | error = scd4x.getDataReadyFlag(isDataReady); 64 | if (error) { 65 | Serial.print("Error trying to execute readMeasurement(): "); 66 | errorToString(error, errorMessage, 256); 67 | Serial.println(errorMessage); 68 | M5.dis.fillpix(0xff0000); 69 | return; 70 | } 71 | if (!isDataReady) { 72 | M5.dis.fillpix(0xff0000); 73 | 74 | return; 75 | } 76 | error = scd4x.readMeasurement(co2, temperature, humidity); 77 | if (error) { 78 | Serial.print("Error trying to execute readMeasurement(): "); 79 | errorToString(error, errorMessage, 256); 80 | Serial.println(errorMessage); 81 | M5.dis.fillpix(0xff0000); 82 | 83 | } else if (co2 == 0) { 84 | Serial.println("Invalid sample detected, skipping."); 85 | M5.dis.fillpix(0xff0000); 86 | } else { 87 | M5.dis.fillpix(0x00ff00); 88 | Serial.printf("Co2:%d\n", co2); 89 | Serial.printf("Temperature:%f\n", temperature); 90 | Serial.printf("Humidity:%f\n\n", humidity); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/src/app_keyboard.cpp: -------------------------------------------------------------------------------- 1 | #include "app_keyboard.h" 2 | 3 | const uint8_t ROWS = 4; // four rows 4 | const uint8_t COLS = 4; // three columns 5 | 6 | uint8_t rowPins[ROWS] = {5, 12, 13, 15}; 7 | uint8_t colPins[COLS] = {17, 16, 21, 22}; 8 | 9 | char keys[ROWS*COLS] = {'a', 'b', 'c', 'd', 10 | 'e', 'f', 'g', 'h', 11 | 'i', 'j', 'k', 'l', 12 | 'm', 'n', 'o', 'p'}; 13 | 14 | void init_keyboard(void) 15 | { 16 | gpio_reset_pin(GPIO_NUM_17); 17 | gpio_reset_pin(GPIO_NUM_16); 18 | gpio_reset_pin(GPIO_NUM_21); 19 | gpio_reset_pin(GPIO_NUM_22); 20 | gpio_reset_pin(GPIO_NUM_5); 21 | gpio_reset_pin(GPIO_NUM_26); 22 | gpio_reset_pin(GPIO_NUM_13); 23 | gpio_reset_pin(GPIO_NUM_15); 24 | 25 | for (int i = 0; i < 4; i++) 26 | { 27 | pinMode(rowPins[i], INPUT_PULLUP); 28 | pinMode(colPins[i], OUTPUT); 29 | } 30 | 31 | } 32 | 33 | char keyboard(void) { 34 | uint8_t key_index = 0; 35 | char key_value = 0; 36 | 37 | digitalWrite(colPins[0], LOW); 38 | digitalWrite(colPins[1], LOW); 39 | digitalWrite(colPins[2], LOW); 40 | digitalWrite(colPins[3], LOW); 41 | 42 | if (!digitalRead(rowPins[0]) || !digitalRead(rowPins[1]) || 43 | !digitalRead(rowPins[2]) || !digitalRead(rowPins[3])) 44 | { 45 | delay(100); 46 | if (!digitalRead(rowPins[0]) || !digitalRead(rowPins[1]) || 47 | !digitalRead(rowPins[2]) || !digitalRead(rowPins[3])) 48 | { 49 | digitalWrite(colPins[0], LOW); 50 | digitalWrite(colPins[1], HIGH); 51 | digitalWrite(colPins[2], HIGH); 52 | digitalWrite(colPins[3], HIGH); 53 | for (int j = 0; j < 4; j++) 54 | { 55 | if (!digitalRead(rowPins[j])) 56 | { 57 | key_index = 0 + j * 4; 58 | key_value = keys[key_index]; 59 | Serial.println(key_value); 60 | // Serial.println(0 + j * 4 + 1);//在串口监视器中输出键盘编号 61 | while (!digitalRead(rowPins[j])); 62 | } 63 | } 64 | digitalWrite(colPins[0], HIGH); 65 | digitalWrite(colPins[1], LOW); 66 | digitalWrite(colPins[2], HIGH); 67 | digitalWrite(colPins[3], HIGH); 68 | for (int j = 0; j < 4; j++) 69 | { 70 | if (!digitalRead(rowPins[j])) 71 | { 72 | key_index = 1 + j * 4; 73 | key_value = keys[key_index]; 74 | Serial.println(key_value); 75 | // Serial.println(1 + j * 4 + 1); 76 | while (!digitalRead(rowPins[j])); 77 | } 78 | } 79 | digitalWrite(colPins[0], HIGH); 80 | digitalWrite(colPins[1], HIGH); 81 | digitalWrite(colPins[2], LOW); 82 | digitalWrite(colPins[3], HIGH); 83 | for (int j = 0; j < 4; j++) 84 | { 85 | if (!digitalRead(rowPins[j])) 86 | { 87 | key_index = 2 + j * 4; 88 | key_value = keys[key_index]; 89 | Serial.println(key_value); 90 | // Serial.println(2 + j * 4 + 1); 91 | while (!digitalRead(rowPins[j])); 92 | } 93 | } 94 | digitalWrite(colPins[0], HIGH); 95 | digitalWrite(colPins[1], HIGH); 96 | digitalWrite(colPins[2], HIGH); 97 | digitalWrite(colPins[3], LOW); 98 | for (int j = 0; j < 4; j++) 99 | { 100 | if (!digitalRead(rowPins[j])) 101 | { 102 | key_index = 3 + j * 4; 103 | key_value = keys[key_index]; 104 | Serial.println(key_value); 105 | // Serial.println(3 + j * 4 + 1); 106 | while (!digitalRead(rowPins[j])); 107 | } 108 | } 109 | } 110 | } 111 | 112 | return key_value; 113 | } -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/src/QMP6988.h: -------------------------------------------------------------------------------- 1 | #ifndef __QMP6988_H 2 | #define __QMP6988_H 3 | 4 | #include "Arduino.h" 5 | #include "Wire.h" 6 | 7 | #define QMP6988_SLAVE_ADDRESS_L (0x70) 8 | #define QMP6988_SLAVE_ADDRESS_H (0x56) 9 | 10 | #define QMP6988_U16_t unsigned short 11 | #define QMP6988_S16_t short 12 | #define QMP6988_U32_t unsigned int 13 | #define QMP6988_S32_t int 14 | #define QMP6988_U64_t unsigned long long 15 | #define QMP6988_S64_t long long 16 | 17 | #define QMP6988_CHIP_ID 0x5C 18 | 19 | #define QMP6988_CHIP_ID_REG 0xD1 20 | #define QMP6988_RESET_REG 0xE0 /* Device reset register */ 21 | #define QMP6988_DEVICE_STAT_REG 0xF3 /* Device state register */ 22 | #define QMP6988_CTRLMEAS_REG 0xF4 /* Measurement Condition Control Register */ 23 | /* data */ 24 | #define QMP6988_PRESSURE_MSB_REG 0xF7 /* Pressure MSB Register */ 25 | #define QMP6988_TEMPERATURE_MSB_REG 0xFA /* Temperature MSB Reg */ 26 | 27 | /* compensation calculation */ 28 | #define QMP6988_CALIBRATION_DATA_START \ 29 | 0xA0 /* QMP6988 compensation coefficients */ 30 | #define QMP6988_CALIBRATION_DATA_LENGTH 25 31 | 32 | #define SHIFT_RIGHT_4_POSITION 4 33 | #define SHIFT_LEFT_2_POSITION 2 34 | #define SHIFT_LEFT_4_POSITION 4 35 | #define SHIFT_LEFT_5_POSITION 5 36 | #define SHIFT_LEFT_8_POSITION 8 37 | #define SHIFT_LEFT_12_POSITION 12 38 | #define SHIFT_LEFT_16_POSITION 16 39 | 40 | /* power mode */ 41 | #define QMP6988_SLEEP_MODE 0x00 42 | #define QMP6988_FORCED_MODE 0x01 43 | #define QMP6988_NORMAL_MODE 0x03 44 | 45 | #define QMP6988_CTRLMEAS_REG_MODE__POS 0 46 | #define QMP6988_CTRLMEAS_REG_MODE__MSK 0x03 47 | #define QMP6988_CTRLMEAS_REG_MODE__LEN 2 48 | 49 | /* oversampling */ 50 | #define QMP6988_OVERSAMPLING_SKIPPED 0x00 51 | #define QMP6988_OVERSAMPLING_1X 0x01 52 | #define QMP6988_OVERSAMPLING_2X 0x02 53 | #define QMP6988_OVERSAMPLING_4X 0x03 54 | #define QMP6988_OVERSAMPLING_8X 0x04 55 | #define QMP6988_OVERSAMPLING_16X 0x05 56 | #define QMP6988_OVERSAMPLING_32X 0x06 57 | #define QMP6988_OVERSAMPLING_64X 0x07 58 | 59 | #define QMP6988_CTRLMEAS_REG_OSRST__POS 5 60 | #define QMP6988_CTRLMEAS_REG_OSRST__MSK 0xE0 61 | #define QMP6988_CTRLMEAS_REG_OSRST__LEN 3 62 | 63 | #define QMP6988_CTRLMEAS_REG_OSRSP__POS 2 64 | #define QMP6988_CTRLMEAS_REG_OSRSP__MSK 0x1C 65 | #define QMP6988_CTRLMEAS_REG_OSRSP__LEN 3 66 | 67 | /* filter */ 68 | #define QMP6988_FILTERCOEFF_OFF 0x00 69 | #define QMP6988_FILTERCOEFF_2 0x01 70 | #define QMP6988_FILTERCOEFF_4 0x02 71 | #define QMP6988_FILTERCOEFF_8 0x03 72 | #define QMP6988_FILTERCOEFF_16 0x04 73 | #define QMP6988_FILTERCOEFF_32 0x05 74 | 75 | #define QMP6988_CONFIG_REG 0xF1 /*IIR filter co-efficient setting Register*/ 76 | #define QMP6988_CONFIG_REG_FILTER__POS 0 77 | #define QMP6988_CONFIG_REG_FILTER__MSK 0x07 78 | #define QMP6988_CONFIG_REG_FILTER__LEN 3 79 | 80 | #define SUBTRACTOR 8388608 81 | 82 | typedef struct _qmp6988_cali_data { 83 | QMP6988_S32_t COE_a0; 84 | QMP6988_S16_t COE_a1; 85 | QMP6988_S16_t COE_a2; 86 | QMP6988_S32_t COE_b00; 87 | QMP6988_S16_t COE_bt1; 88 | QMP6988_S16_t COE_bt2; 89 | QMP6988_S16_t COE_bp1; 90 | QMP6988_S16_t COE_b11; 91 | QMP6988_S16_t COE_bp2; 92 | QMP6988_S16_t COE_b12; 93 | QMP6988_S16_t COE_b21; 94 | QMP6988_S16_t COE_bp3; 95 | } qmp6988_cali_data_t; 96 | 97 | typedef struct _qmp6988_fk_data { 98 | float a0, b00; 99 | float a1, a2, bt1, bt2, bp1, b11, bp2, b12, b21, bp3; 100 | } qmp6988_fk_data_t; 101 | 102 | typedef struct _qmp6988_ik_data { 103 | QMP6988_S32_t a0, b00; 104 | QMP6988_S32_t a1, a2; 105 | QMP6988_S64_t bt1, bt2, bp1, b11, bp2, b12, b21, bp3; 106 | } qmp6988_ik_data_t; 107 | 108 | typedef struct _qmp6988_data { 109 | uint8_t slave; 110 | uint8_t chip_id; 111 | uint8_t power_mode; 112 | float temperature; 113 | float pressure; 114 | float altitude; 115 | qmp6988_cali_data_t qmp6988_cali; 116 | qmp6988_ik_data_t ik; 117 | } qmp6988_data_t; 118 | 119 | class QMP6988 { 120 | private: 121 | qmp6988_data_t qmp6988; 122 | uint8_t slave_addr; 123 | TwoWire* device_wire; 124 | void delayMS(unsigned int ms); 125 | 126 | // read calibration data from otp 127 | int getCalibrationData(); 128 | QMP6988_S32_t getPressure02e(qmp6988_ik_data_t* ik, QMP6988_S32_t dp, 129 | QMP6988_S16_t tx); 130 | QMP6988_S16_t convTx02e(qmp6988_ik_data_t* ik, QMP6988_S32_t dt); 131 | 132 | void softwareReset(); 133 | 134 | public: 135 | uint8_t init(uint8_t slave_addr = 0x56, TwoWire* wire_in = &Wire); 136 | uint8_t deviceCheck(); 137 | 138 | float calcAltitude(float pressure, float temp); 139 | float calcPressure(); 140 | 141 | void setpPowermode(int power_mode); 142 | void setFilter(unsigned char filter); 143 | void setOversamplingP(unsigned char oversampling_p); 144 | void setOversamplingT(unsigned char oversampling_t); 145 | 146 | uint8_t writeReg(uint8_t slave, uint8_t reg_add, uint8_t reg_dat); 147 | uint8_t readData(uint16_t slave, uint8_t reg_add, unsigned char* Read, 148 | uint8_t num); 149 | }; 150 | 151 | #endif 152 | -------------------------------------------------------------------------------- /examples/FactoryTest_V2.0/lib/M5Unit-ENV/src/QMP6988.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "stdint.h" 3 | #include "stdio.h" 4 | #include "QMP6988.h" 5 | 6 | // DISABLE LOG 7 | #define QMP6988_LOG(format...) 8 | #define QMP6988_ERR(format...) 9 | 10 | // ENABLE LOG 11 | // #define QMP6988_LOG Serial.printf 12 | // #define QMP6988_ERR Serial.printf 13 | 14 | void QMP6988::delayMS(unsigned int ms) { 15 | delay(ms); 16 | } 17 | 18 | uint8_t QMP6988::writeReg(uint8_t slave, uint8_t reg_add, uint8_t reg_dat) { 19 | device_wire->beginTransmission(slave); 20 | device_wire->write(reg_add); 21 | device_wire->write(reg_dat); 22 | device_wire->endTransmission(); 23 | return 1; 24 | } 25 | 26 | uint8_t QMP6988::readData(uint16_t slave, uint8_t reg_add, unsigned char* Read, 27 | uint8_t num) { 28 | device_wire->beginTransmission(slave); 29 | device_wire->write(reg_add); 30 | device_wire->endTransmission(); 31 | device_wire->requestFrom(slave, num); 32 | for (int i = 0; i < num; i++) { 33 | *(Read + i) = device_wire->read(); 34 | } 35 | return 1; 36 | } 37 | 38 | uint8_t QMP6988::deviceCheck() { 39 | uint8_t slave_addr_list[2] = {QMP6988_SLAVE_ADDRESS_L, 40 | QMP6988_SLAVE_ADDRESS_H}; 41 | uint8_t ret = 0; 42 | uint8_t i; 43 | 44 | for (i = 0; i < 2; i++) { 45 | slave_addr = slave_addr_list[i]; 46 | ret = readData(slave_addr, QMP6988_CHIP_ID_REG, &(qmp6988.chip_id), 1); 47 | if (ret == 0) { 48 | QMP6988_LOG("%s: read 0xD1 failed\r\n", __func__); 49 | continue; 50 | } 51 | QMP6988_LOG("qmp6988 read chip id = 0x%x\r\n", qmp6988.chip_id); 52 | if (qmp6988.chip_id == QMP6988_CHIP_ID) { 53 | return 1; 54 | } 55 | } 56 | 57 | return 0; 58 | } 59 | 60 | int QMP6988::getCalibrationData() { 61 | int status = 0; 62 | // BITFIELDS temp_COE; 63 | uint8_t a_data_uint8_tr[QMP6988_CALIBRATION_DATA_LENGTH] = {0}; 64 | int len; 65 | 66 | for (len = 0; len < QMP6988_CALIBRATION_DATA_LENGTH; len += 1) { 67 | status = readData(slave_addr, QMP6988_CALIBRATION_DATA_START + len, 68 | &a_data_uint8_tr[len], 1); 69 | if (status == 0) { 70 | QMP6988_LOG("qmp6988 read 0xA0 error!"); 71 | return status; 72 | } 73 | } 74 | 75 | qmp6988.qmp6988_cali.COE_a0 = 76 | (QMP6988_S32_t)(((a_data_uint8_tr[18] << SHIFT_LEFT_12_POSITION) | 77 | (a_data_uint8_tr[19] << SHIFT_LEFT_4_POSITION) | 78 | (a_data_uint8_tr[24] & 0x0f)) 79 | << 12); 80 | qmp6988.qmp6988_cali.COE_a0 = qmp6988.qmp6988_cali.COE_a0 >> 12; 81 | 82 | qmp6988.qmp6988_cali.COE_a1 = 83 | (QMP6988_S16_t)(((a_data_uint8_tr[20]) << SHIFT_LEFT_8_POSITION) | 84 | a_data_uint8_tr[21]); 85 | qmp6988.qmp6988_cali.COE_a2 = 86 | (QMP6988_S16_t)(((a_data_uint8_tr[22]) << SHIFT_LEFT_8_POSITION) | 87 | a_data_uint8_tr[23]); 88 | 89 | qmp6988.qmp6988_cali.COE_b00 = 90 | (QMP6988_S32_t)(((a_data_uint8_tr[0] << SHIFT_LEFT_12_POSITION) | 91 | (a_data_uint8_tr[1] << SHIFT_LEFT_4_POSITION) | 92 | ((a_data_uint8_tr[24] & 0xf0) >> 93 | SHIFT_RIGHT_4_POSITION)) 94 | << 12); 95 | qmp6988.qmp6988_cali.COE_b00 = qmp6988.qmp6988_cali.COE_b00 >> 12; 96 | 97 | qmp6988.qmp6988_cali.COE_bt1 = 98 | (QMP6988_S16_t)(((a_data_uint8_tr[2]) << SHIFT_LEFT_8_POSITION) | 99 | a_data_uint8_tr[3]); 100 | qmp6988.qmp6988_cali.COE_bt2 = 101 | (QMP6988_S16_t)(((a_data_uint8_tr[4]) << SHIFT_LEFT_8_POSITION) | 102 | a_data_uint8_tr[5]); 103 | qmp6988.qmp6988_cali.COE_bp1 = 104 | (QMP6988_S16_t)(((a_data_uint8_tr[6]) << SHIFT_LEFT_8_POSITION) | 105 | a_data_uint8_tr[7]); 106 | qmp6988.qmp6988_cali.COE_b11 = 107 | (QMP6988_S16_t)(((a_data_uint8_tr[8]) << SHIFT_LEFT_8_POSITION) | 108 | a_data_uint8_tr[9]); 109 | qmp6988.qmp6988_cali.COE_bp2 = 110 | (QMP6988_S16_t)(((a_data_uint8_tr[10]) << SHIFT_LEFT_8_POSITION) | 111 | a_data_uint8_tr[11]); 112 | qmp6988.qmp6988_cali.COE_b12 = 113 | (QMP6988_S16_t)(((a_data_uint8_tr[12]) << SHIFT_LEFT_8_POSITION) | 114 | a_data_uint8_tr[13]); 115 | qmp6988.qmp6988_cali.COE_b21 = 116 | (QMP6988_S16_t)(((a_data_uint8_tr[14]) << SHIFT_LEFT_8_POSITION) | 117 | a_data_uint8_tr[15]); 118 | qmp6988.qmp6988_cali.COE_bp3 = 119 | (QMP6988_S16_t)(((a_data_uint8_tr[16]) << SHIFT_LEFT_8_POSITION) | 120 | a_data_uint8_tr[17]); 121 | 122 | QMP6988_LOG("<-----------calibration data-------------->\r\n"); 123 | QMP6988_LOG("COE_a0[%d] COE_a1[%d] COE_a2[%d] COE_b00[%d]\r\n", 124 | qmp6988.qmp6988_cali.COE_a0, qmp6988.qmp6988_cali.COE_a1, 125 | qmp6988.qmp6988_cali.COE_a2, qmp6988.qmp6988_cali.COE_b00); 126 | QMP6988_LOG("COE_bt1[%d] COE_bt2[%d] COE_bp1[%d] COE_b11[%d]\r\n", 127 | qmp6988.qmp6988_cali.COE_bt1, qmp6988.qmp6988_cali.COE_bt2, 128 | qmp6988.qmp6988_cali.COE_bp1, qmp6988.qmp6988_cali.COE_b11); 129 | QMP6988_LOG("COE_bp2[%d] COE_b12[%d] COE_b21[%d] COE_bp3[%d]\r\n", 130 | qmp6988.qmp6988_cali.COE_bp2, qmp6988.qmp6988_cali.COE_b12, 131 | qmp6988.qmp6988_cali.COE_b21, qmp6988.qmp6988_cali.COE_bp3); 132 | QMP6988_LOG("<-----------calibration data-------------->\r\n"); 133 | 134 | qmp6988.ik.a0 = qmp6988.qmp6988_cali.COE_a0; // 20Q4 135 | qmp6988.ik.b00 = qmp6988.qmp6988_cali.COE_b00; // 20Q4 136 | 137 | qmp6988.ik.a1 = 3608L * (QMP6988_S32_t)qmp6988.qmp6988_cali.COE_a1 - 138 | 1731677965L; // 31Q23 139 | qmp6988.ik.a2 = 16889L * (QMP6988_S32_t)qmp6988.qmp6988_cali.COE_a2 - 140 | 87619360L; // 30Q47 141 | 142 | qmp6988.ik.bt1 = 2982L * (QMP6988_S64_t)qmp6988.qmp6988_cali.COE_bt1 + 143 | 107370906L; // 28Q15 144 | qmp6988.ik.bt2 = 329854L * (QMP6988_S64_t)qmp6988.qmp6988_cali.COE_bt2 + 145 | 108083093L; // 34Q38 146 | qmp6988.ik.bp1 = 19923L * (QMP6988_S64_t)qmp6988.qmp6988_cali.COE_bp1 + 147 | 1133836764L; // 31Q20 148 | qmp6988.ik.b11 = 2406L * (QMP6988_S64_t)qmp6988.qmp6988_cali.COE_b11 + 149 | 118215883L; // 28Q34 150 | qmp6988.ik.bp2 = 3079L * (QMP6988_S64_t)qmp6988.qmp6988_cali.COE_bp2 - 151 | 181579595L; // 29Q43 152 | qmp6988.ik.b12 = 6846L * (QMP6988_S64_t)qmp6988.qmp6988_cali.COE_b12 + 153 | 85590281L; // 29Q53 154 | qmp6988.ik.b21 = 13836L * (QMP6988_S64_t)qmp6988.qmp6988_cali.COE_b21 + 155 | 79333336L; // 29Q60 156 | qmp6988.ik.bp3 = 2915L * (QMP6988_S64_t)qmp6988.qmp6988_cali.COE_bp3 + 157 | 157155561L; // 28Q65 158 | QMP6988_LOG("<----------- int calibration data -------------->\r\n"); 159 | QMP6988_LOG("a0[%d] a1[%d] a2[%d] b00[%d]\r\n", qmp6988.ik.a0, 160 | qmp6988.ik.a1, qmp6988.ik.a2, qmp6988.ik.b00); 161 | QMP6988_LOG("bt1[%lld] bt2[%lld] bp1[%lld] b11[%lld]\r\n", 162 | qmp6988.ik.bt1, qmp6988.ik.bt2, qmp6988.ik.bp1, qmp6988.ik.b11); 163 | QMP6988_LOG("bp2[%lld] b12[%lld] b21[%lld] bp3[%lld]\r\n", 164 | qmp6988.ik.bp2, qmp6988.ik.b12, qmp6988.ik.b21, qmp6988.ik.bp3); 165 | QMP6988_LOG("<----------- int calibration data -------------->\r\n"); 166 | return 1; 167 | } 168 | 169 | QMP6988_S16_t QMP6988::convTx02e(qmp6988_ik_data_t* ik, QMP6988_S32_t dt) { 170 | QMP6988_S16_t ret; 171 | QMP6988_S64_t wk1, wk2; 172 | 173 | // wk1: 60Q4 // bit size 174 | wk1 = ((QMP6988_S64_t)ik->a1 * (QMP6988_S64_t)dt); // 31Q23+24-1=54 (54Q23) 175 | wk2 = ((QMP6988_S64_t)ik->a2 * (QMP6988_S64_t)dt) >> 176 | 14; // 30Q47+24-1=53 (39Q33) 177 | wk2 = (wk2 * (QMP6988_S64_t)dt) >> 10; // 39Q33+24-1=62 (52Q23) 178 | wk2 = ((wk1 + wk2) / 32767) >> 19; // 54,52->55Q23 (20Q04) 179 | ret = (QMP6988_S16_t)((ik->a0 + wk2) >> 4); // 21Q4 -> 17Q0 180 | return ret; 181 | } 182 | 183 | QMP6988_S32_t QMP6988::getPressure02e(qmp6988_ik_data_t* ik, QMP6988_S32_t dp, 184 | QMP6988_S16_t tx) { 185 | QMP6988_S32_t ret; 186 | QMP6988_S64_t wk1, wk2, wk3; 187 | 188 | // wk1 = 48Q16 // bit size 189 | wk1 = 190 | ((QMP6988_S64_t)ik->bt1 * (QMP6988_S64_t)tx); // 28Q15+16-1=43 (43Q15) 191 | wk2 = ((QMP6988_S64_t)ik->bp1 * (QMP6988_S64_t)dp) >> 192 | 5; // 31Q20+24-1=54 (49Q15) 193 | wk1 += wk2; // 43,49->50Q15 194 | wk2 = ((QMP6988_S64_t)ik->bt2 * (QMP6988_S64_t)tx) >> 195 | 1; // 34Q38+16-1=49 (48Q37) 196 | wk2 = (wk2 * (QMP6988_S64_t)tx) >> 8; // 48Q37+16-1=63 (55Q29) 197 | wk3 = wk2; // 55Q29 198 | wk2 = ((QMP6988_S64_t)ik->b11 * (QMP6988_S64_t)tx) >> 199 | 4; // 28Q34+16-1=43 (39Q30) 200 | wk2 = (wk2 * (QMP6988_S64_t)dp) >> 1; // 39Q30+24-1=62 (61Q29) 201 | wk3 += wk2; // 55,61->62Q29 202 | wk2 = ((QMP6988_S64_t)ik->bp2 * (QMP6988_S64_t)dp) >> 203 | 13; // 29Q43+24-1=52 (39Q30) 204 | wk2 = (wk2 * (QMP6988_S64_t)dp) >> 1; // 39Q30+24-1=62 (61Q29) 205 | wk3 += wk2; // 62,61->63Q29 206 | wk1 += wk3 >> 14; // Q29 >> 14 -> Q15 207 | wk2 = 208 | ((QMP6988_S64_t)ik->b12 * (QMP6988_S64_t)tx); // 29Q53+16-1=45 (45Q53) 209 | wk2 = (wk2 * (QMP6988_S64_t)tx) >> 22; // 45Q53+16-1=61 (39Q31) 210 | wk2 = (wk2 * (QMP6988_S64_t)dp) >> 1; // 39Q31+24-1=62 (61Q30) 211 | wk3 = wk2; // 61Q30 212 | wk2 = ((QMP6988_S64_t)ik->b21 * (QMP6988_S64_t)tx) >> 213 | 6; // 29Q60+16-1=45 (39Q54) 214 | wk2 = (wk2 * (QMP6988_S64_t)dp) >> 23; // 39Q54+24-1=62 (39Q31) 215 | wk2 = (wk2 * (QMP6988_S64_t)dp) >> 1; // 39Q31+24-1=62 (61Q20) 216 | wk3 += wk2; // 61,61->62Q30 217 | wk2 = ((QMP6988_S64_t)ik->bp3 * (QMP6988_S64_t)dp) >> 218 | 12; // 28Q65+24-1=51 (39Q53) 219 | wk2 = (wk2 * (QMP6988_S64_t)dp) >> 23; // 39Q53+24-1=62 (39Q30) 220 | wk2 = (wk2 * (QMP6988_S64_t)dp); // 39Q30+24-1=62 (62Q30) 221 | wk3 += wk2; // 62,62->63Q30 222 | wk1 += wk3 >> 15; // Q30 >> 15 = Q15 223 | wk1 /= 32767L; 224 | wk1 >>= 11; // Q15 >> 7 = Q4 225 | wk1 += ik->b00; // Q4 + 20Q4 226 | // wk1 >>= 4; // 28Q4 -> 24Q0 227 | ret = (QMP6988_S32_t)wk1; 228 | return ret; 229 | } 230 | 231 | void QMP6988::softwareReset() { 232 | uint8_t ret = 0; 233 | 234 | ret = writeReg(slave_addr, QMP6988_RESET_REG, 0xe6); 235 | if (ret == 0) { 236 | QMP6988_LOG("softwareReset fail!!! \r\n"); 237 | } 238 | delayMS(20); 239 | ret = writeReg(slave_addr, QMP6988_RESET_REG, 0x00); 240 | } 241 | 242 | void QMP6988::setpPowermode(int power_mode) { 243 | uint8_t data; 244 | 245 | QMP6988_LOG("qmp_set_powermode %d \r\n", power_mode); 246 | 247 | qmp6988.power_mode = power_mode; 248 | readData(slave_addr, QMP6988_CTRLMEAS_REG, &data, 1); 249 | data = data & 0xfc; 250 | if (power_mode == QMP6988_SLEEP_MODE) { 251 | data |= 0x00; 252 | } else if (power_mode == QMP6988_FORCED_MODE) { 253 | data |= 0x01; 254 | } else if (power_mode == QMP6988_NORMAL_MODE) { 255 | data |= 0x03; 256 | } 257 | writeReg(slave_addr, QMP6988_CTRLMEAS_REG, data); 258 | 259 | QMP6988_LOG("qmp_set_powermode 0xf4=0x%x \r\n", data); 260 | 261 | delayMS(20); 262 | } 263 | 264 | void QMP6988::setFilter(unsigned char filter) { 265 | uint8_t data; 266 | 267 | data = (filter & 0x03); 268 | writeReg(slave_addr, QMP6988_CONFIG_REG, data); 269 | 270 | delayMS(20); 271 | } 272 | 273 | void QMP6988::setOversamplingP(unsigned char oversampling_p) { 274 | uint8_t data; 275 | 276 | readData(slave_addr, QMP6988_CTRLMEAS_REG, &data, 1); 277 | data &= 0xe3; 278 | data |= (oversampling_p << 2); 279 | writeReg(slave_addr, QMP6988_CTRLMEAS_REG, data); 280 | delayMS(20); 281 | } 282 | 283 | void QMP6988::setOversamplingT(unsigned char oversampling_t) { 284 | uint8_t data; 285 | 286 | readData(slave_addr, QMP6988_CTRLMEAS_REG, &data, 1); 287 | data &= 0x1f; 288 | data |= (oversampling_t << 5); 289 | writeReg(slave_addr, QMP6988_CTRLMEAS_REG, data); 290 | delayMS(20); 291 | } 292 | 293 | float QMP6988::calcAltitude(float pressure, float temp) { 294 | float altitude; 295 | 296 | altitude = 297 | (pow((101325 / pressure), 1 / 5.257) - 1) * (temp + 273.15) / 0.0065; 298 | QMP6988_LOG("altitude = %f\r\n", altitude); 299 | return altitude; 300 | } 301 | 302 | float QMP6988::calcPressure() { 303 | uint8_t err = 0; 304 | QMP6988_U32_t P_read, T_read; 305 | QMP6988_S32_t P_raw, T_raw; 306 | uint8_t a_data_uint8_tr[6] = {0}; 307 | QMP6988_S32_t T_int, P_int; 308 | 309 | // press 310 | err = readData(slave_addr, QMP6988_PRESSURE_MSB_REG, a_data_uint8_tr, 6); 311 | if (err == 0) { 312 | QMP6988_LOG("qmp6988 read press raw error! \r\n"); 313 | return 0.0f; 314 | } 315 | P_read = (QMP6988_U32_t)((((QMP6988_U32_t)(a_data_uint8_tr[0])) 316 | << SHIFT_LEFT_16_POSITION) | 317 | (((QMP6988_U16_t)(a_data_uint8_tr[1])) 318 | << SHIFT_LEFT_8_POSITION) | 319 | (a_data_uint8_tr[2])); 320 | P_raw = (QMP6988_S32_t)(P_read - SUBTRACTOR); 321 | 322 | T_read = (QMP6988_U32_t)((((QMP6988_U32_t)(a_data_uint8_tr[3])) 323 | << SHIFT_LEFT_16_POSITION) | 324 | (((QMP6988_U16_t)(a_data_uint8_tr[4])) 325 | << SHIFT_LEFT_8_POSITION) | 326 | (a_data_uint8_tr[5])); 327 | T_raw = (QMP6988_S32_t)(T_read - SUBTRACTOR); 328 | 329 | T_int = convTx02e(&(qmp6988.ik), T_raw); 330 | P_int = getPressure02e(&(qmp6988.ik), P_raw, T_int); 331 | qmp6988.temperature = (float)T_int / 256.0f; 332 | qmp6988.pressure = (float)P_int / 16.0f; 333 | 334 | return qmp6988.pressure; 335 | } 336 | 337 | uint8_t QMP6988::init(uint8_t slave_addr_in, TwoWire* wire_in) { 338 | device_wire = wire_in; 339 | uint8_t ret; 340 | slave_addr = slave_addr_in; 341 | ret = deviceCheck(); 342 | if (ret == 0) { 343 | return 0; 344 | } 345 | softwareReset(); 346 | getCalibrationData(); 347 | setpPowermode(QMP6988_NORMAL_MODE); 348 | setFilter(QMP6988_FILTERCOEFF_4); 349 | setOversamplingP(QMP6988_OVERSAMPLING_8X); 350 | setOversamplingT(QMP6988_OVERSAMPLING_1X); 351 | return 1; 352 | } 353 | -------------------------------------------------------------------------------- /examples/FactoryTest_V1.1/FactoryTest_V1.1.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "MFRC522_I2C.h" 6 | #include "M5_ENV.h" 7 | 8 | int count_flag = 0; 9 | uint8_t setup_flag = 0; 10 | 11 | void joystick(void) { 12 | // put your main code here, to run repeatedly: 13 | uint16_t x_data = 0; 14 | uint16_t y_data = 0; 15 | uint8_t button_data = 0; 16 | 17 | M5.Lcd.setCursor(100, 30, 4); 18 | M5.Lcd.println("JOYSTICK"); 19 | M5.Lcd.setCursor(0, 60, 4); 20 | M5.Lcd.println("PIN : x_adc:35,y_adc:36,button:2"); 21 | 22 | if (!setup_flag) { 23 | setup_flag = 1; 24 | gpio_reset_pin(GPIO_NUM_2); 25 | gpio_reset_pin(GPIO_NUM_35); 26 | gpio_reset_pin(GPIO_NUM_36); 27 | pinMode(2, INPUT_PULLUP); 28 | } 29 | x_data = analogRead(35); 30 | y_data = analogRead(36); 31 | button_data = digitalRead(2); 32 | 33 | Serial.printf("x:%0d y:%0d button:%d\n", x_data, y_data, button_data); 34 | 35 | M5.Lcd.setCursor(30, 120, 4); 36 | M5.Lcd.printf("x:%04d y:%04d button:%d\n", x_data, y_data, button_data); 37 | 38 | delay(200); 39 | } 40 | 41 | #define DAC_ADDR 0x4C 42 | 43 | void outVoltage(uint8_t ch, uint16_t v) { 44 | Wire.beginTransmission(DAC_ADDR); 45 | Wire.write(0x10 | (ch << 1)); 46 | 47 | Wire.write((v >> 2) & 0xff); 48 | Wire.write((v << 6) & 0xff); 49 | Wire.endTransmission(); 50 | } 51 | void dac() { 52 | if (!setup_flag) { 53 | setup_flag = 1; 54 | gpio_reset_pin(GPIO_NUM_21); 55 | gpio_reset_pin(GPIO_NUM_22); 56 | Wire.begin(); 57 | 58 | M5.Lcd.setCursor(140, 30, 4); 59 | M5.Lcd.print("DAC"); 60 | M5.Lcd.setCursor(0, 60, 4); 61 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 62 | 63 | outVoltage(0, 256); // 1.25v 64 | outVoltage(1, 512); // 2.50v 65 | outVoltage(2, 768); // 3.75v 66 | outVoltage(3, 1023); // 5.00v 67 | } 68 | outVoltage(0, 256); // 1.25v 69 | outVoltage(1, 512); // 2.50v 70 | outVoltage(2, 768); // 3.75v 71 | outVoltage(3, 1023); // 5.00v 72 | } 73 | 74 | #define ADC_ADDR 0x48 75 | 76 | uint16_t InVoltage(uint8_t ch) { 77 | uint8_t data_L = 0; 78 | uint8_t data_H = 0; 79 | uint16_t data_adc = 0; 80 | 81 | Wire.beginTransmission(ADC_ADDR); 82 | Wire.write(0X01); 83 | Wire.write(0XC0 | (ch << 4)); 84 | Wire.write(0X83); 85 | Wire.endTransmission(); 86 | 87 | Wire.beginTransmission(ADC_ADDR); 88 | Wire.write(0x00); 89 | Wire.endTransmission(); 90 | 91 | delay(50); 92 | 93 | Wire.requestFrom(ADC_ADDR, 2); 94 | while (Wire.available()) { 95 | data_H = Wire.read(); 96 | data_L = Wire.read(); 97 | } 98 | 99 | data_adc = (data_H << 8) | data_L; 100 | return data_adc; 101 | } 102 | 103 | void adc() { 104 | uint16_t adc_ch0 = 0; 105 | uint16_t adc_ch1 = 0; 106 | uint16_t adc_ch2 = 0; 107 | uint16_t adc_ch3 = 0; 108 | 109 | if (!setup_flag) { 110 | setup_flag = 1; 111 | gpio_reset_pin(GPIO_NUM_21); 112 | gpio_reset_pin(GPIO_NUM_22); 113 | Wire.begin(); 114 | 115 | M5.Lcd.setCursor(140, 30, 4); 116 | M5.Lcd.print("ADC"); 117 | M5.Lcd.setCursor(0, 60, 4); 118 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 119 | } 120 | adc_ch0 = InVoltage(0); 121 | adc_ch1 = InVoltage(1); 122 | adc_ch2 = InVoltage(2); 123 | adc_ch3 = InVoltage(3); 124 | Serial.printf("ch0:%d ch1:%d ch2:%d ch3:%d\n", adc_ch0, adc_ch1, adc_ch2, 125 | adc_ch3); 126 | 127 | M5.Lcd.setCursor(0, 120, 4); 128 | M5.Lcd.printf("ch0:%05d ch1:%05d\n", adc_ch0, adc_ch1); 129 | M5.Lcd.printf("ch2:%05d ch3:%05d\n", adc_ch2, adc_ch3); 130 | // delay(500); 131 | } 132 | 133 | const int phaseA = 2; 134 | const int phaseB = 13; 135 | const int Button = 15; 136 | #define GET_CODE() uint8_t(digitalRead(phaseA) << 4 | digitalRead(phaseB)) 137 | 138 | int32_t count = 65536; 139 | int32_t count_last = 65536; 140 | int32_t count_change = 0; 141 | uint8_t code = 0; 142 | uint8_t code_old = 0; 143 | void encoderEvent() { 144 | code = GET_CODE(); 145 | if (code != code_old) { 146 | if (code == 0x00) { 147 | count_last = count; 148 | if (code_old == 0x10) { 149 | count--; 150 | count_change == -65536 ? count_change : count_change--; 151 | } else { 152 | count_change == 65536 ? count_change : count_change++; 153 | } 154 | } 155 | code_old = code; 156 | } 157 | } 158 | 159 | void encoder() { 160 | if (!setup_flag) { 161 | setup_flag = 1; 162 | gpio_reset_pin(GPIO_NUM_2); 163 | gpio_reset_pin(GPIO_NUM_13); 164 | gpio_reset_pin(GPIO_NUM_15); 165 | pinMode(phaseA, INPUT_PULLUP); 166 | pinMode(phaseB, INPUT_PULLUP); 167 | pinMode(Button, INPUT_PULLUP); 168 | // M5.Lcd.setCursor(100, 0, 4); 169 | // M5.Lcd.print("ENCODER"); 170 | 171 | M5.Lcd.setCursor(100, 30, 4); 172 | M5.Lcd.println("ENCODER"); 173 | M5.Lcd.setCursor(0, 60, 4); 174 | M5.Lcd.println("PIN : a:2,b:13,button:15"); 175 | 176 | code = GET_CODE(); 177 | code_old = code; 178 | } 179 | 180 | uint8_t value = digitalRead(Button); 181 | encoderEvent(); 182 | Serial.printf("code = %d,button = %d\r\n ", count_change, value); 183 | 184 | M5.Lcd.setCursor(30, 120, 4); 185 | M5.Lcd.printf("code = %05d,button = %d\r\n ", count_change, value); 186 | delay(1); 187 | } 188 | 189 | const byte ROWS = 4; // four rows 190 | const byte COLS = 4; // three columns 191 | 192 | byte rowPins[ROWS] = {17, 16, 21, 22}; 193 | byte colPins[COLS] = {5, 26, 13, 15}; 194 | 195 | char keys[ROWS][COLS] = {{'a', 'b', 'c', 'd'}, 196 | {'e', 'f', 'g', 'h'}, 197 | {'i', 'j', 'k', 'l'}, 198 | {'m', 'n', 'o', 'p'}}; 199 | Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 200 | // Keypad keypad; 201 | void key() { 202 | if (!setup_flag) { 203 | setup_flag = 1; 204 | // Wire.end(); 205 | 206 | // Wire.end(); 207 | // pinMode(21,INPUT_PULLUP); 208 | // pinMode(22,INPUT_PULLUP); 209 | gpio_reset_pin(GPIO_NUM_17); 210 | gpio_reset_pin(GPIO_NUM_16); 211 | gpio_reset_pin(GPIO_NUM_21); 212 | gpio_reset_pin(GPIO_NUM_22); 213 | gpio_reset_pin(GPIO_NUM_5); 214 | gpio_reset_pin(GPIO_NUM_26); 215 | gpio_reset_pin(GPIO_NUM_13); 216 | gpio_reset_pin(GPIO_NUM_15); 217 | 218 | M5.Lcd.setCursor(100, 30, 4); 219 | M5.Lcd.println("KEYBOARD"); 220 | M5.Lcd.setCursor(0, 60, 4); 221 | M5.Lcd.println("PIN : r0:17,r1:16,r2:21,r3:22"); 222 | M5.Lcd.println("PIN : c0: 5,c1:26,c2:13,c3:15"); 223 | keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 224 | } 225 | 226 | char key = keypad.getKey(); 227 | if (key) { 228 | Serial.println(key); 229 | M5.Lcd.fillRect(150, 150, 80, 50, BLACK); 230 | M5.Lcd.setCursor(150, 140, 4); 231 | M5.Lcd.printf("%c", key); 232 | } 233 | } 234 | 235 | #define DATA_PIN 15 236 | //#define CLK_PIN 4 237 | #define LED_TYPE WS2811 238 | #define COLOR_ORDER GRB 239 | #define NUM_LEDS 64 240 | CRGB leds[NUM_LEDS]; 241 | #define BRIGHTNESS 5 242 | 243 | int led_count = 0; 244 | void matrix() { 245 | if (!setup_flag) { 246 | setup_flag = 1; 247 | 248 | gpio_reset_pin(GPIO_NUM_15); 249 | 250 | M5.Lcd.setCursor(100, 30, 4); 251 | M5.Lcd.println("MATRIX"); 252 | M5.Lcd.setCursor(0, 60, 4); 253 | M5.Lcd.println("PIN : SIGNAL:15"); 254 | 255 | // FastLED.addLeds(leds, 256 | // NUM_LEDS).setCorrection(TypicalLEDStrip); 257 | // FastLED.addLeds(leds, 258 | // NUM_LEDS).setCorrection(TypicalLEDStrip); 259 | FastLED.addLeds(leds, NUM_LEDS); 260 | // NEOPIXEL 261 | FastLED.setBrightness(BRIGHTNESS); 262 | led_count = 0; 263 | } 264 | led_count++; 265 | if (led_count > 2000) led_count = 0; 266 | if ((led_count / 500) == 0) { 267 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Red; 268 | FastLED.show(); 269 | } 270 | 271 | if ((led_count / 500) == 1) { 272 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Green; 273 | FastLED.show(); 274 | } 275 | if ((led_count / 500) == 2) { 276 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Blue; 277 | FastLED.show(); 278 | } 279 | } 280 | 281 | int dc_count = 0; 282 | void dcmotor() { 283 | if (!setup_flag) { 284 | setup_flag = 1; 285 | 286 | M5.Lcd.setCursor(100, 30, 4); 287 | M5.Lcd.println("DC-MOTOR"); 288 | M5.Lcd.setCursor(0, 60, 4); 289 | M5.Lcd.println("PIN : M+:13,M-:15,ea:21,eb:22"); 290 | 291 | pinMode(13, OUTPUT); 292 | pinMode(15, OUTPUT); 293 | pinMode(21, INPUT_PULLUP); 294 | pinMode(22, INPUT_PULLUP); 295 | dc_count = 0; 296 | } 297 | 298 | dc_count++; 299 | if (dc_count > 4000) dc_count = 0; 300 | 301 | digitalWrite(13, LOW); 302 | digitalWrite(15, HIGH); 303 | } 304 | const int In_0 = 13; 305 | int relay_count = 0; 306 | void relay(void) { 307 | if (!setup_flag) { 308 | setup_flag = 1; 309 | M5.Lcd.setCursor(100, 30, 4); 310 | M5.Lcd.println("RELAY * 8"); 311 | M5.Lcd.setCursor(0, 60, 4); 312 | M5.Lcd.println("PIN : 13"); 313 | pinMode(In_0, OUTPUT); 314 | digitalWrite(In_0, LOW); 315 | relay_count = 0; 316 | digitalWrite(In_0, HIGH); 317 | } 318 | relay_count++; 319 | if (relay_count > 2000) relay_count = 0; 320 | // if(relay_count == 1) 321 | // digitalWrite(In_0, LOW); 322 | // if(relay_count == 1000) 323 | digitalWrite(In_0, HIGH); 324 | } 325 | int servo_count = 0; 326 | int freq = 50; 327 | int ledChannel = 0; 328 | int resolution = 8; 329 | void servo() { 330 | if (!setup_flag) { 331 | setup_flag = 1; 332 | gpio_reset_pin(GPIO_NUM_15); 333 | 334 | M5.Lcd.setCursor(100, 30, 4); 335 | M5.Lcd.println("SERVO"); 336 | M5.Lcd.setCursor(0, 60, 4); 337 | M5.Lcd.println("PIN : 15"); 338 | 339 | ledcSetup(ledChannel, freq, resolution); 340 | ledcAttachPin(15, ledChannel); 341 | servo_count = 0; 342 | } 343 | 344 | servo_count++; 345 | if (servo_count > 3000) servo_count = 0; 346 | if ((servo_count / 1000) == 0) { 347 | ledcWrite(ledChannel, 6); // 0° 348 | } 349 | if ((servo_count / 1000) == 1) { 350 | ledcWrite(ledChannel, 18); // 0° 351 | } 352 | 353 | if ((servo_count / 1000) == 2) { 354 | ledcWrite(ledChannel, 30); // 0° 355 | } 356 | } 357 | const int MOTOR_A = 2; 358 | const int MOTOR_B = 5; 359 | const int MOTOR_C = 12; 360 | const int MOTOR_D = 13; 361 | void stmpmotor(void) { 362 | if (!setup_flag) { 363 | setup_flag = 1; 364 | gpio_reset_pin(GPIO_NUM_2); 365 | gpio_reset_pin(GPIO_NUM_5); 366 | gpio_reset_pin(GPIO_NUM_12); 367 | gpio_reset_pin(GPIO_NUM_13); 368 | 369 | M5.Lcd.setCursor(100, 30, 4); 370 | M5.Lcd.println("STEPMOTOR"); 371 | M5.Lcd.setCursor(0, 60, 4); 372 | M5.Lcd.println("PIN :A:2,B:5,C:12,D:13"); 373 | pinMode(MOTOR_A, OUTPUT); 374 | pinMode(MOTOR_B, OUTPUT); 375 | pinMode(MOTOR_C, OUTPUT); 376 | pinMode(MOTOR_D, OUTPUT); 377 | } 378 | 379 | digitalWrite(MOTOR_A, HIGH); 380 | digitalWrite(MOTOR_B, HIGH); 381 | digitalWrite(MOTOR_C, LOW); 382 | digitalWrite(MOTOR_D, LOW); 383 | delay(2); 384 | digitalWrite(MOTOR_A, LOW); 385 | digitalWrite(MOTOR_B, HIGH); 386 | digitalWrite(MOTOR_C, HIGH); 387 | digitalWrite(MOTOR_D, LOW); 388 | delay(2); 389 | 390 | digitalWrite(MOTOR_A, LOW); 391 | digitalWrite(MOTOR_B, LOW); 392 | digitalWrite(MOTOR_C, HIGH); 393 | digitalWrite(MOTOR_D, HIGH); 394 | delay(2); 395 | digitalWrite(MOTOR_A, HIGH); 396 | digitalWrite(MOTOR_B, LOW); 397 | digitalWrite(MOTOR_C, LOW); 398 | digitalWrite(MOTOR_D, HIGH); 399 | delay(2); 400 | } 401 | MFRC522 mfrc522(0x28); 402 | 403 | void ShowReaderDetails() { 404 | // Get the MFRC522 software version 405 | byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); 406 | Serial.print(F("MFRC522 Software Version: 0x")); 407 | Serial.print(v, HEX); 408 | if (v == 0x91) 409 | Serial.print(F(" = v1.0")); 410 | else if (v == 0x92) 411 | Serial.print(F(" = v2.0")); 412 | else 413 | Serial.print(F(" (unknown)")); 414 | Serial.println(""); 415 | // When 0x00 or 0xFF is returned, communication probably failed 416 | if ((v == 0x00) || (v == 0xFF)) { 417 | Serial.println( 418 | F("WARNING: Communication failure, is the MFRC522 properly " 419 | "connected?")); 420 | } 421 | } 422 | void rfid() { 423 | if (!setup_flag) { 424 | setup_flag = 1; 425 | gpio_reset_pin(GPIO_NUM_22); 426 | gpio_reset_pin(GPIO_NUM_21); 427 | Wire.begin(); // Initialize I2C 428 | 429 | M5.Lcd.setCursor(140, 30, 4); 430 | M5.Lcd.print("RFID"); 431 | M5.Lcd.setCursor(0, 60, 4); 432 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 433 | mfrc522.PCD_Init(); // Init MFRC522 434 | ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader 435 | // details 436 | // Serial.println(F("Scan PICC to see UID, type, and data blocks...")); 437 | // M5.Lcd.println("Scan PICC to see UID, type, and data blocks..."); 438 | } 439 | if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) { 440 | // delay(50); 441 | // M5.Lcd.println("Scan PICC to see UID, type, and data blocks..."); 442 | // return; 443 | } 444 | 445 | Serial.print(F("Card UID:")); 446 | M5.Lcd.println(" "); 447 | M5.Lcd.setCursor(0, 90, 4); 448 | for (byte i = 0; i < mfrc522.uid.size; i++) { 449 | // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 450 | // Serial.print(mfrc522.uid.uidByte[i], HEX); 451 | // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 452 | // Serial.print(mfrc522.uid.uidByte[i], HEX); 453 | M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 454 | M5.Lcd.print(mfrc522.uid.uidByte[i], HEX); 455 | } 456 | // Serial.println(); 457 | } 458 | 459 | SHT3X sht30; 460 | QMP6988 qmp6988; 461 | void env() { 462 | float tmp = 0.0, hum = 0.0, pressure = 0.0; 463 | 464 | if (!setup_flag) { 465 | setup_flag = 1; 466 | 467 | M5.Lcd.setCursor(100, 30, 4); 468 | M5.Lcd.print("ENV Test"); 469 | M5.Lcd.setCursor(0, 60, 4); 470 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 471 | Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 472 | // 加入i2c总线 473 | Serial.printf("qmp6886:%d", qmp6988.init(0x70, &Wire)); 474 | } 475 | 476 | if (sht30.get() == 0) { // Obtain the data of shT30. 获取sht30的数据 477 | tmp = sht30.cTemp; // Store the temperature obtained from shT30. 478 | // 将sht30获取到的温度存储 479 | hum = sht30.humidity; // Store the humidity obtained from the SHT30. 480 | // 将sht30获取到的湿度存储 481 | } else { 482 | tmp = 0, hum = 0; 483 | } 484 | pressure = qmp6988.calcPressure(); 485 | M5.lcd.fillRect(0, 100, 140, 60, 486 | BLACK); // Fill the screen with black (to clear the 487 | // screen). 将屏幕填充黑色(用来清屏) 488 | M5.Lcd.setCursor(0, 100, 4); 489 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", 490 | tmp, hum, pressure); 491 | delay(2000); 492 | } 493 | 494 | const int Analog = 35; 495 | const int Digtal = 2; 496 | uint16_t a_data; 497 | uint16_t d_data; 498 | void luminosity() { 499 | if (!setup_flag) { 500 | setup_flag = 1; 501 | gpio_reset_pin(GPIO_NUM_35); 502 | gpio_reset_pin(GPIO_NUM_2); 503 | 504 | M5.Lcd.setCursor(80, 30, 4); 505 | if (count_flag == 7) 506 | M5.Lcd.print("LUMINOSITY"); 507 | else 508 | M5.Lcd.print("MICROPHONE"); 509 | M5.Lcd.setCursor(0, 60, 4); 510 | M5.Lcd.println("PIN : a:35,d:2"); 511 | 512 | pinMode(Digtal, INPUT_PULLUP); 513 | } 514 | 515 | a_data = analogRead(Analog); 516 | d_data = digitalRead(Digtal); 517 | 518 | Serial.printf("Analog:%0d Digtal:%0d\n", a_data, d_data); 519 | 520 | M5.Lcd.setCursor(30, 120, 4); 521 | M5.Lcd.printf("Analog:%0d Digtal:%0d\n", a_data, d_data); 522 | 523 | delay(50); 524 | } 525 | int rx_num = 0; 526 | int rx_count = 0; 527 | void uart232() { 528 | if (!setup_flag) { 529 | setup_flag = 1; 530 | gpio_reset_pin(GPIO_NUM_16); 531 | gpio_reset_pin(GPIO_NUM_17); 532 | Serial2.begin(115200, SERIAL_8N1, 16, 17); 533 | M5.Lcd.setCursor(80, 30, 4); 534 | M5.Lcd.print("RS232"); 535 | M5.Lcd.setCursor(0, 60, 4); 536 | M5.Lcd.println("PIN :TX:17,RX:16"); 537 | rx_count = 0; 538 | rx_num = 0; 539 | } 540 | 541 | if (Serial2.available()) { 542 | int ch = Serial2.read(); 543 | // Serial.write(ch); 544 | if (ch == 'a') { 545 | rx_num++; 546 | } 547 | M5.Lcd.setCursor(30, 120, 4); 548 | M5.Lcd.printf("rx_num = %d\n", rx_num); 549 | } 550 | rx_count++; 551 | if (rx_count > 6000) rx_count = 0; 552 | if (rx_count == 10) Serial2.write('a'); 553 | } 554 | 555 | // int rx_num = 0; 556 | // int rx_count = 0; 557 | void uart485() { 558 | if (!setup_flag) { 559 | setup_flag = 1; 560 | gpio_reset_pin(GPIO_NUM_16); 561 | gpio_reset_pin(GPIO_NUM_17); 562 | gpio_reset_pin(GPIO_NUM_21); 563 | gpio_reset_pin(GPIO_NUM_22); 564 | Serial2.begin(115200, SERIAL_8N1, 16, 17); 565 | Serial1.begin(115200, SERIAL_8N1, 22, 21); 566 | M5.Lcd.setCursor(80, 30, 4); 567 | M5.Lcd.print("RS485"); 568 | M5.Lcd.setCursor(0, 60, 4); 569 | M5.Lcd.println("PIN :TX:17,RX:16"); 570 | rx_count = 0; 571 | rx_num = 0; 572 | } 573 | 574 | if (Serial1.available()) { 575 | int ch = Serial1.read(); 576 | // Serial.write(ch); 577 | if (ch == 'a') { 578 | rx_num++; 579 | } 580 | M5.Lcd.setCursor(30, 120, 4); 581 | M5.Lcd.printf("rx_num = %d\n", rx_num); 582 | } 583 | rx_count++; 584 | if (rx_count > 6000) rx_count = 0; 585 | if (rx_count == 10) Serial2.write('a'); 586 | } 587 | void setup() { 588 | // put your setup code here, to run once: 589 | M5.begin(); 590 | dacWrite(25, 0); 591 | 592 | M5.Lcd.setCursor(90, 0, 4); 593 | M5.Lcd.print("FactoryTest"); 594 | 595 | pinMode(37, INPUT_PULLUP); 596 | 597 | M5.Lcd.setCursor(220, 210, 4); 598 | M5.Lcd.println("mode"); 599 | } 600 | 601 | void loop() { 602 | // put your main code here, to run repeatedly: 603 | 604 | if (digitalRead(37) == LOW) { 605 | count_flag++; 606 | setup_flag = 0; 607 | M5.Lcd.fillRect(0, 0, 80, 50, BLACK); 608 | M5.Lcd.setCursor(0, 0, 4); 609 | M5.Lcd.println(count_flag); 610 | M5.Lcd.fillRect(0, 30, 360, 170, BLACK); 611 | if (count_flag > 16) count_flag = 0; 612 | while (digitalRead(37) == LOW) 613 | ; 614 | } 615 | 616 | switch (count_flag) { 617 | case 0: 618 | joystick(); 619 | break; 620 | case 1: 621 | dac(); 622 | break; 623 | case 2: 624 | adc(); 625 | break; 626 | case 3: 627 | encoder(); 628 | break; 629 | case 4: 630 | matrix(); 631 | break; 632 | case 5: 633 | env(); 634 | break; 635 | case 6: 636 | luminosity(); 637 | break; 638 | case 7: 639 | key(); 640 | break; 641 | case 8: 642 | dcmotor(); 643 | break; 644 | case 9: 645 | relay(); 646 | break; 647 | case 10: 648 | servo(); 649 | break; 650 | case 11: 651 | stmpmotor(); 652 | break; 653 | case 12: 654 | rfid(); 655 | break; 656 | case 13: 657 | uart232(); 658 | break; 659 | case 14: 660 | uart485(); 661 | break; 662 | default: 663 | break; 664 | } 665 | } 666 | -------------------------------------------------------------------------------- /examples/FactoryTest_V1.0/FactoryTest_V1.0.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "MFRC522_I2C.h" 6 | #include "DHT12.h" 7 | #include "Adafruit_Sensor.h" 8 | #include 9 | 10 | int count_flag = 0; 11 | uint8_t setup_flag = 0; 12 | 13 | void joystick(void) { 14 | // put your main code here, to run repeatedly: 15 | uint16_t x_data = 0; 16 | uint16_t y_data = 0; 17 | uint8_t button_data = 0; 18 | 19 | M5.Lcd.setCursor(100, 30, 4); 20 | M5.Lcd.println("JOYSTICK"); 21 | M5.Lcd.setCursor(0, 60, 4); 22 | M5.Lcd.println("PIN : x_adc:35,y_adc:36,button:2"); 23 | 24 | if (!setup_flag) { 25 | setup_flag = 1; 26 | gpio_reset_pin(GPIO_NUM_2); 27 | gpio_reset_pin(GPIO_NUM_35); 28 | gpio_reset_pin(GPIO_NUM_36); 29 | pinMode(2, INPUT_PULLUP); 30 | } 31 | x_data = analogRead(35); 32 | y_data = analogRead(36); 33 | button_data = digitalRead(2); 34 | 35 | Serial.printf("x:%0d y:%0d button:%d\n", x_data, y_data, button_data); 36 | 37 | M5.Lcd.setCursor(30, 120, 4); 38 | M5.Lcd.printf("x:%04d y:%04d button:%d\n", x_data, y_data, button_data); 39 | 40 | delay(200); 41 | } 42 | 43 | #define DAC_ADDR 0x4C 44 | 45 | void outVoltage(uint8_t ch, uint16_t v) { 46 | Wire.beginTransmission(DAC_ADDR); 47 | Wire.write(0x10 | (ch << 1)); 48 | 49 | Wire.write((v >> 2) & 0xff); 50 | Wire.write((v << 6) & 0xff); 51 | Wire.endTransmission(); 52 | } 53 | void dac() { 54 | if (!setup_flag) { 55 | setup_flag = 1; 56 | gpio_reset_pin(GPIO_NUM_21); 57 | gpio_reset_pin(GPIO_NUM_22); 58 | Wire.begin(); 59 | 60 | M5.Lcd.setCursor(140, 30, 4); 61 | M5.Lcd.print("DAC"); 62 | M5.Lcd.setCursor(0, 60, 4); 63 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 64 | 65 | outVoltage(0, 256); // 1.25v 66 | outVoltage(1, 512); // 2.50v 67 | outVoltage(2, 768); // 3.75v 68 | outVoltage(3, 1023); // 5.00v 69 | } 70 | outVoltage(0, 256); // 1.25v 71 | outVoltage(1, 512); // 2.50v 72 | outVoltage(2, 768); // 3.75v 73 | outVoltage(3, 1023); // 5.00v 74 | } 75 | 76 | #define ADC_ADDR 0x48 77 | 78 | uint16_t InVoltage(uint8_t ch) { 79 | uint8_t data_L = 0; 80 | uint8_t data_H = 0; 81 | uint16_t data_adc = 0; 82 | 83 | Wire.beginTransmission(ADC_ADDR); 84 | Wire.write(0X01); 85 | Wire.write(0XC0 | (ch << 4)); 86 | Wire.write(0X83); 87 | Wire.endTransmission(); 88 | 89 | Wire.beginTransmission(ADC_ADDR); 90 | Wire.write(0x00); 91 | Wire.endTransmission(); 92 | 93 | delay(50); 94 | 95 | Wire.requestFrom(ADC_ADDR, 2); 96 | while (Wire.available()) { 97 | data_H = Wire.read(); 98 | data_L = Wire.read(); 99 | } 100 | 101 | data_adc = (data_H << 8) | data_L; 102 | return data_adc; 103 | } 104 | 105 | void adc() { 106 | uint16_t adc_ch0 = 0; 107 | uint16_t adc_ch1 = 0; 108 | uint16_t adc_ch2 = 0; 109 | uint16_t adc_ch3 = 0; 110 | 111 | if (!setup_flag) { 112 | setup_flag = 1; 113 | gpio_reset_pin(GPIO_NUM_21); 114 | gpio_reset_pin(GPIO_NUM_22); 115 | Wire.begin(); 116 | 117 | M5.Lcd.setCursor(140, 30, 4); 118 | M5.Lcd.print("ADC"); 119 | M5.Lcd.setCursor(0, 60, 4); 120 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 121 | } 122 | adc_ch0 = InVoltage(0); 123 | adc_ch1 = InVoltage(1); 124 | adc_ch2 = InVoltage(2); 125 | adc_ch3 = InVoltage(3); 126 | Serial.printf("ch0:%d ch1:%d ch2:%d ch3:%d\n", adc_ch0, adc_ch1, adc_ch2, 127 | adc_ch3); 128 | 129 | M5.Lcd.setCursor(0, 120, 4); 130 | M5.Lcd.printf("ch0:%05d ch1:%05d\n", adc_ch0, adc_ch1); 131 | M5.Lcd.printf("ch2:%05d ch3:%05d\n", adc_ch2, adc_ch3); 132 | // delay(500); 133 | } 134 | 135 | const int phaseA = 2; 136 | const int phaseB = 13; 137 | const int Button = 15; 138 | #define GET_CODE() uint8_t(digitalRead(phaseA) << 4 | digitalRead(phaseB)) 139 | 140 | int32_t count = 65536; 141 | int32_t count_last = 65536; 142 | int32_t count_change = 0; 143 | uint8_t code = 0; 144 | uint8_t code_old = 0; 145 | void encoderEvent() { 146 | code = GET_CODE(); 147 | if (code != code_old) { 148 | if (code == 0x00) { 149 | count_last = count; 150 | if (code_old == 0x10) { 151 | count--; 152 | count_change == -65536 ? count_change : count_change--; 153 | } else { 154 | count_change == 65536 ? count_change : count_change++; 155 | } 156 | } 157 | code_old = code; 158 | } 159 | } 160 | 161 | void encoder() { 162 | if (!setup_flag) { 163 | setup_flag = 1; 164 | gpio_reset_pin(GPIO_NUM_2); 165 | gpio_reset_pin(GPIO_NUM_13); 166 | gpio_reset_pin(GPIO_NUM_15); 167 | pinMode(phaseA, INPUT_PULLUP); 168 | pinMode(phaseB, INPUT_PULLUP); 169 | pinMode(Button, INPUT_PULLUP); 170 | // M5.Lcd.setCursor(100, 0, 4); 171 | // M5.Lcd.print("ENCODER"); 172 | 173 | M5.Lcd.setCursor(100, 30, 4); 174 | M5.Lcd.println("ENCODER"); 175 | M5.Lcd.setCursor(0, 60, 4); 176 | M5.Lcd.println("PIN : a:2,b:13,button:15"); 177 | 178 | code = GET_CODE(); 179 | code_old = code; 180 | } 181 | 182 | uint8_t value = digitalRead(Button); 183 | encoderEvent(); 184 | Serial.printf("code = %d,button = %d\r\n ", count_change, value); 185 | 186 | M5.Lcd.setCursor(30, 120, 4); 187 | M5.Lcd.printf("code = %05d,button = %d\r\n ", count_change, value); 188 | delay(1); 189 | } 190 | 191 | const byte ROWS = 4; // four rows 192 | const byte COLS = 4; // three columns 193 | 194 | byte rowPins[ROWS] = {17, 16, 21, 22}; 195 | byte colPins[COLS] = {5, 26, 13, 15}; 196 | 197 | char keys[ROWS][COLS] = {{'a', 'b', 'c', 'd'}, 198 | {'e', 'f', 'g', 'h'}, 199 | {'i', 'j', 'k', 'l'}, 200 | {'m', 'n', 'o', 'p'}}; 201 | Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 202 | // Keypad keypad; 203 | void key() { 204 | if (!setup_flag) { 205 | setup_flag = 1; 206 | // Wire.end(); 207 | 208 | // Wire.end(); 209 | // pinMode(21,INPUT_PULLUP); 210 | // pinMode(22,INPUT_PULLUP); 211 | gpio_reset_pin(GPIO_NUM_17); 212 | gpio_reset_pin(GPIO_NUM_16); 213 | gpio_reset_pin(GPIO_NUM_21); 214 | gpio_reset_pin(GPIO_NUM_22); 215 | gpio_reset_pin(GPIO_NUM_5); 216 | gpio_reset_pin(GPIO_NUM_26); 217 | gpio_reset_pin(GPIO_NUM_13); 218 | gpio_reset_pin(GPIO_NUM_15); 219 | 220 | M5.Lcd.setCursor(100, 30, 4); 221 | M5.Lcd.println("KEYBOARD"); 222 | M5.Lcd.setCursor(0, 60, 4); 223 | M5.Lcd.println("PIN : r0:17,r1:16,r2:21,r3:22"); 224 | M5.Lcd.println("PIN : c0: 5,c1:26,c2:13,c3:15"); 225 | keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 226 | } 227 | 228 | char key = keypad.getKey(); 229 | if (key) { 230 | Serial.println(key); 231 | M5.Lcd.fillRect(150, 150, 80, 50, BLACK); 232 | M5.Lcd.setCursor(150, 140, 4); 233 | M5.Lcd.printf("%c", key); 234 | } 235 | } 236 | 237 | #define DATA_PIN 15 238 | //#define CLK_PIN 4 239 | #define LED_TYPE WS2811 240 | #define COLOR_ORDER GRB 241 | #define NUM_LEDS 64 242 | CRGB leds[NUM_LEDS]; 243 | #define BRIGHTNESS 5 244 | 245 | int led_count = 0; 246 | void matrix() { 247 | if (!setup_flag) { 248 | setup_flag = 1; 249 | 250 | gpio_reset_pin(GPIO_NUM_15); 251 | 252 | M5.Lcd.setCursor(100, 30, 4); 253 | M5.Lcd.println("MATRIX"); 254 | M5.Lcd.setCursor(0, 60, 4); 255 | M5.Lcd.println("PIN : SIGNAL:15"); 256 | 257 | // FastLED.addLeds(leds, 258 | // NUM_LEDS).setCorrection(TypicalLEDStrip); 259 | // FastLED.addLeds(leds, 260 | // NUM_LEDS).setCorrection(TypicalLEDStrip); 261 | FastLED.addLeds(leds, NUM_LEDS); 262 | // NEOPIXEL 263 | FastLED.setBrightness(BRIGHTNESS); 264 | led_count = 0; 265 | } 266 | led_count++; 267 | if (led_count > 2000) led_count = 0; 268 | if ((led_count / 500) == 0) { 269 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Red; 270 | FastLED.show(); 271 | } 272 | 273 | if ((led_count / 500) == 1) { 274 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Green; 275 | FastLED.show(); 276 | } 277 | if ((led_count / 500) == 2) { 278 | for (int i = 0; i < 64; i++) leds[i] = CRGB::Blue; 279 | FastLED.show(); 280 | } 281 | } 282 | 283 | int dc_count = 0; 284 | void dcmotor() { 285 | if (!setup_flag) { 286 | setup_flag = 1; 287 | 288 | M5.Lcd.setCursor(100, 30, 4); 289 | M5.Lcd.println("DC-MOTOR"); 290 | M5.Lcd.setCursor(0, 60, 4); 291 | M5.Lcd.println("PIN : M+:13,M-:15,ea:21,eb:22"); 292 | 293 | pinMode(13, OUTPUT); 294 | pinMode(15, OUTPUT); 295 | pinMode(21, INPUT_PULLUP); 296 | pinMode(22, INPUT_PULLUP); 297 | dc_count = 0; 298 | } 299 | 300 | dc_count++; 301 | if (dc_count > 4000) dc_count = 0; 302 | 303 | digitalWrite(13, LOW); 304 | digitalWrite(15, HIGH); 305 | } 306 | const int In_0 = 13; 307 | int relay_count = 0; 308 | void relay(void) { 309 | if (!setup_flag) { 310 | setup_flag = 1; 311 | M5.Lcd.setCursor(100, 30, 4); 312 | M5.Lcd.println("RELAY * 8"); 313 | M5.Lcd.setCursor(0, 60, 4); 314 | M5.Lcd.println("PIN : 13"); 315 | pinMode(In_0, OUTPUT); 316 | digitalWrite(In_0, LOW); 317 | relay_count = 0; 318 | digitalWrite(In_0, HIGH); 319 | } 320 | relay_count++; 321 | if (relay_count > 2000) relay_count = 0; 322 | // if(relay_count == 1) 323 | // digitalWrite(In_0, LOW); 324 | // if(relay_count == 1000) 325 | digitalWrite(In_0, HIGH); 326 | } 327 | int servo_count = 0; 328 | int freq = 50; 329 | int ledChannel = 0; 330 | int resolution = 8; 331 | void servo() { 332 | if (!setup_flag) { 333 | setup_flag = 1; 334 | gpio_reset_pin(GPIO_NUM_15); 335 | 336 | M5.Lcd.setCursor(100, 30, 4); 337 | M5.Lcd.println("SERVO"); 338 | M5.Lcd.setCursor(0, 60, 4); 339 | M5.Lcd.println("PIN : 15"); 340 | 341 | ledcSetup(ledChannel, freq, resolution); 342 | ledcAttachPin(15, ledChannel); 343 | servo_count = 0; 344 | } 345 | 346 | servo_count++; 347 | if (servo_count > 3000) servo_count = 0; 348 | if ((servo_count / 1000) == 0) { 349 | ledcWrite(ledChannel, 6); // 0° 350 | } 351 | if ((servo_count / 1000) == 1) { 352 | ledcWrite(ledChannel, 18); // 0° 353 | } 354 | 355 | if ((servo_count / 1000) == 2) { 356 | ledcWrite(ledChannel, 30); // 0° 357 | } 358 | } 359 | const int MOTOR_A = 2; 360 | const int MOTOR_B = 5; 361 | const int MOTOR_C = 12; 362 | const int MOTOR_D = 13; 363 | void stmpmotor(void) { 364 | if (!setup_flag) { 365 | setup_flag = 1; 366 | gpio_reset_pin(GPIO_NUM_2); 367 | gpio_reset_pin(GPIO_NUM_5); 368 | gpio_reset_pin(GPIO_NUM_12); 369 | gpio_reset_pin(GPIO_NUM_13); 370 | 371 | M5.Lcd.setCursor(100, 30, 4); 372 | M5.Lcd.println("STEPMOTOR"); 373 | M5.Lcd.setCursor(0, 60, 4); 374 | M5.Lcd.println("PIN :A:2,B:5,C:12,D:13"); 375 | pinMode(MOTOR_A, OUTPUT); 376 | pinMode(MOTOR_B, OUTPUT); 377 | pinMode(MOTOR_C, OUTPUT); 378 | pinMode(MOTOR_D, OUTPUT); 379 | } 380 | 381 | digitalWrite(MOTOR_A, HIGH); 382 | digitalWrite(MOTOR_B, HIGH); 383 | digitalWrite(MOTOR_C, LOW); 384 | digitalWrite(MOTOR_D, LOW); 385 | delay(2); 386 | digitalWrite(MOTOR_A, LOW); 387 | digitalWrite(MOTOR_B, HIGH); 388 | digitalWrite(MOTOR_C, HIGH); 389 | digitalWrite(MOTOR_D, LOW); 390 | delay(2); 391 | 392 | digitalWrite(MOTOR_A, LOW); 393 | digitalWrite(MOTOR_B, LOW); 394 | digitalWrite(MOTOR_C, HIGH); 395 | digitalWrite(MOTOR_D, HIGH); 396 | delay(2); 397 | digitalWrite(MOTOR_A, HIGH); 398 | digitalWrite(MOTOR_B, LOW); 399 | digitalWrite(MOTOR_C, LOW); 400 | digitalWrite(MOTOR_D, HIGH); 401 | delay(2); 402 | } 403 | MFRC522 mfrc522(0x28); 404 | void ShowReaderDetails() { 405 | // Get the MFRC522 software version 406 | byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); 407 | Serial.print(F("MFRC522 Software Version: 0x")); 408 | Serial.print(v, HEX); 409 | if (v == 0x91) 410 | Serial.print(F(" = v1.0")); 411 | else if (v == 0x92) 412 | Serial.print(F(" = v2.0")); 413 | else 414 | Serial.print(F(" (unknown)")); 415 | Serial.println(""); 416 | // When 0x00 or 0xFF is returned, communication probably failed 417 | if ((v == 0x00) || (v == 0xFF)) { 418 | Serial.println( 419 | F("WARNING: Communication failure, is the MFRC522 properly " 420 | "connected?")); 421 | } 422 | } 423 | void rfid() { 424 | if (!setup_flag) { 425 | setup_flag = 1; 426 | gpio_reset_pin(GPIO_NUM_22); 427 | gpio_reset_pin(GPIO_NUM_21); 428 | Wire.begin(); // Initialize I2C 429 | 430 | M5.Lcd.setCursor(140, 30, 4); 431 | M5.Lcd.print("RFID"); 432 | M5.Lcd.setCursor(0, 60, 4); 433 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 434 | mfrc522.PCD_Init(); // Init MFRC522 435 | ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader 436 | // details 437 | // Serial.println(F("Scan PICC to see UID, type, and data blocks...")); 438 | // M5.Lcd.println("Scan PICC to see UID, type, and data blocks..."); 439 | } 440 | if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) { 441 | // delay(50); 442 | // M5.Lcd.println("Scan PICC to see UID, type, and data blocks..."); 443 | // return; 444 | } 445 | 446 | Serial.print(F("Card UID:")); 447 | M5.Lcd.println(" "); 448 | M5.Lcd.setCursor(0, 90, 4); 449 | for (byte i = 0; i < mfrc522.uid.size; i++) { 450 | // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 451 | // Serial.print(mfrc522.uid.uidByte[i], HEX); 452 | // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 453 | // Serial.print(mfrc522.uid.uidByte[i], HEX); 454 | M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); 455 | M5.Lcd.print(mfrc522.uid.uidByte[i], HEX); 456 | } 457 | // Serial.println(); 458 | } 459 | DHT12 dht12; // Preset scale CELSIUS and ID 0x5c. 460 | void dht() { 461 | if (!setup_flag) { 462 | setup_flag = 1; 463 | gpio_reset_pin(GPIO_NUM_22); 464 | gpio_reset_pin(GPIO_NUM_21); 465 | Wire.begin(); 466 | 467 | M5.Lcd.setCursor(100, 30, 4); 468 | M5.Lcd.print("TEMPERATURE"); 469 | M5.Lcd.setCursor(0, 60, 4); 470 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 471 | } 472 | 473 | float tmp = dht12.readTemperature(); 474 | float hum = dht12.readHumidity(); 475 | M5.Lcd.setCursor(0, 100, 4); 476 | M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%%\r\n", tmp, hum); 477 | } 478 | 479 | Adafruit_BMP280 bme; 480 | 481 | void air() { 482 | if (!setup_flag) { 483 | setup_flag = 1; 484 | gpio_reset_pin(GPIO_NUM_22); 485 | gpio_reset_pin(GPIO_NUM_21); 486 | Wire.begin(); 487 | 488 | M5.Lcd.setCursor(80, 30, 4); 489 | M5.Lcd.print("AIR_PRESSURE"); 490 | M5.Lcd.setCursor(0, 60, 4); 491 | M5.Lcd.println("PIN : SCL:22,SDA:21"); 492 | 493 | // while(!bme.begin(0x76)){ 494 | // Serial.println("Could not find a valid BMP280 sensor, check 495 | // wiring!"); 496 | // } 497 | } 498 | 499 | float pressure = bme.readPressure(); 500 | M5.Lcd.setCursor(0, 100, 4); 501 | M5.Lcd.printf("Pressure:%2.0fPa\r\n", pressure); 502 | delay(100); 503 | } 504 | 505 | const int Analog = 35; 506 | const int Digtal = 2; 507 | uint16_t a_data; 508 | uint16_t d_data; 509 | void luminosity() { 510 | if (!setup_flag) { 511 | setup_flag = 1; 512 | gpio_reset_pin(GPIO_NUM_35); 513 | gpio_reset_pin(GPIO_NUM_2); 514 | 515 | M5.Lcd.setCursor(80, 30, 4); 516 | if (count_flag == 7) 517 | M5.Lcd.print("LUMINOSITY"); 518 | else 519 | M5.Lcd.print("MICROPHONE"); 520 | M5.Lcd.setCursor(0, 60, 4); 521 | M5.Lcd.println("PIN : a:35,d:2"); 522 | 523 | pinMode(Digtal, INPUT_PULLUP); 524 | } 525 | 526 | a_data = analogRead(Analog); 527 | d_data = digitalRead(Digtal); 528 | 529 | Serial.printf("Analog:%0d Digtal:%0d\n", a_data, d_data); 530 | 531 | M5.Lcd.setCursor(30, 120, 4); 532 | M5.Lcd.printf("Analog:%0d Digtal:%0d\n", a_data, d_data); 533 | 534 | delay(50); 535 | } 536 | int rx_num = 0; 537 | int rx_count = 0; 538 | void uart232() { 539 | if (!setup_flag) { 540 | setup_flag = 1; 541 | gpio_reset_pin(GPIO_NUM_16); 542 | gpio_reset_pin(GPIO_NUM_17); 543 | Serial2.begin(115200, SERIAL_8N1, 16, 17); 544 | M5.Lcd.setCursor(80, 30, 4); 545 | M5.Lcd.print("RS232"); 546 | M5.Lcd.setCursor(0, 60, 4); 547 | M5.Lcd.println("PIN :TX:17,RX:16"); 548 | rx_count = 0; 549 | rx_num = 0; 550 | } 551 | 552 | if (Serial2.available()) { 553 | int ch = Serial2.read(); 554 | // Serial.write(ch); 555 | if (ch == 'a') { 556 | rx_num++; 557 | } 558 | M5.Lcd.setCursor(30, 120, 4); 559 | M5.Lcd.printf("rx_num = %d\n", rx_num); 560 | } 561 | rx_count++; 562 | if (rx_count > 6000) rx_count = 0; 563 | if (rx_count == 10) Serial2.write('a'); 564 | } 565 | 566 | // int rx_num = 0; 567 | // int rx_count = 0; 568 | void uart485() { 569 | if (!setup_flag) { 570 | setup_flag = 1; 571 | gpio_reset_pin(GPIO_NUM_16); 572 | gpio_reset_pin(GPIO_NUM_17); 573 | gpio_reset_pin(GPIO_NUM_21); 574 | gpio_reset_pin(GPIO_NUM_22); 575 | Serial2.begin(115200, SERIAL_8N1, 16, 17); 576 | Serial1.begin(115200, SERIAL_8N1, 22, 21); 577 | M5.Lcd.setCursor(80, 30, 4); 578 | M5.Lcd.print("RS485"); 579 | M5.Lcd.setCursor(0, 60, 4); 580 | M5.Lcd.println("PIN :TX:17,RX:16"); 581 | rx_count = 0; 582 | rx_num = 0; 583 | } 584 | 585 | if (Serial1.available()) { 586 | int ch = Serial1.read(); 587 | // Serial.write(ch); 588 | if (ch == 'a') { 589 | rx_num++; 590 | } 591 | M5.Lcd.setCursor(30, 120, 4); 592 | M5.Lcd.printf("rx_num = %d\n", rx_num); 593 | } 594 | rx_count++; 595 | if (rx_count > 6000) rx_count = 0; 596 | if (rx_count == 10) Serial2.write('a'); 597 | } 598 | void setup() { 599 | // put your setup code here, to run once: 600 | M5.begin(); 601 | dacWrite(25, 0); 602 | 603 | M5.Lcd.setCursor(90, 0, 4); 604 | M5.Lcd.print("FactoryTest"); 605 | 606 | pinMode(37, INPUT_PULLUP); 607 | 608 | M5.Lcd.setCursor(220, 210, 4); 609 | M5.Lcd.println("mode"); 610 | } 611 | 612 | void loop() { 613 | // put your main code here, to run repeatedly: 614 | 615 | if (digitalRead(37) == LOW) { 616 | count_flag++; 617 | setup_flag = 0; 618 | M5.Lcd.fillRect(0, 0, 80, 50, BLACK); 619 | M5.Lcd.setCursor(0, 0, 4); 620 | M5.Lcd.println(count_flag); 621 | M5.Lcd.fillRect(0, 30, 360, 170, BLACK); 622 | if (count_flag > 16) count_flag = 0; 623 | while (digitalRead(37) == LOW) 624 | ; 625 | } 626 | 627 | switch (count_flag) { 628 | case 0: 629 | joystick(); 630 | break; 631 | case 1: 632 | dac(); 633 | break; 634 | case 2: 635 | adc(); 636 | break; 637 | case 3: 638 | encoder(); 639 | break; 640 | case 4: 641 | matrix(); 642 | break; 643 | case 5: 644 | dht(); 645 | break; 646 | case 6: 647 | air(); 648 | break; 649 | case 7: 650 | luminosity(); 651 | break; 652 | case 8: 653 | key(); 654 | break; 655 | case 9: 656 | dcmotor(); 657 | break; 658 | case 10: 659 | relay(); 660 | break; 661 | case 11: 662 | servo(); 663 | break; 664 | case 12: 665 | stmpmotor(); 666 | break; 667 | case 13: 668 | rfid(); 669 | break; 670 | case 14: 671 | uart232(); 672 | break; 673 | case 15: 674 | uart485(); 675 | break; 676 | default: 677 | break; 678 | } 679 | } 680 | -------------------------------------------------------------------------------- /examples/RFID/MFRC522_I2C.h: -------------------------------------------------------------------------------- 1 | /** 2 | * MFRC522_I2C.h - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS I2C BY AROZCAN 3 | * MFRC522_I2C.h - Based on ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI Library BY COOQROBOT. 4 | * Based on code Dr.Leong ( WWW.B2CQSHOP.COM ) 5 | * Created by Miguel Balboa (circuitito.com), Jan, 2012. 6 | * Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.) 7 | * Extended by Tom Clement with functionality to write to sector 0 of UID changeable Mifare cards. 8 | * Extended by Ahmet Remzi Ozcan with I2C functionality. 9 | * Author: arozcan @ https://github.com/arozcan/MFRC522-I2C-Library 10 | * Released into the public domain. 11 | * 12 | * Please read this file for an overview and then MFRC522.cpp for comments on the specific functions. 13 | * Search for "mf-rc522" on ebay.com to purchase the MF-RC522 board. 14 | * 15 | * There are three hardware components involved: 16 | * 1) The micro controller: An Arduino 17 | * 2) The PCD (short for Proximity Coupling Device): NXP MFRC522 Contactless Reader IC 18 | * 3) The PICC (short for Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203. 19 | * 20 | * The microcontroller and card reader uses I2C for communication. 21 | * The protocol is described in the MFRC522 datasheet: http://www.nxp.com/documents/data_sheet/MFRC522.pdf 22 | * 23 | * The card reader and the tags communicate using a 13.56MHz electromagnetic field. 24 | * The protocol is defined in ISO/IEC 14443-3 Identification cards -- Contactless integrated circuit cards -- Proximity cards -- Part 3: Initialization and anticollision". 25 | * A free version of the final draft can be found at http://wg8.de/wg8n1496_17n3613_Ballot_FCD14443-3.pdf 26 | * Details are found in chapter 6, Type A – Initialization and anticollision. 27 | * 28 | * If only the PICC UID is wanted, the above documents has all the needed information. 29 | * To read and write from MIFARE PICCs, the MIFARE protocol is used after the PICC has been selected. 30 | * The MIFARE Classic chips and protocol is described in the datasheets: 31 | * 1K: http://www.nxp.com/documents/data_sheet/MF1S503x.pdf 32 | * 4K: http://www.nxp.com/documents/data_sheet/MF1S703x.pdf 33 | * Mini: http://www.idcardmarket.com/download/mifare_S20_datasheet.pdf 34 | * The MIFARE Ultralight chip and protocol is described in the datasheets: 35 | * Ultralight: http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf 36 | * Ultralight C: http://www.nxp.com/documents/short_data_sheet/MF0ICU2_SDS.pdf 37 | * 38 | * MIFARE Classic 1K (MF1S503x): 39 | * Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes. 40 | * The blocks are numbered 0-63. 41 | * Block 3 in each sector is the Sector Trailer. See http://www.nxp.com/documents/data_sheet/MF1S503x.pdf sections 8.6 and 8.7: 42 | * Bytes 0-5: Key A 43 | * Bytes 6-8: Access Bits 44 | * Bytes 9: User data 45 | * Bytes 10-15: Key B (or user data) 46 | * Block 0 is read-only manufacturer data. 47 | * To access a block, an authentication using a key from the block's sector must be performed first. 48 | * Example: To read from block 10, first authenticate using a key from sector 3 (blocks 8-11). 49 | * All keys are set to FFFFFFFFFFFFh at chip delivery. 50 | * Warning: Please read section 8.7 "Memory Access". It includes this text: if the PICC detects a format violation the whole sector is irreversibly blocked. 51 | * To use a block in "value block" mode (for Increment/Decrement operations) you need to change the sector trailer. Use PICC_SetAccessBits() to calculate the bit patterns. 52 | * MIFARE Classic 4K (MF1S703x): 53 | * Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes. 54 | * The blocks are numbered 0-255. 55 | * The last block in each sector is the Sector Trailer like above. 56 | * MIFARE Classic Mini (MF1 IC S20): 57 | * Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes. 58 | * The blocks are numbered 0-19. 59 | * The last block in each sector is the Sector Trailer like above. 60 | * 61 | * MIFARE Ultralight (MF0ICU1): 62 | * Has 16 pages of 4 bytes = 64 bytes. 63 | * Pages 0 + 1 is used for the 7-byte UID. 64 | * Page 2 contains the last check digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2) 65 | * Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0. 66 | * Pages 4-15 are read/write unless blocked by the lock bytes in page 2. 67 | * MIFARE Ultralight C (MF0ICU2): 68 | * Has 48 pages of 4 bytes = 192 bytes. 69 | * Pages 0 + 1 is used for the 7-byte UID. 70 | * Page 2 contains the last check digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2) 71 | * Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0. 72 | * Pages 4-39 are read/write unless blocked by the lock bytes in page 2. 73 | * Page 40 Lock bytes 74 | * Page 41 16 bit one way counter 75 | * Pages 42-43 Authentication configuration 76 | * Pages 44-47 Authentication key 77 | */ 78 | #ifndef MFRC522_h 79 | #define MFRC522_h 80 | 81 | #include 82 | #include 83 | 84 | // Firmware data for self-test 85 | // Reference values based on firmware version 86 | // Hint: if needed, you can remove unused self-test data to save flash memory 87 | // 88 | // Version 0.0 (0x90) 89 | // Philips Semiconductors; Preliminary Specification Revision 2.0 - 01 August 2005; 16.1 Sefttest 90 | const byte MFRC522_firmware_referenceV0_0[] PROGMEM = { 91 | 0x00, 0x87, 0x98, 0x0f, 0x49, 0xFF, 0x07, 0x19, 92 | 0xBF, 0x22, 0x30, 0x49, 0x59, 0x63, 0xAD, 0xCA, 93 | 0x7F, 0xE3, 0x4E, 0x03, 0x5C, 0x4E, 0x49, 0x50, 94 | 0x47, 0x9A, 0x37, 0x61, 0xE7, 0xE2, 0xC6, 0x2E, 95 | 0x75, 0x5A, 0xED, 0x04, 0x3D, 0x02, 0x4B, 0x78, 96 | 0x32, 0xFF, 0x58, 0x3B, 0x7C, 0xE9, 0x00, 0x94, 97 | 0xB4, 0x4A, 0x59, 0x5B, 0xFD, 0xC9, 0x29, 0xDF, 98 | 0x35, 0x96, 0x98, 0x9E, 0x4F, 0x30, 0x32, 0x8D 99 | }; 100 | // Version 1.0 (0x91) 101 | // NXP Semiconductors; Rev. 3.8 - 17 September 2014; 16.1.1 Self test 102 | const byte MFRC522_firmware_referenceV1_0[] PROGMEM = { 103 | 0x00, 0xC6, 0x37, 0xD5, 0x32, 0xB7, 0x57, 0x5C, 104 | 0xC2, 0xD8, 0x7C, 0x4D, 0xD9, 0x70, 0xC7, 0x73, 105 | 0x10, 0xE6, 0xD2, 0xAA, 0x5E, 0xA1, 0x3E, 0x5A, 106 | 0x14, 0xAF, 0x30, 0x61, 0xC9, 0x70, 0xDB, 0x2E, 107 | 0x64, 0x22, 0x72, 0xB5, 0xBD, 0x65, 0xF4, 0xEC, 108 | 0x22, 0xBC, 0xD3, 0x72, 0x35, 0xCD, 0xAA, 0x41, 109 | 0x1F, 0xA7, 0xF3, 0x53, 0x14, 0xDE, 0x7E, 0x02, 110 | 0xD9, 0x0F, 0xB5, 0x5E, 0x25, 0x1D, 0x29, 0x79 111 | }; 112 | // Version 2.0 (0x92) 113 | // NXP Semiconductors; Rev. 3.8 - 17 September 2014; 16.1.1 Self test 114 | const byte MFRC522_firmware_referenceV2_0[] PROGMEM = { 115 | 0x00, 0xEB, 0x66, 0xBA, 0x57, 0xBF, 0x23, 0x95, 116 | 0xD0, 0xE3, 0x0D, 0x3D, 0x27, 0x89, 0x5C, 0xDE, 117 | 0x9D, 0x3B, 0xA7, 0x00, 0x21, 0x5B, 0x89, 0x82, 118 | 0x51, 0x3A, 0xEB, 0x02, 0x0C, 0xA5, 0x00, 0x49, 119 | 0x7C, 0x84, 0x4D, 0xB3, 0xCC, 0xD2, 0x1B, 0x81, 120 | 0x5D, 0x48, 0x76, 0xD5, 0x71, 0x61, 0x21, 0xA9, 121 | 0x86, 0x96, 0x83, 0x38, 0xCF, 0x9D, 0x5B, 0x6D, 122 | 0xDC, 0x15, 0xBA, 0x3E, 0x7D, 0x95, 0x3B, 0x2F 123 | }; 124 | // Clone 125 | // Fudan Semiconductor FM17522 (0x88) 126 | const byte FM17522_firmware_reference[] PROGMEM = { 127 | 0x00, 0xD6, 0x78, 0x8C, 0xE2, 0xAA, 0x0C, 0x18, 128 | 0x2A, 0xB8, 0x7A, 0x7F, 0xD3, 0x6A, 0xCF, 0x0B, 129 | 0xB1, 0x37, 0x63, 0x4B, 0x69, 0xAE, 0x91, 0xC7, 130 | 0xC3, 0x97, 0xAE, 0x77, 0xF4, 0x37, 0xD7, 0x9B, 131 | 0x7C, 0xF5, 0x3C, 0x11, 0x8F, 0x15, 0xC3, 0xD7, 132 | 0xC1, 0x5B, 0x00, 0x2A, 0xD0, 0x75, 0xDE, 0x9E, 133 | 0x51, 0x64, 0xAB, 0x3E, 0xE9, 0x15, 0xB5, 0xAB, 134 | 0x56, 0x9A, 0x98, 0x82, 0x26, 0xEA, 0x2A, 0x62 135 | }; 136 | 137 | class MFRC522 { 138 | public: 139 | // MFRC522 registers. Described in chapter 9 of the datasheet. 140 | enum PCD_Register { 141 | // Page 0: Command and status 142 | // 0x00 // reserved for future use 143 | CommandReg = 0x01 , // starts and stops command execution 144 | ComIEnReg = 0x02 , // enable and disable interrupt request control bits 145 | DivIEnReg = 0x03 , // enable and disable interrupt request control bits 146 | ComIrqReg = 0x04 , // interrupt request bits 147 | DivIrqReg = 0x05 , // interrupt request bits 148 | ErrorReg = 0x06 , // error bits showing the error status of the last command executed 149 | Status1Reg = 0x07 , // communication status bits 150 | Status2Reg = 0x08 , // receiver and transmitter status bits 151 | FIFODataReg = 0x09 , // input and output of 64 byte FIFO buffer 152 | FIFOLevelReg = 0x0A , // number of bytes stored in the FIFO buffer 153 | WaterLevelReg = 0x0B , // level for FIFO underflow and overflow warning 154 | ControlReg = 0x0C , // miscellaneous control registers 155 | BitFramingReg = 0x0D , // adjustments for bit-oriented frames 156 | CollReg = 0x0E , // bit position of the first bit-collision detected on the RF interface 157 | // 0x0F // reserved for future use 158 | 159 | // Page 1: Command 160 | // 0x10 // reserved for future use 161 | ModeReg = 0x11 , // defines general modes for transmitting and receiving 162 | TxModeReg = 0x12 , // defines transmission data rate and framing 163 | RxModeReg = 0x13 , // defines reception data rate and framing 164 | TxControlReg = 0x14 , // controls the logical behavior of the antenna driver pins TX1 and TX2 165 | TxASKReg = 0x15 , // controls the setting of the transmission modulation 166 | TxSelReg = 0x16 , // selects the internal sources for the antenna driver 167 | RxSelReg = 0x17 , // selects internal receiver settings 168 | RxThresholdReg = 0x18 , // selects thresholds for the bit decoder 169 | DemodReg = 0x19 , // defines demodulator settings 170 | // 0x1A // reserved for future use 171 | // 0x1B // reserved for future use 172 | MfTxReg = 0x1C , // controls some MIFARE communication transmit parameters 173 | MfRxReg = 0x1D , // controls some MIFARE communication receive parameters 174 | // 0x1E // reserved for future use 175 | SerialSpeedReg = 0x1F , // selects the speed of the serial UART interface 176 | 177 | // Page 2: Configuration 178 | // 0x20 // reserved for future use 179 | CRCResultRegH = 0x21 , // shows the MSB and LSB values of the CRC calculation 180 | CRCResultRegL = 0x22 , 181 | // 0x23 // reserved for future use 182 | ModWidthReg = 0x24 , // controls the ModWidth setting? 183 | // 0x25 // reserved for future use 184 | RFCfgReg = 0x26 , // configures the receiver gain 185 | GsNReg = 0x27 , // selects the conductance of the antenna driver pins TX1 and TX2 for modulation 186 | CWGsPReg = 0x28 , // defines the conductance of the p-driver output during periods of no modulation 187 | ModGsPReg = 0x29 , // defines the conductance of the p-driver output during periods of modulation 188 | TModeReg = 0x2A , // defines settings for the internal timer 189 | TPrescalerReg = 0x2B , // the lower 8 bits of the TPrescaler value. The 4 high bits are in TModeReg. 190 | TReloadRegH = 0x2C , // defines the 16-bit timer reload value 191 | TReloadRegL = 0x2D , 192 | TCounterValueRegH = 0x2E , // shows the 16-bit timer value 193 | TCounterValueRegL = 0x2F , 194 | 195 | // Page 3: Test Registers 196 | // 0x30 // reserved for future use 197 | TestSel1Reg = 0x31 , // general test signal configuration 198 | TestSel2Reg = 0x32 , // general test signal configuration 199 | TestPinEnReg = 0x33 , // enables pin output driver on pins D1 to D7 200 | TestPinValueReg = 0x34 , // defines the values for D1 to D7 when it is used as an I/O bus 201 | TestBusReg = 0x35 , // shows the status of the internal test bus 202 | AutoTestReg = 0x36 , // controls the digital self test 203 | VersionReg = 0x37 , // shows the software version 204 | AnalogTestReg = 0x38 , // controls the pins AUX1 and AUX2 205 | TestDAC1Reg = 0x39 , // defines the test value for TestDAC1 206 | TestDAC2Reg = 0x3A , // defines the test value for TestDAC2 207 | TestADCReg = 0x3B // shows the value of ADC I and Q channels 208 | // 0x3C // reserved for production tests 209 | // 0x3D // reserved for production tests 210 | // 0x3E // reserved for production tests 211 | // 0x3F // reserved for production tests 212 | }; 213 | 214 | // MFRC522 commands. Described in chapter 10 of the datasheet. 215 | enum PCD_Command { 216 | PCD_Idle = 0x00, // no action, cancels current command execution 217 | PCD_Mem = 0x01, // stores 25 bytes into the internal buffer 218 | PCD_GenerateRandomID = 0x02, // generates a 10-byte random ID number 219 | PCD_CalcCRC = 0x03, // activates the CRC coprocessor or performs a self test 220 | PCD_Transmit = 0x04, // transmits data from the FIFO buffer 221 | PCD_NoCmdChange = 0x07, // no command change, can be used to modify the CommandReg register bits without affecting the command, for example, the PowerDown bit 222 | PCD_Receive = 0x08, // activates the receiver circuits 223 | PCD_Transceive = 0x0C, // transmits data from FIFO buffer to antenna and automatically activates the receiver after transmission 224 | PCD_MFAuthent = 0x0E, // performs the MIFARE standard authentication as a reader 225 | PCD_SoftReset = 0x0F // resets the MFRC522 226 | }; 227 | 228 | // MFRC522 RxGain[2:0] masks, defines the receiver's signal voltage gain factor (on the PCD). 229 | // Described in 9.3.3.6 / table 98 of the datasheet at http://www.nxp.com/documents/data_sheet/MFRC522.pdf 230 | enum PCD_RxGain { 231 | RxGain_18dB = 0x00 << 4, // 000b - 18 dB, minimum 232 | RxGain_23dB = 0x01 << 4, // 001b - 23 dB 233 | RxGain_18dB_2 = 0x02 << 4, // 010b - 18 dB, it seems 010b is a duplicate for 000b 234 | RxGain_23dB_2 = 0x03 << 4, // 011b - 23 dB, it seems 011b is a duplicate for 001b 235 | RxGain_33dB = 0x04 << 4, // 100b - 33 dB, average, and typical default 236 | RxGain_38dB = 0x05 << 4, // 101b - 38 dB 237 | RxGain_43dB = 0x06 << 4, // 110b - 43 dB 238 | RxGain_48dB = 0x07 << 4, // 111b - 48 dB, maximum 239 | RxGain_min = 0x00 << 4, // 000b - 18 dB, minimum, convenience for RxGain_18dB 240 | RxGain_avg = 0x04 << 4, // 100b - 33 dB, average, convenience for RxGain_33dB 241 | RxGain_max = 0x07 << 4 // 111b - 48 dB, maximum, convenience for RxGain_48dB 242 | }; 243 | 244 | // Commands sent to the PICC. 245 | enum PICC_Command { 246 | // The commands used by the PCD to manage communication with several PICCs (ISO 14443-3, Type A, section 6.4) 247 | PICC_CMD_REQA = 0x26, // REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame. 248 | PICC_CMD_WUPA = 0x52, // Wake-UP command, Type A. Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anticollision or selection. 7 bit frame. 249 | PICC_CMD_CT = 0x88, // Cascade Tag. Not really a command, but used during anti collision. 250 | PICC_CMD_SEL_CL1 = 0x93, // Anti collision/Select, Cascade Level 1 251 | PICC_CMD_SEL_CL2 = 0x95, // Anti collision/Select, Cascade Level 2 252 | PICC_CMD_SEL_CL3 = 0x97, // Anti collision/Select, Cascade Level 3 253 | PICC_CMD_HLTA = 0x50, // HaLT command, Type A. Instructs an ACTIVE PICC to go to state HALT. 254 | // The commands used for MIFARE Classic (from http://www.nxp.com/documents/data_sheet/MF1S503x.pdf, Section 9) 255 | // Use PCD_MFAuthent to authenticate access to a sector, then use these commands to read/write/modify the blocks on the sector. 256 | // The read/write commands can also be used for MIFARE Ultralight. 257 | PICC_CMD_MF_AUTH_KEY_A = 0x60, // Perform authentication with Key A 258 | PICC_CMD_MF_AUTH_KEY_B = 0x61, // Perform authentication with Key B 259 | PICC_CMD_MF_READ = 0x30, // Reads one 16 byte block from the authenticated sector of the PICC. Also used for MIFARE Ultralight. 260 | PICC_CMD_MF_WRITE = 0xA0, // Writes one 16 byte block to the authenticated sector of the PICC. Called "COMPATIBILITY WRITE" for MIFARE Ultralight. 261 | PICC_CMD_MF_DECREMENT = 0xC0, // Decrements the contents of a block and stores the result in the internal data register. 262 | PICC_CMD_MF_INCREMENT = 0xC1, // Increments the contents of a block and stores the result in the internal data register. 263 | PICC_CMD_MF_RESTORE = 0xC2, // Reads the contents of a block into the internal data register. 264 | PICC_CMD_MF_TRANSFER = 0xB0, // Writes the contents of the internal data register to a block. 265 | // The commands used for MIFARE Ultralight (from http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf, Section 8.6) 266 | // The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight. 267 | PICC_CMD_UL_WRITE = 0xA2 // Writes one 4 byte page to the PICC. 268 | }; 269 | 270 | // MIFARE constants that does not fit anywhere else 271 | enum MIFARE_Misc { 272 | MF_ACK = 0xA, // The MIFARE Classic uses a 4 bit ACK/NAK. Any other value than 0xA is NAK. 273 | MF_KEY_SIZE = 6 // A Mifare Crypto1 key is 6 bytes. 274 | }; 275 | 276 | // PICC types we can detect. Remember to update PICC_GetTypeName() if you add more. 277 | enum PICC_Type { 278 | PICC_TYPE_UNKNOWN = 0, 279 | PICC_TYPE_ISO_14443_4 = 1, // PICC compliant with ISO/IEC 14443-4 280 | PICC_TYPE_ISO_18092 = 2, // PICC compliant with ISO/IEC 18092 (NFC) 281 | PICC_TYPE_MIFARE_MINI = 3, // MIFARE Classic protocol, 320 bytes 282 | PICC_TYPE_MIFARE_1K = 4, // MIFARE Classic protocol, 1KB 283 | PICC_TYPE_MIFARE_4K = 5, // MIFARE Classic protocol, 4KB 284 | PICC_TYPE_MIFARE_UL = 6, // MIFARE Ultralight or Ultralight C 285 | PICC_TYPE_MIFARE_PLUS = 7, // MIFARE Plus 286 | PICC_TYPE_TNP3XXX = 8, // Only mentioned in NXP AN 10833 MIFARE Type Identification Procedure 287 | PICC_TYPE_NOT_COMPLETE = 255 // SAK indicates UID is not complete. 288 | }; 289 | 290 | // Return codes from the functions in this class. Remember to update GetStatusCodeName() if you add more. 291 | enum StatusCode { 292 | STATUS_OK = 1, // Success 293 | STATUS_ERROR = 2, // Error in communication 294 | STATUS_COLLISION = 3, // Collission detected 295 | STATUS_TIMEOUT = 4, // Timeout in communication. 296 | STATUS_NO_ROOM = 5, // A buffer is not big enough. 297 | STATUS_INTERNAL_ERROR = 6, // Internal error in the code. Should not happen ;-) 298 | STATUS_INVALID = 7, // Invalid argument. 299 | STATUS_CRC_WRONG = 8, // The CRC_A does not match 300 | STATUS_MIFARE_NACK = 9 // A MIFARE PICC responded with NAK. 301 | }; 302 | 303 | // A struct used for passing the UID of a PICC. 304 | typedef struct { 305 | byte size; // Number of bytes in the UID. 4, 7 or 10. 306 | byte uidByte[10]; 307 | byte sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection. 308 | } Uid; 309 | 310 | // A struct used for passing a MIFARE Crypto1 key 311 | typedef struct { 312 | byte keyByte[MF_KEY_SIZE]; 313 | } MIFARE_Key; 314 | 315 | // Member variables 316 | Uid uid; // Used by PICC_ReadCardSerial(). 317 | 318 | // Size of the MFRC522 FIFO 319 | static const byte FIFO_SIZE = 64; // The FIFO is 64 bytes. 320 | 321 | ///////////////////////////////////////////////////////////////////////////////////// 322 | // Functions for setting up the Arduino 323 | ///////////////////////////////////////////////////////////////////////////////////// 324 | MFRC522(byte chipAddress); 325 | 326 | ///////////////////////////////////////////////////////////////////////////////////// 327 | // Basic interface functions for communicating with the MFRC522 328 | ///////////////////////////////////////////////////////////////////////////////////// 329 | void PCD_WriteRegister(byte reg, byte value); 330 | void PCD_WriteRegister(byte reg, byte count, byte *values); 331 | byte PCD_ReadRegister(byte reg); 332 | void PCD_ReadRegister(byte reg, byte count, byte *values, byte rxAlign = 0); 333 | void setBitMask(unsigned char reg, unsigned char mask); 334 | void PCD_SetRegisterBitMask(byte reg, byte mask); 335 | void PCD_ClearRegisterBitMask(byte reg, byte mask); 336 | byte PCD_CalculateCRC(byte *data, byte length, byte *result); 337 | 338 | ///////////////////////////////////////////////////////////////////////////////////// 339 | // Functions for manipulating the MFRC522 340 | ///////////////////////////////////////////////////////////////////////////////////// 341 | void PCD_Init(); 342 | void PCD_Reset(); 343 | void PCD_AntennaOn(); 344 | void PCD_AntennaOff(); 345 | byte PCD_GetAntennaGain(); 346 | void PCD_SetAntennaGain(byte mask); 347 | bool PCD_PerformSelfTest(); 348 | 349 | ///////////////////////////////////////////////////////////////////////////////////// 350 | // Functions for communicating with PICCs 351 | ///////////////////////////////////////////////////////////////////////////////////// 352 | byte PCD_TransceiveData(byte *sendData, byte sendLen, byte *backData, byte *backLen, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false); 353 | byte PCD_CommunicateWithPICC(byte command, byte waitIRq, byte *sendData, byte sendLen, byte *backData = NULL, byte *backLen = NULL, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false); 354 | byte PICC_RequestA(byte *bufferATQA, byte *bufferSize); 355 | byte PICC_WakeupA(byte *bufferATQA, byte *bufferSize); 356 | byte PICC_REQA_or_WUPA(byte command, byte *bufferATQA, byte *bufferSize); 357 | byte PICC_Select(Uid *uid, byte validBits = 0); 358 | byte PICC_HaltA(); 359 | 360 | ///////////////////////////////////////////////////////////////////////////////////// 361 | // Functions for communicating with MIFARE PICCs 362 | ///////////////////////////////////////////////////////////////////////////////////// 363 | byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid); 364 | void PCD_StopCrypto1(); 365 | byte MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize); 366 | byte MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize); 367 | byte MIFARE_Decrement(byte blockAddr, long delta); 368 | byte MIFARE_Increment(byte blockAddr, long delta); 369 | byte MIFARE_Restore(byte blockAddr); 370 | byte MIFARE_Transfer(byte blockAddr); 371 | byte MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize); 372 | byte MIFARE_GetValue(byte blockAddr, long *value); 373 | byte MIFARE_SetValue(byte blockAddr, long value); 374 | 375 | ///////////////////////////////////////////////////////////////////////////////////// 376 | // Support functions 377 | ///////////////////////////////////////////////////////////////////////////////////// 378 | byte PCD_MIFARE_Transceive(byte *sendData, byte sendLen, bool acceptTimeout = false); 379 | // old function used too much memory, now name moved to flash; if you need char, copy from flash to memory 380 | //const char *GetStatusCodeName(byte code); 381 | const __FlashStringHelper *GetStatusCodeName(byte code); 382 | byte PICC_GetType(byte sak); 383 | // old function used too much memory, now name moved to flash; if you need char, copy from flash to memory 384 | //const char *PICC_GetTypeName(byte type); 385 | const __FlashStringHelper *PICC_GetTypeName(byte type); 386 | void PICC_DumpToSerial(Uid *uid); 387 | void PICC_DumpMifareClassicToSerial(Uid *uid, byte piccType, MIFARE_Key *key); 388 | void PICC_DumpMifareClassicSectorToSerial(Uid *uid, MIFARE_Key *key, byte sector); 389 | void PICC_DumpMifareUltralightToSerial(); 390 | void MIFARE_SetAccessBits(byte *accessBitBuffer, byte g0, byte g1, byte g2, byte g3); 391 | bool MIFARE_OpenUidBackdoor(bool logErrors); 392 | bool MIFARE_SetUid(byte *newUid, byte uidSize, bool logErrors); 393 | bool MIFARE_UnbrickUidSector(bool logErrors); 394 | 395 | ///////////////////////////////////////////////////////////////////////////////////// 396 | // Convenience functions - does not add extra functionality 397 | ///////////////////////////////////////////////////////////////////////////////////// 398 | bool PICC_IsNewCardPresent(); 399 | bool PICC_ReadCardSerial(); 400 | 401 | private: 402 | byte _chipAddress; 403 | byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) 404 | byte MIFARE_TwoStepHelper(byte command, byte blockAddr, long data); 405 | }; 406 | 407 | #endif 408 | --------------------------------------------------------------------------------