Skip to content

Commit 2ea36b1

Browse files
authored
Merge pull request #279 from Tinyu-Zhao/master
fixed wire begin overload bug
2 parents 49a2822 + 52022a0 commit 2ea36b1

File tree

4 files changed

+141
-108
lines changed

4 files changed

+141
-108
lines changed

examples/Basics/bmm150/bmm150.ino

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,51 @@
1212
*/
1313

1414
#include <Arduino.h>
15-
#include "Preferences.h"
15+
1616
#include "M5Stack.h"
17-
#include "math.h"
1817
#include "M5_BMM150.h"
1918
#include "M5_BMM150_DEFS.h"
19+
#include "Preferences.h"
20+
#include "math.h"
2021

2122
Preferences prefs;
2223

2324
struct bmm150_dev dev;
24-
bmm150_mag_data mag_offset; // Compensation magnetometer float data storage 储存补偿磁强计浮子数据
25+
bmm150_mag_data mag_offset; // Compensation magnetometer float data storage
26+
// 储存补偿磁强计浮子数据
2527
bmm150_mag_data mag_max;
2628
bmm150_mag_data mag_min;
2729
TFT_eSprite img = TFT_eSprite(&M5.Lcd);
2830

29-
int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *read_data, uint16_t len){
30-
if(M5.I2C.readBytes(dev_id, reg_addr, len, read_data)){ // Check whether the device ID, address, data exist.
31-
return BMM150_OK; //判断器件的Id、地址、数据是否存在
32-
}else{
31+
int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *read_data,
32+
uint16_t len) {
33+
if (M5.I2C.readBytes(
34+
dev_id, reg_addr, len,
35+
read_data)) { // Check whether the device ID, address, data exist.
36+
return BMM150_OK; //判断器件的Id、地址、数据是否存在
37+
} else {
3338
return BMM150_E_DEV_NOT_FOUND;
3439
}
3540
}
3641

37-
int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *read_data, uint16_t len){
38-
if(M5.I2C.writeBytes(dev_id, reg_addr, read_data, len)){ //Writes data of length len to the specified device address.
39-
return BMM150_OK; //向指定器件地址写入长度为len的数据
40-
}else{
42+
int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *read_data,
43+
uint16_t len) {
44+
if (M5.I2C.writeBytes(dev_id, reg_addr, read_data,
45+
len)) { // Writes data of length len to the specified
46+
// device address.
47+
return BMM150_OK; //向指定器件地址写入长度为len的数据
48+
} else {
4149
return BMM150_E_DEV_NOT_FOUND;
4250
}
4351
}
4452

45-
int8_t bmm150_initialization(){
53+
int8_t bmm150_initialization() {
4654
int8_t rslt = BMM150_OK;
4755

48-
dev.dev_id = 0x10; //Device address setting. 设备地址设置
49-
dev.intf = BMM150_I2C_INTF; //SPI or I2C interface setup. SPI或I2C接口设置
50-
dev.read = i2c_read; //Read the bus pointer. 读总线指针
51-
dev.write = i2c_write; //Write the bus pointer. 写总线指针
56+
dev.dev_id = 0x10; // Device address setting. 设备地址设置
57+
dev.intf = BMM150_I2C_INTF; // SPI or I2C interface setup. SPI或I2C接口设置
58+
dev.read = i2c_read; // Read the bus pointer. 读总线指针
59+
dev.write = i2c_write; // Write the bus pointer. 写总线指针
5260
dev.delay_ms = delay;
5361

5462
// Set the maximum range range
@@ -63,70 +71,89 @@ int8_t bmm150_initialization(){
6371
mag_min.y = 2000;
6472
mag_min.z = 2000;
6573

66-
rslt = bmm150_init(&dev); //Memory chip ID. 存储芯片ID
74+
rslt = bmm150_init(&dev); // Memory chip ID. 存储芯片ID
6775
dev.settings.pwr_mode = BMM150_NORMAL_MODE;
68-
rslt |= bmm150_set_op_mode(&dev); //Set the sensor power mode. 设置传感器电源工作模式
76+
rslt |= bmm150_set_op_mode(
77+
&dev); // Set the sensor power mode. 设置传感器电源工作模式
6978
dev.settings.preset_mode = BMM150_PRESETMODE_ENHANCED;
70-
rslt |= bmm150_set_presetmode(&dev); //Set the preset mode of . 设置传感器的预置模式
79+
rslt |= bmm150_set_presetmode(
80+
&dev); // Set the preset mode of . 设置传感器的预置模式
7181
return rslt;
7282
}
7383

74-
void bmm150_offset_save(){ //Store the data. 存储bmm150的数据
84+
void bmm150_offset_save() { // Store the data. 存储bmm150的数据
7585
prefs.begin("bmm150", false);
7686
prefs.putBytes("offset", (uint8_t *)&mag_offset, sizeof(bmm150_mag_data));
7787
prefs.end();
7888
}
7989

80-
void bmm150_offset_load(){ //load the data. 加载bmm150的数据
81-
if(prefs.begin("bmm150", true)){
82-
prefs.getBytes("offset", (uint8_t *)&mag_offset, sizeof(bmm150_mag_data));
90+
void bmm150_offset_load() { // load the data. 加载bmm150的数据
91+
if (prefs.begin("bmm150", true)) {
92+
prefs.getBytes("offset", (uint8_t *)&mag_offset,
93+
sizeof(bmm150_mag_data));
8394
prefs.end();
8495
Serial.println("bmm150 load offset finish....");
85-
}else{
96+
} else {
8697
Serial.println("bmm150 load offset failed....");
8798
}
8899
}
89100

90101
void setup() {
91-
M5.begin(true, false, true, false); //Init M5Core(Initialize LCD, serial port). 初始化 M5Core(初始化LCD、串口)
92-
M5.Power.begin(); //Init Power module. 初始化电源设置
93-
Wire.begin(21, 22, 400000); //Set the frequency of the SDA SCL. 设置SDA和SCL的频率
94-
95-
img.setColorDepth(1); //Set bits per pixel for colour. 设置色深为1
96-
img.setTextColor(TFT_WHITE); //Set the font foreground colour (background is. 设置字体的前景色为TFT_WHITE
97-
img.createSprite(320, 240); //Create a sprite (bitmap) of defined width and height 创建一个指定宽度和高度的Sprite图
98-
img.setBitmapColor(TFT_WHITE, 0); //Set the foreground and background colour. 设置位图的前景色和背景颜色
99-
100-
if(bmm150_initialization() != BMM150_OK){
101-
img.fillSprite(0); //Fill the whole sprite with defined colour. 用定义的颜色填充整个Sprite图
102-
img.drawCentreString("BMM150 init failed", 160, 110, 4); //Use font 4 in (160,110)draw string. 使用字体4在(160,110)处绘制字符串
103-
img.pushSprite(0, 0); //Push the sprite to the TFT at 0, 0. 将Sprite图打印在(0,0)处
104-
for(;;){
105-
delay(100); //delay 100ms. 延迟100ms
102+
M5.begin(true, false, true,
103+
false); // Init M5Core(Initialize LCD, serial port). 初始化
104+
// M5Core(初始化LCD、串口)
105+
M5.Power.begin(); // Init Power module. 初始化电源设置
106+
Wire.begin(
107+
21, 22,
108+
400000UL); // Set the frequency of the SDA SCL. 设置SDA和SCL的频率
109+
110+
img.setColorDepth(1); // Set bits per pixel for colour. 设置色深为1
111+
img.setTextColor(TFT_WHITE); // Set the font foreground colour (background
112+
// is. 设置字体的前景色为TFT_WHITE
113+
img.createSprite(320, 240); // Create a sprite (bitmap) of defined width
114+
// and height 创建一个指定宽度和高度的Sprite图
115+
img.setBitmapColor(TFT_WHITE, 0); // Set the foreground and background
116+
// colour. 设置位图的前景色和背景颜色
117+
118+
if (bmm150_initialization() != BMM150_OK) {
119+
img.fillSprite(0); // Fill the whole sprite with defined colour.
120+
// 用定义的颜色填充整个Sprite图
121+
img.drawCentreString("BMM150 init failed", 160, 110,
122+
4); // Use font 4 in (160,110)draw string.
123+
// 使用字体4在(160,110)处绘制字符串
124+
img.pushSprite(
125+
0,
126+
0); // Push the sprite to the TFT at 0, 0. 将Sprite图打印在(0,0)处
127+
for (;;) {
128+
delay(100); // delay 100ms. 延迟100ms
106129
}
107130
}
108131

109132
bmm150_offset_load();
110133
}
111134

112-
void bmm150_calibrate(uint32_t calibrate_time){ //bbm150 data calibrate. bbm150数据校准
135+
void bmm150_calibrate(
136+
uint32_t calibrate_time) { // bbm150 data calibrate. bbm150数据校准
113137
uint32_t calibrate_timeout = 0;
114138

115139
calibrate_timeout = millis() + calibrate_time;
116-
Serial.printf("Go calibrate, use %d ms \r\n", calibrate_time); //The serial port outputs formatting characters. 串口输出格式化字符
140+
Serial.printf("Go calibrate, use %d ms \r\n",
141+
calibrate_time); // The serial port outputs formatting
142+
// characters. 串口输出格式化字符
117143
Serial.printf("running ...");
118144

119-
while (calibrate_timeout > millis()){
120-
bmm150_read_mag_data(&dev); //read the magnetometer data from registers. 从寄存器读取磁力计数据
121-
if(dev.data.x){
145+
while (calibrate_timeout > millis()) {
146+
bmm150_read_mag_data(&dev); // read the magnetometer data from
147+
// registers. 从寄存器读取磁力计数据
148+
if (dev.data.x) {
122149
mag_min.x = (dev.data.x < mag_min.x) ? dev.data.x : mag_min.x;
123150
mag_max.x = (dev.data.x > mag_max.x) ? dev.data.x : mag_max.x;
124151
}
125-
if(dev.data.y){
152+
if (dev.data.y) {
126153
mag_max.y = (dev.data.y > mag_max.y) ? dev.data.y : mag_max.y;
127154
mag_min.y = (dev.data.y < mag_min.y) ? dev.data.y : mag_min.y;
128155
}
129-
if(dev.data.z){
156+
if (dev.data.z) {
130157
mag_min.z = (dev.data.z < mag_min.z) ? dev.data.z : mag_min.z;
131158
mag_max.z = (dev.data.z > mag_max.z) ? dev.data.z : mag_max.z;
132159
}
@@ -146,16 +173,21 @@ void bmm150_calibrate(uint32_t calibrate_time){ //bbm150 data calibrate. bbm150
146173

147174
void loop() {
148175
char text_string[100];
149-
M5.update(); //Read the press state of the key. 读取按键的状态
176+
M5.update(); // Read the press state of the key. 读取按键的状态
150177
bmm150_read_mag_data(&dev);
151-
float head_dir = atan2(dev.data.x - mag_offset.x, dev.data.y - mag_offset.y) * 180.0 / M_PI;
178+
float head_dir =
179+
atan2(dev.data.x - mag_offset.x, dev.data.y - mag_offset.y) * 180.0 /
180+
M_PI;
152181
Serial.printf("Magnetometer data, heading %.2f\n", head_dir);
153-
Serial.printf("MAG X : %.2f \t MAG Y : %.2f \t MAG Z : %.2f \n", dev.data.x, dev.data.y, dev.data.z);
154-
Serial.printf("MID X : %.2f \t MID Y : %.2f \t MID Z : %.2f \n", mag_offset.x, mag_offset.y, mag_offset.z);
182+
Serial.printf("MAG X : %.2f \t MAG Y : %.2f \t MAG Z : %.2f \n", dev.data.x,
183+
dev.data.y, dev.data.z);
184+
Serial.printf("MID X : %.2f \t MID Y : %.2f \t MID Z : %.2f \n",
185+
mag_offset.x, mag_offset.y, mag_offset.z);
155186

156187
img.fillSprite(0);
157188
sprintf(text_string, "MAG X: %.2f", dev.data.x);
158-
img.drawString(text_string, 10, 20, 4); //draw string with padding. 绘制带有填充的字符串
189+
img.drawString(text_string, 10, 20,
190+
4); // draw string with padding. 绘制带有填充的字符串
159191
sprintf(text_string, "MAG Y: %.2f", dev.data.y);
160192
img.drawString(text_string, 10, 50, 4);
161193
sprintf(text_string, "MAG Z: %.2f", dev.data.z);
@@ -165,7 +197,7 @@ void loop() {
165197
img.drawCentreString("Press BtnA enter calibrate", 160, 150, 4);
166198
img.pushSprite(0, 0);
167199

168-
if(M5.BtnA.wasPressed()){
200+
if (M5.BtnA.wasPressed()) {
169201
img.fillSprite(0);
170202
img.drawCentreString("Flip + rotate core calibration", 160, 110, 4);
171203
img.pushSprite(0, 0);

examples/Modules/SERVO/SERVO.ino

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* Copyright (c) 2021 by M5Stack
44
* Equipped with M5Core sample source code
55
* 配套 M5Core 示例源代码
6-
* Visit the website for more information:https://docs.m5stack.com/en/module/servo
6+
* Visit the website for more
7+
information:https://docs.m5stack.com/en/module/servo
78
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/module/servo
89
*
910
* describe: SERVO.
@@ -19,41 +20,40 @@
1920

2021
#define SERVO_ADDR 0x53
2122
void setup() {
22-
M5.begin(true, false, true);
23-
M5.Power.begin();
24-
M5.Lcd.setTextFont(4);
25-
M5.Lcd.setCursor(70, 100);
26-
M5.Lcd.print("Servo Example");
23+
M5.begin(true, false, true);
24+
M5.Power.begin();
25+
M5.Lcd.setTextFont(4);
26+
M5.Lcd.setCursor(70, 100);
27+
M5.Lcd.print("Servo Example");
2728

28-
Wire.begin(21, 22, 100000UL);
29-
}
29+
Wire.begin(21, 22, 100000UL);
3030

3131
// addr 0x01 mean control the number 1 servo by us
3232
void Servo_write_us(uint8_t number, uint16_t us) {
33-
Wire.beginTransmission(SERVO_ADDR);
34-
Wire.write(0x00 | number);
35-
Wire.write(us & 0x00ff);
36-
Wire.write(us >> 8 & 0x00ff);
37-
Wire.endTransmission();
33+
Wire.beginTransmission(SERVO_ADDR);
34+
Wire.write(0x00 | number);
35+
Wire.write(us & 0x00ff);
36+
Wire.write(us >> 8 & 0x00ff);
37+
Wire.endTransmission();
3838
}
3939

4040
// addr 0x11 mean control the number 1 servo by angle
4141
void Servo_write_angle(uint8_t number, uint8_t angle) {
42-
Wire.beginTransmission(SERVO_ADDR);
43-
Wire.write(0x10 | number);
44-
Wire.write(angle);
45-
Wire.endTransmission();
42+
Wire.beginTransmission(SERVO_ADDR);
43+
Wire.write(0x10 | number);
44+
Wire.write(angle);
45+
Wire.endTransmission();
4646
}
4747

4848
void loop() {
49-
for(uint8_t i = 0; i < 12; i++){
50-
Servo_write_us(i, 700);
51-
// Servo_write_angle(i, 0);
52-
}
53-
delay(1000);
54-
for(uint8_t i = 0; i < 12; i++){
55-
Servo_write_us(i, 2300);
56-
// Servo_write_angle(i, 180);
57-
}
58-
delay(1000);
59-
}
49+
for (uint8_t i = 0; i < 12; i++) {
50+
Servo_write_us(i, 700);
51+
// Servo_write_angle(i, 0);
52+
}
53+
delay(1000);
54+
for (uint8_t i = 0; i < 12; i++) {
55+
Servo_write_us(i, 2300);
56+
// Servo_write_angle(i, 180);
57+
}
58+
delay(1000);
59+
}

examples/Unit/ENVIII_SHT30_QMP6988/ENVIII_SHT30_QMP6988.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
请连接端口A(22、21),读取温度、湿度和大气压强并在显示屏上显示
1414
*/
1515
#include <M5Stack.h>
16-
#include "Adafruit_Sensor.h"
17-
#include <Adafruit_BMP280.h>
1816
#include "UNIT_ENV.h"
1917

2018
SHT3X sht30;

examples/Unit/JOYSTICK/JOYSTICK.ino

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,49 @@
33
* Copyright (c) 2021 by M5Stack
44
* Equipped with M5Core sample source code
55
* 配套 M5Core 示例源代码
6-
* Visit the website for more information:https://docs.m5stack.com/en/unit/joystick
6+
* Visit the website for more
7+
information:https://docs.m5stack.com/en/unit/joystick
78
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/unit/joystick
89
*
910
* describe: JOYSTICK.
1011
* date:2021/8/30
1112
*******************************************************************************
12-
Please connect to Port A,Read JOYSTICK Unit X, Y axis offset data and button status
13-
请连接端口 A,读取操纵杆单位X, Y轴偏移数据和按钮状态
13+
Please connect to Port A,Read JOYSTICK Unit X, Y axis offset data and button
14+
status 请连接端口 A,读取操纵杆单位X, Y轴偏移数据和按钮状态
1415
*/
1516

1617
#include <M5Stack.h>
1718

18-
#define JOY_ADDR 0x52 //define Joystick I2C address. 定义摇杆的I2C地址
19+
#define JOY_ADDR 0x52 // define Joystick I2C address. 定义摇杆的I2C地址
1920

2021
void setup() {
21-
M5.begin();
22-
M5.Power.begin();
23-
M5.Lcd.setCursor(70, 0, 4);
24-
M5.Lcd.println(("Joystick Test"));
25-
dacWrite(25, 0); //disable the speak noise. 禁用语音噪音
26-
Wire.begin(21, 22, 400000);
22+
M5.begin();
23+
M5.Power.begin();
24+
M5.Lcd.setCursor(70, 0, 4);
25+
M5.Lcd.println(("Joystick Test"));
26+
dacWrite(25, 0); // disable the speak noise. 禁用语音噪音
27+
Wire.begin(21, 22, 400000UL);
2728
}
2829

2930
char data[100];
3031
void loop() {
31-
static uint8_t x_data,y_data,button_data;
32-
Wire.requestFrom(JOY_ADDR, 3); //Request 3 bytes from the slave device. 向从设备请求3个字节
33-
if (Wire.available()) { //If data is received. 如果接收到数据
34-
x_data = Wire.read();
35-
y_data = Wire.read();
36-
button_data = Wire.read();
37-
sprintf(data, "x:%d y:%d button:%d\n", x_data, y_data, button_data);
38-
Serial.print(data);
32+
static uint8_t x_data, y_data, button_data;
33+
Wire.requestFrom(
34+
JOY_ADDR,
35+
3); // Request 3 bytes from the slave device. 向从设备请求3个字节
36+
if (Wire.available()) { // If data is received. 如果接收到数据
37+
x_data = Wire.read();
38+
y_data = Wire.read();
39+
button_data = Wire.read();
40+
sprintf(data, "x:%d y:%d button:%d\n", x_data, y_data, button_data);
41+
Serial.print(data);
3942

40-
M5.Lcd.setCursor(100, 50, 4);
41-
M5.Lcd.printf("X:%d ",x_data);
42-
M5.Lcd.setCursor(100, 80, 4);
43-
M5.Lcd.printf("Y:%d ",y_data);
44-
M5.Lcd.setCursor(100, 110, 4);
45-
M5.Lcd.printf("B:%d ",button_data);
46-
}
47-
delay(200);
43+
M5.Lcd.setCursor(100, 50, 4);
44+
M5.Lcd.printf("X:%d ", x_data);
45+
M5.Lcd.setCursor(100, 80, 4);
46+
M5.Lcd.printf("Y:%d ", y_data);
47+
M5.Lcd.setCursor(100, 110, 4);
48+
M5.Lcd.printf("B:%d ", button_data);
49+
}
50+
delay(200);
4851
}

0 commit comments

Comments
 (0)