Skip to content

Commit

Permalink
Nicla Sense ME tutorial: formatting alginment
Browse files Browse the repository at this point in the history
  • Loading branch information
marqdevx committed Sep 30, 2021
1 parent 59e6146 commit 4fd4aeb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
#include "Nicla_System.h"

#include "Arduino_BHY2.h"

#include "Wire.h"

SensorXYZ accel(SENSOR_ID_ACC);

SensorXYZ gyro(SENSOR_ID_GYRO);

Sensor temp(SENSOR_ID_TEMP);

uint8_t command = 0x00;

void requestHandler(); //request event callback

void receiveEvent(int howMany); //receive event callback

void setup()
{

void setup(){
BHY2.begin();

//Configure Sensors

accel.configure(1, 0);

gyro.configure(1, 0);

temp.configure(1, 0);

//Give LED feedback to the user

nicla::leds.begin();

nicla::leds.setColor(green);

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

void loop()
{

void loop(){
BHY2.update(10);
}

void I2CWrite16(int16_t data)
{

void I2CWrite16(int16_t data){
Wire.write((byte)(data & 0xFF));

Wire.write((byte)((data >> 8) & 0xFF));
}

void receiveEvent(int howMany)
{
void receiveEvent(int howMany){

nicla::leds.setColor(blue);

while (Wire.available())

{

while (Wire.available()){
command = Wire.read();
}

nicla::leds.setColor(off);
}

void requestHandler()
{

void requestHandler(){
nicla::leds.setColor(green);

int16_t dataX;

int16_t dataY;

int16_t dataZ;

switch (command)

{

//Update readings command

switch (command){

//Update readings command
case 0:

break;

case 1:

dataX = accel.x();

I2CWrite16(dataX);

Serial.println(accel.toString());

break;

case 2:

dataY = accel.y();

I2CWrite16(dataY);

break;

case 3:

dataZ = accel.z();

I2CWrite16(dataZ);

break;

default:

break;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,52 @@
#include "Wire.h"

#define NICLA_I2C_ADDRESS 0x1A

void setup()
{
void setup(){
Serial.begin(9600);
while (!Serial)
;
while (!Serial);

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

void loop()
{
void loop(){

I2CWrite(1); // Request Acceleration X

int16_t acX = getData16();

I2CWrite(2); // Request Acceleration Y

int16_t acY = getData16();

I2CWrite(3); // Request Acceleration Z

int16_t acZ = getData16();

Serial.print("ACCELERATION :");

Serial.print(" X: ");
Serial.print(acX);

Serial.print(" Y: ");
Serial.print(acY);

Serial.print(" Z: ");
Serial.print(acZ);

Serial.println();

delay(2500);
}

void I2CWrite(int command)
{
void I2CWrite(int command){
Wire.beginTransmission(NICLA_I2C_ADDRESS);
Wire.write(command);
Wire.endTransmission();
delay(100);
}

int16_t getData16()
{

int16_t getData16(){
int16_t data = 0;

Wire.requestFrom(0x1A, 2);

while (Wire.available())
{

for (int i = 0; i < 2; i++)
{

while (Wire.available()){
for (int i = 0; i < 2; i++){
data |= Wire.read() << (8 * i);
}
}
Expand Down

0 comments on commit 4fd4aeb

Please sign in to comment.