64 lines
2.2 KiB
C

/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Graphics Backend for drawing via JavaScript callback
* ----------------------------------------------------------------------------
*/
#include "lcd_arraybuffer.h"
#include "jsvar.h"
#include "jsparse.h"
#include "jsinteractive.h"
void lcdSetPixel_JS(JsGraphics *gfx, int x, int y, unsigned int col) {
// look up setPixel and execute it!
// JsVar *lcdProto = jsvObjectGetChildIfExists(gfx->graphicsVar, JSPARSE_PROTOTYPE_VAR);
// if (lcdProto) {
JsVar *setPixel = jsvObjectGetChildIfExists(gfx->graphicsVar/*lcdProto*/, "iSetPixel");
if (setPixel) {
JsVar *args[3];
args[0] = jsvNewFromInteger(x);
args[1] = jsvNewFromInteger(y);
args[2] = jsvNewFromInteger((JsVarInt)col);
jsvUnLock(jspExecuteFunction(setPixel, gfx->graphicsVar, 3, args));
jsvUnLockMany(3, args);
jsvUnLock(setPixel);
}
// jsvUnLock(lcdProto);
// }
}
void lcdFillRect_JS(struct JsGraphics *gfx, int x1, int y1, int x2, int y2, unsigned int col) {
JsVar *fillRect = jsvObjectGetChildIfExists(gfx->graphicsVar/*lcdProto*/, "iFillRect");
if (fillRect) {
JsVar *args[5];
args[0] = jsvNewFromInteger(x1);
args[1] = jsvNewFromInteger(y1);
args[2] = jsvNewFromInteger(x2);
args[3] = jsvNewFromInteger(y2);
args[4] = jsvNewFromInteger((JsVarInt)col);
jsvUnLock(jspExecuteFunction(fillRect, gfx->graphicsVar, 5, args));
jsvUnLockMany(5, args);
jsvUnLock(fillRect);
} else
graphicsFallbackFillRect(gfx, x1,y1,x2,y2,col);
}
void lcdInit_JS(JsGraphics *gfx, JsVar *setPixelCallback, JsVar *fillRectCallback) {
jsvObjectSetChild(gfx->graphicsVar, "iSetPixel", setPixelCallback);
jsvObjectSetChild(gfx->graphicsVar, "iFillRect", fillRectCallback);
}
void lcdSetCallbacks_JS(JsGraphics *gfx) {
gfx->setPixel = lcdSetPixel_JS;
gfx->fillRect = lcdFillRect_JS;
}