Debian Codegraph @

extras/graphic_lcd/src/osram.cc

raw
1 /*
2 Copyright (C) 2007 T. Scott Dattalo
3
4 This file is part of gpsim.
5
6 gpsim is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 gpsim is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with gpsim; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <config.h>
22 #ifdef HAVE_GUI
23
24 #include "osram.h"
25 #include "glcd.h"
26 #include "ssd0323.h"
27
28 #include <src/stimuli.h>
29 #include <src/ioports.h>
30 #include <src/trace.h>
31
32 #include <cassert>
33 #include <string>
34
35 namespace OSRAM {
36
37 //------------------------------------------------------------------------
38 // Debug Stuff
39 //------------------------------------------------------------------------
40
41 class StateAttribute : public Integer {
42 public:
43 explicit StateAttribute(SSD0323 *pSSD)
44 : Integer("state", 0, "Display the state of the SSD0323 graphics controller"),
45 m_pSSD(pSSD)
46 {
47 assert(pSSD);
48 }
49 virtual std::string toString()
50 {
51 m_pSSD->showState();
52 return "";
53 }
54
55 private:
56 SSD0323 *m_pSSD;
57 };
58
59
60 //------------------------------------------------------------------------
61 // I/O pins for the SSD 0323 controller
62 //------------------------------------------------------------------------
63 // The LCD_InputPin is the base class for the E1, E2, RW and A0
64 // pins. This class is derived from the IO_bi_directional class,
65 // although the pins are never driven as outputs. The setDrivenState
66 // method is overridden to capture when this pin is driven.
67 // NOTE: an alternative implementation would be to instantiate
68 // PinModule objects for each of the pins and supply them with
69 // standard IO_bi_directional objects. Then SignalSinks could be
70 // created to capture pin state changes.
71
72 class SSD0323_InputPin : public IO_bi_directional {
73 public:
74 SSD0323_InputPin(SSD0323 *pSSD, PortRegister *pDataBus, const char *pinName);
75
76 // Capture when a node drives this pin.
77 virtual void setDrivenState(bool new_dstate);
78
79 virtual void UpdateControllerPin(bool /* bState */ ) {}
80 char CurrentState()
81 {
82 return m_cDrivenState;
83 }
84
85 protected:
86 SSD0323 *m_pSSD0323;
87 PortRegister *m_pDataBus;
88 char m_cDrivenState;
89
90 private:
91 // The IO_bi_directional constructor will initialize the direction
92 // to be an input. We'll override the update_direction method so that
93 // this will never change.
94
95 virtual void update_direction(unsigned int, bool /* refresh */ )
96 {
97 // Disallow pin direction changes.
98 }
99 };
100
101
102 SSD0323_InputPin::SSD0323_InputPin(SSD0323 *pSSD, PortRegister *pDataBus, const char *pinName)
103 : IO_bi_directional(pinName),
104 m_pSSD0323(pSSD),
105 m_pDataBus(pDataBus),
106 m_cDrivenState('Z')
107 {
108 assert(m_pSSD0323);
109 }
110
111
112 void SSD0323_InputPin::setDrivenState(bool new_dstate)
113 {
114 IO_bi_directional::setDrivenState(new_dstate);
115
116 if (!m_pSSD0323->dataBusDirection()) {
117 m_pSSD0323->driveDataBus(m_pDataBus->get());
118 }
119
120 char cState = getBitChar();
121
122 if (m_cDrivenState != cState) {
123 m_cDrivenState = cState;
124 //cout << "LCD pin:" << name() << "-->"<<cState<<endl;
125 UpdateControllerPin((cState == '1') || (cState == 'W'));
126 }
127
128 if (m_pSSD0323->dataBusDirection()) {
129 m_pDataBus->put(m_pSSD0323->getDataBus());
130 }
131 }
132
133
134 //------------------------------------------------------------------------
135 // SSD E Pin
136 //------------------------------------------------------------------------
137 class SSD0323_EPin : public SSD0323_InputPin {
138 public:
139 SSD0323_EPin(SSD0323 *pSSD, PortRegister *pDataBus, const char *pinName)
140 : SSD0323_InputPin(pSSD, pDataBus, pinName)
141 {}
142 virtual void UpdateControllerPin(bool);
143 };
144
145
146 void SSD0323_EPin::UpdateControllerPin(bool bState)
147 {
148 m_pSSD0323->setE_RD(bState);
149 }
150
151
152 //------------------------------------------------------------------------
153 // SSD CS Pin
154 //------------------------------------------------------------------------
155 class SSD0323_CSPin : public SSD0323_InputPin {
156 public:
157 SSD0323_CSPin(SSD0323 *pSSD, PortRegister *pDataBus, const char *pinName)
158 : SSD0323_InputPin(pSSD, pDataBus, pinName)
159 {}
160 virtual void UpdateControllerPin(bool);
161 };
162
163
164 void SSD0323_CSPin::UpdateControllerPin(bool bState)
165 {
166 m_pSSD0323->setCS(bState);
167 }
168
169
170 //------------------------------------------------------------------------
171 // SSD RES Pin
172 //------------------------------------------------------------------------
173 class SSD0323_RESPin : public SSD0323_InputPin {
174 public:
175 SSD0323_RESPin(SSD0323 *pSSD, PortRegister *pDataBus, const char *pinName)
176 : SSD0323_InputPin(pSSD, pDataBus, pinName)
177 {}
178 virtual void UpdateControllerPin(bool);
179 };
180
181
182 void SSD0323_RESPin::UpdateControllerPin(bool bState)
183 {
184 m_pSSD0323->setRES(bState);
185 }
186
187
188 //------------------------------------------------------------------------
189 // SSD CS Pin
190 //------------------------------------------------------------------------
191 class SSD0323_DCPin : public SSD0323_InputPin {
192 public:
193 SSD0323_DCPin(SSD0323 *pSSD, PortRegister *pDataBus, const char *pinName)
194 : SSD0323_InputPin(pSSD, pDataBus, pinName)
195 {}
196 virtual void UpdateControllerPin(bool);
197 };
198
199
200 void SSD0323_DCPin::UpdateControllerPin(bool bState)
201 {
202 m_pSSD0323->setDC(bState);
203 }
204
205
206 //------------------------------------------------------------------------
207 // SSD RW Pin
208 //------------------------------------------------------------------------
209 class SSD0323_RWPin : public SSD0323_InputPin {
210 public:
211 SSD0323_RWPin(SSD0323 *pSSD, PortRegister *pDataBus, const char *pinName)
212 : SSD0323_InputPin(pSSD, pDataBus, pinName)
213 {}
214 virtual void UpdateControllerPin(bool);
215 };
216
217
218 void SSD0323_RWPin::UpdateControllerPin(bool bState)
219 {
220 m_pSSD0323->setRW(bState);
221 }
222
223
224 //------------------------------------------------------------------------
225 // SSD BS1, BS2 Pin
226 //------------------------------------------------------------------------
227 class SSD0323_BSPin : public SSD0323_InputPin {
228 public:
229 SSD0323_BSPin(SSD0323 *pSSD, PortRegister *pDataBus, const char *pinName, unsigned pin)
230 : SSD0323_InputPin(pSSD, pDataBus, pinName),
231 m_pin(pin)
232 {
233 assert(m_pin == 1 || m_pin == 2);
234 }
235 virtual void UpdateControllerPin(bool);
236
237 private:
238 unsigned int m_pin;
239 };
240
241
242 void SSD0323_BSPin::UpdateControllerPin(bool bState)
243 {
244 m_pSSD0323->setBS(m_pin, bState);
245 }
246
247
248 //------------------------------------------------------------------------
249 // SSD BS1, BS2 Pin
250 //------------------------------------------------------------------------
251 class SSD_SPISignalSink : public SignalSink {
252 public:
253 SSD_SPISignalSink(SSD0323 *pSSD, bool bClk_or_Data)
254 : m_pSSD0323(pSSD),
255 m_bClk(bClk_or_Data),
256 m_cState(0)
257 {
258 assert(m_pSSD0323);
259 }
260
261 virtual void release()
262 {
263 }
264
265 virtual void setSinkState(char c)
266 {
267 if (m_cState != c) {
268 //cout << __FUNCTION__ << " new state: " << c << endl;
269 if (m_bClk) {
270 m_pSSD0323->setSCLK(c == '1' || c == 'W');
271
272 } else {
273 m_pSSD0323->setSDIN(c == '1' || c == 'W');
274 }
275
276 m_cState = c;
277 }
278 }
279
280 private:
281 SSD0323 *m_pSSD0323;
282 bool m_bClk;
283 char m_cState;
284 };
285
286
287 //------------------------------------------------------------------------
288 //
289
290 class LCDSignalControl : public SignalControl {
291 public:
292 explicit LCDSignalControl(PK27_Series *pLCD)
293 : m_pLCD(pLCD)
294 {
295 assert(m_pLCD);
296 }
297 virtual void release()
298 {
299 }
300 virtual char getState()
301 {
302 // return the data bus direction (note the negative true logic.
303 return m_pLCD->dataBusDirection() ? '0' : '1';
304 }
305
306 private:
307 PK27_Series *m_pLCD;
308 };
309
310
311 //------------------------------------------------------------------------
312 // PK27_Series -- Module constructor.
313 //------------------------------------------------------------------------
314
315 Module *PK27_Series::construct(const char *_new_name)
316 {
317 PK27_Series *pLCD = new PK27_Series(_new_name);
318 pLCD->create_widget();
319 return pLCD;
320 }
321
322
323 //------------------------------------------------------------------------
324 // PK27_Series -- 128X64 - 4-bit gray scale.
325 //------------------------------------------------------------------------
326 PK27_Series::PK27_Series(const char *_new_name)
327 : gLCD_Module(_new_name, "OSRAM 128X64 Graphics OLED module", 128, 64)
328 {
329 m_pSSD0323 = new SSD0323();
330 m_pSSD0323->setBS(0, false);
331 m_dataBus = new LcdPortRegister(this, ".data", "LCD Data Port");
332 addSymbol(m_dataBus);
333 m_dataBus->setEnableMask(0xff);
334 m_CS = new SSD0323_CSPin(m_pSSD0323, m_dataBus, "cs");
335 m_RES = new SSD0323_RESPin(m_pSSD0323, m_dataBus, "res");
336 m_DC = new SSD0323_DCPin(m_pSSD0323, m_dataBus, "dc");
337 m_E = new SSD0323_EPin(m_pSSD0323, m_dataBus, "e");
338 m_RW = new SSD0323_RWPin(m_pSSD0323, m_dataBus, "rw");
339 m_BS1 = new SSD0323_BSPin(m_pSSD0323, m_dataBus, "bs1", 1);
340 m_BS2 = new SSD0323_BSPin(m_pSSD0323, m_dataBus, "bs2", 2);
341 addSymbol(m_CS);
342 addSymbol(m_RES);
343 addSymbol(m_DC);
344 addSymbol(m_E);
345 addSymbol(m_RW);
346 addSymbol(m_BS1);
347 addSymbol(m_BS2);
348 m_state = new StateAttribute(m_pSSD0323);
349 addSymbol(m_state);
350 /*
351 m_sed1->randomizeRAM();
352 m_sed2->randomizeRAM();
353 */
354 create_iopin_map();
355 //create_widget();
356 }
357
358
359 //------------------------------------------------------------------------
360 // ~PK27_Series
361 //------------------------------------------------------------------------
362 PK27_Series::~PK27_Series()
363 {
364 removeSymbol(m_CS);
365 removeSymbol(m_RES);
366 removeSymbol(m_DC);
367 removeSymbol(m_E);
368 removeSymbol(m_RW);
369 removeSymbol(m_BS1);
370 removeSymbol(m_BS2);
371 removeSymbol(m_state);
372 gtk_widget_destroy(darea);
373 delete m_pSSD0323;
374 delete m_dataBus;
375 delete m_state;
376 }
377
378
379 gboolean PK27_Series::lcd_expose_event(GtkWidget *widget,
380 GdkEventExpose *,
381 PK27_Series *pLCD)
382 {
383 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
384 pLCD->m_plcd->clear(cr);
385
386 for (unsigned int row = 0; row < pLCD->m_nRows; ++row) {
387 for (unsigned int i = 0; i < pLCD->m_nColumns / 2; ++i) {
388 unsigned int displayByte = (*(pLCD->m_pSSD0323))[i + 64 * row];
389
390 for (unsigned int b = 0; b < 2; b++, displayByte <<= 4) {
391 pLCD->m_plcd->setPixel(cr, 2 * i + b, row, (displayByte >> 4) & 0xf);
392 }
393 }
394 }
395
396 cairo_destroy(cr);
397 return TRUE;
398 }
399
400
401 //------------------------------------------------------------------------
402 // PK27_Series::Update
403 //------------------------------------------------------------------------
404 const int pixelScale = 2;
405
406 void PK27_Series::Update(GtkWidget *)
407 {
408 gtk_widget_queue_draw(darea);
409 }
410
411
412 //------------------------------------------------------------------------
413 // PK27_Series::create_widget
414 //------------------------------------------------------------------------
415 void PK27_Series::create_widget()
416 {
417 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
418 gtk_window_set_title(GTK_WINDOW(window), "LCD");
419 GtkWidget *frame = gtk_frame_new("OSRAM PK27_Series");
420 gtk_container_add(GTK_CONTAINER(window), frame);
421 darea = gtk_drawing_area_new();
422 gtk_widget_set_size_request(darea,
423 (m_nColumns + 3)*pixelScale, (m_nRows + 3)*pixelScale);
424 gtk_container_add(GTK_CONTAINER(frame), darea);
425 g_signal_connect(darea, "expose_event", G_CALLBACK(lcd_expose_event), this);
426 gtk_widget_set_events(darea, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
427 gtk_widget_show_all(window);
428 m_plcd = new gLCD(m_nColumns, m_nRows, pixelScale, pixelScale, 0, 16);
429
430 for (int i = 0; i < 16; ++i) {
431 double c = double(i) / 16.0;
432 m_plcd->setColor(i, c, c, 0.0);
433 }
434 }
435
436
437 //------------------------------------------------------------------------
438 // PK27_Series::create_widget
439 //------------------------------------------------------------------------
440
441 void PK27_Series::create_iopin_map()
442 {
443 create_pkg(30);
444 assign_pin(20, m_BS1);
445 assign_pin(19, m_BS2);
446 assign_pin(17, m_CS);
447 assign_pin(16, m_RES);
448 assign_pin(15, m_DC);
449 assign_pin(14, m_RW);
450 assign_pin(13, m_E);
451 // Add the individual io pins to the data bus.
452 char text[] = "d0";
453
454 for (int i = 0; i < 8; i++) {
455 text[1] = '0' + i;
456 io_bus[i] = new IO_bi_directional(text);
457 addSymbol(io_bus[i]);
458 assign_pin(12 - i, m_dataBus->addPin(io_bus[i], i));
459 }
460
461 m_dataBus->addSink(new SSD_SPISignalSink(m_pSSD0323, true), 0); // SPI CLK
462 m_dataBus->addSink(new SSD_SPISignalSink(m_pSSD0323, false), 1); // SPI Data
463 // Provide a SignalControl object that the dataBus port can query
464 // to determine which direction to drive the data bus.
465 // (See <gpsim/ioports.h> for more documentation on port behavior.
466 // But in summary, when an I/O port updates its I/O pins, it will
467 // query the pin drive direction via the SignalControl object. )
468 SignalControl *pPortDirectionControl = new LCDSignalControl(this);
469
470 for (int i = 0; i < 8; i++) {
471 (*m_dataBus)[i].setControl(pPortDirectionControl);
472 }
473 }
474
475
476 bool PK27_Series::dataBusDirection()
477 {
478 return m_pSSD0323->dataBusDirection();
479 }
480
481
482 } // end of namespace OSRAM
483
484 #endif // HAVE_GUI