Skip to content

Commit

Permalink
CYD touchscreen support
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Dec 14, 2024
1 parent 640b45e commit 79aeaf6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
11 changes: 9 additions & 2 deletions boards/ESP32_CYD.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
# as various source and header files for Espruino.
# ----------------------------------------------------------------------------------------

# ESP32 CYD LCD DISPLAY
# * Graphics initialised as 'g'
# * E.on("touch", e => ...x,y,b ) for tocuh events
# * CN1/P1/P3 for connectors
# * SPK for speaker (analog)
# * FAT Filesystem vioa require("fs")


# A Note about the 'variables' parameter on ESP32 Builds
Expand Down Expand Up @@ -73,7 +79,7 @@
'DEFINES+=-DESP_STACK_SIZE=25000',
'DEFINES+=-DJSVAR_MALLOC', # Allocate space for variables at jsvInit time
'DEFINES+=-DESPR_GRAPHICS_INTERNAL -DESPR_GRAPHICS_SELF_INIT', # ensure graphics instantiates itself
'DEFINES+=-DUSE_FONT_6X8 -DSPISENDMANY_BUFFER_SIZE=1600 -DLCD_SPI_BITRATE=32000000 -DESPR_TERMNINAL_NO_SCROLL',
'DEFINES+=-DUSE_FONT_6X8 -DSPISENDMANY_BUFFER_SIZE=1600 -DLCD_SPI_BITRATE=55000000 -DESPR_TERMNINAL_NO_SCROLL',
'WRAPPERSOURCES += libs/misc/jswrap_esp32_cyd.c libs/misc/jswrap_qwiic.c',
'ESP32_FLASH_MAX=1572864'
]
Expand Down Expand Up @@ -122,7 +128,8 @@
'pin_scl' : 'D22',
'pin_vcc' : 'D21',
},
'TOUCHSCREEN' : { # XPT2046
'TOUCH' : {
'device' : 'XPT2046',
'pin_irq' : 'D36',
'pin_cs' : 'D33',
'pin_sck' : 'D25',
Expand Down
75 changes: 75 additions & 0 deletions libs/misc/jswrap_esp32_cyd.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@
#include "jswrap_curio.h"
#include "jshardware.h"
#include "jsparse.h"
#include "jsspi.h"
#include "jsinteractive.h"

spi_sender_data touchSPI;
bool wasTouched;
int touchx, touchy;

/* DO_NOT_INCLUDE_IN_DOCS - this is a special token for common.py */

/*JSON{
"type" : "event",
"class" : "E",
"name" : "touch",
"params" : [["xy","JsVar","An object `{x,y,b}`"]]
}
*/


/*JSON{
"type" : "variable",
"name" : "CN1",
Expand Down Expand Up @@ -66,6 +81,13 @@ JsVar *jswrap_cyd_p3() {
jsvObjectSetChild(execInfo.root, "P3", o);
return o;
}
/*JSON{
"type" : "variable",
"name" : "SPK",
"generate_full" : "(Pin)26",
"return" : ["pin","the pin for the speaker"]
}
*/

/*JSON{
"type" : "init",
Expand All @@ -74,4 +96,57 @@ JsVar *jswrap_cyd_p3() {
void jswrap_cyd_init() {
jshPinOutput(LED1_PININDEX,0); // LED1 is glowing by default?
jshPinOutput(LED3_PININDEX,0); // LED3 is on by default?

jshPinSetState(TOUCH_PIN_SCK, JSHPINSTATE_GPIO_OUT);
jshPinSetState(TOUCH_PIN_MISO, JSHPINSTATE_GPIO_IN);
jshPinSetState(TOUCH_PIN_MOSI, JSHPINSTATE_GPIO_OUT);
jshPinSetState(TOUCH_PIN_CS, JSHPINSTATE_GPIO_OUT);
jshPinSetState(TOUCH_PIN_IRQ, JSHPINSTATE_GPIO_IN);

jshDelayMicroseconds(1000);

jshSPIInitInfo(&touchSPI);
touchSPI.pinMISO = TOUCH_PIN_MISO;
touchSPI.pinMOSI = TOUCH_PIN_MOSI;
touchSPI.pinSCK = TOUCH_PIN_SCK;

unsigned char d[2] = {0x90,0};
jsspiSoftwareFunc(d, NULL, 2, &touchSPI); // turn on
}

/*JSON{
"type" : "idle",
"generate" : "jswrap_cyd_idle"
}*/
bool jswrap_cyd_idle() {
bool sendEvent = false;
if (!jshPinGetValue(TOUCH_PIN_IRQ)) {
unsigned char d[5] = {0x90,0,0xD0,0,0};
jsspiSoftwareFunc(d, d, 5, &touchSPI); // get data
int x = d[1]<<8 | d[2];
int y = d[3]<<8 | d[4];
x = x * LCD_WIDTH / 31000;
y = y * LCD_HEIGHT / 31000;
if (!wasTouched || x!=touchx || y!=touchy) {
touchx = x;
touchy = y;
sendEvent = true;
}
wasTouched = true;
} else {
if (wasTouched) sendEvent = true;
wasTouched = false;
}
if (sendEvent) {
JsVar *E = jsvObjectGetChildIfExists(execInfo.root, "E");
if (E) {
JsVar *o = jsvNewObject();
jsvObjectSetChildAndUnLock(o,"x", jsvNewFromInteger(touchx));
jsvObjectSetChildAndUnLock(o,"y", jsvNewFromInteger(touchy));
jsvObjectSetChildAndUnLock(o,"b", jsvNewFromInteger(wasTouched?1:0));
jsiQueueObjectCallbacks(E, JS_EVENT_PREFIX"touch", &o, 1);
jsvUnLock2(E,o);
}
}
return false;
}
1 change: 1 addition & 0 deletions libs/misc/jswrap_esp32_cyd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ JsVar *jswrap_cyd_cn1();
JsVar *jswrap_cyd_p1();
JsVar *jswrap_cyd_p3();
void jswrap_cyd_init();
bool jswrap_cyd_idle();
3 changes: 2 additions & 1 deletion scripts/build_platform_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ def codeOutDevicePins(device, definition_name):

if "TOUCH" in board.devices:
codeOut("#define TOUCH_DEVICE \""+board.devices["TOUCH"]["device"].upper()+"\"")
codeOut("#define TOUCH_ADDR "+str(board.devices["TOUCH"]["addr"]))
if "addr" in board.devices["TOUCH"]:
codeOut("#define TOUCH_ADDR "+str(board.devices["TOUCH"]["addr"]))
codeOutDevicePins("TOUCH", "TOUCH")

if "QWIIC0" in board.devices:
Expand Down

0 comments on commit 79aeaf6

Please sign in to comment.