-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakeduino.cpp
165 lines (117 loc) · 2.72 KB
/
fakeduino.cpp
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
#include "fakeduino.h"
#include "fakeduino_defs.h"
#include <QtWidgets/QApplication>
#include <QTimer>
#include <QDebug>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <QIcon>
#include <impl/workspace.h>
struct FakeduinoPrivate
{
QApplication *app = nullptr;
Workspace *wspc = nullptr;
int eeprom_fd = -1;
QTimer *mainLoop = nullptr;
void (*loopFunc)() = nullptr;
QSet<uint32_t> pressedKeys;
} __attribute__((packed));
static Fakeduino * _fd_instance = nullptr;
Fakeduino::Fakeduino()
{
static int one = 1;
d = new FakeduinoPrivate();
d->app = new QApplication(one, &program_invocation_name);
Q_INIT_RESOURCE(fakeduino);
d->app->setWindowIcon(QIcon(":/fakeduino/icon_256.png"));
d->wspc = new Workspace();
d->wspc->setWindowIcon(QIcon(":/fakeduino/icon_256.png"));
d->wspc->show();
d->mainLoop = new QTimer();
eepromPrepare();
d->wspc->showStatus("fakeduino ready");
}
Fakeduino::~Fakeduino()
{
delete d;
}
Fakeduino* Fakeduino::get()
{
if(!_fd_instance)
_fd_instance = new Fakeduino();
return _fd_instance;
}
#include <functional>
void Fakeduino::exec(void (*voidfunc)())
{
d->loopFunc = voidfunc;
d->mainLoop = new QTimer();
d->mainLoop->setTimerType(Qt::CoarseTimer);
d->mainLoop->connect(d->mainLoop, &QTimer::timeout, [this](){d->loopFunc();});
d->mainLoop->start(2);
d->app->exec();
}
void Fakeduino::showGadget( QWidget * qw )
{
d->wspc->showGadget(qw);
}
// eeprom
uint8_t Fakeduino::eepromRead( uint8_t addr )
{
lseek(d->eeprom_fd, addr, SEEK_SET);
uint8_t ret;
read(d->eeprom_fd, &ret, 1);
return ret;
}
void Fakeduino::eepromWrite( uint8_t addr, uint8_t value )
{
lseek(d->eeprom_fd, addr, SEEK_SET);
write(d->eeprom_fd, &value, 1);
}
void Fakeduino::eepromPrepare()
{
d->eeprom_fd = open( FDO_EEPROM_LOCATION, O_RDWR | O_CREAT, 0644 );
if(d->eeprom_fd < 0)
perror("EEPROM");
ftruncate( d->eeprom_fd, FDO_EEPROM_SIZE );
}
void Fakeduino::eepromTerminate()
{
close(d->eeprom_fd);
}
// keys
void Fakeduino::keyPressed( uint32_t kc )
{
d->pressedKeys.insert(kc);
}
void Fakeduino::keyReleased( uint32_t kc )
{
d->pressedKeys.remove(kc);
}
bool Fakeduino::isPressed( uint32_t kc )
{
return d->pressedKeys.contains(kc);
}
// library init
static void _fd_init() __attribute__((constructor));
static void _fd_term() __attribute__((destructor));
void _fd_init()
{
Fakeduino::get();
}
void fakeduino_loop(void (*voidfunc)())
{
Fakeduino::get()->exec(voidfunc);
}
void _fd_term()
{
// TODO
//delete _fd_instance;
}
void yield()
{
QApplication::processEvents();
}