/* Populates the .heading fields in the sensors_vec_t struct with the right angular data (0-359°) 
Heading increases when measuring clockwise */
void magGetOrientation(sensors_axis_t axis, sensors_event_t *event, sensors_vec_t *orientation)
/* Populates the .heading fields in the sensors_vec_t struct with the right angular data (0-359°) 
Heading increases when measuring clockwise */
void magGetOrientation(sensors_axis_t axis, sensors_event_t *event, sensors_vec_t *orientation)
{
  float const PI = 3.14159265F;
  switch (axis)
  {
    case SENSOR_AXIS_X:
      orientation->heading = (float)atan2(event->magnetic.z, event->magnetic.y) * 180 / PI;
      break;
    case SENSOR_AXIS_Y:
      orientation->heading = (float)atan2(event->magnetic.x, event->magnetic.z) * 180 / PI;
      break;
    case SENSOR_AXIS_Z:
    default:
      orientation->heading = (float)atan2(event->magnetic.y, event->magnetic.x) * 180 / PI;
      break;
    }
  if (orientation->heading < 0)
  {
    orientation->heading = 360 + orientation->heading;
  }
}