summaryrefslogtreecommitdiff
path: root/metawatch/metawatch.h
blob: badfa831d5c8f49d9152970f559b489f3d7be88b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef METAWATCH_H
#define METAWATCH_H

#include <QtCore/QQueue>
#include <QtCore/QTimer>
#include <QtCore/QSettings>
#include <QtConnectivity/QBluetoothAddress>
#include <QtConnectivity/QBluetoothSocket>
#include <QtSystemInfo/QSystemAlignedTimer>
#include "watch.h"

using QTM_PREPEND_NAMESPACE(QBluetoothSocket);
using QTM_PREPEND_NAMESPACE(QBluetoothAddress);
using QTM_PREPEND_NAMESPACE(QSystemAlignedTimer);

namespace sowatch
{

class MetaWatchPaintEngine;

class MetaWatch : public Watch
{
    Q_OBJECT

public:
	explicit MetaWatch(const QBluetoothAddress& address, QSettings* settings = 0, QObject *parent = 0);
	~MetaWatch();

	enum MessageType {
		NoMessage = 0,
		GetDeviceType = 0x01,
		GetDeviceTypeResponse = 0x02,
		GetInformationString = 0x03,
		GetInformationStringResponse = 0x04,
		WriteOledBuffer = 0x10,
		ChangeOledMode = 0x12,
		WriteOledScrollBuffer = 0x13,
		AdvanceWatchHands = 0x20,
		SetVibrateMode = 0x23,
		SetRealTimeClock = 0x26,
		GetRealTimeClock = 0x27,
		GetRealTimeClockResponse = 0x28,
		NvalOperation = 0x30,
		NvalOperationResponse = 0x31,
		StatusChangeEvent = 0x33,
		ButtonEvent = 0x34,
		GeneralPurposePhone = 0x35,
		GeneralPurposeWatch = 0x36,
		WriteLcdBuffer = 0x40,
		ConfigureLcdIdleBufferSize = 0x42,
		UpdateLcdDisplay = 0x43,
		LoadLcdTemplate = 0x44,
		EnableButton = 0x46,
		DisableButton = 0x47,
		BatteryConfiguration = 0x53,
		LowBatteryWarning = 0x54,
		LowBatteryBluetoothOff = 0x55,
		ReadBatteryVoltage = 0x56,
		ReadBatteryVoltageResponse = 0x57,
		ReadLightSensor = 0x58,
		ReadLightSensorResponse = 0x59
	};

	enum Mode {
		IdleMode = 0,
		ApplicationMode = 1,
		NotificationMode = 2
	};

	enum Button {
		BtnA = 0,
		BtnB,
		BtnC,
		BtnD,
		BtnE,
		BtnF
	};

	enum ButtonPress {
		PressOnly = 0,
		PressAndRelease = 1,
		HoldAndRelease = 2,
		LongHoldAndRelease = 3
	};

	QPaintEngine* paintEngine() const;
	int metric(PaintDeviceMetric metric) const = 0;

	QString model() const;
	QStringList buttons() const;
	bool isConnected() const;
	bool busy() const;

	QDateTime dateTime();
	void setDateTime(const QDateTime& dateTime);

	void grabButton(int button);
	void ungrabButton(int button);

	void updateNotificationCount(Notification::Type type, int count);

	void displayIdleScreen();
	void displayNotification(Notification *notification);
	void displayApplication();

	void vibrate(int msecs);

	Mode currentMode() const;
	Mode paintTargetMode() const;

	QImage* imageFor(Mode mode);
	QRect rectFor(Mode mode);

	virtual void clear(Mode mode, bool black = false) = 0;
	virtual void update(Mode mode, const QList<QRect>& rects = QList<QRect>()) = 0;
	void grabButton(Mode mode, Button button);
	void ungrabButton(Mode mode, Button button);

protected:
	// Some configurable stuff.
	short _notificationTimeout;
	bool _24hMode : 1;
	bool _dayMonthOrder : 1;

	// Notifications: timers
	QTimer* _idleTimer;
	QTimer* _ringTimer;

	// Buttons
	static const char btnToWatch[8];
	QStringList _buttonNames;

	// Current watch state
	Mode _currentMode;
	Mode _paintMode;

	// For QPaintDevice
	mutable MetaWatchPaintEngine* _paintEngine;
	QImage _image[3];

	// Timers to retry the connection when the watch is not found.
	static const int connectRetryTimesSize = 6;
	static const int connectRetryTimes[connectRetryTimesSize];
	short _connectRetries;
	bool _connected;
	QTimer* _connectTimer;
	QSystemAlignedTimer* _connectAlignedTimer;

	// Connection stuff
	QBluetoothAddress _address;
	QBluetoothSocket* _socket;

	// Base watch protocol stuff
	struct Message {
		MessageType type;
		quint8 options;
		QByteArray data;
		Message(MessageType ntype = NoMessage, QByteArray ndata = QByteArray(), quint8 noptions = 0) :
			type(ntype), options(noptions), data(ndata)
		{ }
	};

	QQueue<Message> _toSend;
	QTimer* _sendTimer;
	Message _partialReceived;

	static const quint8 bitRevTable[16];
	static const quint16 crcTable[256];
	static quint16 calcCrc(const QByteArray& data, int size);
	static quint16 calcCrc(const Message& msg);

	void send(const Message& msg);

	void setVibrateMode(bool enable, uint on, uint off, uint cycles);
	void updateLcdLine(Mode mode, const QImage& image, int line);
	void updateLcdLines(Mode mode, const QImage& image, int lineA, int lineB);
	void updateLcdLines(Mode mode, const QImage& image, const QVector<bool>& lines);
	void configureLcdIdleSystemArea(bool entireScreen);
	void updateLcdDisplay(Mode mode, bool copy = true);
	void loadLcdTemplate(Mode mode, int templ);
	void enableButton(Mode mode, Button button, ButtonPress press);
	void disableButton(Mode mode, Button button, ButtonPress press);

	void handleMessage(const Message& msg);
	void handleStatusChangeMessage(const Message& msg);
	void handleButtonEventMessage(const Message& msg);

	virtual void handleWatchConnected() = 0;

private slots:
	void socketConnected();
	void socketDisconnected();
	void socketData();
	void socketError(QBluetoothSocket::SocketError error);
	void socketState(QBluetoothSocket::SocketState error);
	void retryConnect();
	void timedSend();
	void timedRing();

private:
	void realSend(const Message& msg);
	void realReceive(bool block);
};

}

#endif // METAWATCH_H