12
12
*/
13
13
14
14
#include < Arduino.h>
15
- # include " Preferences.h "
15
+
16
16
#include " M5Stack.h"
17
- #include " math.h"
18
17
#include " M5_BMM150.h"
19
18
#include " M5_BMM150_DEFS.h"
19
+ #include " Preferences.h"
20
+ #include " math.h"
20
21
21
22
Preferences prefs;
22
23
23
24
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
+ // 储存补偿磁强计浮子数据
25
27
bmm150_mag_data mag_max;
26
28
bmm150_mag_data mag_min;
27
29
TFT_eSprite img = TFT_eSprite(&M5.Lcd);
28
30
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 {
33
38
return BMM150_E_DEV_NOT_FOUND;
34
39
}
35
40
}
36
41
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 {
41
49
return BMM150_E_DEV_NOT_FOUND;
42
50
}
43
51
}
44
52
45
- int8_t bmm150_initialization (){
53
+ int8_t bmm150_initialization () {
46
54
int8_t rslt = BMM150_OK;
47
55
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. 写总线指针
52
60
dev.delay_ms = delay;
53
61
54
62
// Set the maximum range range
@@ -63,70 +71,89 @@ int8_t bmm150_initialization(){
63
71
mag_min.y = 2000 ;
64
72
mag_min.z = 2000 ;
65
73
66
- rslt = bmm150_init (&dev); // Memory chip ID. 存储芯片ID
74
+ rslt = bmm150_init (&dev); // Memory chip ID. 存储芯片ID
67
75
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. 设置传感器电源工作模式
69
78
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 . 设置传感器的预置模式
71
81
return rslt;
72
82
}
73
83
74
- void bmm150_offset_save (){ // Store the data. 存储bmm150的数据
84
+ void bmm150_offset_save () { // Store the data. 存储bmm150的数据
75
85
prefs.begin (" bmm150" , false );
76
86
prefs.putBytes (" offset" , (uint8_t *)&mag_offset, sizeof (bmm150_mag_data));
77
87
prefs.end ();
78
88
}
79
89
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));
83
94
prefs.end ();
84
95
Serial.println (" bmm150 load offset finish...." );
85
- }else {
96
+ } else {
86
97
Serial.println (" bmm150 load offset failed...." );
87
98
}
88
99
}
89
100
90
101
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
106
129
}
107
130
}
108
131
109
132
bmm150_offset_load ();
110
133
}
111
134
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数据校准
113
137
uint32_t calibrate_timeout = 0 ;
114
138
115
139
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. 串口输出格式化字符
117
143
Serial.printf (" running ..." );
118
144
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 ) {
122
149
mag_min.x = (dev.data .x < mag_min.x ) ? dev.data .x : mag_min.x ;
123
150
mag_max.x = (dev.data .x > mag_max.x ) ? dev.data .x : mag_max.x ;
124
151
}
125
- if (dev.data .y ){
152
+ if (dev.data .y ) {
126
153
mag_max.y = (dev.data .y > mag_max.y ) ? dev.data .y : mag_max.y ;
127
154
mag_min.y = (dev.data .y < mag_min.y ) ? dev.data .y : mag_min.y ;
128
155
}
129
- if (dev.data .z ){
156
+ if (dev.data .z ) {
130
157
mag_min.z = (dev.data .z < mag_min.z ) ? dev.data .z : mag_min.z ;
131
158
mag_max.z = (dev.data .z > mag_max.z ) ? dev.data .z : mag_max.z ;
132
159
}
@@ -146,16 +173,21 @@ void bmm150_calibrate(uint32_t calibrate_time){ //bbm150 data calibrate. bbm150
146
173
147
174
void loop () {
148
175
char text_string[100 ];
149
- M5.update (); // Read the press state of the key. 读取按键的状态
176
+ M5.update (); // Read the press state of the key. 读取按键的状态
150
177
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;
152
181
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 );
155
186
156
187
img.fillSprite (0 );
157
188
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. 绘制带有填充的字符串
159
191
sprintf (text_string, " MAG Y: %.2f" , dev.data .y );
160
192
img.drawString (text_string, 10 , 50 , 4 );
161
193
sprintf (text_string, " MAG Z: %.2f" , dev.data .z );
@@ -165,7 +197,7 @@ void loop() {
165
197
img.drawCentreString (" Press BtnA enter calibrate" , 160 , 150 , 4 );
166
198
img.pushSprite (0 , 0 );
167
199
168
- if (M5.BtnA .wasPressed ()){
200
+ if (M5.BtnA .wasPressed ()) {
169
201
img.fillSprite (0 );
170
202
img.drawCentreString (" Flip + rotate core calibration" , 160 , 110 , 4 );
171
203
img.pushSprite (0 , 0 );
0 commit comments