Hello,
How can I store last X hours of magnetometer and accelerometer data, I tried logging it:
import Toybox.Graphics;
import Toybox.WatchUi;
import Toybox.System;
import Toybox.Timer;
import Toybox.Sensor;
class magnetView extends WatchUi.View {
var dataTimer = new Timer.Timer();
var lastAccel;
var lastMag;
function initialize() {
View.initialize();
lastAccel = [0, 0, 0]; // Initialize last accelerometer reading
lastMag = [0, 0, 0]; // Initialize last magnetometer reading
}
// Load your resources here
function onLayout(dc as Dc) as Void {
setLayout(Rez.Layouts.MainLayout(dc));
}
// Called when this View is brought to the foreground. Restore
// the state of this View and prepare it to be shown. This includes
// loading resources into memory.
function onShow() as Void {
if (dataTimer == null) {
dataTimer = new Timer.Timer();
dataTimer.start(method(:updateSensors), 1000, true); // Update sensors every second
}
}
// Update the view
function onUpdate(dc as Dc) as Void {
// Call the parent onUpdate function to redraw the layout
View.onUpdate(dc);
}
// Called when this View is removed from the screen. Save the
// state of this View here. This includes freeing resources from
// memory.
function onHide() as Void {
dataTimer.stop();
}
// Method to update sensor data
function updateSensors() as Void {
var sensorInfo = Sensor.getInfo();
if (sensorInfo has :accel && sensorInfo.accel != null) {
lastAccel = sensorInfo.accel;
}
if (sensorInfo has :mag && sensorInfo.mag != null) {
lastMag = sensorInfo.mag;
}
System.println("Accelerometer: " + lastAccel[0] + ", " + lastAccel[1] + ", " + lastAccel[2]);
System.println("Magnetometer: " + lastMag[0] + ", " + lastMag[1] + ", " + lastMag[2]);
}
}
but nothing got saved.