classdef mpu6050 < mqttconnection
    properties
        subscriptions
        accel_x
        accel_y
        accel_z
    end

    methods
        function obj = mpu6050(brokerAddress)
            obj@mqttconnection(brokerAddress);

            obj.accel_x.time = datetime([],[],[]);
            obj.accel_x.data = [];

            obj.accel_y.time = datetime([],[],[]);
            obj.accel_y.data = [];

            obj.accel_z.time = datetime([],[],[]);
            obj.accel_z.data = [];
        end

        function startRx(obj)
            if obj.getConnection().Connected == 1
                obj.subscriptions = subscribe(obj.getConnection(), "6050example/sensor/mpu6050_accel_x/state", Callback=@obj.rxAccelX);
                obj.subscriptions = subscribe(obj.getConnection(), "6050example/sensor/mpu6050_accel_y/state", Callback=@obj.rxAccelY);
                obj.subscriptions = subscribe(obj.getConnection(), "6050example/sensor/mpu6050_accel_z/state", Callback=@obj.rxAccelZ);
            end
        end

        function stopRx(obj)
            if obj.getConnection().Connected == 1
                unsubscribe(obj.getConnection());
            end
        end

        function rxAccelX(obj, topic, data)
            obj.accel_x.time(end+1) = datetime("now");
            obj.accel_x.data(end+1) = data;
        end

        function rxAccelY(obj, topic, data)
            obj.accel_y.time(end+1) = datetime("now");
            obj.accel_y.data(end+1) = data;
        end

        function rxAccelZ(obj, topic, data)
            obj.accel_z.time(end+1) = datetime("now");
            obj.accel_z.data(end+1) = data;
        end
    end
end