module g15lcd; import lglcd; import std.utf; import std.stdio; import win32.windows; alias ubyte[160][43] LcdPixels; /// interface IG15Lcd { /// name will show up in the lcd screen. returns null if no lcd support is found. static IG15Lcd create(wchar[] name); /// pointer to the backbuffer array. pixels are represented as ubytes, 0x00 for black and 0xff for white. //ubyte[160][43]* displayHandle(); LcdPixels* displayHandle(); /// displays the backbuffer uint updateDisplay(); /// void setButtonCallback(void delegate(ubyte) dg); wchar[] name(); int onButton(int button); void open(); void close(); bool isOpen(); } /// class G15Lcd : IG15Lcd { static IG15Lcd create(wchar[] name) { if (lglcd.isAvailable == false) return null; auto result = new G15Lcd(); result._name = name ~ "\0"w; with (result.context) { appFriendlyName = result._name.ptr; isAutostartable = false; isPersistent = false; onConfigure.configCallback = null; onConfigure.configContext = null; connection = LGLCD_INVALID_CONNECTION; } return result; } wchar[] name() { return _name; } LcdPixels* displayHandle() { return backBuffer; } void open() { auto res = lgLcdConnectW(&this.context); if (res) throw new Exception("could not open lcd device"); lgLcdOpenContext openContext; openContext.connection = this.context.connection; openContext.index = 0; openContext.onSoftbuttonsChanged.softbuttonsChangedCallback = &onLcdButton; openContext.onSoftbuttonsChanged.softbuttonsChangedContext = cast(void*)cast(IG15Lcd)(this); openContext.device = LGLCD_INVALID_DEVICE; res = lgLcdOpen(&openContext); if (res || openContext.connection == LGLCD_INVALID_CONNECTION) throw new Exception("could not open lcd device"); this.deviceNum = openContext.device; _isOpen = true; } bool isOpen() { return _isOpen; } void close() { auto res = lgLcdClose(this.deviceNum); if (res) throw new Exception("exception while closing lcd device"); res = lgLcdDisconnect(this.context.connection); if (res) throw new Exception("exception while closing lcd device"); } void setButtonCallback(void delegate(ubyte) dg) { this.callback = dg; } uint updateDisplay() { return lgLcdUpdateBitmap(this.deviceNum, &this.bitmap.hdr, LGLCD_ASYNC_UPDATE(LGLCD_PRIORITY_NORMAL)); } int onButton(int button) { if (this.callback != null) this.callback(button); return 0; } private { bool _isOpen = false; wchar[] _name; lgLcdConnectContext context; lgLcdBitmap160x43x1 bitmap; LcdPixels* backBuffer; int deviceNum; void delegate(ubyte) callback = null; this() { backBuffer = cast(LcdPixels*)&this.bitmap.pixels; } } } class G15LcdApplet : G15Lcd { static IG15Lcd create(wchar[] name) { if (lglcd.isAvailable == false) return null; auto result = new G15LcdApplet(); result._name = name ~ "\0"w; with (result.context) { appFriendlyName = result._name.ptr; isAutostartable = true; isPersistent = true; onConfigure.configCallback = null; onConfigure.configContext = null; connection = LGLCD_INVALID_CONNECTION; } return result; } } extern (Windows) { uint onLcdButton(int device, uint button, void* connection) { return (cast(IG15Lcd)connection).onButton(button); } }