[PATCH] Use accelerometer library for autorotation

Neil Jerram neil at ossau.homelinux.net
Tue Jul 17 22:33:31 CEST 2012


---
 src/qbuild.pro       |    1 +
 src/utils/rotate.cpp |  102 ++++++++++++----------------------------------=
----
 src/utils/rotate.h   |   13 +++----
 3 files changed, 31 insertions(+), 85 deletions(-)

diff --git a/src/qbuild.pro b/src/qbuild.pro
index 33f294e..4db82b6 100644
--- a/src/qbuild.pro
+++ b/src/qbuild.pro
@@ -3,6 +3,7 @@ TARGET =3D arora
=20
 CONFIG +=3D qtopia
 QT +=3D webkit network
+MODULES*=3Daccelerometer
=20
 #DEFINES +=3D QT_NO_CAST_FROM_ASCII
 DEFINES +=3D QT_NO_UITOOLS
diff --git a/src/utils/rotate.cpp b/src/utils/rotate.cpp
index c5a82b7..32b0399 100644
--- a/src/utils/rotate.cpp
+++ b/src/utils/rotate.cpp
@@ -5,6 +5,8 @@
=20
 #include <stdlib.h>
=20
+#include <accelerometers.h>
+
 /**
  * The acceleromer algorithms and code taken from omnewrotate-0.5.4
  * Copyright =C2=A9 2008 Rui Miguel Silva Seabra <rms at 1407.org>
@@ -37,7 +39,6 @@
=20
 RotateHelper::RotateHelper(QObject *parent, int dflg) : QObject(parent)
 {
-	timer=3D NULL;
 	x =3D 0;
 	y =3D 0;
 	z =3D 0;
@@ -51,9 +52,7 @@ RotateHelper::RotateHelper(QObject *parent, int dflg) : Q=
Object(parent)
 	down =3D 0;
 	last_pos=3D -1;
 	current_pos =3D -1;
-	event3 =3D -1;
 	debug =3D dflg;
-	skip_zero =3D 1;
 	initial_rotation=3D -1;
 }
=20
@@ -77,32 +76,16 @@ void RotateHelper::start(int timeinms)
 {
 	// remember where we were when we started
 	QValueSpaceItem vsiRot("/UI/Rotation/Current");
-    initial_rotation=3D vsiRot.value().toUInt();
-=09
-	// don't allow multiple timers to run
-	if(timer !=3D NULL){
-		stop();
-	}
+	initial_rotation=3D vsiRot.value().toUInt();
=20
-	// start up a single shot timer to check accelerometers
-	// it will be restarted each time
-	timer =3D new QTimer(this);
-	connect(timer, SIGNAL(timeout()), this, SLOT(sample()));
-	timer->start(timeinms);
+	// start accelerometer
+	accelerometer_start(timeinms, RotateHelper::accel_callback, this);
 }
=20
 void RotateHelper::stop()
 {
-	if(timer !=3D NULL){
-		timer->stop();
-		delete timer;
-		timer=3D NULL;
-	}
-
-	if(event3 !=3D -1){
-		close(event3);
-		event3=3D -1;
-	}
+	// stop accelerometer
+	accelerometer_stop();
 }
=20
 // restore to whatever the rotation was when we started
@@ -204,64 +187,27 @@ int RotateHelper::define_position(void)
 	return current_pos;
 }
=20
-
-int RotateHelper::read_packet()
+bool RotateHelper::packet_reader()
 {
-	static struct input_event event_x, event_y, event_z, event_syn;
-	void *packet_memcpy_result =3D NULL;
-	int packet_size =3D sizeof(struct input_event);
-	int size_of_packet =3D 4 * packet_size;
-	int bytes_read =3D 0;
-	char packet[size_of_packet];
-
-	bytes_read =3D read(event3, packet, size_of_packet);
-
-	if (bytes_read < packet_size)
-	{
-		qWarning("RotateHelper: fread failed");
-		stop();
-		return -1;
-	}
-
-	/* obtain the full packet */
-	packet_memcpy_result =3D memcpy(&event_x,   packet,                   pac=
ket_size);
-	packet_memcpy_result =3D memcpy(&event_y,   packet +     packet_size, pac=
ket_size);
-	packet_memcpy_result =3D memcpy(&event_z,   packet + 2 * packet_size, pac=
ket_size);
-	packet_memcpy_result =3D memcpy(&event_syn, packet + 3 * packet_size, pac=
ket_size);
-
-	if(skip_zero && (event_x.value =3D=3D 0 || event_y.value =3D=3D 0 || even=
t_z.value =3D=3D 0))
-	{
-		//	qDebug("Bad packet!");
-		return(0);
-	}
-
-	if (event_syn.type =3D=3D EV_SYN)
-	{
-		x =3D event_x.value;
-		y =3D event_y.value;
-		z =3D event_z.value;
-
-		return (1);
-	}
-	else
-		return (0);
+	return (x || y || z);
 }
=20
-bool RotateHelper::packet_reader()
+void RotateHelper::accel_sample(double acx,
+				double acy,
+				double acz)
 {
-	if(event3 =3D=3D -1){
-		event3 =3D open(EVENT_PATH, O_RDONLY);
-
-		if (event3 < 0){
-			qWarning("Can't open '%s': %s\n", EVENT_PATH, strerror(errno));
-			return false;
-		}
-		qDebug("Opened: %s", EVENT_PATH);
-	}
+	x =3D 1000 * acx;
+	y =3D 1000 * acy;
+	z =3D 1000 * acz;
=20
-	while(read_packet() =3D=3D 0);
-=09
-	// qDebug("read packet");
+	sample();
+}
=20
-	return true;
+void RotateHelper::accel_callback(void *closure,
+				  double acx,
+				  double acy,
+				  double acz)
+{
+  RotateHelper *rotHelper =3D (RotateHelper *)closure;
+  rotHelper->accel_sample(acx, acy, acz);
 }
diff --git a/src/utils/rotate.h b/src/utils/rotate.h
index 30957a1..792cbba 100644
--- a/src/utils/rotate.h
+++ b/src/utils/rotate.h
@@ -11,8 +11,6 @@
=20
 #include <QTimer>
=20
-#define EVENT_PATH "/dev/input/event3"
-
=20
 /**
  * Detects the orientation of the device, and will rotate the screen
@@ -30,6 +28,7 @@ class RotateHelper : public QObject
 	void stop();
 	void restore();
 	bool isLandscape();
+	void accel_sample(double acx, double acy, double acz);
=20
  private slots:
 	void sample();
@@ -41,10 +40,7 @@ signals:
 	void maybe_rotate(int deg);
 	int neighbour(int value, int target, int neighbour);
 	int define_position(void);
-	int read_packet();
 	bool packet_reader();
-=09
-	QTimer *timer;
=20
 	enum Orientation {UP=3D0, RIGHT=3D90, DOWN=3D180, LEFT=3D270};
=20
@@ -61,9 +57,12 @@ signals:
 	int down;
 	int last_pos;
 	int current_pos;
-	int event3;
 	int debug;
 	int initial_rotation;
-	ushort skip_zero;
+
+	static void accel_callback(void *closure,
+				   double acx,
+				   double acy,
+				   double acz);
 };
 #endif
--=20
1.7.10.4


--=-=-=--



More information about the community mailing list