/*JSON{ "type" : "staticmethod", "class" : "NetworkJS", "name" : "create", "generate" : "jswrap_networkjs_create", "params" : [ ["obj","JsVar","An object containing functions to access the network device"] ], "return" : ["JsVar","The object passed in"] } Initialise the WIZnet module and return an Ethernet object. For instance: ``` require("NetworkJS").create({ create : function(host,port) { // Create a socket and return its index, host is a string, port is an integer. // If host isn't defined, create a server socket console.log("Create",host,port); return 1; }, close : function(sckt) { // Close the socket. returns nothing }, accept : function(sckt) { // Accept the connection on the server socket. Returns socket number or -1 if no connection return -1; }, recv : function(sckt, maxLen) { // Receive data. Returns a string (even if empty). // If non-string returned, socket is then closed return null;//or ""; }, send : function(sckt, data) { // Send data (as string). Returns the number of bytes sent - 0 is ok. // Less than 0 return data.length; } }); ``` */ JsVar *jswrap_networkjs_create(JsVar *obj) { JsNetwork net; networkCreate(&net, JSNETWORKTYPE_JS); networkSet(&net); networkFree(&net); net_js_setObj(obj); networkState = NETWORKSTATE_ONLINE; return jsvLockAgain(obj); }
/*JSON{ "type" : "staticmethod", "class" : "CC3000", "name" : "connect", "generate" : "jswrap_cc3000_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default). SPI should be 1,000,000 baud, and set to 'mode 1'"], ["cs", "pin", "The pin to use for Chip Select"], ["en", "pin", "The pin to use for Enable"], ["irq", "pin", "The pin to use for Interrupts"] ], "return" : ["JsVar","A WLAN Object"], "return_object" : "WLAN" } Initialise the CC3000 and return a WLAN object */ JsVar *jswrap_cc3000_connect(JsVar *spi, Pin cs, Pin en, Pin irq) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = WLAN_CLK_PIN; inf.pinMISO = WLAN_MISO_PIN; inf.pinMOSI = WLAN_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_1; // Mode 1 CPOL= 0 CPHA= 1 jshSPISetup(WLAN_SPI, &inf); spiDevice = WLAN_SPI; } if (!jshIsPinValid(cs)) cs = WLAN_CS_PIN; if (!jshIsPinValid(en)) en = WLAN_EN_PIN; if (!jshIsPinValid(irq)) irq = WLAN_IRQ_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_CC3000); net.data.device = spiDevice; net.data.pinCS = cs; net.data.pinEN = en; net.data.pinIRQ = irq; networkSet(&net); JsVar *wlanObj = jspNewObject(0, "WLAN"); cc3000_initialise(wlanObj); networkFree(&net); return wlanObj; }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266", "name" : "connect", "generate" : "jswrap_esp8266_connect_device", "params" : [ ["serial","JsVar","The Serial port used for communications with the ESP8266 (must already be setup)"], ["callback","JsVar","Function to call back when connected"] ], "return" : ["JsVar","An ESP8266 Object"], "return_object" : "ESP8266" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_esp8266_connect_device(JsVar *usart, JsVar *callback) { IOEventFlags usartDevice; usartDevice = jsiGetDeviceFromClass(usart); if (!DEVICE_IS_USART(usartDevice)) { jsExceptionHere(JSET_ERROR, "Expecting USART device, got %q", usart); return 0; } JsNetwork net; networkCreate(&net, JSNETWORKTYPE_ESP8266); net.data.device = usartDevice; networkSet(&net); JsVar *wifiObj = 0; JsVar *cmd = jsvNewFromString("AT+RST\r\n"); esp8266_send(cmd); jsvUnLock(cmd); if (esp8266_wait_for("OK", 100, false)) { if (esp8266_wait_for("ready", 4000, false)) { networkState = NETWORKSTATE_ONLINE; wifiObj = jspNewObject(0, "ESPWifi"); } else { jsExceptionHere(JSET_ERROR, "Module not ready"); } } else { jsExceptionHere(JSET_ERROR, "No Acknowledgement"); } networkFree(&net); if (callback) jsiQueueEvents(callback, 0, 0); return wifiObj; }
static void setupJsNetwork() { JsNetwork net; networkCreate(&net, JSNETWORKTYPE_ESP8266_BOARD); networkSet(&net); }
/*JSON{ "type" : "staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default)"], ["cs", "pin", "The pin to use for Chip Select"] ], "return" : ["JsVar","An Ethernet Object"], "return_object" : "Ethernet" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_wiznet_connect(JsVar *spi, Pin cs) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); spiDevice = ETH_SPI; } if (!jshIsPinValid(cs)) cs = ETH_CS_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); net.data.device = spiDevice; net.data.pinCS = cs; networkSet(&net); JsVar *ethObj = jspNewObject(0, "Ethernet"); // CS Configuration jshSetPinStateIsManual(net.data.pinCS, false); jshPinOutput(net.data.pinCS, 1); // de-assert CS // Initialise WIZnet functions reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZnet Initialize failed.\r\n"); networkFree(&net); return 0; } #if _WIZCHIP_ == 5500 /* PHY link status check - W5100 doesn't have this */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); networkFree(&net); return 0; } } while (tmp == PHY_LINK_OFF); #endif networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }