forked from ghewgill/emulino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulino-gui.cpp
346 lines (303 loc) · 8.02 KB
/
emulino-gui.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* emulino - arduino emulator GUI
* Copyright 2009 Greg Hewgill
*
* This file is part of Emulino.
*
* Emulino is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Emulino is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Emulino. If not, see <http://www.gnu.org/licenses/>.
*/
#include <qapplication.h>
#include <qframe.h>
#include <qhash.h>
#include <qlayout.h>
#include <qmessagebox.h>
#include <qpainter.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include "cpu.h"
#include "loader.h"
class EmulinoApp: public QApplication {
Q_OBJECT
public:
EmulinoApp(int &argc, char *argv[]);
public slots:
void onIdle();
void onButtonPress();
void onButtonRelease();
private:
QTimer timer;
};
EmulinoApp::EmulinoApp(int &argc, char *argv[])
: QApplication(argc, argv)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(onIdle()));
timer.start(0);
}
void EmulinoApp::onIdle()
{
if (cpu_run() == CPU_HALT) {
timer.stop();
}
}
void EmulinoApp::onButtonPress()
{
cpu_set_pin(PIN_PORTD+2, true);
}
void EmulinoApp::onButtonRelease()
{
cpu_set_pin(PIN_PORTD+2, false);
}
class BoardWidget: public QWidget {
Q_OBJECT
public:
BoardWidget(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
private:
QImage board;
};
BoardWidget::BoardWidget(QWidget *parent)
: QWidget(parent)
{
board.load("arduino.jpg");
setMinimumSize(board.size());
}
void BoardWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawImage(board.rect(), board, board.rect());
}
class Component {
public:
virtual ~Component() {}
virtual void setPin(int pin, bool state) = 0;
};
class PinMapper: QObject {
Q_OBJECT
public:
PinMapper(Component *dest);
void connect(QObject *source, int id);
public slots:
void stateChanged(bool state);
private:
Component *dest;
QHash<QObject *, int> pins;
};
PinMapper::PinMapper(Component *dest)
{
this->dest = dest;
}
void PinMapper::connect(QObject *source, int id)
{
QObject::connect(source, SIGNAL(stateChanged(bool)), this, SLOT(stateChanged(bool)));
pins[source] = id;
}
void PinMapper::stateChanged(bool state)
{
QHash<QObject *, int>::iterator i = pins.find(sender());
if (i != pins.end()) {
dest->setPin(i.value(), state);
}
}
class Led: public QWidget {
Q_OBJECT
public:
Led(QColor color, QWidget *parent = 0);
public slots:
void setState(bool newState);
protected:
void paintEvent(QPaintEvent *event);
private:
QColor color;
bool state;
};
Led::Led(QColor color, QWidget *parent)
: QWidget(parent)
{
this->color = color;
state = false;
}
void Led::setState(bool newState)
{
state = newState;
update();
}
void Led::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
if (state) {
painter.setPen(color);
painter.setBrush(color);
painter.drawEllipse(rect());
}
}
class Lcd: public QWidget, public Component {
Q_OBJECT
public:
Lcd(QWidget *parent = 0);
enum Pin {pinRS, pinRW, pinE, pinD4, pinD5, pinD6, pinD7};
public slots:
void setPin(int pin, bool state);
protected:
void paintEvent(QPaintEvent *event);
private:
bool RS;
bool RW;
u8 data;
bool phase;
u8 display[80];
int cursor;
};
#include "lcd.inc"
Lcd::Lcd(QWidget *parent)
: QWidget(parent), RS(false), RW(false), data(0), phase(false), cursor(0)
{
memset(display, ' ', sizeof(display));
}
void Lcd::setPin(int _pin, bool state)
{
Pin pin = static_cast<Pin>(_pin);
switch (pin) {
case pinRS:
RS = state;
break;
case pinRW:
RW = state;
break;
case pinE:
if (state) {
if (phase) {
if (RS) {
display[cursor] = data;
cursor = (cursor + 1) % sizeof(display);
} else {
switch (data) {
case 0x01:
memset(display, ' ', sizeof(display));
cursor = 0;
break;
case 0x02:
cursor = 0;
break;
}
}
update();
}
phase = !phase;
} else {
data = (data << 4) | (data & 0xf);
}
break;
case pinD4: data = (data & ~0x01) | (state ? 0x01 : 0); break;
case pinD5: data = (data & ~0x02) | (state ? 0x02 : 0); break;
case pinD6: data = (data & ~0x04) | (state ? 0x04 : 0); break;
case pinD7: data = (data & ~0x08) | (state ? 0x08 : 0); break;
}
}
void Lcd::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(Qt::gray);
painter.setBrush(Qt::gray);
painter.drawRect(rect());
painter.setBrush(Qt::black);
for (size_t i = 0; i < sizeof(display); i++) {
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 8; y++) {
if (Chars[display[i]][x] & (0x80 >> y)) {
painter.drawRect(20*i+3*x, 3*y, 3, 3);
}
}
}
}
}
class Pin: public QObject {
Q_OBJECT
public:
Pin();
void setState(bool newState);
signals:
void stateChanged(bool state);
private:
bool state;
};
Pin::Pin()
: state(false)
{
}
void Pin::setState(bool newState)
{
if (newState != state) {
state = newState;
stateChanged(state);
}
}
Pin *Pins[PIN_COUNT];
void pin_change(int pin, bool state)
{
//fprintf(stderr, "pin %d %d\n", pin, state);
if (Pins[pin] != NULL) {
Pins[pin]->setState(state);
}
}
int main(int argc, char *argv[])
{
EmulinoApp a(argc, argv);
for (size_t i = 0; i < LENGTHOF(Pins); i++) {
Pins[i] = new Pin();
}
QFrame frame;
BoardWidget board(&frame);
QPushButton hello("Hello world!", &frame);
hello.resize(100, 30);
Led led(Qt::yellow, &frame);
led.setGeometry(225, 86, 10, 10);
QObject::connect(Pins[PIN_PORTB+5], SIGNAL(stateChanged(bool)), &led, SLOT(setState(bool)));
for (int i = 2; i < 8; i++) {
Led *b = new Led(Qt::red, &frame);
b->setGeometry(438-i*15, 10, 10, 10);
QObject::connect(Pins[PIN_PORTD+i], SIGNAL(stateChanged(bool)), b, SLOT(setState(bool)));
}
Lcd lcd(&frame);
lcd.setGeometry(20, 350, 20*20, 25);
PinMapper mapper(&lcd);
mapper.connect(Pins[PIN_PORTB+4], 0);
mapper.connect(Pins[PIN_PORTB+3], 1);
mapper.connect(Pins[PIN_PORTB+2], 2);
mapper.connect(Pins[PIN_PORTD+5], 3);
mapper.connect(Pins[PIN_PORTD+4], 4);
mapper.connect(Pins[PIN_PORTD+3], 5);
mapper.connect(Pins[PIN_PORTD+2], 6);
frame.show();
QObject::connect(&hello, SIGNAL(pressed()), &a, SLOT(onButtonPress()));
QObject::connect(&hello, SIGNAL(released()), &a, SLOT(onButtonRelease()));
u8 prog[PROGRAM_SIZE_WORDS*2];
//u32 progsize = load_file("/Users/greg/arduino-0015/examples/Digital/Button/applet/Button.hex", prog, sizeof(prog));
//u32 progsize = load_file("/Users/greg/arduino-0015/examples/Digital/Loop/applet/Loop.hex", prog, sizeof(prog));
u32 progsize = load_file("/Users/greg/arduino-0015/hardware/libraries/LiquidCrystal/examples/HelloWorld/applet/HelloWorld.hex", prog, sizeof(prog));
if (progsize == 0) {
perror(argv[1]);
exit(1);
}
u8 eeprom[512];
u32 eepromsize = load_file("emulino.eeprom", eeprom, sizeof(eeprom));
cpu_init();
cpu_load_flash(prog, progsize);
cpu_load_eeprom(eeprom, eepromsize);
for (int i = 0; i < PIN_COUNT; i++) {
cpu_pin_callback(i, pin_change);
}
return a.exec();
}
#include "emulino-gui.moc"