Added shlibs step
[ameter] / ameterwidget-dbus.cpp
1 #include <QtCore/QFile>
2 #include <QtGui/QPainter>
3
4 #include <math.h>
5
6 #include "ameterwidget.h"
7
8 #define SET_IM_STATUS 1
9
10 AMeterWidget::AMeterWidget(QWidget *parent)
11     : QWidget(parent)
12 {
13         // Reciprocal of gravity
14         rg = 1.0 / G_N;
15 //    timer = new QTimer(this);
16     connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
17 //      timer->start(250);
18         image = 0;
19
20         ax = 0;
21         ay = 0;
22         az = 0;
23
24         bx = 0;
25         by = 0;
26         bz = 0;
27
28         status = 2;
29         telepathist = new Telepathist(this);
30         tt = 0;
31
32         m_sensor = new QAccelerometer(this);
33         m_sensor->addFilter(this);
34         m_sensor->start();
35 }
36
37 AMeterWidget::~AMeterWidget()
38 {
39
40 }
41
42 void AMeterWidget::paintEvent(QPaintEvent *e)
43 {
44     QPainter paint(this);
45     qreal cx, cy, rx, ry;
46     qreal x1, y1, a;
47
48     a = sqrt(ax * ax + ay * ay + az * az);
49
50     if (image)
51     {
52         paint.drawImage(QPoint(0, 0), *image);
53     }
54
55     paint.setRenderHints(QPainter::Antialiasing);
56
57     cx = width() / 2;
58     cy = height() / 2;
59     rx = cy / 2;
60     ry = cy / 2;
61
62     x1 = cx - ax * rx;
63         y1 = cy + ay * ry;
64
65     paint.setPen(QPen(QBrush(QColor(255, 255, 255, 255)), 1));
66     paint.setBrush(QBrush(QColor(255, 255, 255, 255)));
67     paint.drawEllipse(QPointF(x1, y1), 3, 3);
68     paint.drawLine(QPointF(cx, cy), QPointF(x1, y1));
69 }
70
71 void AMeterWidget::resizeEvent(QResizeEvent *e)
72 {
73     QPainter *paint;
74     qreal cx, cy, rx, ry;
75
76     if (image)
77     {
78         delete image;
79         image = 0;
80     }
81
82     image = new QImage(size(), QImage::Format_ARGB32);
83     paint = new QPainter(image);
84     paint->setRenderHints(QPainter::Antialiasing);
85
86     cx = width() / 2;
87     cy = height() / 2;
88     rx = cy / 2;
89     ry = cy / 2;
90
91         paint->setPen(QPen(QBrush(QColor(64, 64, 64, 255)), 3));
92
93     paint->drawLine(QPointF(cx, 0), QPointF(cx, cy + cy));
94     paint->drawLine(QPointF(0, cy), QPointF(cx + cx, cy));
95
96     paint->drawEllipse(QPointF(cx, cy), rx, ry);
97     paint->drawEllipse(QPointF(cx, cy), 2 * rx, 2 * ry);
98     paint->drawEllipse(QPointF(cx, cy), 3 * rx, 3 * ry);
99     paint->drawEllipse(QPointF(cx, cy), 4 * rx, 4 * ry);
100
101     paint->setPen(QPen(QBrush(QColor(64, 64, 64, 255)), 1));
102
103         paint->drawLine(QPointF(cx - cx, cy + cx), QPointF(cx + cx, cy - cx));
104         paint->drawLine(QPointF(cx - cx, cy - cx), QPointF(cx + cx, cy + cx));
105
106         paint->drawEllipse(QPointF(cx, cy), 0.25 * rx, 0.25 * ry);
107         paint->drawEllipse(QPointF(cx, cy), 0.50 * rx, 0.50 * ry);
108         paint->drawEllipse(QPointF(cx, cy), 0.75 * rx, 0.75 * ry);
109
110         paint->drawEllipse(QPointF(cx, cy), 1.25 * rx, 1.25 * ry);
111         paint->drawEllipse(QPointF(cx, cy), 1.50 * rx, 1.50 * ry);
112         paint->drawEllipse(QPointF(cx, cy), 1.75 * rx, 1.75 * ry);
113
114         paint->drawEllipse(QPointF(cx, cy), 2.25 * rx, 2.25 * ry);
115         paint->drawEllipse(QPointF(cx, cy), 2.50 * rx, 2.50 * ry);
116         paint->drawEllipse(QPointF(cx, cy), 2.75 * rx, 2.75 * ry);
117
118
119         paint->drawEllipse(QPointF(cx, cy), 3.25 * rx, 3.25 * ry);
120         paint->drawEllipse(QPointF(cx, cy), 3.50 * rx, 3.50 * ry);
121         paint->drawEllipse(QPointF(cx, cy), 3.75 * rx, 3.75 * ry);
122
123     delete paint;
124 }
125
126 bool AMeterWidget::filter(QAccelerometerReading *reading)
127 {
128         ax = reading->x() * rg;
129         ay = reading->y() * rg;
130         az = reading->z() * rg;
131
132         update();
133
134         return true;
135 }
136
137 void AMeterWidget::tick()
138 {
139     char strbuf[128];
140     int x, y, z;
141     QFile *f;
142
143         bx = ax;
144         by = ay;
145         bz = az;
146
147     f = new QFile("/sys/class/i2c-adapter/i2c-3/3-001d/coord");
148     if (f->open(QFile::ReadOnly))
149     {
150         f->read(strbuf, 127);
151         if (sscanf(strbuf, "%d %d %d", &x, &y, &z) == 3)
152         {
153             ax = x * 0.001;
154             ay = y * 0.001;
155             az = z * 0.001;
156         }
157     }
158     delete f;
159
160 #ifdef SET_IM_STATUS
161
162         if (fabs(ax - bx) > 0.125 || fabs(ay - by) > 0.125 || fabs(az - bz) > 0.125)
163         {
164                 tt = 0;
165                 if (status != 2)
166                 {
167                         telepathist->setStatus(2, "available", "In the pocket");
168                         status = 0;
169                 }
170         } else {
171                 tt++;
172         }
173
174         if (tt == 480)
175         {
176                 telepathist->setStatus(3, "away", "Fishing");
177                 status = 3;
178         }
179
180 #endif
181
182     update();
183 }