Skip to content

Commit 4fd4aeb

Browse files
committed
Nicla Sense ME tutorial: formatting alginment
1 parent 59e6146 commit 4fd4aeb

File tree

2 files changed

+17
-75
lines changed

2 files changed

+17
-75
lines changed

examples/Nicla Sense ME as a MKR Shield/NiclaShieldController/NiclaShieldController.ino

+9-51
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,25 @@
11
#include "Nicla_System.h"
2-
32
#include "Arduino_BHY2.h"
4-
53
#include "Wire.h"
64

75
SensorXYZ accel(SENSOR_ID_ACC);
8-
96
SensorXYZ gyro(SENSOR_ID_GYRO);
10-
117
Sensor temp(SENSOR_ID_TEMP);
128

139
uint8_t command = 0x00;
14-
1510
void requestHandler(); //request event callback
16-
1711
void receiveEvent(int howMany); //receive event callback
1812

19-
void setup()
20-
{
21-
13+
void setup(){
2214
BHY2.begin();
2315

2416
//Configure Sensors
25-
2617
accel.configure(1, 0);
27-
2818
gyro.configure(1, 0);
29-
3019
temp.configure(1, 0);
3120

3221
//Give LED feedback to the user
33-
3422
nicla::leds.begin();
35-
3623
nicla::leds.setColor(green);
3724

3825
Wire.begin(0x1A); // join i2c bus with address #0x1A , do not use 0x60 nor 0x6B, the MKR board has those i2c devices
@@ -42,84 +29,55 @@ void setup()
4229
Wire.onReceive(receiveEvent); // Callback triggered from `Wire.beginTransmission()` done by the Host
4330
}
4431

45-
void loop()
46-
{
47-
32+
void loop(){
4833
BHY2.update(10);
4934
}
5035

51-
void I2CWrite16(int16_t data)
52-
{
53-
36+
void I2CWrite16(int16_t data){
5437
Wire.write((byte)(data & 0xFF));
55-
5638
Wire.write((byte)((data >> 8) & 0xFF));
5739
}
5840

59-
void receiveEvent(int howMany)
60-
{
41+
void receiveEvent(int howMany){
6142

6243
nicla::leds.setColor(blue);
63-
64-
while (Wire.available())
65-
66-
{
67-
44+
while (Wire.available()){
6845
command = Wire.read();
6946
}
7047

7148
nicla::leds.setColor(off);
7249
}
7350

74-
void requestHandler()
75-
{
76-
51+
void requestHandler(){
7752
nicla::leds.setColor(green);
7853

7954
int16_t dataX;
80-
8155
int16_t dataY;
82-
8356
int16_t dataZ;
8457

85-
switch (command)
86-
87-
{
88-
89-
//Update readings command
90-
58+
switch (command){
59+
60+
//Update readings command
9161
case 0:
92-
9362
break;
9463

9564
case 1:
96-
9765
dataX = accel.x();
98-
9966
I2CWrite16(dataX);
100-
10167
Serial.println(accel.toString());
102-
10368
break;
10469

10570
case 2:
106-
10771
dataY = accel.y();
108-
10972
I2CWrite16(dataY);
110-
11173
break;
11274

11375
case 3:
114-
11576
dataZ = accel.z();
116-
11777
I2CWrite16(dataZ);
118-
11978
break;
12079

12180
default:
122-
12381
break;
12482
}
12583

examples/Nicla Sense ME as a MKR Shield/NiclaShieldHost/NiclaShieldHost.ino

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,52 @@
11
#include "Wire.h"
2+
23
#define NICLA_I2C_ADDRESS 0x1A
34

4-
void setup()
5-
{
5+
void setup(){
66
Serial.begin(9600);
7-
while (!Serial)
8-
;
7+
while (!Serial);
98

109
Wire.begin(); // Join the I2C bus as a Host
1110
Serial.println("Host started");
1211
}
1312

14-
void loop()
15-
{
13+
void loop(){
1614

1715
I2CWrite(1); // Request Acceleration X
18-
1916
int16_t acX = getData16();
2017

2118
I2CWrite(2); // Request Acceleration Y
22-
2319
int16_t acY = getData16();
2420

2521
I2CWrite(3); // Request Acceleration Z
26-
2722
int16_t acZ = getData16();
2823

2924
Serial.print("ACCELERATION :");
30-
3125
Serial.print(" X: ");
3226
Serial.print(acX);
33-
3427
Serial.print(" Y: ");
3528
Serial.print(acY);
36-
3729
Serial.print(" Z: ");
3830
Serial.print(acZ);
39-
4031
Serial.println();
4132

4233
delay(2500);
4334
}
4435

45-
void I2CWrite(int command)
46-
{
36+
void I2CWrite(int command){
4737
Wire.beginTransmission(NICLA_I2C_ADDRESS);
4838
Wire.write(command);
4939
Wire.endTransmission();
5040
delay(100);
5141
}
5242

53-
int16_t getData16()
54-
{
55-
43+
int16_t getData16(){
5644
int16_t data = 0;
5745

5846
Wire.requestFrom(0x1A, 2);
5947

60-
while (Wire.available())
61-
{
62-
63-
for (int i = 0; i < 2; i++)
64-
{
65-
48+
while (Wire.available()){
49+
for (int i = 0; i < 2; i++){
6650
data |= Wire.read() << (8 * i);
6751
}
6852
}

0 commit comments

Comments
 (0)